Switch-Case C Programming Mock Test -1


Question 1
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int a=1;
    switch(a)
    {
        printf("GATE-CSE");
        
        case 0: printf("Zero");
        case 1: printf("One");
        default: printf("None");
    }
    return 0;
}
A
GATE-CSE One
B
GATE-CSE One None
C
One None
D
Compiler Error
   C Programming
Question 1 Explanation: 
In this program, printf("GATE-CSE"); statement is not part of any case. Compiler transfer control with Switch(0) to case 0 if found in switch block, otherwise check for default case in swich block.

Hence, printf("GATE-CSE"); statemt will not be executed by compiler.
Therefore, compiler execute case 1 and print One. As there is not break statement after that compiler continue execute all statements till break statement or end of switch block.
OUTPUT: One None

Click to Join Our Telegram Group for Latest Update of MOCK TEST
Question 2
What is the output of the following program?
#include < stdio.h > 
int main()
{   
    int a=5;
      switch(a)
    {
        default: printf("None ");
        case 1: printf("One ");
        case 2: printf("Two");
    }
    return 0;
}
A
None
B
None One Two
C
one Two None
D
Compiler Error
   C Programming
Question 2 Explanation: 
As there is no case 5 in switch block, so compiler execure default case
So, it will print None
As there is not break statement after that compiler continue execute all statements till break statement or end of switch block.
OUTPUT: None One Two

Click to Join Our Telegram Group for Latest Update of MOCK TEST


Question 3
What is the output of the following program?
#include < stdio.h > 
int main()
{
    switch(*("practicepaper"+5))
    {
        case 'a': printf("IITB ");
        default: printf("IITD ");
        case 'i': printf("IISC ");
    }
    return 0;
}
A
IITB IITD
B
IISC IITD
C
IISC
D
IITB IITD IISC
   C Programming
Question 3 Explanation: 
*("practicepaper"+5) return the index-5 char (with 'p' as index-0), which is char 'i'.
Compiler directly transfer control to case 'i'.
NOTE: default is written above case'i', but compiler executes default case only when no case matches with switch.

OUTPUT: IISC

Click to Join Our Telegram Group for Latest Update of MOCK TEST
Question 4
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int a=0;
    switch(a)
    {
        case 0: printf("Zero ");
        default: printf("None ");
        case 1: printf("One ");
    }
    return 0;
}
A
Zero None
B
Zero None One
C
None One
D
Compiler Error
   C Programming
Question 4 Explanation: 
Statement switch(a=0) evaluated by compiler as switch(0) (NOTE: a=0 transfer 0 into a and return 0)
Hence, case 0 match in switch block. It will print Zero, As there is not break statement after that compiler continue execute all statements till break statement or end of switch block.

OUTPUT: Zero None One

Click to Join Our Telegram Group for Latest Update of MOCK TEST
Question 5
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int a=2;
    switch(a=5)
    {
        default: printf("GATE-CSE ");
        case 0: printf("Zero ");
        case 1: printf("One ");
                break;
        case 2:printf("Two ");
        case 3:printf("Three ");
    }
    return 0;
}
A
Two Three
B
GATE-CSE Zero One
C
Zero One
D
Compiler Error
   C Programming
Question 5 Explanation: 
Statement switch(a=5) evaluated by compiler as switch(5) (NOTE: a=5 transfer 5 into a and return 5)
Now, there is not a case 5 in switch block.
NOTE: default is written at top in swich block but it will execute only when no case match in switch block.
As there is no case 5 in switch block, so compiler execure default case
So, it will print GATE-CSE
As there is not break statement after that compiler continue execute all statements till break statement or end of switch block.
OUTPUT: GATE-CSE Zero One

Click to Join Our Telegram Group for Latest Update of MOCK TEST


There are 5 questions to complete.

Leave a Comment