Q: Choose TRUE or FALSE. A SWITCH can be used to compare values for high or low.
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.
Solution: SWITCH case constants must be unique.
Q: State TRUE or FALSE. SWITCH works faster than the IF-ELSE ladder in Java.
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.
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"); }
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; } }
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"); }
Solution: Apple and APPLE are different strings.
You Have Score    | /7 |