Home »
Java »
Java find output programs
Java find output programs (switch case) | set 3
Find the output of Java programs | switch case | Set 3: 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) {
String val = "India";
switch (val.charAt(0)) {
case "I":
System.out.println("@@@@@@@@@@@@@@@");
break;
case 'i':
System.out.println("$$$$$$$$$$$$$$$");
break;
case 'I':
System.out.println("!!!!!!!!!!!!!!!!");
break;
default:
System.out.println("###############");
break;
}
}
}
Output:
Main.java:6: error: incompatible types: String cannot be converted to char
case "I":
^
1 error
Explanation:
The above program will generate a syntax error because we used "I" which is a string. But we passed val.chatAt(0) in the switch that's why we can use the only character in the cases within switch block.
Note: One or more character(s) are enclosed with double quotes, is known as a string. And character is represented using single quotes C#
Question 2:
public class Main {
public static void main(String[] args) {
String val = "India";
switch (val.charAt(0)) {
case 'i':
System.out.println("$$$$$$$$$$$$$$$");
break;
case 'I':
System.out.println("!!!!!!!!!!!!!!!!");
break;
default:
System.out.println("###############");
break;
}
}
}
Output:
!!!!!!!!!!!!!!!!
Explanation:
In the above program, we declared a string variable val, which is initialized with "India" and we pass val.chatAt(0) in the switch block, it will access the first character from the string that is 'I', that's why " case 'I' " will execute and print "!!!!!!!!!!!!!!!!" on the console screen.
Question 3:
public class Main {
public static void main(String[] args) {
String val = "India";
switch (val[0]) {
case 'i':
System.out.println("$$$$$$$$$$$$$$$");
break;
case 'I':
System.out.println("!!!!!!!!!!!!!!!!");
break;
default:
System.out.println("###############");
break;
}
}
}
Output:
Main.java:5: error: array required, but String found
switch (val[0]) {
^
1 error
Explanation:
The above program will generate a syntax error because we cannot access characters from a string using subscript "[]" operator in java, here we need to use charAt() method.
Question 4:
public class Main {
public static void main(String[] args) {
int val = 66;
switch (val) {
case 'B':
System.out.println("$$$$$$$$$$$$$$$");
break;
case 'b':
System.out.println("!!!!!!!!!!!!!!!!");
break;
default:
System.out.println("###############");
break;
}
}
}
Output:
$$$$$$$$$$$$$$
Explanation:
In the above program, we created a local variable val which is initialized with 66. As we know that the ASCII value of 66 is 'B" that’s why (case 'B') will execute and print "$$$$$$$$$$$$$$" on the console screen.
Question 5:
public class Main {
public static void main(String[] args) {
int val = 0x0C;
switch (val) {
case 11:
System.out.println("$$$$$$$$$$$$$$$");
break;
case 12:
System.out.println("!!!!!!!!!!!!!!!!");
break;
default:
System.out.println("###############");
break;
}
}
}
Output:
!!!!!!!!!!!!!!!!
Explanation:
In the above program, we created a class Main that contains main() method, In the main() method we created an integer variable val which is initialized with hex value 0x0C, and we know that the hex value 0x0C is equivalent to 12 in decimal numbers. That's why "case 12" will execute and print "!!!!!!!!!!!!!!!!" on the console screen.