C# Questions & Answers – Initialization of Variables

«
»

This section of our 1000+ C# multiple choice questions focuses on Initialization of variables in C# Programming Language.

1. What will be the output of the following C# code?

advertisement
  1.  static void Main(string[] args)
  2.  {
  3.      int a = 5;
  4.      int b = 10;
  5.      int c;
  6.      Console.WriteLine(c = ++ a + b ++);
  7.      Console.WriteLine(b);
  8.      Console.ReadLine();
  9.  }

a) 11, 10
b) 16, 10
c) 16, 11
d) 15, 11
View Answer

Answer: c
Explanation: c = 6 + 10 = 16 and b = 11 as we know ++operator increments and then executes similarly operator++ executes and then increments.
Output:

advertisement
advertisement
16, 11

2. Storage location used by computer memory to store data for usage by an application is?
a) Pointers
b) Constants
c) Variable
d) None of the mentioned
View Answer

Answer: c
Explanation: ‘Variables’ are essential locations in memory of computer that are reserved for storing data used by an application. Each variable is given a name by programmer and hence assigned a value. The name assigned to variable then used in C# code to access value assigned to variable.
advertisement

3. DIFFERENCE BETWEEN KEYWORDS ‘VAR’ AND ‘DYNAMIC’?
a) ‘Var’ is introduced in C# (3.0) and ‘Dynamic’ is introduced in C# (4.0)
b) ‘Var’ is a type of variable where declaration is done at compile time by compiler while ‘Dynamic’ declaration is achieved at runtime by compiler
c) For ‘Var’ Error is caught at compile time and for ‘Dynamic’ Error is caught at runtime
d) All of the mentioned
View Answer

Answer: d
Explanation: None.

4. The following C# codes are?

advertisement
  1.  1. Myclass class;
  2.     Myclass class2 = null;
  1.  2. int i;
  2.     int j = 0;

a) True for (1);False for (2)
b) True for (2);False for (1)
c) Both (1) and (2) are equivalents
d) Both (1) and (2) are not equivalents
View Answer

Answer: c
Explanation: When we create a type in ‘C#’, It automatically gets filled with padded zeros. For the case of class (reference types) this equates to a null pointer. Hence, for code 1) Both variable values are equivalent to each other.Similarly, for code 2) i.e for value type (including int/float/double etc.), the type is passed with zeros. Hence, they are equivalent.
advertisement

5. What will be the output of the following C# code?

  1.   int a,b;
  2.   a = (b = 10) + 5;

a) b = 10, a = 5
b) b = 15, a = 5
c) a = 15, b = 10
d) a = 10, b = 10
View Answer

Answer: c
Explanation: b is assigned 10 and after that its value is added with 5 and then saved in a, so a will be 15.

6. What will be the output of the following C# code conversion?

  1.  static void Main(string[] args)
  2.  {
  3.      char a = 'A';
  4.      string b = "a";
  5.      Console.WriteLine(Convert.ToInt32(a));
  6.      Console.WriteLine(Convert.ToInt32(Convert.Tochar(b)));
  7.      Console.ReadLine();
  8.  }

a) 1, 97
b) 65, 97
c) 65, 97
d) 97, 1
View Answer

Answer: c
Explanation: ASCII value of character ‘a’ is 65 and ASCII value of string “a” is 97.
Output:

65, 97

7. What will be the output of the following C# code?

  1. static void Main(string[] args)
  2. {
  3.     String name = "Dr.Gupta";
  4.     Console.WriteLine("Good Morning" + name);
  5. }

a) Dr.Gupta
b) Good Morning
c) Good Morning Dr.Gupta
d) Good Morning name
View Answer

Answer: c
Explanation: How to initialize a string variable and concatenate string using ‘+’ operator.
Output:

 Good Morning Dr.Gupta.

8. What will be the output of the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 5;
  4.      int b = 10;
  5.      int c;
  6.      Console.WriteLine(c = a-- - ++b);
  7.      Console.WriteLine(b);
  8.      Console.ReadLine();
  9.  }

a) -7, 10
b) -5, 11
c) -6, 11
d) 15, 11
View Answer

Answer: c
Explanation: None.
Output:

-6, 11

9. What will be the output of the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      const int a = 5;
  4.      const int b = 6;
  5.      for (int i = 1; i <= 5; i++)
  6.      {
  7.          a = a * i;
  8.          b = b * i;
  9.      }
  10.      Console.WriteLine(a);
  11.      Console.WriteLine(b);
  12.      Console.ReadLine();
  13.  }

a) 600, 720
b) Compile time error
c) 25, 30
d) 5, 6
View Answer

Answer: b
Explanation: The left hand side of an assignment must be a variable, property or indexer i.e for both ‘a’ and ‘b’

10. What will be the output of the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      string Name = "He is playing in a ground.";
  4.      char[] characters = Name.ToCharArray();
  5.      StringBuilder sb = new StringBuilder();
  6.      for (int i = Name.Length - 1; i >= 0; --i)
  7.      {
  8.          sb.Append(characters[i]);
  9.      }
  10.      Console.Write(sb.ToString());
  11.      Console.ReadLine(); 
  12.  }

a) He is playing in a grou
b) .ground a in playing is He
c) .dnuorg a ni gniyalp si eH
d) He playing a
View Answer

Answer: c
Explanation: Reversal of array of strings character by character.
Output:

.dnuorg a ni gniyalp si eH

Sanfoundry Global Education & Learning Series – C# Programming Language.

To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.