Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

C# MCQ Set 1

1. Which of these methods of class String is used to separate a substring from a String object?
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned

View Answer

Answer: b [Reason:] None.

2. What will be the output for the given set of code?

  1.  static void Main(string[] args)
  2.  {
  3.      String a = "Ilove";
  4.      String b = "CSHARP";
  5.      b = string.Concat(a, '  ', b);
  6.      Console.WriteLine(b);
  7.      Console.ReadLine();
  8.  }

a) IloveCSHARP
b) I loveCSHARP
c) Ilove
d) Ilove CSHARP

View Answer

Answer: d [Reason:] Concat() method is used to join two strings without the use of ‘+’ operator . Output : Ilove CSHARP

3. Which of these methods of class are used to remove the leading and backward whitespaces?
a) startsWith()
b) trim()
c) Trim()
d) doTrim()

View Answer

Answer: c [Reason:] None.

4. What will be the output for the given set of code?

  1.   static void Main(string[] args)
  2.   {
  3.       String a = "Ilove";
  4.       String b = "CSHARP";
  5.       b = string.Concat(a,' ',b);
  6.       string d = b.TrimStart('I', 'l', 'o', 'H');
  7.       Console.WriteLine(d);
  8.       Console.ReadLine();
  9.   }

a) Ilove CSHARP
b) love CSHARP
c) ve CSHARP
d) ve CSARP

View Answer

Answer: c [Reason:] trimstart() removes character mentioned consecutively in front positions not characters in mentioned in between positions. Output : ve CSHARP

5. What will be the output for the given set of code?

  1.  static void Main(string[] args)
  2.  {
  3.      String c = "  Hello Computer ";
  4.      String a = c.Trim();
  5.      Console.WriteLine(""" + s + """);
  6.  }

a) ” Hello Computer ”
b) “HelloComputer”
c) “Hello Computer”
d) Hello Computer

View Answer

Answer: c [Reason:] Trim() method is used to remove forward and backward spaces in strings. Output : “Hello Computer”

6. What will be the output for the given set of code?

  1.  static void Main(string[] args)
  2.  {
  3.      String c = "Hello";
  4.      String a = c + "Bye";
  5.      Console.WriteLine(a);
  6.      Console.ReadLine();
  7.  }

a) “Hello Bye”
b) “HelloBye”
c) Hello Bye
d) HelloBye

View Answer

Answer: d [Reason:] ‘+’ operator method works in the form of concatenate method() and hence is used to join two strings together. Output : HelloBye

7. What will be the output for the given set of code?

  1. static void Main(string[] args)
  2. {
  3.     String c = "Hello";
  4.     String a ;
  5.     a = c.Replace('l', 'w');
  6.     Console.WriteLine(a);
  7.     Console.ReadLine();
  8. }

a) Helloll
b) Hewlo
c) Helwo
d) Hewwo

View Answer

Answer: d [Reason:] None. Output : Hewwo

8. Which of the following statements is correct?
a) replace() replace() method replaces last occurrence of a character in invoking strings with another character
b) replace() method replaces only first occurrence of a character in invoking strings with another character
c) replace() method replaces all occurrences of one character in invoking strings with another character
d) none of the mentioned

View Answer

Answer: c [Reason:] By definition.

9. What will be the output of the given code snippet?

  1.   static void Main(string[] args)
  2.   {
  3.       String c = "Hello i love you";
  4.       String a ;
  5.       a = c.Substring(12, 3);
  6.       Console.WriteLine(a);
  7.       Console.ReadLine();
  8.   }

a) ove
b) you
c) yo
d) love you

View Answer

Answer: c [Reason:] None. Output : yo

10. Which among the following is the correct way to find out the index of second ‘s’ in the string “She sold her beauty in one night to someone else”?

  1. a) String a = "She sold her beauty in one night to someone else";
  2.    int i;
  3.    i = a.SecondIndexOf("s");
  4. b) String a = "She sold her beauty in one night to someone else";
  5.    int i, j;
  6.    i = a.FirstIndexOf("s");
  7.    j = a.IndexOf("s", i + 1);
  8. c) String a = "She sold her beauty in one night to someone else";
  9.    int i, j;
  10.    i = a.IndexOf("s");
  11.    j = a.IndexOf("s", i + 1);
  12. d) None of the mentioned

