AmbitionBox

Discover Best Places to work in India

C# Programming

C# Programming Questions and Answers

Showing 11 - 13 of 13 questions

11

Select the output for the following set of code:

staticvoid Main(string[] args)

{

     float s = 0.1f;

     while(s <= 0.5f)

     {

          ++s;

          Console.WriteLine(s);

     }

     Console.ReadLine();

}

 

 

 

 

 

a

0.1

b

1.1

c

0.1 0.2 0.3 0.4 0.5

d

No output

correct answer b

for the first while condition check when s = 0. If it is true as control goes inside loop ++s increments value of s by 1 as 1+0.1 = 1.1. So, for next condition while loop fails and hence, prints final value of s as 1.1.
Output: 1.1

12

Select the output for the following set of code :

staticvoid Main(string[] args)

{

     int i, j;

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

     {

          j =1;

          while(i % j ==2)

          {

               j++;

          }

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

     }

     Console.ReadLine();

}

 

 

a

11 21 31

b

1 12 13 1

c

11 21 31

d

1 1 2 1 3 1

correct answer c

Since, condition never satisfied for any value of i and j for which (i % j == 2).Hence, j is always constant ‘1’ and ‘i’ increments for i = 1, 2, 3.
Output: 11 21 31.

13

Select the output for the following set of Code:

staticvoid Main(string[] args)

{

     int i;

     i =0;

     while(i++<5)

     {

          Console.WriteLine(i);

     }

     Console.WriteLine("\n");

     i =0;

     while(++i <5)

     {

     Console.WriteLine(i);

     }

     Console.ReadLine();

}

 

 

a

1 2 3 4
1 2 3 4 5

b

1 2 3
1 2 3 4

c

1 2 3 4 5
1 2 3 4

d

1 2 3 4 5
1 2 3 4 5

correct answer c

for while(i++ < 5) current value of 'i' is checked first and hence prints incremented value afterwards.So, i =1, 2, 3, 4, 5.But, for while(++i < 5) current value is incremented first and then checks that value with given condition and hence then prints that value.So, i = 1, 2, 3, 4. Output: 1 2 3 4 5 1 2 3 4

Select a company to compare with

vs

Similar Companies