polymorphism Flashcards
What is the main idea behind polymorphism in programming?
A) To use multiple inheritance to create complex hierarchies
B) To allow methods or variables to appear in multiple forms
C) To enable only static methods in classes
D) To limit the number of classes in a program
Correct Answer: B
How does polymorphism enhance object-oriented programming?
A) By enforcing strict data types
B) By limiting extensibility
C) By allowing a single interface to be used by different classes
D) By requiring only one form of method in all classes
Correct Answer: C
Polymorphism works well with which of the following principles?
A) Encapsulation and information hiding
B) Static variables and constants
C) Method chaining
D) Data duplication
Correct Answer: A
Which type of polymorphism is also known as early binding?
A) Static polymorphism
B) Dynamic polymorphism
C) Runtime polymorphism
D) Late binding
Correct Answer: A
What is an example of static or compile-time polymorphism?
A) Method overriding
B) Abstract methods
C) Method overloading
D) Dynamic binding
Correct Answer: C
How does method overloading work?
A) Methods have the same name but different return types only
B) Methods have the same name but different parameters
C) Methods have different names and parameters
D) Only one method can be defined per class
Correct Answer: B
Which of the following is an example of dynamic polymorphism?
A) Method overloading
B) Constructor overloading
C) Method overriding
D) Static method creation
Correct Answer: C
What is required for method overriding to work?
A) Methods must have the same name and signature
B) Methods must have different return types
C) Methods must belong to the same class
D) Methods must not be inherited
Correct Answer: A
Why is dynamic polymorphism also called late binding?
A) The binding happens at compile-time
B) The compiler binds the method at runtime
C) All method names are determined before execution
D) It allows only one form of a method per class
Correct Answer: B
Which of the following is true about polymorphic variables?
A) They are always primitive types
B) They can refer to objects of different types within the same inheritance hierarchy
C) They must have private access
D) They are used only in static polymorphism
Correct Answer: B
Which of these statements about polymorphic variables is NOT valid?
A) Employee employee = new PartTime();
B) Employee employee = new FullTime();
C) Employee employee = new Person();
D) Employee employee = new Manager();
Correct Answer: C
Which statement best describes dynamic binding?
A) The method call is attached to the method at compile time
B) The method call is attached to the method at runtime
C) All methods are predefined at compile-time
D) No inheritance is used in dynamic binding
Correct Answer: B
In the statement Employee employee = new PartTime();, what kind of relationship does this indicate?
A) An unrelated class relationship
B) A polymorphic relationship
C) A private inheritance relationship
D) A method override relationship
Correct Answer: B
What does it mean if a method is “redefined” in a subclass?
A) The method must be private
B) The method cannot be inherited
C) The method has a similar algorithm but different implementation in the subclass
D) The method signature changes completely
Correct Answer: C
What are accessors in the context of methods?
A) Methods that calculate object state
B) Methods that manage dynamic binding
C) Methods for accessing and modifying attributes
D) Methods that perform inheritance
Correct Answer: C
What is the purpose of the getName() method in the context of accessors?
A) To modify the name attribute
B) To retrieve the name attribute
C) To delete the name attribute
D) To initialize the name attribute
Correct Answer: B
What visibility modifier allows a method to be accessible by subclasses but not other classes?
A) Public
B) Private
C) Protected
D) Internal
Correct Answer: C
What is an example of data hiding in object-oriented programming?
A) Declaring all methods as public
B) Making method parameters accessible by all classes
C) Defining attributes as private and using public methods to access them
D) Using static variables
Correct Answer: C
How does a derived class use method overriding with a base class method?
A) By creating a method with a different signature
B) By providing a different implementation for a base-class method with the same signature
C) By making the base-class method private
D) By hiding the base-class method
Correct Answer: B
Which access modifier allows methods to be visible only within the class in which they are defined?
A) Public
B) Private
C) Protected
D) Internal
Correct Answer: B
Which is a key guideline for using polymorphism effectively?
A) Declare variables at the lowest possible class level
B) Always use dynamic polymorphism over static polymorphism
C) Use the highest level of abstraction possible
D) Avoid using access modifiers
Correct Answer: C
What is the main benefit of using polymorphic methods in a program?
A) To create a single-use code
B) To make method overriding impossible
C) To enhance code reusability and flexibility
D) To simplify variable declaration
Correct Answer: C
Which of the following is an example of a polymorphic message?
A) employee.getSalary()
B) employee = new Employee()
C) employee.salary = 5000
D) Employee employee = new Employee();
Correct Answer: A
Why is it possible for Employee employee = new PartTime(); to work without errors?
A) Because Employee and PartTime are completely unrelated
B) Because PartTime is a derived class of Employee
C) Because Employee has private members
D) Because PartTime does not override any methods
Correct Answer: B
What is the concept of late binding in polymorphism?
A) The method call is determined and attached at compile time
B) The method call is dynamically resolved at runtime
C) The method is hidden by the derived class
D) The method cannot be accessed by other classes
Correct Answer: B