1.
Given:
public class TestString1 {
public static void main(String[] args) {
String str = "420";
str += 42;
System.out.print(str);
}
}
What is the output?
Correct Answer
D. 42042
Explanation
The output of this code is "42042". The code starts with a string "420" and then uses the += operator to concatenate the number 42 to the string. This results in the final string "42042".
2.
Given:
35. String #name = "Jane Doe";
36. int $age = 24;
37. Double _height = 123.5;
38. double ~temp = 37.5;
Which two statements are true?
(Choose two.)
Correct Answer(s)
A. Line 35 will not compile.
D. Line 38 will not compile.
Explanation
Line 35 will not compile because the variable name cannot start with a special character (#).
Line 38 will not compile because the variable temp cannot start with a special character (~).
3.
Given:
public class Test {
public static void main(String[] args) {
int x = 5;
boolean b1 = true;
boolean b2 = false;
if ((x == 4) && !b2)
System.out.print("1 ");
System.out.print("2 ");
if ((b2 = true) && b1)
System.out.print("3 ");
}
}
What is the result?
Correct Answer
D. 23
Explanation
The code first checks if x is equal to 4 and b2 is false, which is false in this case. Therefore, the first if statement is not executed. Then, the code prints "2" because it is outside of the if statement. After that, the code assigns true to b2 and checks if b2 is true and b1 is true, which is true in this case. Therefore, the second if statement is executed and "3" is printed. So, the final output is "23".
4.
Given:
public void go() {
String o = "";
z:
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 2; y++) {
if (x == 1)break;
if (x == 2 && y == 1)break z;
o = o + x + y;
}
}
System.out.println(o);
}
What is the result when the go() method is invoked?
Correct Answer
C. 000120
Explanation
The go() method contains two nested for loops. The outer loop iterates from 0 to 2 and the inner loop iterates from 0 to 1. Inside the inner loop, there are two conditions. If x is equal to 1, the inner loop is terminated using the "break" statement. If x is equal to 2 and y is equal to 1, the outer loop is terminated using the "break z" statement. In all other cases, the value of x and y are concatenated to the string "o".
Therefore, when the go() method is invoked, the value of "o" becomes "000120" and it is printed.
5.
Given:
int x = 0;
int y = 10;
while (x < 5) {
y--;
++x;
} ;
System.out.print(x + "," + y);
What is the result?
Correct Answer
B. 5,5
Explanation
The given code initializes x to 0 and y to 10. It then enters a while loop that continues as long as x is less than 5. Inside the loop, y is decremented by 1 and x is incremented by 1. This process continues until x becomes 5. After the loop, the values of x and y are printed, which are 5 and 5 respectively. Therefore, the correct answer is 5,5.
6.
Given:
public class Breaker2 {
static String o = "";
public static void main(String[] args) {
z: for (int x = 2; x < 7; x++) {
if (x == 3)
continue;
if (x == 5)
break z;
o = o + x;
}
System.out.println(o);
}
}
What is the result?
Correct Answer
B. 24
Explanation
The code uses a label "z" to create a labeled loop. The loop iterates from 2 to 6. If the value of x is 3, the continue statement is executed, which skips the current iteration. If the value of x is 5, the break statement with the label "z" is executed, which terminates the loop. Otherwise, the value of x is concatenated to the string "o". In this case, the loop will iterate from 2 to 6, excluding 3 and terminating at 5. Hence, the value of "o" will be "24".
7.
Given:
String[] elements = {"for", "tea", "too"};
String first = (elements.length>0) ? elements[0] : null;
What is the result?
Correct Answer
D. The variable first is set to elements[0].
Explanation
The correct answer is "The variable first is set to elements[0]." In this code, the ternary operator is used to check if the length of the elements array is greater than 0. If it is, the value of the first element in the array (elements[0]) is assigned to the variable first. Otherwise, null is assigned to first. Since the length of the elements array is 3, the variable first will be set to the value of elements[0], which is "for".
8.
Given:
class Alligator {
public static void main(String[] args) {
int[] x[] = { { 1, 2 }, { 3, 4, 5 }, { 6, 7, 8, 9 } };
int y[][] = x;
System.out.print(y[2][1]);
}
}
What is the result?
Correct Answer
E. 7
Explanation
The code declares and initializes a 2D array called "x" with three rows and different number of columns in each row. Then, it declares another 2D array called "y" and assigns "x" to it. Finally, it prints the value at index [2][1] of array "y", which is 7. Therefore, the result is 7.
9.
Given:
public class Barn {
public static void main(String[] args) {
new Barn.go("hi", 1);
new Barn.go("hi", "world", 2);
}
public void go(String... y, int x) {
System.out.print(y[y.length - 1] + " ");
}
}
What is the result?
Correct Answer
D. Compilation fails.
Explanation
The code will not compile because the method go() is not static, but it is being called from the static main() method without creating an instance of the Barn class. The go() method should either be made static or an instance of the Barn class should be created before calling the go() method.
10.
Given:
1. class Super{
2. private int a;
3. protected Super(int a){this.a = a;}
4. }
...
11. class Sub extends Super{
12. public Sub(int a){super(a);}
13. public Sub(){this.a = 5;}
14. }
Which two, independently, will allow Sub to compile? (Choose two.)
Correct Answer(s)
C. Change line 13 to:
public Sub(){this(5);}
D. Change line 13 to:
public Sub(){super(5);}
Explanation
The first change, changing line 13 to "public Sub(){this(5);}", will allow Sub to compile because it calls the constructor of Sub class with an argument of 5.
The second change, changing line 13 to "public Sub(){super(5);}", will also allow Sub to compile because it calls the constructor of the Super class with an argument of 5.
Both of these changes provide a valid way to initialize the Sub class without any compilation errors.