C# Programming Questions and Answers Part-9

1. 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();
}
a) 0 0 0
b) True True True
c) 1 1 1
d) False False False

Answer: b
Explanation: 1 AND 1 = True. Similarly, non zero number || non zero number = True.
Output: True True True

2. 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();
}
a) 0.05
b) -0.05
c) 0.95
d) -0.04999995

Answer: d
Explanation: Output : -0.04999995

3. 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();
}
a) 5 10 15 20 25 30 35 40 45 50
b) 5 25
c) 5 11 16 21 26 31 36 41 46 51
d) 5 30

Answer: b
Explanation: For first step of loop i = 1 .So, i++ * j = 1 * 5 = 5. For second step of loop i = 5, j = 5. So, i++ * j = 25. As, i = 25 hence, 25 >=10 loop condition breaks.
Output: 5 25.

4. 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.
a) number of digits present in x
b) prints ‘1’
c) prints reverse of x
d) prints sum of digits of ‘x’

Answer: c
Explanation: Reverse of digits using while loop statements.
Output: 4321.

5. 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.
a) It finds binary equivalent of i
b) It finds octal equivalent of i
c) It finds sum of digits of i
d) It finds reverse of i

Answer: b
Explanation: Output :
i = 342.
s = 526.

6. What is the correct syntax for do while loop?
a) do;
{
statement;
}while (condition);
b) do(condition)
{
statement;
}while;
c) do
{
statement;
}while (condition)
d) do
{
statement;
}while (condition);

Answer: d
Explanation: Output:
do
{
statement;
}while (condition);

7. 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();
}
a) 13
b) 15
c) 11
d) 10

Answer: d
Explanation: Here in do while condition ‘&&’ i.e ‘AND’operator return ‘0’ i.e false. So, as condition is false so program comes out of the loop.
Output : 10.

8. 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();
}
a) 0 0 0….infinite times
b) 1 1 1….infinite times
c) 1 1 1 1 1 1
d) System outofflow exception error

Answer: c
Explanation: The execution of for loop is done for six consecutive times.
Output : 1 1 1 1 1 1

9. 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);
a) 1 2 3 4 5
b) 10
c) 5 6 7 8 9 10
d) 1 2 3 4 5 6 7 8 9 10

Answer: d
Explanation: The condition will print the numbers from 1 to 10 when x == 5 and when x does not satisfy if condition until x < 10.
Output: 1 2 3 4 5 6 7 8 9 10

10. 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();
}
a) 1 12 1 3 1
b) 1 12 13 1
c) 12 22 32
d) 11 21 31

Answer: c
Explanation: Output : 12 22 32