Test Series - java

Test Number 34/64

Q: What is the output of the Java program below?
String animal = "GOAT";
switch(animal)
{
  break: System.out.println("DOMESTIC");
}
A. No output
B. GOAT
C. DOMESTIC
D. Compiler error
Solution: Case statements should start with either "case" or "default" only.
Q: What is the output of the Java program below?
String college = "OXFORD";
switch("STANFORD")
{
case college: System.out.println("EXAM TIME"); break;
default: System.out.println("UNKNOWN");
}
A. EXAM TIME
B. UNKNOWN
C. STANFORD
D. Compiler error
Solution: case expressions must be constant expressions.
So, make the variable final.
final String college = "OXFORD";
Q: What is the output of Java program with SWITCH?
int num=20;
switch(num)
{
case 10: System.out.println("TEN"); break;
case 20: System.out.println("TWENTY"); break;
case 30: System.out.println("THIRTY");
}
A. TEN
B. TWENTY
C. THIRTY
D. TEN TWENTY
Solution: no solution
Q: What is the output of Java program below?
int num=40;
switch(num)
{
case 5: System.out.println("FIVE"); break;
case 35+5: System.out.println("FORTY"); break;
case 20+30: System.out.println("FIFTY");
}
A. FIVE
B. FORTY
C. FIFTY
D. Compiler error
Solution: It is allowed to write expressions that result in constant values.
Q: What is the output of the below Java program?
int persons = 45;
int random = 45;
switch(random)
{
  case persons: System.out.print("CRICKET ");
  default: System.out.println("RUGBY");
}
A. CRICKET
B. CRICKET RUGBY
C. RUGBY
D. Compiler error
Solution: Error: case expressions must be constant expressions
So, make the variable final.
final int persons = 45;
//Then, output will be
CRICKET
Q: What is the output of the below Java program?
switch(15)
{
  case 5*2: System.out.println("TEN");break;
  case 5*4-5:System.out.println("FIFTEEN");break;
  case 60/4+5: System.out.println("TWENTY");
}
A. TEN
B. FIFTEEN
C. TWENTY
D. Compiler error
Solution: Any expression that results in a Constant can be used as a "case constant".
Q: A SWITCH fall through occurs in Java only in the absence of ___.
A. case keyword
B. break keyword
C. default keyword
D. None
Solution: no solution
Q: What is the purpose of designing a SWITCH logic with a fall-through in Java?
A. To define ranges.
B. To define additions
C. To improve switch block performance
D. None
Solution: This is the way we define ranges in a SWITCH construct.
Q: Does the following Java code-snippet compile?
switch(45)
{
  case 10: ;
}
A. NO
B. YES
C. ....
D. None
Solution: You can specify dummy statements in java using a Semicolon (;).
Q: What is the output of the below Java program with a SWITCH statement?
int points=6;
switch(points)
{
  case 6: ;
  case 7: System.out.println("PASS");break;
  case 8: ;
  case 9: System.out.println("Excellent");break;
  case 10: System.out.println("Outstanding"); break;
  default: System.out.println("FAIL");
}
A. PASS
B. Excellent
C. Outstanding
D. FAIL
Solution: This is the way we define ranges in a SWITCH construct.

You Have Score    /10