MCQ

C# MCQ – Control Statements in C# – if, else, switch…

Multiple choice questions and answers (MCQs) on C# to prepare for exams, tests, and certifications. These questions are taken from a real written exam and some parts are taken from an interview. So you will find questions on basic techniques such as Variables, Operators, Conditional Statement, Functions, and more. This quiz will easily prepare anyone to pass their online test.
 

 

1. What is the output of the following C# code?
int i = 0, j = 0;

label:
    i++;
    j+=i;
if (i < 10)
{
    Console.Write(i +" ");
    goto label;
}

A Display 1 to 9

B Display 0 to 8

C Display 2 to 8

D Display 2 to 9

E Compilation error at “label:”

A

 

 

2. What is the output of the following C# code?
int i = 10;
for( ; ; )
{
    Console.Write(i + " "); 
    if (i >= -5)
        i -= 2; 
    else 
        break;
}

A 10 8 6 4 2 0

B 10 8 6 4 2 0 -4 -8

C 8 6 4 2 0

D 10 8 6 4 2 0 -2 -4 -6

E 8 0 -8

D

 

 

3. What is the output of the following C# code?
namespace StackHowToApp
{
    public enum color{ brown, orange, green};
    
    class ColorApp
    {
        static void Main (string[ ] args)
        {
            color c = color.green;
            switch (c)
            {
                case color.brown:
                    Console.WriteLine(color.brown); 
                    break; 
                
                case color.orange: 
                    Console.WriteLine(color.orange); 
                    break; 
                
                case color.green: 
                    Console.WriteLine(color.green); 
                    break; 
            } 
        } 
    } 
}

A brown

B orange

C green

D 0

E 2

C

 

 
 

4. What is the output of the following C# code?
int val;
for (val = -5; val <= 5; val++)
{
    switch (val)
    {
        case 0:
            Console.Write ("tackHowT"); 
            break;
    }
    
    if (val > 0)
        Console.Write ("S"); 
    else if (val < 0)
        Console.Write ("o");
}

A StackHowTo

B tackHowTooooo

C SSSSStackHowTooooo

D SSSSStackHowT

E SSSSSooooo

C

 

 

5. Which of the following can be used to end a while loop and pass control outside the loop?

A break

B goto

C exit while

D continue

E exit statement

A, B
  • When a “break” instruction is encountered inside a loop, the loop is immediately ended.
  • The “goto” instruction allows you to pass unconditionally from ‘goto’ to a statement in the same function.

 

 

6. The C# .NET code snippet given below generates a series of ____ in output?
int i = 1, j = 1, val;
while (i < 35)
{
    Console.Write(j + " ");
    val = i + j;
    j = i;
    i = val;
}

A Even numbers

B Odd numbers

C Palindrome

D Fibonacci

E Prime
 

 
D
The output of the program is as follows:

1 1 2 3 5 8 13 21

The Fibonacci sequence is a set of numbers that begins with 1 or 0, followed by 1, and proceeds according to the rule that each number (called the Fibonacci number) is equal to the sum of the two preceding numbers. If the Fibonacci sequence is written as F(n), where n is the first term of the sequence, the following equation is given for n = 0, where the first two terms are defined as 0 and 1 by convention: F (0) = 0, 1, 1, 2, 3, 5, 8, 13, 21, …

 

 

7. What is the output of the following C# code?
int i = 2, j = i;
if (Convert.ToBoolean((i | j & 5) & (j - 15 * 1)))
    Console.WriteLine(1); 
else
    Console.WriteLine(0);

A 0

B 1

C Compilation error

D Runtime error

B
“Convert.ToBoolean” method is used to convert a specified value into an equivalent boolean value.

 

 

8. What is the output of the following C# code?
int i;
for(i = 0; i<=10; i++)
{
    if(i == 2)
    {
        Console.Write(i + " "); continue;
    }
    else if (i != 2)
        Console.Write(i + " "); else
    break;
}

A 0 1 2

B 2

C 0 1 2 3 4 5 6 7 8 9 10

D 3 4 5 6 7 8 9 10

C

 

 
 

9. Which of the following statements is correct?

A It is not possible to extend the “if” statement to handle multiple conditions using “else-if”.

B The “switch” instruction can have two cases with the same value.

C The “break” instruction is required after each “case” block, except for the last block if it is a “default” instruction.

D The “if” statement selects a statement to execute based on the value of a Boolean expression.

D

 

 

10. Which of the following statements is correct?

A The “switch” instruction can be used on numerical types as well as on Boolean types.

B The “switch” instruction can be used on the following types: char, string and enum.

C We cannot declare variables in a “case” statement if it is not surrounded by {}.

D Not all expressions in the “for” statement are optional.

A

 

 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *