AmbitionBox

Discover Best Places to work in India

C# Programming

C# Programming Questions and Answers

Showing 1 - 10 of 13 questions

1

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

correct answer b

Answer: Option (B)

2

Which of the following is not infinite loop?

a

for( ;’0′; )

b

for( ;’0′; )

c

for( ;’1′; )

d

for( ;’1′; )

correct answer b

Answer: Option (B)

3

Select the output for the following set of code :

staticvoid Main(string[] args)

{

     int i =-10;

     for(;Convert.ToBoolean(Convert.ToInt32(i)); 

     Console.WriteLine(i++));

     Console.ReadLine();

}

 

 

 

 

 

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

correct answer c

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

4

Select the output for the following set of code :

staticvoid 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();

}

 

 

 

a

i = 0, j = 1;

b

i = 1, j = 0;

c

j = 0;

d

None of the mentioned.

correct answer c

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.

5

Select the output for the following set of code :

{

     int i;

     Console.WriteLine("Hi");

     for(i =1; i <=10; i++)

          Program.Main(args);

     Console.ReadLine();

}

 

a

Prints ‘Hi’ for one time

b

Prints ‘Hi’ for infinite times

c

Stack overflow exception Condition generated

d

None of above mentioned

correct answer c

Ocurrence of ‘main()’ condition after for loop.
Output: Hi
Hi
.
.
stack overflow exception.

6

Select the output for the following set of code:

staticvoid Main(string[] args)

{

     int i, s =0;

     for(i =1; i <=10; s = s + i, i++);

     {

          Console.WriteLine(s);

     }

     Console.ReadLine();

}

 

 

 

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

correct answer d

Since occurrence of termination symbol(;) at end of for loop.
Output: 55.

7

Select the output for the following set of code :

staticvoid Main(string[] args)

{

     float f;

     for(f = 0.1f; f <=0.5; f +=1)

     Console.WriteLine(++f );

     Console.ReadLine();

}

 

 

a

1.1

b

0.1

c

0.1 0.2 0.3 0.4 0.5

d

None of the mentioned

correct answer a

f =0.1 and ++f = 0.1+1 = 1.1.So,1.1>0.5,Condition fails and hence loop terminates.
Output : 1.1

8

Correct syntax for while statement is:

a

while
{

}(condition);

b

while(condition)
{

};

c

while(condition)
{

}

d

while(condition);
{

}

correct answer c

Answer: Option (C)

9

Select the output for the following set of Code:

staticvoid Main(string[] args)

{

     float i = 1.0f, j = 0.05f;

     while(i < 2.0f && j <= 2.0f)

     {

     Console.WriteLine(i++-++j);

     }

     Console.ReadLine();

}

 

 

 

a

0.05f

b

1.50f

c

-0.04999995f

d

1.50f

correct answer c

for while(i = 1.0f and j = 0.05f). We, had ‘&&’ condition which gives ‘1’. So, control enters while loop. Since, i = 1 and i++ = first execute then increment. So, first with ‘i’ value as 1.0f and ++j = first increment and then executes we had j = 1.05f and Since operation (i++ – ++j) gives us a negative sign number. So, we can stick our choice to option ‘c’ clearly. Now, as i = 2.0f so loop breaks.
Output:-0.04999995f.

10

Select the output for the following set of code:

staticvoid Main(string[] args)

{

     int i =1, j =1;

     while(++i <=10)

     {

          j++;

     }

     Console.WriteLine(i+" "+j);

     Console.ReadLine();

}

a

12 11

b

10 11

c

11 10

d

11 12

correct answer c

As ++i, first increments then execute so, for ++i i is 11 and j++ is first execute then increments. So, j = 10.
Output:11 10.

Select a company to compare with

vs

Similar Companies