Knowee
Questions
Features
Study Tools

What will be the output of the program?public class Sample   {    final static short a = 2;    public static int b = 0;    public static void main(String [] args)      {        for (int c=0; c < 4; c++)        {            switch (c)   {                case a: System.out.print("a ");                default: System.out.print("default ");                case a-1: System.out.print("a-1 ");                            break;                case a-2: System.out.print("a-2 ");            }        }    }}Select one:a.a-2 a-1 a default defaultb.a default a-1c.a-2 a-1 a default a-1 default a-1d.a-2 a-1 a default a-1

Question

What will be the output of the program?public class Sample   {    final static short a = 2;    public static int b = 0;    public static void main(String [] args)      {        for (int c=0; c < 4; c++)        {            switch (c)   {                case a: System.out.print("a ");                default: System.out.print("default ");                case a-1: System.out.print("a-1 ");                            break;                case a-2: System.out.print("a-2 ");            }        }    }}Select one:a.a-2 a-1 a default defaultb.a default a-1c.a-2 a-1 a default a-1 default a-1d.a-2 a-1 a default a-1

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the program will be "a-2 a-1 a default default". So, the correct answer is option a.

Here's the step by step explanation:

  1. The loop starts with c=0. The switch statement checks for the case that matches c. In this case, it's "a-2" because a is 2 and a-2 is 0. So, it prints "a-2 ".

  2. After printing "a-2 ", it doesn't find a break statement, so it continues to the next case which is "a-1". It prints "a-1 " and then encounters a break statement. So, it breaks out of the switch statement.

  3. The loop continues with c=1. The switch statement checks for the case that matches c. In this case, it's "a-1" because a is 2 and a-1 is 1. So, it prints "a-1 " and then encounters a break statement. So, it breaks out of the switch statement.

  4. The loop continues with c=2. The switch statement checks for the case that matches c. In this case, it's "a" because a is 2. So, it prints "a ".

  5. After printing "a ", it doesn't find a break statement, so it continues to the default case. It prints "default " and then encounters a break statement. So, it breaks out of the switch statement.

  6. The loop continues with c=3. The switch statement doesn't find a case that matches c, so it goes to the default case. It prints "default " and then encounters a break statement. So, it breaks out of the switch statement.

  7. The loop ends because c is no longer less than 4.

This problem has been solved

Similar Questions

What will be the output of the program?public class Test { public static void main(String args[]) { int i = 1, j = 0; switch(i) { case 2: j += 6; case 4: j += 1; default: j += 2; case 0: j += 4; } System.out.println("j = " + j); } }j = 0j = 2j = 4j = 6

What will be the output of the following code?public class Solution { public static void main(String args[]) { int i = 5; switch(i) { case 1: System.out.print("Case 1" + " "); break; case 2: System.out.print("Case 2" + " "); break; case 3: System.out.print("Case 3" + " "); break; default: System.out.print("Value of i is greater than 3" + " "); } } }Options: Pick one correct answer from belowCase 1Case 2Case 3Value of i is greater than 3PrevSubmit MCQNext

What all gets printed when the following program is compiled and run.public class Test{ public static void main(String args[]){ int i, j=1; i = (j>1)?2:1; switch(i){ case 0: System.out.println(0); break; case 1: System.out.println(1); case 2: System.out.println(2); break; case 3: System.out.println(3); break; } }}01212

What is the output of the below program?public class Main{ public static void main(String[] args) { int a = 25; if(a > 5) System.out.print("Hi "); if(a < 20) System.out.print("Hello "); else System.out.print("Know Program "); }}

What will be the output of the following program?#include<stdio.h> int main() {     int i=4;     switch(i)     {         default:            printf("This is default.");         case 1:            printf("This is case 1");            break;         case 2:            printf("This is case 2");            break;         case 3:            printf("This is case 3");     }     return 0; }Select one:This is default.This is case 1This is case 3This is default.This is case 1

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.