View Answer

Answer: c [Reason:] static void Main(string[] args) { String c = “She sold her beauty in one night to someone else”; int i,j;

i = c.IndexOf(“s”); j = c.IndexOf(“s”, i + 1); Console.WriteLine(i + ” ” + j); Console.ReadLine(); } Output : 4, 36

C# MCQ Set 2

1.Which of the following is a correct statement about the C#.NET code given below?

  1. struct book
  2. {
  3.     private String name;
  4.     private int pages;
  5.     private Single price;
  6. }
  7. book b = new book();

a) New structure can be inherited from struct book
b) When the program terminates, variable b will get garbage collected
c) The structure variable ‘b’ will be created on the stack
d) When the program terminates, variable b will get garbage collected

View Answer

Answer: c [Reason:] None.

2. Which of the following is a correct statement about the C#.NET code given below?

  1. class trial
  2. {
  3.     int i;
  4.     float d;
  5. }
  6. struct sample
  7. {
  8.     private int x;
  9.     private Single y;
  10.     private trial z;
  11. }
  12. sample s = new sample();

a) trial object referred by z is created on the stack
b) z is created on the heap
c) Both s and z will be created on the heap
d) s will be created on the stack

View Answer

Answer: d [Reason:] None.

3. Choose the correct statement among the following which supports the fact that C# does not allow the creation of empty structures?
a) C#.NET supports creation of abstract user-defined data types using structures
b) By having empty structures,it would mean that the new data types have no data associated with, which does not make any sense in C#.NET
c) Basic reason to create structures is the inability to represent real life objects using standard data types offered by the language
d) All of the mentioned

View Answer

Answer: d [Reason:] Basic definition of structures in C#.NET.

4. Choose the correct statement about structures as to why they are defined as value types but not reference types?
a) Since space required for structure variables is allocated on stack which is a form of memory that is automatically available when a variable to be used is in scope.
b) Structures generally are used to represent user defined data types that consists of small amount of data in them.Hence using stack for declaration of such variables is not a problem.
c) All of the mentioned
d) None of the mentioned

View Answer

Answer: c [Reason:] None.

5. Choose the wrong statement about structures in C#.NET?
a) Structures can be declared within a procedure
b) Structures can implement an interface but they cannot inherit from another structure
c) Structure members cannot be declared as protected
d) A structure cannot be empty

View Answer

Answer: a [Reason:] None.

6. The correct way to define a variable of type struct abc among the following is?

  1.  struct abc
  2.  { 
  3.      public string name;
  4.      protected internal int age;
  5.      private Single sal;
  6.  }

a) abc e = new abc();
b) abc();
c) abc e;
e = new abc;
d) abc e = new abc;

View Answer

Answer: a [Reason:] None.

7. Which of the following is the correct way to settle down values into the structure variable ‘e’ defined in the following code snippet?

  1.  struct emp
  2.  { 
  3.      public String name;
  4.      public int age;
  5.      public Single sal;
  6.  }
  7.  emp e = new emp();

a)

  1. e.name = "Ankit";
  2.      e.age = 24;
  3.      e.sal = 200;

b)

  1. With emp e
  2.    {
  3.      .name = "Ankit";
  4.      .age = 24;
  5.      .sal = 200;
  6.    }

c)

  1. name = "Ankit";
  2.    age = 24;
  3.    sal = 200;

d) All of the mentioned

View Answer

Answer: a [Reason:] None.

8. When does a structure variable get destroyed?
a) When no reference refers to it,it will get garbage collected
b) Depends on whether it is created using new or without new operator
c) As variable goes out of the scope
d) Depends on either we free its memory using free() or delete()

View Answer

Answer: c [Reason:] None.

9. Calculate the number of bytes a structure variable s occupies in the memory if it is defined as follows.

  1.   class abc
  2.   {
  3.       int i;
  4.       Decimal d;
  5.   }
  6.   struct sample
  7.   {
  8.      private int x;
  9.      private Single y;
  10.      private trial z;
  11.   }
  12.   sample s = new sample();

a) 24 bytes
b) 8 bytes
c) 16 bytes
d) 12 bytes

View Answer

Answer: d [Reason:] None. Output – 12 bytes

10. Which of the following is the correct output for the C#.NET program code given below?

  1. {
  2.     struct abc
  3.     {
  4.         public int i;
  5.     }  
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             sample a = new sample();
  11.             a.i = 10;
  12.             fun(ref a);
  13.             Console.WriteLine(a.i);
  14.         }
  15.         public static voidn fun(ref sample x)
  16.         {
  17.             x.i = 20; 
  18.             Console.WriteLine(x.i);
  19.         }
  20.     }
  21. }

a) 10
10
b) 20
10
c) 10
20
d) 20
20

View Answer

Answer: d [Reason:] None. Output – 20 20

11. Select the wrong statements among the following?
a) A structure can contain properties
b) A structure can contain constructors
c) A structure can contain protected data members
d) A structure can contain constants

View Answer

Answer: c [Reason:] None.

12. Which of the following is the correct result for the given statement in the C#.NET statement given below?
p = q

  1. struct employee
  2. {
  3.     private int employee id;
  4.     private string city;
  5. }
  6. employee q = new employee();
  7. employee p;
  8. p = q;

a) Elements of ‘q’ will be copied into corresponding elements of p.
b) Address stored in q will get copied into p
c) Address of first element of q will get copied into p
d) Once assignment is over.q will go out of scope and hence get exited forever

View Answer

Answer: a [Reason:] None.

13. Which of the following will be the correct output for the program given below?

  1.  {
  2.      struct abc
  3.      {
  4.          int i;
  5.      }
  6.      class Program
  7.      {
  8.          static void Main(string[] args)
  9.          {
  10.              abc x = new abc();
  11.              abc z;
  12.              x.i = 10;
  13.              z = x;
  14.              z.i = 15;
  15.              console.Writeline(x.i + "  " + y.i)
  16.          }
  17.      }
  18.  }

a) 10 10
b) 10 15
c) 15 10
d) 15 15

View Answer

Answer: b [Reason:] None. Output: 10 15

C# MCQ Set 3

1. Select the output for the following set of code.

  1.   static void Main(string[] args)
  2.   {
  3.       int movie = 1;
  4.       switch (movie << 2 + movie)
  5.       {
  6.       default: 
  7.           Console.WriteLine("3 Idiots");
  8.           break;
  9.       case 4: 
  10.           Console.WriteLine("Ghazini");
  11.           break;
  12.       case 5: 
  13.           Console.WriteLine("Krishh");
  14.           break;
  15.       case 8: 
  16.           Console.WriteLine("Race");
  17.           break;
  18.       }
  19.       Console.ReadLine();
  20.   }

a) 3 Idiots
b) Ghazini
c) Race
d) Krishh

View Answer

Answer: c [Reason:] We can put ‘default’ case in any order and hence write cases in any order. Output: Race.

2. Select the output for the following set of code :

  1.   static void Main(string[] args)
  2.   {
  3.       int i = 2, j = 4;
  4.       switch (i + j * 2)
  5.       {
  6.       case 1 :
  7.       case 2 :
  8.           Console.WriteLine("1 and 2");
  9.           break;
  10.       case 3 to 10:
  11.           Console.WriteLine("3 to 10");
  12.           break;
  13.       }
  14.       Console.ReadLine();
  15.   }

a) 3 to 10 will be printed
b) 1 and 2 will be printed
c) The code reports an error as missing ; before :
d) The code gives output as 3 to 10

View Answer

Answer: c [Reason:] Syntax error- switch case does not work with syntax as 3 to 10: Output : static void Main(string[] args) { int i = 2,j = 4; switch (i + j * 2) { case 1 : case 2 : Console.WriteLine(“1 and 2”); break; case 3 : Console.WriteLine(“3 to 10”); break; } Console.ReadLine(); } Here i = 2,j = 4.So,(i + j * 2) gives output as 10 and case 10 is missing.So,prints nothing for given code.

3. Select the output for the following set of code :

  1.  static void Main(string[] args)
  2.  {
  3.      int i = 2, k = 3;
  4.      switch (i - k)
  5.      {
  6.      case -1:
  7.          ++i;
  8.          ++k;
  9.          break;
  10.      case 2:
  11.          --i;
  12.          ++k;
  13.          break;
  14.      default:
  15.          i += 3;
  16.          k += i;
  17.          break;
  18.      }
  19.      Console.WriteLine(i + "n" + k);
  20.      Console.ReadLine();
  21.  }

a) 2 3 3
b) 3 2 3
c) 3 4 4
d) 5 10 10

View Answer

Answer: c Output: 3 4 4 [Reason:] i – k = -1.So, case -1 will be executed only.

4. Select output for the following set of code :

  1.    static void Main(string[] args)
  2.    {
  3.        int const p = 0;
  4.        switch (3 * 5 / 6)
  5.        {
  6.        case p: 
  7.            Console.WriteLine("A");
  8.            break;
  9.        case p * 1:
  10.            Console.WriteLine("B");
  11.            break;
  12.        case p - 2:
  13.            Console.WriteLine("C");
  14.            break;
  15.        default: 
  16.            Console.WriteLine("D");
  17.        }
  18.   }

a) A
b) B
c) C
d) Compile time error

View Answer

Answer: d [Reason:] In case expression we don’t have constant variable.

5. Select output for the following set of code :

  1.  static void Main(string[] args)
  2.  {
  3.      int i = 2, j = 3, k = 4;
  4.      switch (i + j - k)
  5.      {
  6.      case 0: case 2: case 4:
  7.          ++i;
  8.          k += j;
  9.          break;
  10.      case 1: case 3: case 5 :
  11.          --i;
  12.          k -= j;
  13.          break;
  14.      default:
  15.          i += j;
  16.          break;
  17.      }
  18.      Console.WriteLine(i + "n" + j + "n" + k);
  19.      Console.ReadLine();
  20.  }

a) 1 3 1
b) 2 3 4
c) 5 3 4
d) Compile time error.

View Answer

Answer: a [Reason:] Solving expression (i + j – k) gives 1 and hence,solving for case 1:case 3:case 5:. Output : 1 3 1

6. Select the output for the following set of code :

  1.   static void Main(string[] args)
  2.   {
  3.       int  i = 9 , j = 7;
  4.       switch (i - j + 3)
  5.       {
  6.       case 9: 7:
  7.           j += 6;
  8.           break;
  9.       case 5:
  10.           i -= 4;
  11.           break;
  12.       }
  13.       Console.WriteLine(i + "n" + j);
  14.       Console.ReadLine();
  15.   }

a) 5 7
b) 9 13
c) Compile time error
d) 9 7

View Answer

Answer: c [Reason:] Invalid expression’7:’ in case 9:7:.

7. Select the output for the following code :

  1.   static void Main(string[] args)
  2.   {
  3.       switch (5)
  4.       {
  5.       case 5.0f: 
  6.           Console.WriteLine("harsh");
  7.           break;
  8.       case 5: 
  9.           Console.WriteLine("amish");
  10.           break;
  11.       case 5.0L: 
  12.           Console.WriteLine("ANKIT");
  13.           break;
  14.       default:
  15.           Console.WriteLine("ashish");
  16.       }
  17.       Console.ReadLine();
  18.    }

a) amish
b) ANKIT
c) harsh
d) Compile time error

View Answer

Answer: d [Reason:] Only integral values are allowed for case expression. 5.0f = (int)5.0f. 5.0L =(int)5.0L.

8. Select output for the following code:

  1.   static void Main(string[] args)
  2.   {
  3.       int i;
  4.       int j = 1;
  5.       int []ar = {21, 22, 13, 4};
  6.       switch (ar[j])
  7.       {
  8.       case 1:
  9.           i++;
  10.           break;
  11.       case 2:
  12.           i += 2;
  13.           j = 3;
  14.           continue;
  15.       case 3: 
  16.          i %= 2;
  17.          j = 4;
  18.          continue;
  19.       default: 
  20.          --i;
  21.       }
  22.       Console.WriteLine(i);
  23.       Console.ReadLine();
  24.   }

a) 23
b) 15
c) Compile time error
d) 12

View Answer

Answer: c [Reason:] Continue cannot be used as a part of switch statement. Output : static void Main(string[] args) { int i; int j = 1; int []ar = {21 , 22, 13, }; switch(ar[j]) { case 1: i++; break; case 2: i += 2; j = 3; continue; case 3: i %= 2; j = 4; default: –i; } Console.WriteLine(i); Console.ReadLine(); }

9. Select the output for the following set of Code:

  1.  static void Main(string[] args)
  2.  {
  3.      char ch = Convert.ToChar('a' | 'b' | 'c');
  4.      switch (ch)
  5.      {
  6.      case 'A':
  7.      case 'a':
  8.          Console.WriteLine("case A|case a");
  9.          break;
  10.      case 'B':
  11.      case 'b':
  12.          Console.WriteLine("case B|case b");
  13.          break;
  14.      case 'C':
  15.      case 'c':
  16.      case 'D':
  17.      case 'd':
  18.          Console.WriteLine("case D|case d");
  19.          break;
  20.      }
  21.      Console.ReadLine();
  22.  }

a) Compile time error
b) case A|case a
c) case B|case b
d) case D|case d

View Answer

Answer: d [Reason:] Case statement declared last will only be executed as no particular casenumber is declared is to be called. Output : case D|case d

10. Select the output for the following set of Code:

  1.   static void Main(string[] args)
  2.   {
  3.       char ch = 'p';
  4.       switch (ch)
  5.       {
  6.       case 'p':
  7.           Console.WriteLine("coco" + "t" + Convert.ToInt32(ch));
  8.           break;
  9.       default:
  10.           Console.WriteLine("default");
  11.           break; 
  12.      }
  13.      Console.WriteLine("main");
  14.   }

a) coco main
b) coco 112
c) coco 112 main
d) compile time error

View Answer

Answer: c [Reason:] ASCII value of ‘p’ is 112.Hence, coco 112 main. Output: coco 112 main.

C# MCQ Set 4

1. What is the use of try & catch?
a) It is used to manually handle the exception
b) It helps to fix the errors
c) It prevents automatic terminating of the program in cases when an exception occurs
d) All of the mentioned

View Answer

Answer: d [Reason:] None.

2. What is the output of this program?

  1.  class Output 
  2.  {
  3.      public static void main(String args[]) 
  4.      {
  5.  
  6.          try 
  7.          {
  8.              int a = 9;
  9.              int b = 5;
  10.              int c = a / b - 5;
  11.              Console.WriteLine("Hello");
  12.          }
  13.          catch(Exception e) 
  14.          {
  15.              Console.WriteLine("C");
  16.          } 
  17.          finally 
  18.          {
  19.              Console.WriteLine("sharp");
  20.          } 
  21.      }
  22.  }

a) Hello
b) C
c) Hellosharp
d) Csharp

View Answer

Answer: d [Reason:] finally block execution takes place after the tryblock, no matter exception is found or not. catch block is executed only when exception is found. Here divide by zero exception is found hence both catch and finally are executed.

3. Choose the statement which is incorrect?
a) try block does not need to be followed by catch block
b) try block can be followed by finally block instead of catch block
c) try can be followed by both catch and finally block
d) try need not to be followed by anything

View Answer

Answer: d [Reason:] try followed by either catch or finally block.

4. What will be the output of the program?

  1.   class Output 
  2.   {
  3.       public static void main(String args[]) 
  4.       {
  5.           try 
  6.           {
  7.               int a = 10;
  8.               int b = 5;
  9.               int c = a / b - 5;
  10.               Console.WriteLine("Hi");
  11.           }
  12.           catch(Exception e) 
  13.           {
  14.               Console.WriteLine("hello");
  15.           } 
  16.       }
  17.   }

a) Hi
b) hello
c) Hihello
d) Compile time error

View Answer

Answer: b [Reason:] None.

5. Which of the keywords are used for the block to be examined for exceptions?
a) try
b) catch
c) throw
d) check

View Answer

Answer: a [Reason:] try is used for the block that needs to be checked for exception.

6. Which of these keywords are used for the block to handle the exceptions generated by try block?
a) try
b) catch
c) throw
d) check

View Answer

Answer :b [Reason:] None.

7. What is the output of this program?

  1.   class Output 
  2.   {
  3.       public static void main(String args[]) 
  4.       {
  5.          try 
  6.          {
  7.              int a = 5;
  8.              int b = 10;
  9.              int c = b / a - 5;
  10.              Console.WriteLine("Csharp");
  11.          } 
  12.      }
  13.  }

a) Csharp
b) sharp
c) C
d) Compile time error

View Answer

Answer: d [Reason:] try should be followed by either catch or finally.

