Search Placementyogi.com
 

C# Control Instructions - Basic Objective Questions

1

Can infinite loop be achieved using While loop?

A. True
B. False
See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

This is true. When you write while (1 == 1); control never stops.

There are other ways too to achieve this. We will explore them in further questions.

 
2

Which of the following keyword can be used to terminate the While loop in C#? (Multiple Choice)

A.

return

B.

continue

C.

break

D.

goto

See Answer & Explanation Lets Discuss
Correct answer is : A,C,D
Explanation

No explanation available. Let's Discuss

 
3

What is the output of the following C# program?

using System;
namespace PlacementYogi
{
    public class WhileLoop
    {
        public static void Main(string[] args)
        {
            int i = 1, j = 1;
            while (++i < 10)
            Console.Write(i);
            Console.WriteLine();
            while (j++ < 10)
            Console.Write(j);
        }
    }
}
A.

1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

B.

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9 10

C.

2 3 4 5 6 7 8 9

2 3 4 5 6 7 8 9 10

D.

2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9 10

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

We know that in a pre-increment operator the value is incremented first and then used, while 

in post-increment operator the current value is used first and then incremented.

Now,

In the first loop,

The incremented value is compared with 10 and is printed.

initially i is 1, the control goes to the condition (++i < 10) in while loop, the value of i is incremented and i becomes 2 and 2<10 is TRUE, 

So, the incremented value 2 is printed. Please Note that 1 is not printed here as the value of i has already become 2 at the time of printing.

Similarly, 

When the value of i is 2 , 3 is printed.

value of i      printed value

        1------------2

        2------------3

        3------------4

        4------------5

        . ------------ .

        . ------------ .

        8------------9

        9------------The loop exits as ++i becomes 10.

 

So, 10 is not printed.

 

In the Second loop,

The current value of j is compared with 10 and the incremented value is printed.

 

So, initially j=1 and  j<10 is TRUE , Hence the printf statement is executed, by this time the value of j has become 2.

Hence 2 is printed.

value of j     printed value

        1------------2

        2------------3

        3------------4

        4------------5

        .  ----------  .

        .  ----------  . 

        8------------9

        9------------10

        10----------- Loop Exits,

 

Since ++j is 11 which is greater than 10, the condition fails and the control comes out of the loop.

 NOTE: The statement Console.WriteLine(); is not inside the while loop.

 
4

What is the output of the following C# program?

using System;
namespace PlacementYogi
{
    public class Control1
    {
        public static void Main(string[] args)
        {
            int i=10;
            if(i ==10)
                Console.Write("I is 10 ");
            else if ( i <= 10)
                 Console.Write(", I is less than or equal to 10");
            else
            {
                break;
            }
        }
    }
}
A.

I is 10

B.

I is 10 , I is less than or equal to 10

C.

Compile Time Error

D.

None of the Above

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

break can only be used inside a loop or switch and is not allowed inside any other control structure like if-else.

The Error printed is:

No enclosing loop out of which to break or continue

 
5

What is the output of the following C# program?

using System;
namespace PlacementYogi
{
    public class WhileLoop
    {
        public static void Main(string[] args)
        {
            int c = 1;
            while (c < 100)
               Console.Write(c +" ");
            c++;
        }
    }
}
A.

Prints the numbers from 1 to 99

B.

Infinite Loop

C.

Compilation Error

D.

Prints the numbers from 1 to 100

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

Inside the body of  while loop, as there is no parenthesis ,there is only one statement i.e. the Console.Write statement.

The statement c++ lies outside the  while loop and is never executed.

Correct program would be:

while(c <100)
    {
        Console.Write(c +" ");
        c++;
    }

// output: 1 2 3 4 . . . 99

If the c++ is placed before Console.Write statement as shown below:

while(c <100)
    {
        c++;
        Console.Write(c +" ");    
    }

// output: 2 3 4 . . . 99 100

Because, when c is 1 , it increments value of c to 2 and then prints 2, and so on .

when c becomes 98 -> 99 is printed and when c is 99 -> 100 is printed. 

c is now 100 and  the control comes out of the loop.

 
 

Banner to LogicGuns.com

Hide Page Information

C# Control Instructions - Basic Objective Questions and Answers with Explanation

Here you can find basic level C# Control Instructions (for, while, do-while, switch, if-else) interview questions with answers and explanation.

Why Control Instructions in C#.NET?

In this section you can learn and practice different types of basic level questions with answers on Control Instructions to improve your skills in order to face the interviews, competitive examinations and various entrance tests with full confidence.

Where can I get C# Control Instructions Basic Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved basic level Control Instructions questions and answers with explanation. Fully solved examples with detailed answer description and explanation are given and it would be easy to understand. All students, freshers can download Control Instructions quiz questions with answers as PDF files and eBooks.

Where can I get Basic Level C# Control Instructions Interview Questions and Answers (objective type, multiple choice)?

Here you can find objective type basic Control Instructions questions and answers for interview and entrance examination. Multiple choice and TRUE or FALSE type and YES or NO type questions are also provided.