C# Questions & Answers on For Loop Statements for Freshers

1. Select the output for the following set of code :
  1.  static void Main(string[] args)
  2.  {
  3.      int i;
  4.      for (i = 0;  ; )
  5.      {
  6.          Console.WriteLine("hello");
  7.      }
  8.      Console.ReadLine();
  9.  }
a) No output
b) hello
c) hello printed infinite times
d) Code will give error as expression syntax
Answer: c

Explanation: Testing condition for the loop is absent.So,loop will continue executing.
Output : hello
hello
hello
.
.
.
2. Select the output for the following set of code :
  1.   static void Main(string[] args)
  2.   {
  3.       float f;
  4.       for (f = 0.1f; f <= 0.5; f += 1)
  5.       Console.WriteLine( ++f );
  6.       Console.ReadLine();
  7.   }
a) 1.1
b) 0.1
c) 0.1 0.2 0.3 0.4 0.5
d) None of the mentioned
Answer: a

Explanation: f =0.1 and ++f = 0.1+1 = 1.1.So,1.1>0.5,Condition fails and hence loop terminates.
Output : 1.1
3. Select the output for the following set of code:
  1.  static void Main(string[] args)
  2.  {
  3.      int I, X;
  4.      for (I = 1; I <= (9 % 2 + I); I++)
  5.      {
  6.          X = (I * 3 + I * 2) / I;
  7.          Console.WriteLine(X);
  8.      }
  9.      Console.ReadLine();
  10.  }
a) Output of code is 5 10
b) Output is 5 5 5 5
c) Print 5 infinite times
d) None of the mentioned
Answer: c

Explanation: Testing condition is always incremented i.e i never ‘>’ (9%2+I).So,loop will never terminate.
Output : 5 5 5…..
4. Select output for the following set of code:
  1.  static void Main(string[] args)
  2.  {
  3.      int I, J = 0;
  4.      for (I = 1; I < 10; ) ;
  5.      {
  6.          J = J + I;
  7.          I += 2;
  8.      }
  9.      Console.WriteLine("Sum of first 10 even numbers is:"+J);
  10.      Console.ReadLine();
  11.  }
a) 1 2 3 4 5 6 7 8 9
b) 25
c) 1
d) Run time error
Answer: d

Explanation: Due to presence of ‘;’ after for()loop condition do not work as according to the statement.Remove the ‘;’.
Output : 25.
5. Select the output for the following set of code:
  1.   static void Main(string[] args)
  2.   {
  3.       int i = 5;
  4.       for (; Convert.ToBoolean(Convert.ToInt32(i)); Console.WriteLine(i--)) ;
  5.       Console.ReadLine();
  6.   }
a) 4 3 2 1
b) 3 2 1
c) 5 4 3 2 1
d) 2 1
Answer: c

Explanation: Since, i = 5 and test condition is executed until i!=0.So, i– decrements value of i till condition is satisfied.
Output: 5 4 3 2 1
6. Select the output for the following set of code:
  1.  static void Main(string[] args)
  2.  {
  3.      int i, s = 0;
  4.      for (i = 1; i <= 10; s = s + i, i++);
  5.      {
  6.          Console.WriteLine(s);
  7.      }
  8.      Console.ReadLine();
  9.  }
a) Code report error
b) Code runs in infinite loop condition
c) Code gives output as 0 1 3 6 10 15 21 28 36 45
d) Code give output as 55
Answer: d

Explanation: Since occurrence of termination symbol(;) at end of for loop.
Output: 55.
7. Which statement is correct among the mentioned statements?

1. The for loop works faster than a while loop
2. for( ; ; )implements an infinite loop

a) Only 1 is correct
b) Only 2 is correct
c) Both 1 and 2 are correct
d) Both 1 and 2 are incorrect
Answer: b

Explanation: By defination.
8. Select the output for the following set of code :
  1.   {
  2.       int i;
  3.       Console.WriteLine("Hi");
  4.       for (i = 1; i <= 10; i++)
  5.           Program.Main(args);
  6.       Console.ReadLine();
  7.   }
a) Prints ‘Hi’ for one time
b) Prints ‘Hi’ for infinite times
c) Stack overflow exception Condition generated
d) None of above mentioned
Answer: c

