Site icon DataFlair

Quiz on Switch Case in Java

quiz on switch case in java

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.

Quiz is loading…

You must sign in or sign up to start the quiz.

You must first complete the following:

Results

Quiz complete. Results are being recorded.

Results

0 of 15 Questions answered correctly

Your time:

Time has elapsed

You have reached 0 of 0 point(s), (0)

Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)

Categories

  1. Not categorized 0%
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  1. Current
  2. Review / Skip
  3. Answered
  4. Correct
  5. Incorrect
  1. Question 1 of 15
    1. Question

    Which of the statements in switch case is executed when all the case statements are false ?

    Correct
    Incorrect
  2. Question 2 of 15
    2. Question

    Which of the following is the syntax of a switch statement ?

    Correct
    Incorrect
  3. 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
  4. Question 4 of 15
    4. Question

    What is the use of a break statement in switch cases ?

    Correct
    Incorrect
  5. 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
  6. 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
  7. Question 7 of 15
    7. Question

    Which of the following can be an input for a Switch case ?

    Correct
    Incorrect
  8. 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
  9. 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
  10. Question 10 of 15
    10. Question

    Which of the following raises an error in the switch case ?

    Correct
    Incorrect
  11. 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
  12. 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
  13. Question 13 of 15
    13. Question

    Which of the following is not possible in switch statements ?

    Correct
    Incorrect
  14. 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
  15. 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.

Exit mobile version