Home Subjects C# Programming

C# Programming

C# and .NET for campus placement

154 Q 4 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 91–100 of 154
Topics in C# Programming
Q.91 Medium OOP in C#
In C# 2024-25, which feature allows defining read-only properties with automatic backing fields?
A Auto-properties with init accessor
B Readonly keyword
C Private setters only
D Const properties
Correct Answer:  A. Auto-properties with init accessor
EXPLANATION

C# auto-properties with 'init' accessor allow creating read-only properties that can only be set during initialization, improving encapsulation.

Test
Q.92 Medium OOP in C#
Which of the following represents proper use of constructor chaining in C#?
A class Child { Child() : base() { } }
B class Child { Child(int x) : this() { } }
C class Child { Child() : Child(5) { } }
D class Child { Child() { Child(5); } }
Correct Answer:  B. class Child { Child(int x) : this() { } }
EXPLANATION

Constructor chaining uses 'this()' to call another constructor in the same class. Option B shows proper syntax for calling a parameterless constructor.

Test
Q.93 Medium OOP in C#
In C#, what is the difference between a class and a struct?
A Classes are reference types, structs are value types
B Structs are reference types, classes are value types
C They are identical
D Structs cannot have methods
Correct Answer:  A. Classes are reference types, structs are value types
EXPLANATION

Classes are reference types stored on heap, structs are value types stored on stack. This affects memory management and behavior.

Test
Q.94 Medium OOP in C#
What is the purpose of the 'sealed' keyword in C#?
A To prevent a class or method from being inherited or overridden
B To make a class public
C To encrypt the code
D To optimize performance
Correct Answer:  A. To prevent a class or method from being inherited or overridden
EXPLANATION

The 'sealed' keyword prevents a class from being inherited or a method from being overridden in derived classes.

Test
Q.95 Medium OOP in C#
Which of the following is true about properties in C#?
A Properties can only have getters
B Properties are slower than direct field access
C Properties provide encapsulation while maintaining clean syntax
D Properties cannot validate data
Correct Answer:  C. Properties provide encapsulation while maintaining clean syntax
EXPLANATION

Properties in C# allow controlled access to fields with validation logic while maintaining clean, intuitive syntax like field access.

Test
Q.96 Medium OOP in C#
In C#, which interface is used to implement custom iteration logic?
A IEnumerable
B IIterable
C ICollection
D IList
Correct Answer:  A. IEnumerable
EXPLANATION

IEnumerable interface in C# is used to provide iteration capability through the GetEnumerator() method.

Test
Q.97 Medium OOP in C#
What is the difference between 'new' keyword and 'override' keyword in C#?
A 'new' hides the base class method, 'override' replaces it
B 'override' hides the base class method, 'new' replaces it
C They are functionally identical
D 'new' is used for interfaces, 'override' for classes
Correct Answer:  A. 'new' hides the base class method, 'override' replaces it
EXPLANATION

'new' keyword hides the base class method (method hiding), while 'override' provides true polymorphic behavior by replacing the method.

Test
Q.98 Medium OOP in C#
What will be the output of the following code?
class A { public virtual void Show() { Console.WriteLine("A"); } }
class B : A { public override void Show() { Console.WriteLine("B"); } }
A obj = new B();
obj.Show();
A A
B B
C Compilation Error
D Runtime Error
Correct Answer:  B. B
EXPLANATION

Due to polymorphism, the overridden method in class B is called, not the virtual method in class A. Output is 'B'.

Test
Q.99 Medium Basics & Syntax
What is the difference between 'is' and '==' operators when comparing object references in C#?
A 'is' checks reference equality, '==' can be overloaded for value comparison
B '==' checks reference equality, 'is' checks type compatibility
C Both are identical for reference types
D 'is' only works with nullable types
Correct Answer:  A. 'is' checks reference equality, '==' can be overloaded for value comparison
EXPLANATION

The 'is' operator checks reference equality by default and is used for type checking, while '==' can be overloaded by classes to provide custom equality logic.

Test
Q.100 Medium Basics & Syntax
What will be the behavior of the following code snippet?
string str = null;
string result = str?.ToUpper() ?? "DEFAULT";
Console.WriteLine(result);
A Prints 'DEFAULT'
B Prints an empty string
C Throws NullReferenceException
D Prints 'null'
Correct Answer:  A. Prints 'DEFAULT'
EXPLANATION

The null-coalescing operator (?.) with null-coalescing assignment (??) ensures that when str is null, the expression evaluates to null, and then ?? provides the default value 'DEFAULT'.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips