Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Mastering the `switch` statement is a fundamental step in your Java programming journey. This versatile control flow structure allows you to efficiently handle multiple conditions and execute specific code blocks based on the matching case.
This interactive quiz by DataFlair challenges your understanding of the `switch` statement in Java. Test your knowledge on key aspects like identifying the statement executed when no cases match, handling primitive data types and strings within cases, and utilizing the `break` statement for proper control flow. Let’s test your knowledge.
Time limit: 0
Quiz Summary
0 of 15 Questions completed
Questions:
Information
You have already completed the quiz before. Hence you can not start it again.
Which of the statements in switch case is executed when all the case statements are false ?
Correct
Incorrect
Question 2 of 15
2. Question
Which of the following is the syntax of a switch statement ?
Correct
Incorrect
Question 3 of 15
3. Question
class Switch {
public static void main ( String args [ ] )
{
boolean a = true;
switch(a)
{
case true:
{
System.out.println(“Boolean values are accepted”);
}
case false:
{
System.out.println(“Boolean values are not accepted”);
}
default:
{
System.out.println(“No values are accepted”);
}
}
}
}
What will be the output of the program ?
Correct
Incorrect
Question 4 of 15
4. Question
What is the use of a break statement in switch cases ?
Correct
Incorrect
Question 5 of 15
5. Question
class Sample {
public static void main ( String args [ ] )
{
int x = 3;
switch(x)
{
case 1:
{
System.out.println(1*1);
}
case 2:
{
System.out.println(2*2);
}
default:
{
System.out.println(“The given value is not present in the case statements”);
}
}
}
}
What will be the output of the program ?
Correct
Incorrect
Question 6 of 15
6. Question
class SwitchStatement {
public static void main ( String args [ ] )
{
char i = ‘a’;
switch(i)
{
case b;
System.out.println(“Second alphabet”);
}
}
}
}
What is the output of the program ?
Correct
Incorrect
Question 7 of 15
7. Question
Which of the following can be an input for a Switch case ?
Correct
Incorrect
Question 8 of 15
8. Question
class Statement {
public static void main ( String args [ ] )
{
int a = 10;
switch(a)
{
case 10:
{
System.out.println(“First case statement”);
}
case 10:
{
System.out.println(“Second case statement”);
}
default:
{
System.out.println(“Default case is executed”);
}
}
}}
What is the error in the program ?
Correct
Incorrect
Question 9 of 15
9. Question
Class Nested {
public static void main ( String args [ ] )
{
int a = 1;
int b = 2;
switch(a)
{
case 1:
switch(b)
{
case 1:
{
System.out.println(“WebServices”);
}
case 2:
{
System.out.println(“DataFlair”);
}
}
}
default:
{
System.out.println(“No statements are executed”);
}
}
}
}
What is the output of the program ?
Correct
Incorrect
Question 10 of 15
10. Question
Which of the following raises an error in the switch case ?
Correct
Incorrect
Question 11 of 15
11. Question
class Break {
public static void main ( String args [ ] )
{
String s = “One”;
switch(s)
{
case one:
{
System.out.println(“1);
}
case two:
{
System.out.println(“2”);
}
}
}
}
What will be the output of the program ?
Correct
Incorrect
Question 12 of 15
12. Question
class SwitchCase {
public static void main ( String args [ ] )
{
int case = 10/2;
switch(case)
{
Case 1:
{
System.out.println(“Ten divided by two is one”);
break;
}
Case 2:
{
System.out.println(“Ten divided by two is Three”);
break;
}
Case 3:
{
System.out.println(“Ten divided by two is Five”);
break;
}
default:
{
System.out.println(“Ten is indivisible by two”);
}
}
}
}
What is the output of the program ?
Correct
Incorrect
Question 13 of 15
13. Question
Which of the following is not possible in switch statements ?
Correct
Incorrect
Question 14 of 15
14. Question
class Mathematics {
public static void main ( String args [ ] )
{
String x = “add”;
String y = “subtract”;
switch(x)
{
int a = 10 ;
int b = 15;
case “add”:
{
System.out.println(a+b);
}
case “subtract”:
{
System.out.println(b-a);
}
}
}
}
What is the output of the program ?
Correct
Incorrect
Question 15 of 15
15. Question
class Example {
public static void main ( String args [ ] )
{
int a = 5;
switch(a)
{
case 3:
{
System.out.println(3+3);
}
break;
}
case 5:
{
System.out.println(5+5);
}
}
} What will be the output of the program ?
Correct
Incorrect
Summary:
Have you just conquered the `switch` statement quiz? Congratulations! This interactive challenge was designed to be both informative and enjoyable, helping you assess your grasp of this fundamental Java control flow structure.
By tackling these questions, you’ve gained valuable insights into how to effectively leverage the `switch` statement to make your Java code cleaner, more efficient, and easier to understand.
Remember, a solid understanding of control flow structures is vital for writing well-structured and efficient Java programs. The `switch` statement plays a crucial role in this by providing a concise way to handle multiple conditions.
By mastering its use, you can streamline your code, avoiding lengthy chains of `if-else` statements and improving readability for yourself and other developers.
You give me 15 seconds I promise you best tutorials Please share your happy experience on Google
DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.
Recheck quiz. Wrong questions and wrong answers given.