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 81–90 of 154
Topics in C# Programming
Q.81 Medium OOP in C#
Consider a BankAccount class with private balance field. How should external classes access this field safely?
A Make balance public
B Use public properties with get/set accessors
C Use friend classes
D Use reflection
Correct Answer:  B. Use public properties with get/set accessors
EXPLANATION

Properties with accessors (get/set) follow encapsulation principles, allowing controlled access while maintaining data validation logic within the property.

Test
Q.82 Medium OOP in C#
What does the 'virtual' keyword enable in C#?
A Creating abstract methods
B Method overriding in derived classes
C Preventing inheritance
D Creating static methods
Correct Answer:  B. Method overriding in derived classes
EXPLANATION

The 'virtual' keyword marks a method in the base class as overridable. Derived classes can use 'override' to provide their own implementation.

Test
Q.83 Medium OOP in C#
In C# 11, which feature enables you to define constraints on generic type parameters at compile-time?
A Generic constraints with 'where' keyword
B Runtime type checking
C Dynamic typing with 'dynamic' keyword
D Reflection-based validation
Correct Answer:  A. Generic constraints with 'where' keyword
EXPLANATION

'where' clause in generic types constrains type parameters (e.g., where T : class, where T : IComparable). C# 11 added 'allows ref struct' constraint.

Test
Q.84 Medium OOP in C#
In C#, what is the relationship between composition and inheritance in OOP design?
A Composition is always better than inheritance
B Inheritance should be preferred when there's an 'is-a' relationship; composition for 'has-a' relationship
C They are identical in functionality
D Composition can only be used with abstract classes
Correct Answer:  B. Inheritance should be preferred when there's an 'is-a' relationship; composition for 'has-a' relationship
EXPLANATION

Composition (HAS-A) and Inheritance (IS-A) serve different purposes. Composition is more flexible and often preferred over inheritance to avoid tight coupling.

Test
Q.85 Medium OOP in C#
In C#, what is the correct way to implement explicit interface implementation?
A public void InterfaceName.MethodName() { }
B private void InterfaceName.MethodName() { }
C protected void InterfaceName.MethodName() { }
D internal void InterfaceName.MethodName() { }
Correct Answer:  B. private void InterfaceName.MethodName() { }
EXPLANATION

Explicit interface implementation is always private by convention and accessed through interface reference only, not through class instance directly.

Test
Q.86 Medium OOP in C#
Which statement about record types in C# 9+ is correct?
A Records are mutable reference types by default
B Records provide built-in value-based equality and ToString() implementation
C Records cannot have methods or computed properties
D Records require explicit constructor declaration
Correct Answer:  B. Records provide built-in value-based equality and ToString() implementation
EXPLANATION

Records (C# 9+) are designed for immutable data with positional parameters, automatic value-based equality, and ToString() implementation.

Test
Q.87 Medium OOP in C#
What will be the behavior if a base class method is not marked as 'virtual' and a derived class attempts to override it?
A The derived class method hides the base class method with a warning
B Compile-time error occurs
C The override is ignored at runtime
D The method is automatically converted to virtual
Correct Answer:  A. The derived class method hides the base class method with a warning
EXPLANATION

If you override a non-virtual method, C# will hide it and show a compiler warning. Use 'new' keyword to suppress the warning and explicitly hide the base method.

Test
Q.88 Medium OOP in C#
What is the primary difference between method overloading and method overriding in C#?
A Overloading changes method signature, overriding changes method behavior in derived classes
B Overloading is used for inheritance, overriding is used for polymorphism
C Overloading requires virtual keyword, overriding requires abstract keyword
D Overloading is compile-time, overriding is run-time
Correct Answer:  A. Overloading changes method signature, overriding changes method behavior in derived classes
EXPLANATION

Overloading creates multiple methods with the same name but different parameters. Overriding replaces a base class method in a derived class.

Test
Q.89 Medium OOP in C#
What is the output when you access a property marked with 'init' accessor after object initialization in C#?
A Returns the value normally
B Throws a compile-time error if you try to modify it
C Returns null
D Throws a runtime InvalidOperationException
Correct Answer:  B. Throws a compile-time error if you try to modify it
EXPLANATION

The 'init' accessor (introduced in C# 9) allows setting the property only during initialization. Any modification attempt post-initialization causes a compile-time error.

Test
Q.90 Medium OOP in C#
In C# 11, which feature allows you to define required members in a class?
A Using the 'required' keyword on properties
B Using the 'mandatory' keyword on fields
C Using the 'essential' keyword on methods
D Using the 'compulsory' keyword on constructors
Correct Answer:  A. Using the 'required' keyword on properties
EXPLANATION

C# 11 introduced the 'required' keyword that enforces callers to initialize specific members during object instantiation.

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