Example HTML page

C MCQ Practice: Switch Statement

I have given some example programs, guess the output of the following code with out checking the output.

The output is given for only reference.

1. What is the output of the following code. Assume the choice given is 1.

   #include <stdio.h>
    void main()
    {
        int ch;
        printf("Enter a choice between 1 and 2");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("Sudhakar");
            break;
        case 2:
            printf("Hello");
            break;
        default:
        	printf("not correct");
        	break;
        }
    }
Example 1

 

Output:

Command Prompt
Enter a choice between 1 and 2
1
Sudhakar
——————————–
Process exited after 1.848 seconds with return value 8
Press any key to continue . . .

2.  What is the output of the following code.

#include<stdio.h>
int main()
{
   int var = 1;
   switch (var)
   {
       var = var + 1;  // This line is not executed
       case 1: printf("Your choice is one\n");
               break;
       case 2: printf("Your choice is two\n");
                break;
       default: printf("Choice other than 1 and 2\n");
                break;                   
   }
   printf("%d",var);
   return 0;
} 
Example 2

Output:

Command Prompt
Your choice is one
1
——————————–
Process exited after 0.01511 seconds with return value 0
Press any key to continue . . .

3.  What is the output of the following code. Choice is entered as 1.

#include <stdio.h>
    void main()
    {
        int ch,a=20,b=20,c;
        printf("enter a choice:");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("Sudhakar\n");
        case 2:
            printf("Akshaya\n");
        case 3:
            printf("Kaivalya\n");
        case 6: 
            c=a+b;
            break;
        
        }
        printf("%d",c);
    }
Example 3

Output:

Command Prompt
enter a choice:1
Sudhakar
Akshaya
Kaivalya
40
——————————–
Process exited after 1.663 seconds with return value 2
Press any key to continue . . .

4.  What is the output of the following code. Choice is entered as 3.

#include <stdio.h>
int main()
{
   int x = 4;
   switch (x)
   {
       default: printf("Choice other than 1 and 2");
                break;      
       case 1: printf("Choice is 1");
               break;
       case 2: printf("Choice is 2");
                break;
   }
   return 0;
}
Example 4

Output:

Command Prompt
Choice other than 1 and 2
——————————–
Process exited after 0.1645 seconds with return value 0
Press any key to continue . . .

5. What is the output of the following code.

#include<stdio.h>
void main()
{
int choice = 1 ;
    switch(choice)
    {
      case 'A':
      case 'a':
            printf("This is First");
            break;
      case 'B':
      case 'b':
            printf("This is second");
            break;
    }
}
Example 5

Output: No output is generated for the above code.

6. What is the output of the following code.

#include<stdio.h>
void main()
{
int choice = 'A' ;
    switch(choice)
    {
      case 'A':
      case 'a':
            printf("This is First");
            break;
      case 'B':
      case 'b':
            printf("This is second");
            break;
    }
}
Example 6

Output:

Command Prompt
This is First
——————————–
Process exited after 0.01491 seconds with return value 13
Press any key to continue . . .

7. What is the output of the following code. Choice is given as 1.

#include <stdio.h>
    void main()
    {
        int ch;
        printf("Select a choice from 1 to 2");
        scanf("%d", &ch);
        switch (ch, ch + 1)
        {
        case 1:
            printf("1\n");
            break;
        case 2:
            printf("2");
            break;
        }
    }
Example 7

Output:

Command Prompt
Select a choice from 1 to 2
1
2
——————————–
Process exited after 1.545 seconds with return value 50
Press any key to continue . . .

8. What is the output of the following code.

#include <stdio.h>
    int main()
    {
        int p = 1, q = 1;
        switch (p)
        {
        case p*q:
            printf("yes ");
        case p-q:
            printf("no\n");
            break;
        }
    }
Example 8

Output: Compile time error.

The error is:

[Error] case label does not reduce to an integer constant

9. What is the output of the following code.

 #include <stdio.h>
    int main()
    {
        int var = 98;
        switch (var)
        {
        case 'b':
            printf("yes ");
            break;
        case 98:
            printf("no\n");
            break;
        }
    }
Example 9

Output:

Compile time error.

The error is:

[Error] duplicate case value or previously used here.

10. What is the output of the following code.

#include<stdio.h>
void main()
{
int choice = 1 ;
    switch(choice,choice+1,choice+2)
    {
       case 1:
           printf("Sudhakar\n");
           break;
       case 2:
           printf("Rathnam\n");
           break;
       case 3:
           printf("Ramana\n");
           break;
    }
}
Example 10

Output:

Command Prompt

Ramana

——————————–
Process exited after 0.01363 seconds with return value 0
Press any key to continue . . .

Note:  Comma operator return the right most number. In the ‘3’ parameter right most is ‘choice+2’ is returned and hence the selected case is 3.

11. What is the output of the following code.  (Macros)

#include<stdio.h>
#define var 1
void main()
{
 int choice = 1 ;
 switch(choice)
 {
  case var: 
        printf("Sudhakar\n");
        break;

  case 2:
        printf("Rathnam\n");
        break;

  case 3:
        printf("Ramana\n");
        break;
 }
}
Example 11

Output:

Command Prompt

Sudhakar

——————————–
Process exited after 0.009326 seconds with return value 0
Press any key to continue . . .

Note: Macro is replaced var with value before the execution starts

Example HTML page

Leave a Reply

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

Pin It on Pinterest