Compilers Questions and Answers – Switch Case – 1

«
»

This set of Compilers Multiple Choice Questions & Answers (MCQs) focuses on “Switch Case – 1”.

1. Select the output for following set of code.

static void Main(string[] args)
{ 
    int movie = 1;
    switch (movie << 2 + movie)
    {
        default: 
            Console.WriteLine("A");
            break;
        case 4: 
            Console.WriteLine("B");
            break;
        case 5: 
            Console.WriteLine("C");
            break;
        case 8: 
            Console.WriteLine("D");
            break;
      }
      Console.ReadLine();
}

a) A
b) B
c) C
d) D
View Answer

Answer: c
Explanation: ‘default’ case can be put in anywhere.
Output: D.
advertisement
advertisement

2. Select the output for following set of code.

static void Main(string[] args)
{
    int i = 2, j = 4;
    switch (i + j * 2)
    {
        case 1 :
        case 2 :
            Console.WriteLine("1 and 2");
            break;
        case 3 to 10:
            Console.WriteLine("3 to 10");
            break;
    }
    Console.ReadLine();
}

a) 3 to 10 will be printed
b) 1 and 2 will be printed
c) error
d) The code gives output as 3 to 10
View Answer

Answer: c
Explanation:
Output:
Here i = 2,j = 4.

3. Select the output for following set of code.

advertisement
static void Main(string[] args)
{
    int i = 2, k = 3;
    switch (i - k)
    {
         case -1:
             ++i;
             ++k;
             break;
         case 2:
             --i;
             ++k;
            break;
         default:
             i += 3;
             k += i;
             break;
     }
     Console.WriteLine(i + "\n" + k);
     Console.ReadLine();
 }

a) 2 3 3
b) 3 2 3
c) 3 4 4
d) 5 10 10
View Answer

Answer: c
Output: 3
4
4
Explanation: i – k = -1.So, case -1 will be executed only.
advertisement

4. Select output for following set of code.

static void Main(string[] args)
{
     int const p = 0;
     switch (3 * 5 / 6)
     {
         case p: 
             Console.WriteLine("A");
             break;
         case p * 1:
             Console.WriteLine("B");
             break;
         case p - 2:
             Console.WriteLine("C");
             break;
         default: 
             Console.WriteLine("D");
       }
}

a) A
b) B
c) C
d) Compile time error
View Answer

Answer: d
Explanation: No constant variable.

5. Select output for following set of code.

static void Main(string[] args)
{
    int i = 2, j = 3, k = 4;
    switch (i + j - k)
    {
        case 0: case 2: case 4:
            ++i;
            k += j;
            break;
        case 1: case 3: case 5 :
            --i;
            k -= j;
            break;
        default:
            i += j;
            break;
     }
     Console.WriteLine(i + "\n" + j + "\n" + k);
     Console.ReadLine();
}

a) 1 3 1
b) 2 3 4
c) 5 3 4
d) Compile time error
View Answer

Answer: a
Explanation: Solving expression gives 1
Output: 1
3
1

6. Select the output for following set of code.

static void Main(string[] args)
{
    int  i = 9 , j = 7;
    switch (i - j + 3)
    {
        case 9: 7:
            j += 6;
            break;
        case 5:
            i -= 4;
            break;
    }
    Console.WriteLine(i + "\n" + j);
    Console.ReadLine();
  }

a) 5 7
b) 9 13
c) Compile time error
d) 9 7
View Answer

Answer: c
Explanation: Invalid expression.

7. Select the output for code :

static void Main(string[] args)
{
    switch (5)
    {
        case 5.0f: 
            Console.WriteLine("harsh");
            break;
        case 5: 
            Console.WriteLine("amish");
            break;
        case 5.0L: 
            Console.WriteLine("ANKIT");
            break;
        default:
            Console.WriteLine("ashish");
    }
    Console.ReadLine();
}

a) amish
b) ANKIT
c) harsh
d) Compile time error
View Answer

Answer: d
Explanation: Only integers are allowed .
5.0f = (int)5.0f.
5.0L =(int)5.0L.

8. Select output for code.

static void Main(string[] args)
{
    int i;
    int j = 1;
    int []ar = {21, 22, 13, 4};
    switch (ar[j])
    {
        case 1:
            i++;
            break;
        case 2:
            i += 2;
            j = 3;
            continue;
        case 3: 
           i %= 2;
           j = 4;
           continue;
        default: 
           --i;
    }
    Console.WriteLine(i);
    Console.ReadLine();
}

a) 23
b) 15
c) Compile time error
d) 12
View Answer

Answer: c
Explanation: Continue cannot be used.

9. Select the output for following set of Code.

static void Main(string[] args)
{
    char ch = Convert.ToChar('a' | 'b' | 'c');
    switch (ch)
    {
        case 'A':
        case 'a':
            Console.WriteLine("case A|case a");
            break;
        case 'B':
        case 'b':
            Console.WriteLine("case B|case b");
            break;
        case 'C':
        case 'c':
        case 'D':
        case 'd':
            Console.WriteLine("case D|case d");
            break;
     }
     Console.ReadLine();
}

a) Compile time error
b) case A | case a
c) case B | case b
d) case D | case d
View Answer

Answer: d
Explanation: case D|case d

10. Select the output for following set of Code.

static void Main(string[] args)
{
    char ch = 'p';
    switch (ch)
    {
        case 'p':
            Console.WriteLine("coco" + "\t" + Convert.ToInt32(ch));
            break;
        default:
            Console.WriteLine("default");
            break; 
     }
     Console.WriteLine("main");
}

a) coco main
b) coco 112
c) coco 112 main
d) compile time error
View Answer

Answer: c
Explanation: ASCII value of p is 112.
Output: coco 112 main.

Sanfoundry Global Education & Learning Series – Compilers.

To practice all areas of Compilers, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.