SCJP : Flow Control Mock Exam Practice Questions
Questions no -1
What is the output for the below code ?
public class Test{
public static void main(String[] args) {
int i1=1;
switch(i1){
case 1:
System.out.println("one");
case 2:
System.out.println("two");
case 3:
System.out.println("three");
}
}
}
options
A)one two three
B)one
C)one two
D)Compile error.
Correct answer is : A
Explanations : There is no break statement in case 1 so it causes the below case statements to execute regardless of their values.
Questions no -2
What is the output for the below code ?
public class Test {
public static void main(String[] args) {
char c = 'a';
switch(c){
case 65:
System.out.println("one");break;
case 'a':
System.out.println("two");break;
case 3:
System.out.println("three");
}
}
}
options
A)one two three
B)one
C)two
D)Compile error - char can't be in switch statement.
Correct answer is : C
Explanations : Compile properly and print two.
|