Explanation: Ocurrence of ‘main()’ condition after for loop.
Output: Hi
Hi
.
.
stack overflow exception.
9. Which of the code should be added to get the following output?
  1.     * * * * *
  2.     * * * *
  3.     * * *
  4.     * *
  5.     *
  6.    static void Main(string[] args)
  7.    {
  8.        int i,j;
  9.      /* Add code here */
  10.  
  11.   }
a) for (i = 0;i <= 4; i++)
{
for(j = 0;j <= 4; j++)
console.WriteLine(“*”);
console.WriteLine(“\n”);
}
b) for (i = 0;i <= 4; i++)
{
for(j = 4;j <= i; j–)
console.WriteLine(“*”);
console.WriteLine(“\n”);
}
c) for (i = 0;i <= 4; i++)
{
for (j = i;j <= 4; j++)
console.WriteLine(“*”);
console.WriteLine(“\n”);
}
d) for ( i = 0;i <= 4; i++)
{
for (j = 0;j <= i; j++)
console.WriteLine(“*”);
console.WriteLine(“\n”);
}
Answer: c

Explanation: Input in Console and run the code.
10. Predict the output for the following set of code :
  1.    static void Main(string[] args)
  2.    {
  3.        int i;
  4.        for (i =-3; i <= 3; i++)
  5.        {
  6.            switch (i)
  7.            {
  8.            case 0:
  9.                Console.WriteLine("zero");
  10.                break;
  11.            }
  12.            if (i > 0)
  13.                Console.WriteLine("A");
  14.            else if (i < 0)
  15.                Console.WriteLine("B");
  16.        }
  17.        Console.ReadLine();
  18.    }
a) B B zero A A A
b) B zero A A A
c) B B B zero A A A
d) A A A zero B B B
Answer: c

Explanation: for i =-3,-2,-1 statement executed as B.for i = 0,it is zero and for i =1,2,3 again statement printed as A seperately for each value of i.
Output: B B B zero A A A.
11. Which of the following is not infinite loop?

a) for( ;’0′; )
b) for( ;’0′; )
c) for( ;’1′; )
d) for( ;’1′; )
Answer: b

12. Select the output for the following set of code :
  1.   static void Main(string[] args)
  2.   {
  3.       int i, j;
  4.       for (i = 1, j = i; i <= 3 && j >= 0; i++, j--)
  5.       {
  6.           if (i == j)
  7.               continue;
  8.           else
  9.               Console.WriteLine(j);
  10.       }
  11.       Console.ReadLine();
  12.   }
a) i = 0, j = 1;
b) i = 1, j = 0;
c) j = 0;
d) None of the mentioned
Answer: c

Explanation: Since for i = 1, j = 1 and 1 <= 3 also 1 >= 0 we had i == j.But after i++ and j–. The initial value of ‘j’ which is ‘0’ as j– preferred other than value of ‘j’ in i = j.
Output:j = 0.
13. Select the output for the following set of code :
  1.  static void Main(string[] args)
  2.  {
  3.      int i = -10;
  4.      for ( ;Convert.ToBoolean(Convert.ToInt32(i)) ;Console.WriteLine(i++)) ;
  5.      Console.ReadLine();
  6.  }
a) -9 -8 -7 -6 -5 -4 -3 -2 -1
b) -10 -9 -8 -7 -6 -5 -4 -3 -2
c) -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
d) -8 -7 -6 -5 -4 -3 -2 -1
Answer: c

Explanation: For first value of i = -10.Condition is executed until i!=0.
Output: -10 -9 -8 -7 -6 -5 -4 -3 -2 -1.

Related

Oracle Database Multiple Choice Questions and Answers on Normalization for Freshers

1. A table is in BCNF if it is in 3NF and if every determinant is a ___________ key. a) Dependentb) Normalc) Candidated) Both Normal and Candidate Answer: c Explanation: A table is in Boyce-Codd ...

CSS Multiple Choice Questions & Answers on Transforms and Transitions for Freshers

1. Which of the following property defines the length of time that a transition takes? a) transition b) transition-duration c) transform-duration d) transition-property Answer: b&n...

HTML Multiple Choice Questions & Answers on Scalable Vector Graphics for Freshers

1. Which of the following is/are the property of SVG images? a) SVG images are scalableb) SVG images are zoomablec) SVG is an open standardd) All of the mentioned Answer: d Explanation: Scalable ...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item