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
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
1. Consider the following code segment. String str = "AP"; str += "CS " + 1...
Question 33 Consider the following code segment. String letters - ("A", "B", "C", "D"), ("E", "P", "G", "1"), ("I", "J", "K", "L"}}; for (int col = 1; col < letters[0].length; col++) for (int row - 1; row < letters.length; row++) System.out.print(letterstrow][col] + " "); System.out.println(); What is printed as a result of executing this code segment? AET PJ DHL G J K L PJ GK
Question 2 1 point possble (graded) Consider the following code segment. String a- a0 if (a-0") else if (a"1") else if (a" else( System.out.printin("a is 0 System.out.printin("a is 1! System.out.println("a is al") System.out.println(a is something else!) Which of the following statements about the code segment is true? It produced "a is 0!" as output It produced "a is 1!" as output It produced "a is a!" as output It produced "a is something else!" as output It results in an...
Question 8 0 Consider the following class definitions, public class Class public String getValue() return "A"; public void showValue() System.out.print(getValue(); public class Classe extends Class public String getValue() return "B"; The following code segment appears in a class other than ClassA or Classe. ClassA obj = new Class(); obj.showValue(); What, if anything, is printed when the code segment is executed? c) AB ta PendulumPhysical.pdf ^ IN SHM Pendulums ....doc Moh PendulumPhysical.pdf Type here to search o Bi e а в...
Consider the following code segment. ArrayList<String> towns = new ArrayList<String> (); towns.add("Baltimore"); towns.add("Bethesda"); towns.add("Wheaton"); towns.add("Greenbelt"); towns.add("Rockville"); for (int k = 1; k< towns.size(); k ++) System.out.print(towns.get(k) + " "); What is printed as a result of executing the code segment? Baltimore Bethesda Wheaton Greenbelt IndexOutOfBoundsException message Baltimore Bethesda Wheaton Greenbelt Rockville Bethesda Wheaton Greenbelt Rockville Refer to the doSomething method: public static void doSomething (ArrayList list, int i, intj) SomeType temp = list.get(i); list.set(i, list.get()); list.set(j, temp); Which best describes...
please help me on 12 106 Java Concepts Advanced Placement CS Study Guide 3 12. Consider the following code segment. int count = 0; for (int x = 0; x< 3; x++) for (int y = x; y < 3; y++) for (int z - y: z < 3; 2++) count++; What is the value of count after the code segment is executed? a 81 b. 27 JOHNNNN mshe thote c. 10x d. 9 e, 6 13. Each segment of...
1. What is the output of the following code segment? int array[] = { 8, 6, 9, 7, 6, 4, 4, 5, 8, 10 }; System.out.println( "Index Value" ); for ( int i = 0; i < array.length; i++ ) System.out.printf( "%d %d\n", i, array[ i ] ); 2. What is the output of the following code segment? char sentence[] = {'H', 'o', 'w', ' ', 'a', 'r', 'e', ' ', 'y', 'o', 'u' }; String output = "The sentence...
c++ Consider the following statement string str "Now is the time for the party!" What is the output of the following statements? (Assume that all parts are independent of each other.) a. cout <str·size ( ) end 1 ; b. Cout << str. substr (7, 8) <<endl: c. string: :size type indstr.find'£') string s str. substr (ind 4, 9); d. cout << str insert (11,"best " <<endl e. str.erase (16, 14) str.insert (16, "to study for the exam? ") cout...
Consider the following incomplete code segment, which is intended to print the sum of the digits in num. For example, when num is 12345, the code segment should print 15, which represents the sum 1 + 2 + 3 + 4 + 5. int num = 12345; int sum = 0; /* missing loop header */ { sum += num % 10; num /= 10; } System.out.println(sum); Which of the following should replace /* missing loop header */ so that the...
Consider the following codes: public class TestThread extends Thread { public static void main(String[] args) { TestThread thread = new TestThread(); } @Override public void run() { printMyName(); } private void printMyName() { System.out.println("Thread is running"); } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...
Consider the following snippet of code : ArrayList <String> temp = new ArrayList<String>(); temp.add(“a”); temp.add(”b”); temp.add(“c”); temp.remove(“z”); temp.add(2, “d”); temp.set(0, “e”); temp.remove(1); System.out.println(temp.size() + ” “+ temp.get(1)); What will print on the screen? 3 b 3 d 4 a 4 e Runtime Error