C# Questions & Answers on Continue, Goto Statements for Freshers

1. Select output for the following set of code :
  1.   static void Main(string[] args)
  2.   {
  3.       int i;
  4.       Console.WriteLine("enter value of i:");
  5.       i = Convert.ToInt32(Console.ReadLine());
  6.       if (i < 7)
  7.       {
  8.           i++;
  9.           continue;
  10.       }
  11.       Console.WriteLine("final value of i:" +i);
  12.       Console.ReadLine();
  13.   }
a) 12
b) 11
c) Compile time error
d) 13
Answer: c

Explanation: ‘Continue’ loop cannot be used within ‘if’ loop .replace while with if(i <7 br="">Output: Compile time error.
2. Select the output for the following set of Code :
  1.  static void Main(string[] args)
  2.  {
  3.      int i;
  4.      Console.WriteLine("enter value of i:");
  5.      i = Convert.ToInt32(Console.ReadLine());
  6.      if ( i % 2 == 0)
  7.          goto even:
  8.      else
  9.      {
  10.          Console.WriteLine("number is odd:");
  11.          Console.ReadLine();
  12.      }
  13.      even:
  14.      Console.WriteLine("number is even:");
  15.      Console.ReadLine();
  16.  }
  17. for i = 4.
a) number is odd
b) number is even
c) Compile time error
d) none of the mentioned
Answer: c

Explanation: “Undefined label ‘even’ in main().The syntax ‘goto even:’ is incorrect instead use ‘goto even;’.
Output:
  1.     static void Main(string[] args)
  2.     {
  3.         int i;
  4.         Console.WriteLine("enter value of i:");
  5.         i = Convert.ToInt32(Console.ReadLine());
  6.         if (i % 2 == 0)
  7.         goto even;
  8.         else
  9.         {
  10.             Console.WriteLine("number is odd:");
  11.             Console.ReadLine();
  12.         }
  13.         even:
  14.         Console.WriteLine("number is even:");
  15.         Console.ReadLine();
  16.     }
3. Select the output for the following set of code :
  1.   static void Main(string[] args)
  2.   {
  3.       int i = 1, j;
  4.       do
  5.       {
  6.           for (j = 1; ; j++)
  7.           {
  8.               if (j > 2)
  9.                   break;
  10.               if (i == j)
  11.                   continue;
  12.               Console.WriteLine(i + " " + j);
  13.           }
  14.           i++;
  15.       } while (i < 3);
  16.       Console.ReadLine();
  17.   }
a) 1 2
2 1
b) 2 1
1 2
c) 1 3
2 1
d) 1 1
2 1
Answer: a

Explanation: 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. Select the output for the following set of code :
  1.    static void Main(string[] args)
  2.    {
  3.        int i = 10 , j = 0;
  4.        label:
  5.        i--;
  6.        if ( i > 0)
  7.        {
  8.            Console.WriteLine(i+ " ");
  9.            goto label;
  10.        }
  11.        Console.ReadLine();
  12.    }
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

Explanation: 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 :9 8 7 6 5 4 3 2 1.
5. Select the output for the following set of code :
  1.   static void Main(string[] args)
  2.   {
  3.       int i = 0, j = 0;
  4.       while (i < 2)
  5.       {
  6.           l1: i--;
  7.           while (j < 2)
  8.           {
  9.               Console.WriteLine("hi\n");
  10.               goto l1;
  11.           }
  12.       }
  13.       Console.ReadLine();
  14.   }
a) hi hi hi
b) hi hi
c) hi
d) hi hi hi…..infinite times
Answer: d

Explanation: Since,i– so,test condition for ‘i’ never satisfies it fails and hence infinite loop in occurs.
output: hi hi hi…..
6. Select the output for the following set of code :
  1.   static void Main(string[] args)
  2.   {
  3.       int i = 0;
  4.       if (i == 0)
  5.       {
  6.           goto label;
  7.       }
  8.       label: Console.WriteLine("HI...");
  9.       Console.ReadLine();
  10.   }
