This set of C# online test on “Continue, Goto statements”.
1. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i;
-
Console.WriteLine("enter value of i:");
-
i = Convert.ToInt32(Console.ReadLine());
-
if (i < 7)
-
{
-
i++;
-
continue;
-
}
-
Console.WriteLine("final value of i:" +i);
-
Console.ReadLine();
-
}
a) 12
b) 11
c) Compile time error
d) 13
Answer: c
Clarification: ‘Continue’ loop cannot be used within ‘if’ loop. replace while with if(i <7).
Output: Compile time error.
2. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i;
-
Console.WriteLine("enter value of i:");
-
i = Convert.ToInt32(Console.ReadLine());
-
if ( i % 2 == 0)
-
goto even:
-
else
-
{
-
Console.WriteLine("number is odd:");
-
Console.ReadLine();
-
}
-
even:
-
Console.WriteLine("number is even:");
-
Console.ReadLine();
-
}
-
for i = 4.
a) number is odd
b) number is even
c) Compile time error
d) none of the mentioned
Answer: c
Clarification: “Undefined label ‘even’ in main(). The syntax ‘goto even:’ is incorrect instead use ‘goto even;’.
3. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i = 1, j;
-
do
-
{
-
for (j = 1; ; j++)
-
{
-
if (j > 2)
-
break;
-
if (i == j)
-
continue;
-
Console.WriteLine(i + " " + j);
-
}
-
i++;
-
} while (i < 3);
-
Console.ReadLine();
-
}
a)
1 2 2 1
b)
2 1 1 2
c)
1 3 2 1
d)
1 1 2 1
View Answer
Answer: a
Clarification: for i = 1. When control enters in loop first if condition is checked for where j = 1 and as (j > 2) which is false. Control is now passed to console statement with i = 1 and j = 2. Now, in while condition value of ‘i’ reflected is 2 i.e i = 2 as i++. Since, (i < 3) control again enters in for loop with i = 2 but j = 1 not j = 2 for j++ and hence, again same condition executes for console statement.
Output : 1 2
2 1
4. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i = 10 , j = 0;
-
label:
-
i--;
-
if ( i > 0)
-
{
-
Console.WriteLine(i+ " ");
-
goto label;
-
}
-
Console.ReadLine();
-
}
a) 1 2 3 4 5 6 7 8 9 10
b) 10 9 8 7 6 5 4 3 2 1 0
c) 9 8 7 6 5 4 3 2 1
d) 10 9 8 7 6 5 4 3 2 1
Answer: c
Clarification: for i = 10, loop executes for first time in ‘if’ loop as (i>0) i.e (9 > 0) and hence printing ‘9’. Similarly, label condition executes again go for (i–) i.e (9-1=8) and hence again prints i = 8. In this way looping condition executes as 9, 8 to 3, 2, 1.
OUTPUT :
5. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i = 0, j = 0;
-
while (i < 2)
-
{
-
l1: i--;
-
while (j < 2)
-
{
-
Console.WriteLine("hin");
-
goto l1;
-
}
-
}
-
Console.ReadLine();
-
}
a) hi hi hi
b) hi hi
c) hi
d) hi hi hi…..infinite times
Answer: d
Clarification: Since, i– so, test condition for ‘i’ never satisfies it fails and hence infinite loop in occurs.
output:
6. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i = 0;
-
if (i == 0)
-
{
-
goto label;
-
}
-
label: Console.WriteLine("HI...");
-
Console.ReadLine();
-
}
a) Hi…infinite times
b) Code runs prints nothing
c) Hi Hi
d) Hi…
Answer: d
Clarification: for i = 0, if condition is satisfied as (i == 0). So, label statement is printed.
Output :
7. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i = 0, j = 0;
-
l1: while (i < 2)
-
{
-
i++;
-
while (j < 3)
-
{
-
Console.WriteLine("loopn");
-
goto l1;
-
}
-
}
-
Console.ReadLine();
-
}
a) loop is printed infinite times
b) loop
c) loop loop
d) Compile time error
Answer: c
Clarification: Since outer while loop i.e while(i
Output :
8. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i= 0,k;
-
label: Console.WriteLine(i);
-
if (i == 0)
-
goto label;
-
Console.ReadLine();
-
}
a) 0 0 0 0
b) 0 0 0
c) 0 infinite times
d) 0
Answer: c
Clarification: Since, if condition is always true. Loop will continue executing always without any end condition.
Output:
9. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
int i = 0;
-
int j = 0;
-
for (i = 0; i < 4; i++)
-
{
-
for (j = 0; j < 3; j++)
-
{
-
if (i > 1)
-
continue;
-
Console.WriteLine("Hi n");
-
}
-
}
-
Console.ReadLine();
-
}
a) Prints hi 4 times
b) Prints hi 3 times
c) Prints hi 6 times
d) Prints hi infinite times
Answer: c
Clarification: None.
Output :
10. Select the output for the following set of code :
-
static void Main(string[] args)
-
{
-
int a = 0;
-
int i = 0;
-
int b;
-
for (i = 0; i < 5; i++)
-
{
-
a++;
-
Console.WriteLine("Hello n");
-
continue;
-
}
-
Console.ReadLine();
-
}
a) print hello 4 times
b) print hello 3 times
c) print hello 5 times
d) print hello infinite times
Answer: c
Clarification: Condition executes until and unless i
Output :
Hello Hello Hello Hello Hello
11. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{
-
Console.WriteLine("HI");
-
continue;
-
Console.WriteLine("Hello");
-
Console.ReadLine();
-
}
a) Hi Hello
b) Hi
c) Hello
d) Compile time error
Answer: d
Clarification: Absence of any loop condition in order to make decision of break or continue.
To practice all areas of C# for online tests,