8. What is the output of the given code snippet?

  1.  class Output 
  2.  {
  3.      public static void main(String args[]) 
  4.     {
  5.         try 
  6.         {
  7.             int a = 0;
  8.             int b = 5;
  9.             int c = a / b - 5;
  10.             Console.WriteLine("C");
  11.         }
  12.         finally 
  13.         {
  14.             Console.WriteLine("sharp");
  15.         } 
  16.     }
  17. }

a) C
b) sharp
c) Csharp
d) None of the mentioned

View Answer

Answer: c [Reason:] finally block is always executed after try block, no matter if the exception is found or not.

9. What will be the output of the code snippet?

  1.  class Output 
  2.  {
  3.      public static void main(String args[]) 
  4.      {
  5.          try 
  6.          {
  7.              int a = 10;
  8.              int b = 5;
  9.              int c =  b - 5 / 5;
  10.              Console.WriteLine("Hi");
  11.          }
  12.          catch(Exception e) 
  13.          {
  14.              Console.WriteLine("hello");
  15.          } 
  16.      }
  17.  }

a) Hi
b) hello
c) Hihello
d) Compile time error

View Answer

Answer: a [Reason:] None.

10. Which of these keywords are used for generating an exception manually?
a) try
b) catch
c) throw
d) check

View Answer

Answer: c [Reason:] None.

C# MCQ Set 5

1. Select the output for the following set of code :

  1.  static void Main(string[] args)
  2.  {
  3.      int i, j;
  4.      for (i = 1; i <= 3; i++)
  5.      {
  6.          j = 1;
  7.          while (i % j == 2)
  8.          {
  9.              j++;
  10.          }
  11.          Console.WriteLine(i + " " + j);
  12.      }
  13.      Console.ReadLine();
  14.  }

a) 11 21 31
b) 1 12 13 1
c) 11 21 31
d) 1 1 2 1 3 1

View Answer

Answer: c [Reason:] 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.

2. Select the output for the following set of code:

  1.  static void Main(string[] args)
  2.  {
  3.      float s = 0.1f;
  4.      while (s <= 0.5f)
  5.      {
  6.          ++s;
  7.          Console.WriteLine(s);
  8.      }
  9.      Console.ReadLine();
  10.  }

a) 0.1
b) 1.1
c) 0.1 0.2 0.3 0.4 0.5
d) No output

View Answer

Answer: b [Reason:] 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

3. Select the output for the following set of Code:

  1.  static void Main(string[] args)
  2.  {
  3.      int i;
  4.      i = 0;
  5.      while (i++ < 5)
  6.      {
  7.          Console.WriteLine(i);
  8.      }
  9.      Console.WriteLine("n");
  10.      i = 0;
  11.      while ( ++i < 5)
  12.     {
  13.         Console.WriteLine(i);
  14.     }
  15.     Console.ReadLine();
  16. }

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

View Answer

Answer: c [Reason:] 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

4. Select the output for the following set of Code:

  1.  static void Main(string[] args)
  2.  {
  3.      int x = 0;  
  4.      while (x < 20)
  5.      {
  6.          while (x < 10)
  7.          {
  8.              if (x % 2 == 0)
  9.              {
  10.                  Console.WriteLine(x);
  11.              }
  12.              x++;
  13.          }
  14.      }
  15.      Console.ReadLine();
  16.  }

a) 1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
b) 0 2 4 6 8 10 12 14 16 18 20
c) 0 2 4 6 8
d) 0 2 4 6 8 10

View Answer

Answer: c [Reason:] Inner while loop condition checks for even number between 0 an 10 and hence prints number between the given range. Output: 0 2 4 6 8.

5. Predict the output for the following set of code :

  1.  static void Main(string[] args)
  2.  {
  3.      int x;
  4.      x = Convert.ToInt32(Console.ReadLine());
  5.      int c = 1;
  6.      while (c <= x)
  7.      {
  8.          if (c % 2 == 0)
  9.          {
  10.              Console.WriteLine("Execute While " + c + "t" + "time");
  11.          }
  12.          c++;
  13.      }
  14.      Console.ReadLine();
  15.  }
  16. for x = 8.

a) Execute while 1 time
Execute while 3 time
Execute while 5 time
Execute while 7 time
b) Execute while 2 time
Execute while 4 time
Execute while 6 time
Execute while 8 time
c) Execute while 1 time
Execute while 2 time
Execute while 3 time
Execute while 4 time
Execute while 5 time
Execute while 6 time
Execute while 7 time
d) Execute while 2 time
Execute while 3 time
Execute while 4 time
Execute while 5 time

