Question

1. Consider the following code segment. String str = "AP"; str += "CS " + 1...

1. Consider the following code segment.

String str = "AP";
str += "CS " + 1 + 2;
System.out.println(str);
What is printed as a result of executing the code segment?

A: CS AP12

B: AP CS3

C: CSAP 12

D: APCS 12

E: APCS 3

2. Consider the following code segment.

String dessert = "pie";
dessert += "straw" + dessert + "berry";
What is the value of dessert after the code segment has been executed?

A: strawpieberry

B: piestrawpieberry

C: strawpieberrypie

D: strawberry

E: piestrawberry

3. Which of the following code segments can be used to set the value of the string str to "Good morning, sunshine!" ?

I. String str = "Good " + "morning," + " sunshine!";
II. String str = "Good";
str += " morning, sunshine!";
III. String str = " morning, ";
str = "Good" + str + "sunshine!";

A: I only

B: II only

C: III only

D: II and III only

4. Consider the following code segment.

String one = "ABC123";
String two = "C";
String three = "3";
System.out.println(one.indexOf(two));
System.out.println(one.indexOf(three));
System.out.println(two.indexOf(one));
What is printed when the code segment is executed?

A:2
5
-1

B:2
5
2

C:2
6
-1

D:3
6
-1

E:-1
-1
2

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answers

1 ) option D -> APCS 12

Reason :- actually that statement means str = str + "CS "+1+2; , we know that addition is left associative , so first str + "CS " => APCS , then 1 is added to it (here 1 is integer but it is added to a string so it converted automatically to string while adding. therfore it becomes APCS 1 , then 2 is added with this string => APCS 12 , and this is stored to str.

2 ) option B -> piestrawpieberry

Reason :- actually the statement means dessert=dessert + "straw" + dessert + "berry" ; ,we know that addition is left associative , so string "straw" is first append to variable dessert that is "pie" , so it becomes "piestraw ", then dessert variable is again added to it , then it becomes "piestrawpie" , then string "berry" is added to it and it becomes "piestrawpieberry" , and only at  last it is stored to variable dessert , so no dessert contains "piestrawpieberry".

3 ) option D - >  II and III only

Reason : In the first code segment it gives only "Goodmorning, sunshine!" , there is no space between Good and morning as in the question , so it is incorrect .

4 ) option  A - >

2
5
-1

Reason :- System.out.println(one.indexOf(two)); //this will print the position of string one in two ie : the occurence of "C" in  "ABC123"  which is equal to 2.
System.out.println(one.indexOf(three)); //this will print the position of "3" in "ABC123" ie 5 .
System.out.println(two.indexOf(one)); // the string one is not in string two ie  "ABC123" is not in "C" , because of the function return -1 .

If you find this answer useful , please rate positive , thankyou

Add a comment
Know the answer?
Add Answer to:
1. Consider the following code segment. String str = "AP"; str += "CS " + 1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT