My Report

C# Loops Test – 1


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)
  {
      float f;
      for (f = 0.1f; f <= 0.5; f += 1)
      Console.WriteLine( ++f );
      Console.ReadLine();
  }

2. Which of the following is not infinite loop?

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

 
 static void Main(string[] args)
 {
     int i, s = 0;
     for (i = 1; i <= 10; s = s + i, i++);
     {
         Console.WriteLine(s);
     }
     Console.ReadLine();
 }
 

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

  static void Main(string[] args)
  {
      int i, j;
      for (i = 1, j = i; i <= 3 && j >= 0; i++, j--)
      {
          if (i == j)
              continue;
          else
              Console.WriteLine(j);
      }
      Console.ReadLine();
  }

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

 
  static void Main(string[] args)
  {
      int i = 5;
      for (; Convert.ToBoolean(Convert.ToInt32(i)); Console.WriteLine(i--)) ;
      Console.ReadLine();
  }

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

 
 static void Main(string[] args)
 {
     int I, X;
     for (I = 1; I <= (9 % 2 + I); I++)
     {
         X = (I * 3 + I * 2) / I;
         Console.WriteLine(X);
     }
     Console.ReadLine();
 }

7. Which of the C# code should be added to get the following output?

 
    * * * * *
    * * * *
    * * *
    * *
    *
   static void Main(string[] args)
   {
       int i,j;
     /* Add code here */

  }
  

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

 
  {
      int i;
      Console.WriteLine("Hi");
      for (i = 1; i <= 10; i++)
          Program.Main(args);
      Console.ReadLine();
  }
 

9. Which statement is correct among the mentioned statements?
i. The for loop works faster than a while loop
ii. for( ; ; )implements an infinite loop

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

 
 static void Main(string[] args)
 {
     int i;
     for (i = 0;  ; )
     {
         Console.WriteLine("hello");
     }
     Console.ReadLine();
 }
 

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

 
 static void Main(string[] args)
 {
     int I, J = 0;
     for (I = 1; I < 10; ) ;
     {
         J = J + I;
         I += 2;
     }
     Console.WriteLine("Sum of first 10 even numbers is:"+J);
     Console.ReadLine();
 }

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

 
   static void Main(string[] args)
   {
       int i;
       for (i =-3; i <= 3; i++)
       {
           switch (i)
           {
           case 0:
               Console.WriteLine("zero");
               break;
           }
           if (i > 0)
               Console.WriteLine("A");
           else if (i < 0)
               Console.WriteLine("B");
       }
       Console.ReadLine();
   }
  

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

 static void Main(string[] args)
 {
     int i = -10;
     for ( ;Convert.ToBoolean(Convert.ToInt32(i)) ;Console.WriteLine(i++)) ;
     Console.ReadLine();
 }
 

 

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.