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

Discussion

When 2 is entered, The output of the code below is?

#include <stdio.h>
    void main()
    {
        int ch;
        printf("enter a value btw 1 to 2:");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            printf("1\n");
            break;
            printf("Hi");
        default:
            printf("2\n");
        }
    }

 

  • A.1
  • B.Hi 2
  • C.Run time error
  • D.2

Answer: D

Correct answer: 2

ch is 2, which matches no case, so control goes straight to default and prints 2. The printf("Hi") is unreachable in any event: it sits immediately after a break and before the next label, so no path can reach it.

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

Post your comment

Please log in or register to post a comment.