A vehicle management system requires different vehicle types (Car, Bike, Truck) to calculate toll fees differently. Which feature should be used?
Answer: C
Polymorphism through inheritance allows each vehicle type to override the toll calculation method with its own implementation, enabling different behavior for each type.
Q.102Medium
Which of the following statements about virtual methods in C# is TRUE?
Answer: B
Virtual methods are designed to be overridden by derived classes, enabling polymorphic behavior and runtime method resolution.
Q.103Medium
A restaurant ordering system needs to ensure each order has a unique ID generated automatically. Which design pattern and feature should be used?
Answer: A
Static variables are shared across all instances and can be used to generate unique IDs automatically without modifying external data.
Q.104Medium
What is the output of the following C# code?
public class Animal { public virtual void Sound() { Console.WriteLine("Some sound"); } }
public class Dog : Animal { public override void Sound() { Console.WriteLine("Bark"); } }
Animal animal = new Dog();
animal.Sound();
Answer: B
Due to polymorphism, the overridden method in Dog class is called at runtime, printing 'Bark' even though the reference type is Animal.
Q.105Medium
In an inventory management system, a Product class has a Price property. Which approach best ensures price cannot be negative?
Answer: B
Properties with private backing fields allow validation in setters, ensuring data integrity by preventing invalid values like negative prices.
Advertisement
Q.106Medium
A student management system implements IComparable<Student> to sort students. Which method must be implemented?
Answer: B
IComparable<T> interface requires implementation of CompareTo method to define comparison logic between objects of the same type.
Q.107Medium
What happens when a derived class constructor does not explicitly call the base class constructor in C#?
Answer: B
C# automatically calls the parameterless constructor of the base class if no explicit base() call is made, ensuring base class initialization.
Q.108Medium
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?
Answer: B
This design enables polymorphism, allowing different employee types to calculate salaries differently while using the same interface, improving maintainability.
Q.109Medium
Which statement about abstract classes in C# is INCORRECT?
Answer: D
Abstract classes can have properties, fields, concrete methods, and abstract members. The statement that they cannot have properties is incorrect.
Q.110Medium
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();
Answer: B
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.
Q.111Medium
Which keyword in C# is used to prevent a class from being inherited?
Answer: C
The 'sealed' keyword prevents a class from being used as a base class for inheritance, ensuring that derived classes cannot override the sealed class.
Q.112Medium
What will be the result of the following C# code?
class Animal { }
class Dog : Animal { }
Dog dog = new Animal(); // Line 1
Answer: B
You cannot assign a base class instance to a derived class reference without explicit casting. This violates type safety rules in C#.
Q.113Medium
Which of the following correctly demonstrates composition in C#?
Answer: B
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.
Q.114Medium
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?
Answer: C
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.
Q.115Medium
In a logistics system, both Truck and Bicycle need to calculate delivery cost. Instead of duplicating code, you decide to use an interface. What advantage does this provide?
Answer: A
Interfaces establish a contract that forces implementing classes to provide their own logic for calculating delivery costs, promoting code consistency without duplicating implementation.
Q.116Medium
In a real-time inventory management system, a Product class needs to track stock levels that should not be modified directly from outside the class. Which OOP principle should be applied here, and how?
Answer: A
Encapsulation is the principle of bundling data and methods together while hiding internal details. Using private fields with public properties (getters/setters) allows controlled access to the stock level, preventing unauthorized direct modifications. This is the standard practice in inventory systems.
Q.117Medium
Which method removes all elements from a collection in C#?
Answer: C
Clear() removes all elements from a collection. Remove() removes a specific element, and RemoveAt() removes an element at a specific index.
Q.118Medium
What is the time complexity of accessing an element by index in a List<T>?
Answer: C
List<T> is backed by an array, allowing direct index-based access in constant time O(1).
Q.119Medium
Which collection maintains insertion order but uses a hash table for faster lookups?
Answer: A
Dictionary<K,V> in .NET maintains insertion order (in recent versions) and provides O(1) lookup. SortedDictionary maintains sorted order instead.
Q.120Medium
What happens when you try to access an index beyond the Count in a List<T>?
Answer: C
Accessing an invalid index throws IndexOutOfRangeException. This is a runtime error that must be caught to prevent program crashes.