Which keyword is used to create an immutable class member in C#?
Answer: B
readonly keyword prevents reassignment of a field after initialization. const is for compile-time constants, static for class-level, sealed for inheritance prevention.
Q.22Medium
What is the difference between 'var' and explicit type declaration in C#?
Answer: B
var is implicitly typed, meaning the compiler infers the type at compile time, but it's still strongly typed. It's not dynamic.
Q.23Easy
Which of the following is a reference type in C#?
Answer: C
Classes are reference types. byte, decimal are value types, and struct is also a value type.
Q.24Medium
What will happen if you try to access an array element beyond its bounds?
Answer: B
Accessing array elements beyond bounds throws IndexOutOfRangeException at runtime.
Q.25Medium
What is the output of: int x = 5; Console.WriteLine(x++);
Answer: A
x++ is post-increment, so it returns the current value (5) before incrementing. The next Console.WriteLine would print 6.
Advertisement
Q.26Medium
Which of the following statements about abstract classes is correct?
Answer: C
Abstract classes can contain both abstract and non-abstract (concrete) methods and properties. They cannot be instantiated directly.
Q.27Easy
What is the correct syntax for a multi-line comment in C#?
Answer: C
/* */ is used for multi-line comments in C#. // is for single-line comments.
Q.28Medium
Which access modifier allows access from derived classes only?
Answer: C
protected members are accessible from the same class and derived classes, but not from outside the class hierarchy.
Q.29Medium
What is the output of: Console.WriteLine(5 == 5.0);
Answer: A
C# allows comparison between int and double. 5 and 5.0 are numerically equal, so == returns True.
Q.30Medium
Which of the following correctly declares a jagged array?
Answer: B
Jagged arrays use [][] syntax. [,] is for multidimensional arrays. int[][] allows arrays of varying lengths.
Q.31Medium
What is the purpose of the 'using' statement in C#?
Answer: D
using serves two purposes: importing namespaces and automatically disposing resources through IDisposable pattern.
Q.32Hard
Which of the following is NOT a valid C# data type?
Answer: C
C# has float (4 bytes) and double (8 bytes), but not float32. sbyte, uint, and decimal are valid types.
Q.33Hard
Consider: int[] arr = {1, 2, 3}; What happens when you try to resize it using Array.Resize(ref arr, 5)?
Answer: B
Array.Resize() creates a new array of specified size and copies elements from the original array using the ref parameter.
Q.34Medium
What will be the output of: int x = 10; int y = x; y = 20; Console.WriteLine(x);
Answer: A
int is a value type. When y = x, a copy of x's value is assigned to y. Changing y doesn't affect x.
Q.35Medium
What will be the output of: sbyte x = -128; sbyte y = x - 1; Console.WriteLine(y);
Answer: B
sbyte range is -128 to 127. When overflow occurs without checked context, it wraps around to 127.
Q.36Easy
Which keyword is used to create a constant variable in C# that cannot be modified after initialization?
Answer: B
The 'const' keyword creates a constant that must be initialized at declaration and cannot be changed. 'readonly' allows initialization in constructor.
Q.37Medium
Which of the following correctly demonstrates nullable type syntax in C#?
Answer: A
The correct syntax for nullable types is 'type?' where ? makes the value type nullable, allowing null values.
Q.38Medium
Which access modifier provides access within the same assembly only?
Answer: C
'internal' modifier allows access within the same assembly but not from external assemblies.
Q.39Easy
What will be the output of: int x = 10; int y = 20; int z = x > y ? x : y; Console.WriteLine(z);
Answer: B
The ternary operator checks if x > y. Since 10 is not greater than 20, it returns y which is 20.
Q.40Medium
How many dimensions does a jagged array have in the following declaration: int[][] arr;
Answer: B
int[][] is a two-dimensional jagged array where each element is an array of integers of potentially different lengths.