Switch-Case MCQ in C-2

Switch-Case MCQ in C-2 | The switch case statement in C programming is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly. Two keywords are used in the program:- switch and case.

If you are finding any difficulties to answer these SWITCH-CASE statement MCQ in C language then it is recommended to learn these:-

In the previous SWITCH-CASE statement MCQ in C Part-1 we have seen some multiple-choice questions and programs on switch-case statements. Now, let us see some more programs in C.


Q1) Find the output of the below program?

#include<stdio.h>
int main()
{
  int a = 5;
  switch(a)
  {
    case 1:
       printf("Welcome");
       break;
    case 2:
       printf("Know Program");
       break;
  }
  printf("After switch-case");
  return 0;
}

a) Welcome
b) Know Program
c) Compile-time error
d) After switch-case

View Answer Answer:- d) After switch-case

Using the default case in the switch-case block is optional. In this program, a=5 doesn’t match with any case therefore the compiler searches for the default case but it is also not available. Hence the compiler will continue to execute remaining lines of code.

Q2) What is the output of the given below program?

#include<stdio.h>
int main()
{
  int a = 1;
  switch(a + 1)
  {
    case 1:
       printf("Welcome");
       break;
    case 2:
       printf("Know Program");
       break;
    default:
       printf("Hello");
       break;
  }
  return 0;
}

a) Welcome
b) Know Program
c) Hello
d) Compile-time error

View Answer Answer:- b) Know Program

Initially a=1, but the switch block is searching for a case with value a = a + 1 => 1+1 = 2. Hence case 2 will be executed.

Q3) What is the output of the given below program?

#include<stdio.h>
#define n 5
int main()
{
  int a = 5;
  switch(a, a*3)
  {
    case n:
       printf("Welcome");
       break;
    case n*3:
       printf("Hi");
       break;
    case n*5:
       printf("Know Program");
       break;
    default:
       printf("Hello");
  }
  return 0;
}

a) Welcome
b) Hi
c) Know Program
d) Hello
e) Compile-time error

View Answer Answer:- b) Hi

Initially a = 5, switch(a, a3) => switch(5, 53) => switch(5, 15). In comma operator right most value is considered as final value hence it becomes => switch(15). The compiler will search for case 15. Since n = 5, therefore case n*3 is matched and “Hi” will be printed to the screen.

Q4) What will be the output of the below C program?

#include<stdio.h>
int main()
{
  int a = 4;
  switch(a)
  {
    case 1:
       printf("Welcome");
       break;
    case 1+2:
       printf("Hi");
       break;
    case 1+3:
       printf("Know Program");
       break;
    default:
       printf("Hello");
  }
  return 0;
}

a) Welcome
b) Hi
c) Know Program
d) Hello
e) Compile-time error

View Answer Answer:- c) Know Program

In case labels, we must use integer constant. Here all labels contain integer constants therefore it is valid, we won’t get compile time errors. The case 1+3 will be executed.

Q5) Find the output of the given program?

#include<stdio.h>
int main()
{
  int a = 3;
  switch(a)
  {
    printf("1");
    default:
       printf("2");
    case 2:
       printf("3");
    case 3:
       printf("4");
    case 4:
       printf("5");
  }
  printf("6");
}

a) 1246
b) 146
c) 4
d) 456
e) 46

View Answer Answer:- d) 456

The default statement can be placed anywhere in the switch block. For value a = 3, case 3 is matched, and it will be executed but it doesn’t contain a break statement therefore all remaining cases also will be executed.

Q6) Find the output of the given program?

#include<stdio.h>
#define True 1
int main()
{
  switch(True)
  {
    printf("Know Program");
  }
  return 0;
}

a) Know Program
b) Compile time error
c) Compiled Successfully, No Output
d) None of these

View Answer Answer:- c) Compiled Successfully, No Output

In switch condition we can use constant.

Q7) What will be the output of the below C program?

#include<stdio.h>
int main()
{
  int a = 3;
  int n = a;
  
  switch(n)
  {
    case 3:
       printf("Welcome");
       break;
    case a:
       printf("Know Program");
       break;
    default:
       printf("Hello");
       break;
  }
  return 0;
}

a) Welcome
b) Know Program
c) Hello
d) Compile-time error

View Answer Answer:- d) Compile-time error

In the case label, we can use only the constant value but not the variable. Hence for “case a: ” we will get a compile time error: case label does not reduce to an integer constant case a:

Q8) Find the output of the given program?

#include<stdio.h>
int main()
{
  int a = 3;
  switch(a)
  {
    case 3: || case 5:
       printf("Welcome");
       break;
    case 3:
       printf("Hello");
       break;
    default:
       printf("Know Program");
       break;
  }
  return 0;
}

a) Welcome
b) Know Program
c) Compile time error
d) Hello

View Answer Answer:- c) Compile time error

We can merge two cases like above. It will give compile time error: expected expression before ‘||’ token.

Q9) What will be the output of the below C program?

#include<stdio.h>
int main()
{
  int a = 1;
  switch(a)
  {
    case 1:
       printf("Hello");
       break;
    case 1.0:
       printf("Hi");
       break;
    default:
       printf("Know Program");
       break;
  }
  return 0;
}

a) Hello
b) Hi
c) Know Program
d) Compile time error

View Answer Answer:- d) Compile time error

In the case label, we must use only constant integer values. Here we used case 1.0 which is invalid.

Q10) Find the output of the given program?

#include<stdio.h>
int main()
{
  int a = 1;
  switch(a)
  {
    case 1L:
       printf("Hello");
       break;
    case 2L:
       printf("Hi");
       break;
    default:
       printf("Know Program");
       break;
  }
  return 0;
}

a) Hello
b) Hi
c) Know Program
d) Error

View Answer Answer:- a) Hello

The 1L, 2L are also integer constant values therefore we can use them in our program.

Q11) What will be the output of the below C program?

#include<stdio.h>
int main()
{
  int a = 1;
  switch(a)
  {
    case 1:
       printf("Hello");
       break;
    case 1L:
       printf("Hi");
       break;
    default:
       printf("Know Program");
       break;
  }
  return 0;
}

a) Hello
b) Hi
c) Know Program
d) Compile time error

View Answer Answer:- d) Compile time error

In this program 1L and 1 represent the same case. Therefore we will get compile time error:- duplicate case value 1L, previously used here case 1:

Q12) What will be the output of the below C program?

#include<stdio.h>
int main()
{
  int a = 2;
  switch(a)
  {
    a = a+1;
    case 1:
       printf("X");
       break;
    case 2:
       printf("Y");
       break;
    case 3:
       printf("Z");
       break;
  }
  return 0;
}

a) X
b) Y
c) Z
d) Compile-time error

View Answer Answer:- b) Y

Initially the value of a is 2. Hence the compiler will execute case 2.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *