11 Lecture
CS403
Midterm & Final Term Short Notes
Inheritance Is
Inheritance is a fundamental concept in object-oriented programming where a new class is created by deriving characteristics and properties from an existing class. It allows code reuse, promotes code organization, and supports the concept of pol
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
- In object-oriented programming, what is inheritance?
a) A process of creating new objects
b) A process of copying existing objects
c) A process of deriving new classes from existing classes
d) A process of extending the functionality of existing classes
Answer: c) A process of deriving new classes from existing classes
Which keyword is used to implement inheritance in Java?
a) extends
b) implements
c) abstract
d) final
Answer: a) extends
Inheritance enables:
a) Code reuse
b) Code duplication
c) Code obfuscation
d) Code obsolescence
Answer: a) Code reuse
Which of the following statements about inheritance is true?
a) A derived class can access the private members of its base class.
b) A derived class can modify the private members of its base class.
c) A derived class cannot inherit the private members of its base class.
d) A derived class can inherit the private members of its base class, but cannot access them.
Answer: c) A derived class cannot inherit the private members of its base class.
Which of the following is not a type of inheritance?
a) Single inheritance
b) Multiple inheritance
c) Hierarchical inheritance
d) Parallel inheritance
Answer: d) Parallel inheritance
What is the advantage of hierarchical inheritance?
a) It allows multiple classes to inherit from a single base class.
b) It allows a class to inherit from multiple base classes.
c) It allows a class to inherit from itself.
d) It allows a class to inherit from its own child class.
Answer: a) It allows multiple classes to inherit from a single base class.
Which of the following is not a method of implementing inheritance?
a) Interfaces
b) Abstract classes
c) Composition
d) Polymorphism
Answer: c) Composition
Which of the following is not a disadvantage of using inheritance?
a) Tight coupling between classes
b) Fragile base class problem
c) Difficulty in understanding complex class hierarchies
d) Code obfuscation
Answer: d) Code obfuscation
Which of the following statements about protected members is true?
a) Protected members are accessible only within the same package.
b) Protected members are accessible only within the same class.
c) Protected members are accessible within the same package and in derived classes.
d) Protected members are not accessible in any circumstance.
Answer: c) Protected members are accessible within the same package and in derived classes.
Which of the following is true about the final keyword in Java?
a) It prevents a class from being inherited.
b) It prevents a method from being overridden.
c) It prevents a variable from being modified.
d) All of the above.
Answer: d) All of the above.
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is the main benefit of using inheritance in object-oriented programming? Answer: The main benefit of using inheritance is code reuse, as it allows the creation of new classes that inherit the properties and methods of existing classes. How does inheritance promote code organization in software development? Answer: Inheritance promotes code organization by allowing the creation of hierarchies of related classes, where the derived classes inherit and extend the functionality of the base class. What is the difference between single and multiple inheritance? Answer: Single inheritance allows a class to inherit properties and methods from only one base class, while multiple inheritance allows a class to inherit from more than one base class. What is the Fragile Base Class problem, and how can it be avoided? Answer: The Fragile Base Class problem occurs when changes made to the base class can break the functionality of the derived classes. It can be avoided by minimizing the number of public and protected members of the base class and by avoiding modifying the base class once it has been released. Can a derived class override a private method of its base class? Answer: No, a derived class cannot override a private method of its base class, as private methods are not visible to the derived class. What is the diamond problem in multiple inheritance, and how can it be resolved? Answer: The diamond problem occurs when two or more base classes of a derived class have a common method, leading to ambiguity in the method resolution. It can be resolved by using virtual inheritance, which ensures that only one copy of the common base class is present in the object hierarchy. How does inheritance support the concept of polymorphism? Answer: Inheritance supports polymorphism by allowing the same method to be implemented in different ways in different derived classes, and by allowing a derived class to be treated as an instance of its base class. What is the difference between public, protected, and private access modifiers in inheritance? Answer: Public members are accessible from any class, protected members are accessible within the same package and in derived classes, and private members are accessible only within the same class. Can a derived class access the private members of its base class? Answer: No, a derived class cannot access the private members of its base class. Can a base class be instantiated in inheritance? Answer: Yes, a base class can be instantiated in inheritance, but it cannot be used to create objects directly.