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
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;
}
}
}
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;
}
}
}
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;
}
}
}
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;
}
}
}
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;
}
}
}
Answer Question 1:
Output:
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#
Answer Question 2:
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.
Answer Question 3:
Output:
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.
Answer Question 4:
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.
Answer Question 5:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer