Which of the following is the correct syntax to declare a variable in C#?
Answer: A
C# uses the syntax 'type variableName = value;' to declare and initialize variables. The equals sign (=) is the assignment operator.
Q.2Easy
Which keyword is used to create a class in C#?
Answer: A
C# is case-sensitive. The lowercase keyword 'class' is used to define a class. 'Class' and 'CLASS' are not valid keywords.
Q.3Medium
What will be the output of the following code snippet?
int x = 5;
int y = ++x;
Console.WriteLine(x + " " + y);
Answer: B
++x is pre-increment. It increments x first (x becomes 6), then assigns the value to y. So both x and y are 6.
Q.4Medium
Which of the following data types in C# is a value type?
Answer: B
int is a value type (primitive type). string, object, and arrays are reference types in C#.
Q.5Medium
What is the correct way to declare a constant variable in C#?
Answer: A
The 'const' keyword is used to declare constants in C#. 'final' is Java syntax. While 'readonly' can be used, 'const' is the proper keyword for compile-time constants.
Advertisement
Q.6Easy
Which namespace is required to use Console.WriteLine() in C#?
Answer: B
Console class is part of the System namespace. You need to include 'using System;' at the beginning of your code to use Console.WriteLine().
Q.7Medium
What is the difference between '==' and 'Equals()' when comparing strings in C#?
Answer: A
For strings, '==' compares references (memory addresses) while Equals() compares the actual string values. However, C# has optimized string comparison, so both often behave similarly for string literals.
Q.8Medium
Which of the following will correctly convert a string to an integer in C#?
Answer: B
int.Parse() converts a string to an integer. Direct casting doesn't work for strings. Convert.ToInt32() would throw an exception if the string is not a valid number. The 'as' operator doesn't work for strings to primitives.
Q.9Easy
Which of the following is NOT a valid C# variable name?
Answer: B
Variable names in C# can contain letters, digits, and underscores. They cannot contain special characters like '$'. The variable name must start with a letter or underscore.
Q.10Easy
What will be the output of the following code?
string s = "Hello";
s = s + " World";
Console.WriteLine(s);
Answer: B
String concatenation using '+' operator joins the two strings. s becomes 'Hello World' after the concatenation.
Q.11Medium
Which of the following correctly declares a nullable integer in C#?
Answer: A
In C#, 'int?' is used to declare a nullable integer. A regular 'int' cannot be assigned null. int* is a pointer (unsafe code), not a nullable type.
Q.12Medium
What is the output of the following code?
bool result = (5 > 3) && (2 < 1);
Console.WriteLine(result);
Answer: B
(5 > 3) is true, but (2 < 1) is false. The '&&' (AND) operator returns true only if both conditions are true. Since one is false, the result is false.
Q.13Medium
Which keyword is used to prevent a class from being inherited in C#?
Answer: B
'sealed' keyword prevents a class from being inherited in C#. 'final' is Java syntax. 'abstract' requires a class to be inherited. 'static' makes members class-level, not related to inheritance.
Q.14Medium
What will happen when you try to access an array index that is out of bounds in C#?
Answer: C
Accessing an array element beyond its bounds throws an IndexOutOfRangeException at runtime. This is a safety feature to prevent accessing invalid memory.
Q.15Hard
Consider the following code. What will be the output?
int x = 10;
int y = 20;
int z = x++ + ++y;
Console.WriteLine(x + " " + y + " " + z);
Answer: B
x++ returns 10 then increments x to 11. ++y increments y to 21 then returns 21. z = 10 + 21 = 31. Final output: 11 21 31
Q.16Hard
Which of the following statements about structs and classes in C# is TRUE?
Answer: B
In C#, structs are value types (stored on stack) while classes are reference types (stored on heap). This is a fundamental difference between the two.
Q.17Easy
Which of the following is a value type in C#?
Answer: B
Enum is a value type in C#. String, Interface, and Delegate are reference types.
Q.18Easy
What is the default access modifier for a class member in C#?
Answer: B
The default access modifier for class members in C# is private, meaning they are accessible only within the same class.
Q.19Easy
Which of the following correctly declares a variable that can hold null?
Answer: B
Only nullable types (using ?) can hold null values for value types. int? is a nullable integer that can hold null.
Q.20Easy
What will be the output of: Console.WriteLine(310);
Answer: B
Integer division in C# truncates the decimal part. 310 = 3 (not 3.33) because both operands are integers.