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
Question 6
What is the output of the following program?
#include < stdio.h > 
int main()
{
    int a=2;
    switch(a=0)
    {
        default: printf("GATE-CSE ");
        case 0: printf("Zero ");
        case 1: printf("One");
                break;
        case 2:printf("Two ");
        case 3:printf("Three ");
    }
    return 0;
}
A
GATE-CSE Zero One
B
Two Three
C
Zero One
D
Two
   C Programming
Question 6 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 and One as ther is not break statement in case 0.

NOTE: default is written at top in swich block but it will execute only when no case match in switch block.

OUTPUT: Zero One

Click to Join Our Telegram Group for Latest Update of MOCK TEST
Question 7
What is the output of the following program?
#include < stdio.h > 
int main()
{   
    int a=5,i=3;
      switch(a)
    {
        case ++i: printf("One ");
        case ++i: printf("Two");
        default: printf("None ");
    }
    return 0;
}
A
Two None
B
None
C
one Two None
D
Compiler Error
   C Programming
Question 7 Explanation: 
Compiler gives error :case label does not reduce to an integer constant

As in case label must be evaluated as constant.
Case ++i violates this requirement.

Click to Join Our Telegram Group for Latest Update of MOCK TEST
Question 8
What is the output of the following program?
#include < stdio.h > 
int main()
{
    switch(printf("IITB\n"))
    {
        default: printf("GATE-CSE ");
                 break;
        case 1: printf("No ");
                break;
        case 3:
        case 4:printf("practicepaper ");
                break;
        case 5:printf("Mock Test ");
                 break;
    }
    return 0;
}
A
practicepaper
B
practicepaper Mock Test
C
IITB
practicepaper Mock Test
D
IITB
Mock Test
   C Programming
Question 8 Explanation: 
printf("IITB\n") retun 5. (NOTE: printf return number of char it prints including the new line char \n)

So, it match with case 5 in switch block.

OUTPUT: IITB
Mock Test

Click to Join Our Telegram Group for Latest Update of MOCK TEST
Question 9
What is the output of the following program?
NOTE: Consider int size is of 4 Byte.
#include < stdio.h > 
int main()
{
    int  A[6]={1,2,3,4};
    switch(sizeof(A))
    {
        case 1:
        case 2:
        case 3:
        case 4:printf("practice");
        case 5:printf("paper ");
        case 16:printf("Mock Test ");
    }
    printf("GATE-CSE");
    return 0;
}
A
practice paper Mock Test
B
practice paper Mock Test GATE-CSE
C
practice GATE-CSE
D
GATE-CSE
   C Programming
Question 9 Explanation: 
sizeof(A) return the size of array A.
Array A is declared as int A[6], so sizeof(A) return 6*4=24 (as size of int given as 4).
It will not match with any case in switch.
Therefore, program print GATE-CSE
OUTPUT: GATE-CSE


Click to Join Our Telegram Group for Latest Update of MOCK TEST
Question 10
What is the output of the following program?
NOTE: Consider int size is of 4 Byte.
#include < stdio.h > 
int main()
{
    int  A[6]={1,2,3,4};
    switch(sizeof(A[0]))
    {
        case 1:
        case 2:
        case 3:
        case 4:   printf("practice ");
        case 5:   printf("paper ");
        case 16: printf("Mock Test ");
    }
    printf("GATE-CSE");
    return 0;
}
A
practice paper Mock Test
B
practice paper Mock Test GATE-CSE
C
practice GATE-CSE
D
GATE-CSE
   C Programming
Question 10 Explanation: 
sizeof(A[0]) return 4 (as size of int given as 4).
It will match with case 4 in switch.
Therefore, it first print practice
After that compiler continue execute statements without any case mathing till it get the break statement or end of switch block.
So, it also print paper Mock Test GATE-CSE
OUTPUT: practice paper Mock test GATE-CSE

Click to Join Our Telegram Group for Latest Update of MOCK TEST
There are 10 questions to complete.

Leave a Comment