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.2Hard
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.3Hard
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.4Hard
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.5Hard
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.
Advertisement
Q.6Hard
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.7Hard
In C#, what is the primary purpose of the 'using' statement?
Answer: B
The 'using' statement ensures that Dispose() is called on objects implementing IDisposable, preventing resource leaks. Importing namespaces is a different use of 'using'.
Q.8Hard
What will be the result of: string text = null; Console.WriteLine(text ?? "Default");
Answer: B
The null-coalescing operator (??) returns the right operand if the left is null. Since text is null, it prints 'Default'.
Q.9Hard
Which of the following correctly demonstrates method overloading in C#?
Answer: B
Method overloading requires same method name but different parameter types, count, or order. Return type alone is insufficient for overloading.
Q.10Hard
What is the output of: int x = 10; int y = x++; Console.WriteLine(x + ", " + y);
Answer: B
Post-increment (x++) returns the current value before incrementing. So y = 10, then x becomes 11. Output is '11, 10'.
Q.11Hard
In C#, which keyword is used to implement multiple interface inheritance?
Answer: D
C# implements multiple interface inheritance by listing interfaces separated by commas after the class name: class MyClass : Interface1, Interface2. There is no 'extends' or 'implements' keyword in C#.
Q.12Hard
What will be the output of: decimal price = 19.99m; Console.WriteLine(price.GetType().Name);
Answer: A
The GetType().Name returns the type name as "Decimal" (capitalized). The 'm' suffix specifies a decimal literal. The output shows the CLR type name, which is "Decimal".
Q.13Hard
In C# 2024, what is the latest feature for null-safety introduced in recent versions?
Answer: D
C# has progressively introduced nullable reference types, required keyword (C# 11), and init-only properties (C# 9) for better null-safety and immutability.
Q.14Hard
What is the output of: string s = "Hello"; Console.WriteLine(s.GetHashCode() == s.GetHashCode());
Answer: A
GetHashCode() on the same string returns the same hash code value, so comparison is true. Strings are immutable in C#.
Q.15Hard
In C# 2024, which feature allows you to use pattern matching to deconstruct objects and filter simultaneously in a single expression?
Answer: A
C# 12 (2024) introduced advanced list patterns that allow filtering and deconstructing collections in pattern matching expressions, improving code readability and efficiency.
Q.16Hard
What is the output of the following?
class A { public void Test() { Console.WriteLine("A"); } }
class B : A { public new void Test() { Console.WriteLine("B"); } }
A a = new B();
a.Test();
Answer: A
Using 'new' keyword hides the method. Since reference is of type A, A's Test() is called. Output is 'A'.
Q.17Hard
Consider the following code. What will be the output?
interface I1 { void Show(); }
interface I2 { void Show(); }
class C : I1, I2 { public void Show() { Console.WriteLine("C"); } }
C obj = new C();
obj.Show();
Answer: A
A single method implementation satisfies both interface contracts. The method is called and prints 'C'.
Q.18Hard
What will happen if you try to override a non-virtual method?
Answer: B
To override a method, it must be marked as 'virtual' in the base class. Attempting to override a non-virtual method causes a compilation error.
Q.19Hard
What is the output of the following code?
class Base { public virtual void Method() { Console.WriteLine("Base"); } }
class Derived : Base { public override void Method() { base.Method(); Console.WriteLine("Derived"); } }
Derived d = new Derived();
d.Method();
Answer: C
The 'base' keyword calls the parent class method first, printing 'Base', then the derived method prints 'Derived'. Output: 'BaseDerived'.
Q.20Hard
In C#, what happens when you apply the 'static' modifier to a method inside an interface (C# 11+)?
Answer: B
C# 11 allows static abstract members in interfaces. Static methods in interfaces provide default implementations and can be called via the interface type.