What is the correct way to check if a string is null or empty in modern C#?
Answer: D
Both methods work correctly. IsNullOrEmpty checks for null or empty, IsNullOrWhiteSpace also considers whitespace.
Q.42Medium
Which of the following statements about properties in C# is TRUE?
Answer: B
Properties can have get and set accessors independently with different access modifiers like public get, private set.
Q.43Easy
What is the output of: decimal d = 10.5m; int x = (int)d; Console.WriteLine(x);
Answer: B
Explicit casting from decimal to int truncates the decimal part, resulting in 10.
Q.44Medium
What happens when you try to access an array element beyond its length in C#?
Answer: C
Accessing an array index beyond its bounds throws an IndexOutOfRangeException at runtime.
Q.45Medium
Which of the following demonstrates proper use of string interpolation in C# 6.0+?
Answer: A
String interpolation uses the $ prefix followed by curly braces containing expressions.
Advertisement
Q.46Hard
What is the difference between 'struct' and 'class' in C# regarding memory allocation?
Answer: C
Value types (struct) are allocated on the stack, while reference types (class) are allocated on the heap.
Q.47Medium
What will be the output of the following code? int x = 5; int y = ++x; Console.WriteLine(x + " " + y);
Answer: B
++x is pre-increment, so x becomes 6, then y is assigned 6. Both x and y are 6.
Q.48Hard
Which of the following correctly describes method overloading in C#?
Answer: B
Method overloading allows same method name with different signatures (parameter types, count, or order). Return type alone cannot differentiate overloaded methods.
Q.49Easy
In C#, which keyword is used to create a constant variable that cannot be modified after initialization?
Answer: A
The 'const' keyword creates a compile-time constant in C#. 'readonly' creates a runtime constant, 'static' defines class-level members, and 'immutable' is not a C# keyword.
Q.50Easy
What is the default access modifier for a class member in C# if not specified?
Answer: B
The default access modifier for class members is 'private'. For top-level classes, it's 'internal' by default.
Q.51Easy
Which namespace must be included to use LINQ in C#?
Answer: B
LINQ (Language Integrated Query) is available in the System.Linq namespace introduced in .NET 3.5.
Q.52Easy
In C#, what is the correct syntax to declare a nullable integer?
Answer: A
The '?' suffix is used to declare nullable value types in C#. int? means an integer that can also hold null values.
Q.53Easy
What will be printed when the following code executes?
string str = "Hello";
str = str + " World";
Console.WriteLine(str.Length);
Answer: B
"Hello" has 5 characters. After concatenation with " World" (6 characters including space), the total length is 11.
Q.54Medium
Which of the following correctly demonstrates the use of an out parameter in C#?
Answer: A
The 'out' keyword is used when a method needs to return a value through a parameter. The parameter must be assigned before the method exits.
Q.55Medium
What is the output of: bool result = (5 > 3) && (10 < 8); Console.WriteLine(result);
Answer: B
5 > 3 is true, but 10 < 8 is false. The && (AND) operator returns false because one condition is false.
Q.56Medium
Which collection type in C# automatically prevents duplicate values?
Answer: B
HashSet<T> is an unordered collection that automatically prevents duplicate values. List<T> allows duplicates.
Q.57Medium
In C#, what is the difference between '==' and 'Equals()' when comparing strings?
Answer: B
For strings in C#, '==' is overloaded to compare values, and Equals() also compares values. However, for other reference types, '==' compares references while Equals() compares values.
Q.58Medium
What is the correct way to create a generic method in C#?
Answer: A
Generic methods use angle brackets <T> after the method name. The syntax is: public void Method<T>() where T is the type parameter.
Q.59Medium
Which statement about anonymous functions in C# is TRUE?
Answer: B
Lambda expressions (introduced in C# 3.0) are a concise form of anonymous functions. They can infer return types and parameter types, and can access outer scope variables.
Q.60Medium
What is the output of: var x = 5; Console.WriteLine(x.GetType().Name);
Answer: B
The 'var' keyword declares a variable where the type is inferred at compile-time. Here, x is inferred as int, and GetType().Name returns 'Int32'.