a) Hi…infinite times
b) Code runs prints nothing
c) Hi Hi
d) Hi…
Answer: d

Explanation: for i = 0 ,if condition is satisfied as (i == 0).So,label statement is printed.
Output : Hi
7. Select the output for the following set of code :
  1.   static void Main(string[] args)
  2.   {
  3.       int i = 0, j = 0;
  4.       l1: while (i < 2)
  5.       {  
  6.           i++;
  7.           while (j < 3)
  8.           {
  9.               Console.WriteLine("loop\n");
  10.               goto l1;
  11.           }
  12.        }
  13.       Console.ReadLine();
  14.   }
a) loop is printed infinite times
b) loop
c) loop loop
d) Compile time error
Answer: c

Explanation: Since outer while loop i.e while(i<2 able="" and="" as="" be="" br="" breaks="" condition="" control="" could="" executes="" executing="" for="" goes="" i="2.hence,loop" j="" loop.="" loop="" not="" of="" only="" out="" satisfy="" third="" time="" times.hence="" to="" two="" while="">Output : loop loop.
8. Select output for the following set of code :
  1.   static void Main(string[] args)
  2.   {
  3.       int i= 0,k;
  4.       label: Console.WriteLine(i);
  5.       if (i == 0)
  6.           goto label;
  7.       Console.ReadLine();
  8.   }
a) 0 0 0 0
b) 0 0 0
c) 0 infinite times
d) 0
Answer: c

Explanation: Since, if condition is always true.Loop will continue executing always without any end condition.
Output:0 0 0….
9. Select the output for the following set of code :
  1.    static void Main(string[] args)
  2.    {
  3.        int i = 0;
  4.        int j = 0;
  5.        for (i = 0; i < 4; i++)
  6.        {
  7.            for (j = 0; j < 3; j++)
  8.            {
  9.                if (i > 1)
  10.                    continue;
  11.                Console.WriteLine("Hi \n");
  12.            }
  13.        }
  14.        Console.ReadLine();
  15.    }
a) Prints hi 4 times
b) Prints hi 3 times
c) Prints hi 6 times
d) Prints hi infinite times
Answer: c

Output : hi
hi
hi
hi
hi
hi.
10. Select the output for the following set of code :
  1.     static void Main(string[] args)
  2.     {
  3.         int a = 0;
  4.         int i = 0;
  5.         int b;
  6.         for (i = 0; i < 5; i++)
  7.         {
  8.              a++;
  9.              Console.WriteLine("Hello \n");
  10.              continue;
  11.         }
  12.         Console.ReadLine();
  13.     }
a) print hello 4 times
b) print hello 3 times
c) print hello 5 times
d) print hello infinite times
Answer: c

Explanation: Condition executes until and unless i < 5.So,it prints “hello” until ‘i’ condition is satisfied.
Output : Hello
Hello
Hello
Hello
Hello
11. Select the output for the set of code:
  1.     static void Main(string[] args)
  2.     {
  3.         Console.WriteLine("HI");
  4.         continue;
  5.         Console.WriteLine("Hello");
  6.         Console.ReadLine();
  7.     }
a) Hi Hello
b) Hi
c) Hello
d) Compile time error
Answer: d

Explanation: Absence of any loop condition in order to make decision of break or continue.

Related

C Programming Multiple Choice Questions and Answers on Error Handling in File for Freshers

1. What is the output of this C code if there is no error in stream fp? #include int main() { FILE *fp; fp = fopen("newfile", "w"); printf("%d\n", f...

C# Multiple Choice Questions & Answers on Initialization of Variables for Freshers

1. Select output for the following set of code. static void Main(string[] args) { int a = 5; int b = 10; int c; Console.WriteLine(c = ++ a + b ++); Console...

CSS Multiple Choice Questions & Answers on CSS3 Fundamentals for Freshers

1. Which of the following Module is not available in CSS3. a) DOMsb) Fontsc) Backgrounds and Bordersd) Color Answer: a Explanation: The Document Object Model (DOM) is a programming API for HTML a...

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