Please Turn On Java Script To Access This Site.

C Institute | Questions


Previous Topic : switch statement - 1Next Topic : for loop - 1

This section focuses on C interview questions, tricky questions, and puzzles, and viva questions based on “switch case default statement”. One shall practice these questions and puzzles to improve their C programming skills needed for various interviews (campus interviews, walk-in interviews, viva exams and company interviews), placements, entrance exams and other competitive exams. These programming questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate, engineering student, bca student, mca student, mcs students or an experienced IT professional. It will improve their understanding of C Language. Here is a listing of C questions and puzzles on “switch case default statement” along with answers and/or explanations.

Q1: What will be the output of below c code?
#include<stdio.h> 
#define ting 5 
void main() 
{   auto a = 2; 
    switch (a, 10/a) 
    { 
       case ting:        printf("C");       break;   
       case ting*2:     printf("C++");   break;   
       case ting*3:     printf("DSA");  break; 
       default:            printf("Java");
       case ting*4:     printf("PHP");  break; 
    } 
} 
a)  JavaPHP
b)  C
c)  Syntax Error due to auto a
d)  Syntax error due to case values
Correct Answer : b
Explanation : In C, comma is also operator with least precedence. So switch(a,10/a) results in switch(5). Case expression can use macro. When no datatype is used with auto the default type is int.


Q2: What will be the output?
#include <stdio.h>
void main()
{       double abnp55;
        printf("enter a value between 1 to 2:");
        scanf("%lf", &abnp55);
        switch (abnp55)
        {   case 1:
                printf("Bua");
                break;
            case 2:
                printf("Bhatija");
                break;
        }
}
a)  Bua
b)  Bhatija
c)  Runtime Error
d)  Syntax Error
Correct Answer : d
Explanation : In c, switch can not be applied on real values.
Learn switch in depth


Q3: What will be output when you will execute following c code?
#include<stdio.h>
enum drinks
{   Heineken=-3,   Veera,    Breezer=-1,    Tuborg   };
void main()
{    enum drinks kds=0;
     switch(kds)
     {  case Breezer:    printf("Love it");                 break;
        case Veera:       printf("Awkward");             break;
        case Tuborg:     printf("Gareeb Ki Awaaj"); break;
        case Heineken: printf("LupLup");
     }  
}
a)  Love it
b)  Awkward
c)  Gareeb Ki Awaaj
d)  LupLup
Correct Answer : c
Explanation : Enum constant can be used in switch case. Value of enum constant Heineken=-3, Veera=-2, Breezer=-1 and Tuborg is 0.


Q4: //What will be the output of the following C code?
#include <stdio.h>
int main()
{   switch (printf("Why"))
    {   case 1:
            printf("1
");
        case 2:
            printf("2
");
        case 3:
            printf("3
");
    }
    return 0;
}
a)  Why3
b)  3
c)  Why123
d)  None of these
Correct Answer : a
Explanation : The printf("Why") prints Why and returns the count of printed characters that is 3. So after printing Why case 3 is executed and 3 will be printed after Why. Learn c in depth


Previous Topic : switch statement - 1Next Topic : for loop - 1