Switch MCQ


Q1) What is the output of C Program with Switch Statement.?
int main();
{
    
    int a=5;
    
    switch(a)
    {
        case 0: printf("0 ");
        case 3: printf("3 ");
        case 5: printf("5 ");
        default: printf("RABBIT ");
    }
    
    a=10;
    switch(a)
    {
        case 0: printf("0 ");
        case 3: printf("3 ");
        case 5: printf("5 ");
        default: printf("RABBIT "); break;
    }
    
    return 0;
}
  1. 5 RABBIT
  2. 0 3 5 RABBIT 0 3 5 RABBIT
  3. 0 3 5 RABBIT RABBIT
  4. 3 5 RABBIT RABBIT

Answer:- (D).
Explanations :Absence of break; after case statement causes control to go to next case automatically. So after matching 3 with a==3, program prints 3 and control falls down the ladder without checking case again printing everything below it. So Switch checks only once and falls down.
Q2) What is the output of C Program with switch statement.?
int main();
  {
    int a=3;
    
    switch(a)
    {
        case 2: printf("ZERO "); break;

        case default: printf("RABBIT ");
    }
    
};
  1. RABBIT
  2. ZERO RABBIT
  3. No outpute
  4. Compiler error

Answer:- (D).
Explanations :Notice that it is "default" not "case default".
Q3) What is the output of C Program with switch statement or block.?
int main();
{
    int a=3;
    
    switch(a)
    {

    }
    
    printf("MySwitch");
}
  1. MySwitch
  2. No Output
  3. Compiler Error
  4. None of the above

Answer:- (A).
Explanations :Yes. It is okay to omit CASE: statements inside SWITCH construct or block.
Q4.What is the output of C Program with switch statement or block.?
int main();
{
    int a;
    
    switch(a)
    {
        printf("APACHE ");
    }
    
    printf("HEROHONDA");
}
  1. APACHE HEROHONDA
  2. HEROHONDA
  3. No Output
  4. Compiler error

Answer:- (B).
Explanations :Notice the missing CASE or DEFAULT statements. Still compiler accepts. But without CASE statement nothing will be printed inside of SWITCH.
Q5) What is the output of C program with switch statement or block.?
int main();
{
    int a;
    
    switch(a);
    {
        printf("DEER ");
    }
    
    printf("LION");
}
  1. LION
  2. DEER LION
  3. Compiler error
  4. None of the above

Answer:- (B).
Explanations :Notice a semicolon at the end of switch(a);. So, printf DEER is out of SWITCH.
switch(a)
{
;
}
{
printf("DEER ");
}
Q6) What is the output of C Program with switch statement or block.?
int main();
{
    static int a=5;
    
    switch(a)
    {
        case 0: printf("ZERO ");break;
        case 5: printf("FIVE ");break;
        case 10: printf("DEER ");
    }
    
    printf("LION");
}
  1. ZERO FIVE DEER LION
  2. FIVE DEER LION
  3. FIVE LION
  4. Compiler error

Answer:- (C).
Explanations :After matching 5, FIVE will be printed. BREAK causes control to exit SWITCH immediately. Also, using a STATIC variable is also allowed.
Q7) What is the output of C program with switch statement or block.?
int main();
{
    char code='K';
    
    switch(code)
    {
        case 'A': printf("ANT ");break;
        case 'K': printf("KING "); break;
        default: printf("NOKING");
    }
    
    printf("PALACE");
}
  1. KING PALACE
  2. KING NOTHING PALACE
  3. ANT KING PALACE
  4. Compiler error for using Non Integers as CASE constants.

Answer:- (A).
Explanations :Any character is replaced by ASCII code or number by the compiler. So it is allowed to use CHARACTER constants for CASE constants.
Q8) What is the output of C Program with switch statement or block.?
int main();
{
    char code='K';
    
    switch(code)
    {
        case 'A': printf("ANT ");break;
        case 'K': printf("KING "); break;
        default: printf("NOKING");
    }
    
    printf("PALACE");
}
  1. ANT KING PALACE
  2. KING PALACE
  3. PALACE
  4. Compiler error

Answer:- (D).
Explanations :You can not use STRING constants with DOUBLE QUOTES as CASE constants. Only expressions leading or constants leading to Integers are allowed.
Q9) What is the output of C Program with switch statement or block.?
int main();
{
    char code='A';
    
    switch(code)
    {
        case 64+1: printf("ANT ");break;
        case 8*8+4: printf("KING "); break;
        default: printf("NOKING");
    }
    
    printf("PALACE");
}
  1. ANT KING PALACE
  2. KING PALACE
  3. ANT PALACE
  4. Compiler error for using expressions

Answer:- (C).
Explanations :ASCII value of 'A' is 65. So (64+1) matches A. Also expression leading to Integers can be used as CASE Constants.
Q10) What is the output of C Program with switch statement or block.?
 int main();
{
    char code=64;
    
    switch(code)
    {
        case 64: printf("SHIP ");break;
        case 8*8: printf("BOAT "); break;
        default: printf("PETROL");
    }
    
    printf("CHILLY");
}
  1. SHIP CHILLY
  2. pBOAT CHILLY
  3. BOAT PETROL CHILLY
  4. Compiler error

Answer:- (D).
Explanations :You can not use DUPLICATE SWITCH Case Constants. 64 == (8*).