View Answer

Answer: b [Reason:] Checks condition if number is divisible by 2 then it will print it even number times as given for x = 8 so, prints between 2 to 8 times Similarly, for x = 5, Execute 2 and 4 time. OUTPUT: Execute while 2 time. Execute while 4 time. Execute while 6 time. Execute while 8 time.

6. Select the output for the following set of Code:

  1.   static void Main(string[] args)
  2.   {
  3.       int n, r;
  4.       n = Convert.ToInt32(Console.ReadLine());
  5.       while (n > 0)
  6.       {
  7.           r = n % 10;
  8.           n = n / 10;
  9.           Console.WriteLine(+r);
  10.       }
  11.       Console.ReadLine();
  12.   }
  13.  for n = 5432.

a) 3245
b) 2354
c) 2345
d) 5423

View Answer

Answer: c [Reason:] Reverse of number using while loop. Output: 2345.

7. Correct syntax for while statement is:

  1.  a) while
  2.     {
  3.  
  4.  
  5.  
  6.      }(condition);
  7.  b) while(condition)
  8.     {
  9.  
  10.  
  11.      };
  12.  c) while(condition)
  13.     {
  14.  
  15.  
  16.      }
  17.  d) while(condition);
  18.     {
  19.  
  20.  
  21.      }

View Answer

Answer:c [Reason:] By defination.

8. Select the output for the following set of Code:

  1.   static void Main(string[] args)
  2.   {
  3.       float i = 1.0f,  j = 0.05f;
  4.       while (i  < 2.0f  &&  j  <= 2.0f)
  5.       {
  6.           Console.WriteLine(i++ - ++j);
  7.       }
  8.       Console.ReadLine();
  9.   }

a) 0.05f
b) 1.50f
c) -0.04999995f
d) 1.50f

View Answer

Answer: c [Reason:] 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.

9. Select the output for the following set of code :

  1.  static void Main(string[] args)
  2.  {
  3.      int i = 0;
  4.      while (i <= 50)
  5.      {
  6.          if (i % 10 == 0)
  7.          continue;
  8.          else
  9.          break;
  10.          i += 10;
  11.          Console.WriteLine(i % 10);
  12.     }
  13. }

a) code prints output as 0 0 0 0 0
b) code prints output as 10 20 30 40 50
c) infinite loop but donot print anything
d) Code generate error

View Answer

Answer: c [Reason:] None.

10. Select the output for the following set of code:

  1.   static void Main(string[] args)
  2.   {
  3.       int i = 1, j = 1;
  4.       while (++i <= 10)
  5.       {
  6.           j++;
  7.       }
  8.       Console.WriteLine(i+ "  " +j);
  9.       Console.ReadLine();
  10.   }

a) 12 11
b) 10 11
c) 11 10
d) 11 12

View Answer

Answer: c [Reason:] 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.

11. Select the output for following set of Code :

  1.    static void Main(string[] args)
  2.    {
  3.        int i = 1;
  4.        while (i <= 1)
  5.        {
  6.            if ('A' < 'a')
  7.            {
  8.                Console.WriteLine("Hello...");
  9.            }
  10.            else
  11.            {
  12.               Console.WriteLine("Hi...");
  13.            }
  14.               i++;
  15.        }
  16.        Console.ReadLine();
  17.    }

a) Hi…
b) Hello….
c) Hi…infinite times
d) Hello infinite times

View Answer

Answer: b [Reason:] Ascii value of ‘A’ is 65 and ‘a’ is 97.So, clearly ‘A’ < ‘a’. Output:Hello.

12. Select the output for the following set of codes:

  1.   static void Main(string[] args)
  2.   {
  3.       int i = 0;
  4.       while (i++ != 0) ;
  5.       Console.WriteLine(i);
  6.       Console.ReadLine();
  7.   }

a) -127 to +127
b) 0 to 127
c) 1
d) Infinite loop condition

View Answer

Answer: c [Reason:] i++ = first executes then increments as i = 0. So, i++ != 0, which is false clearly as i = 0. Now, control goes inside loop with i = 1. So, statement prints i = 1. Output: 1.