Home »
Java »
Java find output programs
Java find output programs (switch case) | set 2
Find the output of Java programs | switch case | Set 2: Enhance the knowledge of Java switch case concepts by solving and finding the output of some Java programs.
Submitted by Nidhi, on January 30, 2021
Question 1:
public class Main {
public static void main(String[] args) {
int num = -4;
switch (num) {
case 5 - 1 : System.out.println("www.includehelp.com");
break;
case 15 - 11 : System.out.println("www.google.com");
break;
default:
System.out.println("Wrong option");
break;
}
}
}
Output:
Main.java:8: error: duplicate case label
case 15 - 11 : System.out.println("www.google.com");
^
1 error
Explanation:
The above program will generate a syntax error because we cannot use duplicate cases in the switch block.
Question 2:
public class Main {
public static void main(String[] args) {
int num = -4;
switch (num) {
default:
System.out.println("Wrong option");
case 5 - 1 : System.out.println("www.includehelp.com");
break;
case 15 - 12 : System.out.println("www.google.com");
break;
}
}
}
Output:
Wrong option
www.includehelp.com
Explanation:
In the above program, we create a local variable initialized with -4. Here we did not use the break statement in default case that's why it will execute default case as well as "case 5-1".
Question 3:
public class Main {
public static void main(String[] args) {
switch (14 % 4) {
case 1:
System.out.println("www.includehelp.com");
break;
case 2:
System.out.println("www.fb.com");
case 3:
System.out.println("www.google.com");
break;
default:
System.out.println("wrong choice");
break;
}
}
}
Output:
www.fb.com
www.google.com
Explanation:
In the above program, we used "14%4" in switch condition, then it will return 2 then "case 2" will execute but we did not use break statement in "case 2" that’s why "case 3" will also execute.
Question 4:
public class Main {
public static void main(String[] args) {
switch (30 * 30 / 30) {
case 10:
System.out.println("www.includehelp.com");
break;
case 20:
System.out.println("www.fb.com");
break;
case 30:
System.out.println("www.google.com");
break;
default:
System.out.println("wrong choice");
break:
}
}
}
Output:
Main.java:15: error: ';' expected
break:
^
1 error
Explanation:
The above program will generate a syntax error because we below statement.
default:
System.out.println("wrong choice");
break:
Here we used colon ":" instead of ";" in above break statement.
Question 5:
public class Main {
public static void main(String[] args) {
String STR = "hello";
switch (STR) {
case "HELLO":
System.out.println("@@@@@@@@@@@@@@@");
break;
case "hello":
System.out.println("$$$$$$$$$$$$$$$");
break;
case "STR":
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!");
break;
default:
System.out.println("###############");
break;
}
}
}
Output:
$$$$$$$$$$$$$$$
Explanation:
In the above program, we created a class Main that contains a main() method, in the main() method we created a string variable STR, which is initialized with "hello". Then (case "hello") will execute and print "$$$$$$$$$$$$$$$" on the console screen.