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.22Medium
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.23Medium
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.
Q.24Medium
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.25Medium
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.
Advertisement
Q.26Medium
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.27Medium
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.
Q.28Medium
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.29Medium
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.30Medium
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.31Medium
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.32Medium
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.33Medium
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.34Medium
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.35Medium
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'.
Q.36Medium
Which of the following is a correct implementation of property with auto-backing field in C#?
Answer: A
Auto-properties with get; set; syntax automatically create backing fields. Option C is an expression-bodied property (read-only).
Q.37Medium
What will be the output of the following code? int a = 5; int b = ++a; Console.WriteLine(a + " " + b);
Answer: B
++a is pre-increment, which increments 'a' to 6 first, then assigns it to 'b'. So both a and b become 6. Output is '6 6'.
Q.38Medium
Which of the following correctly demonstrates string interpolation in C# 6.0 and above?
Answer: C
String interpolation uses the prefixwithcurlybracestoembedexpressions.OptionCshowsthecorrectsyntax:"Hello {name}". The @ symbol creates verbatim strings, not interpolated strings.
Q.39Medium
In C#, what is the difference between 'struct' and 'class'?
Answer: B
struct is a value type stored on the stack, while class is a reference type stored on the heap. struct does not support inheritance from other structs/classes (except interfaces), while classes do.
Q.40Medium
Which method is used to convert a string to an integer in C#?
Answer: D
Both int.Parse() and Convert.ToInt32() can convert strings to integers. int.TryParse() is safer as it returns a boolean indicating success/failure. All are valid approaches.