Today, I’m going to review my online test, chapter 2 of Oracle Certified Associate (OCA), provided by SYBEX. If you need to access to this online resources, you need to buy their book first. See https://sybextestbanks.wiley.com/.
Question 3
What is the output of the following application?
1
2
3
4
5
6
7
8
public class CompareValues {
public static void main(String[] args) {
int x = 0;
while (x++ < 0) {}
String message = x > 10 ? "Greater than" : false;
System.out.println(message + "," + x);
}
}
Greater than,10
false,10
Greater than,11
false,11
- The code will not compile because of line 4.
- The code will not compile because of line 5.
The answer is F. In this question, the ternary operator has two
expressions, one of them a String
and the other a boolean
. The ternary
operator is permitted to have expressions that don’t have matching types, but
the key here is the assignment to the String
reference. The compiler knows
how to assign the first expression value as a String
, but the second boolean
expression cannot be set as a String
; so this line does not compile.
Question 9
How many times will the following code print "Hello World"
?
1
2
3
4
for (int i = 0; i < 10; ) {
i = i++;
System.out.println("Hello World");
}
- 9
- 10
- 11
- The code will not compile because of line 1.
- The code will not compile because of line 3.
- The code contains an infinite loop and does not terminate.
The answer is F. In this example, the update statement of the for loop is
missing, which is fine as the statement is optional, so the option D is
incorrect. The expression inside the loop increments i
but then assigns i
the old value. Therefore, i
ends up with the same value that it starts with:
0
. The loop will repeat infinitely and outputting always 0
.