My Report

C# Loops Test – 3


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. What will be the output of the following C# code?

static void Main(string[] args)
{
    int x = 10;
    do
    {
        Console.WriteLine( x++);
    }
    while(Convert.ToBoolean(5) && Convert.ToBoolean(4) && Convert.ToBoolean(3) 
    && Convert.ToBoolean(2) && Convert.ToBoolean(1) && Convert.ToBoolean(0));    
    Console.ReadLine();
}

2. What will be the output of the following C# code?

static void Main(string[] args)
{
    int x = 0;
    do
    {
        x++;
        if (x == 5)
        {
            x++;
            continue;
            break;
        }
        Console.WriteLine(x + " ");
    }
}while (x < 10);

3. What will be the output of the following C# code?

 static void Main(string[] args)
 {
     long  x;
     x = Convert.ToInt32(Console.ReadLine());
     do
     {
         Console.WriteLine(x % 10);
     }while ((x = x / 10) != 0);
     Console.ReadLine();
 }
 enter x = 1234.

4. What will be the output of the following C# code?

 static void Main(string[] args)
 {
     int i, s = 0, a = 1, d;
     i = Convert.ToInt32(Console.ReadLine());
     do
     {
         d = i % (2 * 4);
         s = s + d * a;
     }while ((Convert.ToInt32(i = i / (2 * 4))) != 0 
      && (Convert.ToBoolean(Convert.ToInt32((a) = (a * 10)))));
     Console.WriteLine(s);
     Console.ReadLine();
 }
enter i = 342.

5. What will be the output of the following C# code?

 static void Main(string[] args)
 {
     int i = 1, j = 2, k = 3;
     do
     {
         Console.WriteLine((Convert.ToBoolean(Convert.ToInt32(i++))) 
         && (Convert.ToBoolean(Convert.ToInt32(++j))));
     }while (i <= 3);
     Console.ReadLine();
 }

6. What will be the output of the following C# code?

 
static void Main(string[] args)
{
    int i = 1, j = 5;
    do
    {
        Console.WriteLine(i = i++ * j);
    }while (i <= 10);
    Console.ReadLine();
}

7. What is the correct syntax for do while loop?

8. What will be the output of the following C# code?

static void Main(string[] args)
{
    int x;
    for (x = 1; x <= 3; x++)
    {
        int j = 1;
        do
        {
            j++;
        }while (x % j == 2);
        Console.WriteLine(x + " " + j);
    }
    Console.ReadLine();
}

9. What will be the output of the following C# code?

static void Main(string[] args)
{
    float i = 1.0f, j = 0.05f;
    do
    {
        Console.WriteLine(i++ - ++j);
    }while (i < 2.0f && j <= 2.0f);
    Console.ReadLine();
}

10. What will be the output of the following C# code?

static void Main(string[] args)
{
    int x;
    for (x = 10; x <= 15; x++)
    while (Convert.ToBoolean(Convert.ToInt32(x)))
    {
        do
        {
            Console.WriteLine(1);
            if (Convert.ToBoolean(x >> 1))
            continue;
        }while (Convert.ToBoolean(0));
        break;
    }
    Console.ReadLine();
}

11. For the incomplete C# program below, which of the C# code fragment will not result in an infinite loop?

 
 static void Main(string[] args)
 {
     int i = 1234 ,j = 0;
      /*ADD CODE HERE */
     Console.WriteLine(j);
 }

 

Start practicing “1000 MCQs on C#”, and once you are ready, you can take tests on all topics by attempting our “C# Test Series”.

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 & discussions at Telegram SanfoundryClasses.