Multiple Choice Questions
Software Development: Advanced Principles and Practices
Topic: Object-Oriented Programming
Grade: 11
Question 1:
Which of the following best describes encapsulation in object-oriented programming?
a) It is the process of hiding the internal details of an object and providing access only through methods.
b) It is the process of making a class a subclass of another class.
c) It is the process of creating multiple instances of a class.
d) It is the process of defining a class with only abstract methods.
Answer: a) It is the process of hiding the internal details of an object and providing access only through methods.
Explanation: Encapsulation is a fundamental principle in object-oriented programming that allows the internal state of an object to be hidden from external access. By providing access to the internal state only through methods, encapsulation ensures that the object\’s state is accessed and modified in a controlled manner. For example, in a class representing a bank account, the account balance can be encapsulated by making it private and providing methods to deposit and withdraw funds.
Simple Example: Consider a class representing a car. The internal details of the car, such as the engine, transmission, and fuel tank, are hidden from external access. Instead, methods like startEngine() and refuel() are provided to interact with the car.
Complex Example: In a complex software system, encapsulation allows different components to communicate with each other through well-defined interfaces, without exposing their internal implementation details. This promotes modularity and maintainability in the system, as changes to one component do not affect others as long as the interface remains unchanged.
Question 2:
Which of the following is true about inheritance in object-oriented programming?
a) It allows a class to have multiple superclasses.
b) It allows a class to inherit properties and methods from another class.
c) It allows a class to override the properties and methods of another class.
d) It allows a class to define properties and methods that are specific to itself.
Answer: b) It allows a class to inherit properties and methods from another class.
Explanation: Inheritance is a key feature of object-oriented programming that allows a class to inherit the properties and methods of another class, known as the superclass or parent class. The subclass or child class can then extend or modify the inherited behavior as needed. For example, a class representing a Square can inherit properties and methods from a class representing a Shape.
Simple Example: Consider a class representing a Vehicle. This class can have properties and methods common to all vehicles, such as make, model, and startEngine(). Specific types of vehicles, such as Car and Motorcycle, can then inherit these properties and methods.
Complex Example: In a complex software system, inheritance allows for code reuse and promotes a hierarchical organization of classes. For instance, a class representing a GUI component can inherit properties and methods from a more general class representing a UIElement. This allows for common functionality to be shared among different types of UI elements, such as buttons, text boxes, and checkboxes.
Question 3:
Which of the following is an advantage of polymorphism in object-oriented programming?
a) It allows a class to have multiple superclasses.
b) It allows a class to inherit properties and methods from another class.
c) It allows a class to define properties and methods that are specific to itself.
d) It allows objects of different classes to be treated as instances of a common superclass.
Answer: d) It allows objects of different classes to be treated as instances of a common superclass.
Explanation: Polymorphism is a key concept in object-oriented programming that allows objects of different classes to be treated as instances of a common superclass. This enables the writing of more generic code that can operate on objects with different implementations, as long as they adhere to a common interface. For example, a method that takes a Shape object can work with instances of subclasses like Circle, Triangle, or Rectangle.
Simple Example: Consider a class hierarchy representing different types of animals, with a common superclass Animal. By treating instances of different animal classes, such as Dog, Cat, and Bird, as instances of Animal, generic operations like feeding or making them perform actions can be defined.
Complex Example: In a complex software system, polymorphism allows for code flexibility and extensibility. It enables the addition of new classes that conform to an existing interface without modifying the existing code that uses the interface. For example, in a music player application, new audio formats can be added by implementing a common Audio interface, without changing the code that handles playback operations.