Test Series - java

Test Number 35/64

Q: Choose TRUE or FALSE. A SWITCH can be used to compare values for high or low.
A. FALSE
B. TRUE
C. none
D. ...
Solution: More or Less conditions can not be checked with a SWITCH statement in Java.
Q: State TRUE or FALSE. It is allowed to use duplicate case constants inside a Java SWITCH statement.
A. FALSE
B. TRUE
C. none
D. ...
Solution: SWITCH case constants must be unique.
Q: State TRUE or FALSE. SWITCH works faster than the IF-ELSE ladder in Java.
A. FALSE
B. TRUE
C. none
D. ...
Solution: The compiler (JIT-Just In Time) creates a JUMP-TABLE for switch case branchings. So, it does not take time during Runtime. So, the SWITCH statement is fast.
Q: Choose the correct statement about Java SWITCH statements.
A. A SWITCH can contain another SWITCH statement.
B. Switch case statements are allowed inside IF-ELSE ladders.
C. Switch statements are allowed inside Loops like for, while and do while.
D. All
Solution: no solution
Q: What is the output of the below Java program?
int hours = 10;
switch(hours)
{
  case 10: System.out.println("TEN");break;
  case 10: System.out.println("TEN AGAIN"); break;
  default: System.out.println("TEN AS USUAL");
}
A. TEN
B. TEN AGAIN
C. TEN AS USUAL
D. Compiler error
Solution: Case constant 10 is duplicate. So, it causes compiler error.
Q: What is the output of the below Java program with SWICH and ENUM?
static enum ANIMAL {GOAT, TIGER, CAMEL}
public static void main(String args[])
{
  ANIMAL ani = ANIMAL.CAMEL;
  switch(ani)
  {
    case GOAT: System.out.println("GOAT");break;
    case CAMEL: System.out.println("CAMEL");break;
    case TIGER: System.out.println("TIGER");break;
  }

}
A. CAMEL
B. GOAT
C. TIGER
D. Compiler error
Solution: A SWITCH in java works well with enum constants. CASE Constants are defined without enum type.
Q: What is the output of the below Java program with SWITCH and Strings?
String phone = "APPLE";
switch(phone)
{
case "Apple": System.out.println("Apple");break;
case "APPLE": System.out.println("APPLE");break;
case "SAMSUNG": System.out.println("SAMSUNG");
}
A. Apple
B. APPLE
C. SAMSUNG
D. Compiler error
Solution: Apple and APPLE are different strings.

You Have Score    /7