Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

#include <stdio.h>
int main()
{
    int i=4;
    switch(i)
    {
        default:
           printf("This is default\n");
        case 1:
           printf("This is case 1\n");
           break;
        case 2:
           printf("This is case 2\n");
           break;
        case 3:
           printf("This is case 3\n");
    }
    return 0;
}

 

  • A.This is default
    This is case 1
  • B.This is case 3
    This is default
  • C.This is case 1
    This is case 3
  • D.This is default

Answer: A

Correct answer: This is default This is case 1

i is 4, which matches no case, so control jumps to default - and default need not be last. Having no break, execution then falls through into case 1, whose break finally ends the switch. So two lines are printed.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.