Flow Control
Flow Control
Flow Control
2. switch(x)
{
default:
System.out.println("Hello");
}
Which two are acceptable types for x?
1. byte
2. long
3. char
4. float
5. Short
6. Long
A. 1 and 3
B. 2 and 4
C. 3 and 5
D. 4 and 6
Answer: Option A
Explanation:
Switch statements are based on integer expressions and since both bytes and chars can implicitly be
widened to an integer, these can also be used. Also shorts can be used. Short and Long are wrapper
classes and reference types can not be used as variables.
{
x++;
}
System.out.println("x = " + x +" y = " + y);
A. x=6y=0
B. x=7y=0
C. x = 6 y = -1
D. Compilation fails.
Answer: Option D
Explanation:
Compilation fails because the while loop demands a boolean argument for it's looping condition, but in
the code, it's given an int argument.
while(true) { //insert code here }