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 41–50 of 154
Topics in C# Programming
Q.41 Medium OOP in C#
In a banking application, you need to create a base class that cannot be instantiated but defines common properties for SavingsAccount and CheckingAccount. Which should you use?
A sealed class
B interface
C abstract class
D static class
Correct Answer:  C. abstract class
EXPLANATION

An abstract class is ideal when you want to provide common functionality and properties to derived classes while preventing direct instantiation. It can have both abstract and concrete members.

Test
Q.42 Medium OOP in C#
Which of the following correctly demonstrates composition in C#?
A class Car : Vehicle { }
B class Car { private Engine engine; }
C class Car : Vehicle, IVehicle { }
D abstract class Car { }
Correct Answer:  B. class Car { private Engine engine; }
EXPLANATION

Composition is represented by having an object of one class as a member variable of another class, allowing code reuse through object containment rather than inheritance.

Test
Q.43 Medium OOP in C#
What will be the result of the following C# code?

class Animal { }
class Dog : Animal { }
Dog dog = new Animal(); // Line 1
A Code compiles and runs successfully
B Compilation error: Cannot convert type 'Animal' to 'Dog'
C Runtime error
D Code runs but throws InvalidCastException
Correct Answer:  B. Compilation error: Cannot convert type 'Animal' to 'Dog'
EXPLANATION

You cannot assign a base class instance to a derived class reference without explicit casting. This violates type safety rules in C#.

Test
Q.44 Medium OOP in C#
Which keyword in C# is used to prevent a class from being inherited?
A abstract
B virtual
C sealed
D static
Correct Answer:  C. sealed
EXPLANATION

The 'sealed' keyword prevents a class from being used as a base class for inheritance, ensuring that derived classes cannot override the sealed class.

Test
Q.45 Medium OOP in C#
What is the output of the following C# code?

class Base { public virtual void Display() { Console.WriteLine("Base"); } }
class Derived : Base { public override void Display() { Console.WriteLine("Derived"); } }
Base obj = new Derived();
obj.Display();
A Base
B Derived
C Compilation error
D Runtime error
Correct Answer:  B. Derived
EXPLANATION

Due to polymorphism and method overriding, the Derived class's Display() method is called even though the reference type is Base. This demonstrates runtime polymorphism.

Test
Q.46 Medium OOP in C#
Which statement about abstract classes in C# is INCORRECT?
A Abstract classes cannot be instantiated
B Abstract classes can contain abstract methods
C Abstract classes can have concrete methods with implementation
D Abstract classes cannot have any properties
Correct Answer:  D. Abstract classes cannot have any properties
EXPLANATION

Abstract classes can have properties, fields, concrete methods, and abstract members. The statement that they cannot have properties is incorrect.

Test
Q.47 Medium OOP in C#
In a company payroll system, Employee is the base class with CalculateSalary() as virtual method. Manager and Intern classes override it differently. Why is this design preferred?
A It increases code size
B It provides flexibility to implement different salary calculations while maintaining a common interface
C It makes the code harder to understand
D It prevents inheritance
Correct Answer:  B. It provides flexibility to implement different salary calculations while maintaining a common interface
EXPLANATION

This design enables polymorphism, allowing different employee types to calculate salaries differently while using the same interface, improving maintainability.

Test
Q.48 Medium OOP in C#
What happens when a derived class constructor does not explicitly call the base class constructor in C#?
A Compilation error occurs
B Parameterless base constructor is called automatically
C Runtime error occurs
D The program crashes
Correct Answer:  B. Parameterless base constructor is called automatically
EXPLANATION

C# automatically calls the parameterless constructor of the base class if no explicit base() call is made, ensuring base class initialization.

Test
Q.49 Medium OOP in C#
A student management system implements IComparable to sort students. Which method must be implemented?
A GetHashCode()
B CompareTo(Student other)
C Equals(object obj)
D ToString()
Correct Answer:  B. CompareTo(Student other)
EXPLANATION

IComparable<T> interface requires implementation of CompareTo method to define comparison logic between objects of the same type.

Test
Q.50 Medium OOP in C#
In an inventory management system, a Product class has a Price property. Which approach best ensures price cannot be negative?
A Make Price public
B Use private backing field with property validation in setter
C Make Price abstract
D Use static Price
Correct Answer:  B. Use private backing field with property validation in setter
EXPLANATION

Properties with private backing fields allow validation in setters, ensuring data integrity by preventing invalid values like negative prices.

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