👁 Preview — flashcards and revision are unlocked. Tracking which cards you've reviewed needs a subscription. Unlock all · ₹4,999
← Back to OOP Concepts
Revise mode

Polymorphism

Subtopic mindmap

Quick recall · 290 cards

Short MCQ-style retrieval prompts. Tap a card to reveal the answer.
PYQ Tap to reveal →
Which keyword is used to define a class in C++?
B · class
PYQ Tap to reveal →
_____ represents an entity in the real world with its identity and behaviour.
B · An object
PYQ Tap to reveal →
What will be the output of the following C++ code? cpp class Test { public: int x; Test() { x = 12; } }; \nint main() { Test t; cout << t.x << " " << sizeof(t); return 0; }
A · 12 12
PYQ Tap to reveal →
What is the main purpose of encapsulation? A. Data hiding B. Code reusability C. Memory management D. Increasing execution speed
A · Data hiding
PYQ Tap to reveal →
Which among the following best defines abstraction?
A · Hiding the implementation complexity
PYQ Tap to reveal →
Hiding the implementation complexity can _______.
D · All of the mentioned
PYQ Tap to reveal →
Class is ______ abstraction.
A · Logical
PYQ Tap to reveal →
Object is ______ abstraction.
B · Real
PYQ Tap to reveal →
Which among the following best describes Inheritance?
B · Using data and functions provided by a parent class in a derived class
PYQ Tap to reveal →
How many basic types of inheritance are provided as an OOP feature?
C · 4
PYQ Tap to reveal →
Which among the following best defines single-level inheritance?
C · A class inheriting from a single base class
PYQ Tap to reveal →
What is hierarchical inheritance?
A · One base class is inherited by two or more derived classes
PYQ Tap to reveal →
Which of the following best describes polymorphism in Object-Oriented Programming?
B · B) The property of some code to behave differently for different contexts
PYQ Tap to reveal →
Which of the following demonstrates compile-time polymorphism in Java?
B · B) Method overloading with different parameter types
PYQ Tap to reveal →
In Java, polymorphism can be achieved through which of the following mechanisms?
C · C) Through method overloading, method overriding, interfaces, and abstract classes
PYQ Tap to reveal →
Which statement about polymorphism is TRUE?
A · A) Polymorphism allows objects of different classes to be treated as objects of a common parent class
PYQ Tap to reveal →
Which of the following is an example of compile-time polymorphism?
B · B) Multiple methods with the same name but different parameter types in the same class
Question bank Tap to reveal →
Which of the following best defines a class in Object-Oriented Programming?
A · A blueprint or template for creating objects
A class is a blueprint or template that defines attributes and methods for objects.
Question bank Tap to reveal →
In OOP, a class primarily serves as a ____.
A · Data type for objects
A class acts as a data type for creating objects with specific attributes and behaviors.
Question bank Tap to reveal →
Which statement about classes is correct?
A · A class can be instantiated multiple times to create objects
Classes are templates from which multiple objects can be instantiated.
Question bank Tap to reveal →
Which of the following best describes an object in OOP?
A · An instance of a class with state and behavior
An object is an instance of a class that has attributes (state) and methods (behavior).
Question bank Tap to reveal →
Which of the following is true about objects?
A · Each object has its own copy of data members
Each object maintains its own state via its own copy of data members.
Question bank Tap to reveal →
Which statement correctly distinguishes an object from a class?
A · A class is a blueprint; an object is an instance
A class defines the structure and behavior, while an object is a concrete instance of that class.
Question bank Tap to reveal →
Which of the following are members of a class?
A · Attributes and methods
Class members include attributes (data members) and methods (functions).
Question bank Tap to reveal →
Which of the following is NOT a class member?
A · Global variables
Global variables are not members of a class; class members are defined inside the class.
Question bank Tap to reveal →
Which of the following correctly describes methods in a class?
A · Functions that define behaviors of objects
Methods are functions defined inside a class that describe object behavior.
Question bank Tap to reveal →
Which of the following is an example of a class attribute?
A · int age;
Attributes are variables like 'int age;' declared inside a class.
Question bank Tap to reveal →
Which of the following is the correct way to instantiate an object in C++?
A · ClassName obj;
Objects are instantiated by declaring them as 'ClassName obj;'.
Question bank Tap to reveal →
Which of the following statements correctly creates an object named 'car' of class 'Vehicle'?
A · Vehicle car;
The syntax 'Vehicle car;' declares an object 'car' of class 'Vehicle'.
Question bank Tap to reveal →
Which of the following is true about object instantiation?
A · It allocates memory for the object
Instantiating an object allocates memory to hold its data members.
Question bank Tap to reveal →
Which of the following correctly creates an object dynamically in C++?
A · ClassName *obj = new ClassName();
Dynamic allocation uses pointers and the 'new' keyword: ClassName *obj = new ClassName();
Question bank Tap to reveal →
Which access modifier allows members of a class to be accessible from anywhere?
A · public
Public members are accessible from any part of the program.
Question bank Tap to reveal →
Which access modifier restricts access to class members only within the class itself?
A · private
Private members are accessible only within the class they are declared in.
Question bank Tap to reveal →
Which access modifier allows derived classes to access members but restricts access from outside classes?
A · protected
Protected members are accessible within the class and its derived classes.
Question bank Tap to reveal →
Which of the following is NOT a valid use of access modifiers?
B · To define the data type of a member
Access modifiers control visibility, not data types.
Question bank Tap to reveal →
Which access modifier would you use to allow access only within the same class and derived classes, but not from other classes?
A · protected
Protected members are accessible within the class and its subclasses only.
Question bank Tap to reveal →
What is the purpose of a constructor in a class?
A · To initialize objects when they are created
Constructors initialize object attributes at the time of creation.
Question bank Tap to reveal →
Which of the following correctly describes a destructor?
A · It is called automatically when an object is destroyed
Destructors clean up resources when an object goes out of scope or is deleted.
Question bank Tap to reveal →
Which of the following is a valid constructor declaration in C++?
A · ClassName();
Constructors have the same name as the class and no return type.
Question bank Tap to reveal →
Which of the following is true about constructors?
A · They can be overloaded with different parameter lists
Constructors can be overloaded to initialize objects in different ways.
Question bank Tap to reveal →
Which of the following statements about destructors is correct?
A · There can be only one destructor per class, and it cannot be overloaded
Destructors have a fixed name and cannot be overloaded or take parameters.
Question bank Tap to reveal →
Which of the following correctly distinguishes member functions from data members?
A · Member functions define behavior; data members hold state
Member functions implement behavior, while data members store object state.
Question bank Tap to reveal →
Which of the following is a data member in a class?
A · int count;
Data members are variables declared inside a class.
Question bank Tap to reveal →
Which of the following is true about member functions?
A · They can access and modify data members of the class
Member functions operate on data members and define object behavior.
Question bank Tap to reveal →
Which of the following is an example of a member function declaration?
A · void setAge(int a);
Member functions are declared as functions inside the class.
Question bank Tap to reveal →
Which of the following correctly describes object initialization?
A · Assigning initial values to an object's data members at creation
Object initialization sets initial values for data members when an object is created.
Question bank Tap to reveal →
Which of the following is a valid way to assign one object to another in C++?
A · obj2 = obj1;
Objects can be assigned using the assignment operator if the class supports it.
Question bank Tap to reveal →
Which of the following is true about copy constructors in object initialization?
A · They initialize a new object using an existing object
Copy constructors create a new object as a copy of an existing object.
Question bank Tap to reveal →
Which of the following statements about object assignment is correct?
A · Assignment copies the values of data members from one object to another
Assignment copies the state of one object to another existing object.
Question bank Tap to reveal →
Which of the following correctly describes memory allocation for objects in C++?
A · Memory is allocated when an object is instantiated
Memory for data members is allocated when an object is created.
Question bank Tap to reveal →
Which of the following statements about memory allocation for objects is true?
A · Each object has its own memory space for data members
Each object gets its own memory space for its data members.
Question bank Tap to reveal →
Which of the following is true about static members in relation to memory allocation?
A · Static members are shared among all objects and have a single memory location
Static members are shared and have a single memory location regardless of number of objects.
Question bank Tap to reveal →
Encapsulation in OOP is best described as:
A · Wrapping data and methods into a single unit (class)
Encapsulation bundles data and methods within a class to protect and manage access.
Question bank Tap to reveal →
Which of the following is a benefit of encapsulation?
A · Improved data security by restricting direct access
Encapsulation restricts direct access to data, enhancing security and integrity.
Question bank Tap to reveal →
Which of the following best illustrates encapsulation in a class?
A · Using private data members with public getter and setter methods
Encapsulation is implemented by hiding data members and providing controlled access via methods.
Question bank Tap to reveal →
Which of the following violates the principle of encapsulation?
A · Making all data members public
Making data members public exposes internal state and breaks encapsulation.
Question bank Tap to reveal →
Which of the following statements correctly distinguishes a class from an object?
A · A class is a blueprint; an object is an instance created from the blueprint
A class defines structure and behavior; an object is a concrete instance of a class.
Question bank Tap to reveal →
Which of the following is an example of an object?
A · Student s1;
Student s1; declares an object s1 of class Student.
Question bank Tap to reveal →
Which of the following best describes the relationship between class and object?
A · Objects are instances of classes
Objects are concrete instances created based on the class blueprint.
Question bank Tap to reveal →
Which of the following is the correct syntax to define a class named 'Car' in C++?
A · class Car { };
In C++, a class is defined using the keyword 'class' followed by the class name and a pair of braces. The syntax 'class Car { };' is correct.
Question bank Tap to reveal →
What is the default access specifier for members of a class in C++?
B · private
By default, all members of a class in C++ are private unless specified otherwise.
Question bank Tap to reveal →
Which of the following correctly declares a class named 'Book' with no members?
A · class Book {};
The correct syntax for an empty class declaration is 'class Book {};'. The semicolon is mandatory after the closing brace.
Question bank Tap to reveal →
Given the class 'Person', which of the following statements correctly creates an object named 'p1'?
A · Person p1;
In C++, an object of a class is created by declaring a variable of that class type, e.g., 'Person p1;'.
Question bank Tap to reveal →
Which of the following is the correct way to access a public member function 'display()' of an object 'obj'?
A · obj.display();
For an object (not a pointer), member functions are accessed using the dot operator, e.g., 'obj.display();'.
Question bank Tap to reveal →
Which of the following statements about objects is TRUE?
B · An object can access private members of another object of the same class.
Objects of the same class can access each other's private members in C++.
Question bank Tap to reveal →
What is the output of the following code snippet? class Sample { public: void show() { cout << "Hello"; } }; \nint main() { Sample *ptr = new Sample(); ptr->show(); return 0; }
A · Hello
The object is created dynamically using 'new', and the member function is accessed using the pointer with '->'. It prints 'Hello'.
Question bank Tap to reveal →
Which of the following is the correct way to declare an integer attribute 'age' and a member function 'getAge()' inside a class?
A · int age; int getAge();
Attributes are declared as variables and member functions as function prototypes inside the class.
Question bank Tap to reveal →
Which of the following correctly defines a member function 'setName' that sets a private string attribute 'name'?
C · Both A and B
Both direct assignment and using 'this' pointer to assign the parameter to the attribute are correct.
Question bank Tap to reveal →
Which of the following statements about private members of a class is correct?
B · Private members can be accessed only by member functions of the class.
Private members are accessible only within the class and by its member functions, not directly by objects or outside classes.
Question bank Tap to reveal →
Which access specifier allows members to be accessible within the class and by derived classes but not outside?
C · protected
The 'protected' access specifier allows access within the class and its subclasses but not from outside.
Question bank Tap to reveal →
Which of the following access specifiers is the most restrictive?
C · private
Private members are the most restrictive, accessible only within the class itself.
Question bank Tap to reveal →
Which of the following correctly declares a constructor for a class 'Employee' that takes no parameters?
A · Employee() { }
A constructor has the same name as the class and no return type. 'Employee() { }' is the correct syntax.
Question bank Tap to reveal →
What is the purpose of a destructor in a class?
B · To free resources before object destruction
A destructor is used to release resources or perform cleanup before an object is destroyed.
Question bank Tap to reveal →
Which of the following is the correct syntax for a destructor of class 'Car'?
A · ~Car() { }
Destructor syntax uses a tilde (~) before the class name with no return type or parameters.
Question bank Tap to reveal →
Which of the following statements about constructors is FALSE?
C · Constructors are called explicitly by the programmer.
Constructors are called automatically when an object is created, not explicitly by the programmer.
Question bank Tap to reveal →
Which constructor is called when an object is created without any arguments?
A · Default constructor
The default constructor is called when no arguments are provided during object creation.
Question bank Tap to reveal →
Which of the following correctly demonstrates object initialization using assignment after declaration?
A · Car c; c = Car();
An object can be declared and then assigned a new object using the assignment operator as in 'c = Car();'.
Question bank Tap to reveal →
What is the difference between copy initialization and direct initialization of an object?
A · Copy initialization uses '=' while direct initialization uses parentheses.
Copy initialization uses the '=' operator, e.g., 'Car c = Car();', while direct initialization uses parentheses, e.g., 'Car c(Car());'.
Question bank Tap to reveal →
Which of the following statements about object assignment is TRUE?
A · Assignment copies the values of data members from one object to another.
Assignment copies the data members' values from one existing object to another existing object.
Question bank Tap to reveal →
Which of the following describes how memory is allocated for objects created on the stack?
A · Memory is allocated automatically and freed when the object goes out of scope.
Stack objects have automatic storage duration; their memory is allocated on creation and freed when they go out of scope.
Question bank Tap to reveal →
What is the difference in memory allocation between objects created with 'new' and those created without it?
A · Objects created with 'new' are allocated on the heap; others on the stack.
Objects created with 'new' are dynamically allocated on the heap, while those created normally are allocated on the stack.
Question bank Tap to reveal →
Which of the following statements about memory allocation for objects is FALSE?
C · Objects on the stack persist after the function returns.
Objects on the stack are destroyed when the function returns; they do not persist beyond their scope.
Question bank Tap to reveal →
Which of the following best describes the difference between a class and an object?
A · A class is a blueprint; an object is an instance of that blueprint.
A class defines the structure and behavior; an object is a concrete instance created from the class.
Question bank Tap to reveal →
Which of the following is NOT true about objects?
C · Objects can exist without classes.
Objects cannot exist without classes; they are instances of classes.
Question bank Tap to reveal →
Which of the following is an example of encapsulation in classes?
A · Declaring data members private and providing public getter/setter methods.
Encapsulation hides data by making members private and controlling access via public methods.
Question bank Tap to reveal →
Which of the following best illustrates encapsulation?
A · A class with private data members and public methods to access them.
Encapsulation is achieved by restricting direct access to data members and providing controlled access through methods.
Question bank Tap to reveal →
Which of the following is a benefit of encapsulation?
A · Improved data security and integrity.
Encapsulation protects data from unauthorized access, enhancing security and integrity.
Question bank Tap to reveal →
Which of the following statements about static members is TRUE?
A · Static members belong to the class, not to any object.
Static members are shared by all objects of the class and belong to the class itself.
Question bank Tap to reveal →
Which of the following is the correct way to access a static member function 'show()' of class 'Demo'?
A · Demo::show();
Static member functions are accessed using the class name and scope resolution operator, e.g., 'Demo::show();'.
Question bank Tap to reveal →
Which of the following statements about the 'this' pointer is TRUE?
A · 'this' pointer holds the address of the current object.
'this' pointer points to the current object and is used within member functions to refer to the invoking object.
Question bank Tap to reveal →
How can the 'this' pointer be used to resolve naming conflicts between data members and parameters?
A · By using 'this->memberName' to refer to the data member.
The 'this' pointer explicitly refers to the object's data member, resolving conflicts with parameters of the same name.
Question bank Tap to reveal →
Which of the following is NOT a valid use of the 'this' pointer?
C · Accessing static members of the class.
'this' pointer cannot be used to access static members because static members belong to the class, not any object.
Question bank Tap to reveal →
Which of the following code snippets correctly uses the 'this' pointer to return the current object?
C · MyClass getObject() { return *this; }
'*this' dereferences the pointer to return the current object by value.
Question bank Tap to reveal →
In a class hierarchy where `Base` has a virtual destructor and `Derived` inherits from `Base`, what happens if a `Derived` object is deleted through a `Base*` pointer when the destructor in `Base` is not virtual?
A · Only `Base` destructor is called, causing resource leaks in `Derived`.
Question bank Tap to reveal →
In C++, a class `A` has a private copy constructor and a public assignment operator. What happens when an object of `A` is passed by value to a function?
A · Compilation error due to inaccessible copy constructor.
Question bank Tap to reveal →
A class `Data` has a private integer array of size 7 and a public method `getElement(int index)` that returns the element at the given index. If `getElement` does not check bounds, what happens when called with index 10?
A · Undefined behavior, possibly returning garbage or causing crash.
Question bank Tap to reveal →
What is encapsulation in Object-Oriented Programming?
A · A technique to bind data and methods that operate on the data within one unit
Encapsulation is the technique of bundling data and the methods that manipulate that data into a single unit or class.
Question bank Tap to reveal →
Which of the following best describes the purpose of encapsulation?
A · To restrict access to some of the object's components
Encapsulation restricts direct access to some of an object's components, which helps in protecting the integrity of the data.
Question bank Tap to reveal →
How does encapsulation improve software design?
A · By hiding internal object details and exposing only necessary parts
Encapsulation improves design by hiding internal details and exposing only what is necessary, reducing complexity and increasing modularity.
Question bank Tap to reveal →
Which access specifier allows a class member to be accessible only within the class it is declared?
A · private
The private access specifier restricts access to members only within the class they are declared in.
Question bank Tap to reveal →
In C++, which access specifier allows members to be accessible within the class and by derived classes?
A · protected
Protected members are accessible within the class and its derived classes but not outside these classes.
Question bank Tap to reveal →
Which of the following is NOT true about the public access specifier?
C · Public members are hidden from derived classes
Public members are accessible everywhere, including derived classes; they are not hidden.
Question bank Tap to reveal →
What will happen if no access specifier is declared for a class member in C++?
A · It is private by default
In C++, class members are private by default if no access specifier is provided.
Question bank Tap to reveal →
What is the primary purpose of getter and setter methods in encapsulation?
A · To provide controlled access to private data members
Getter and setter methods allow controlled read and write access to private data members, maintaining encapsulation.
Question bank Tap to reveal →
Which of the following is a correct signature of a setter method for a private integer variable 'age' in Java?
A · public void setAge(int age)
Setter methods are usually public, return void, and accept a parameter to set the private variable.
Question bank Tap to reveal →
Why are getter methods important in encapsulation?
A · They provide read-only access to private data members
Getter methods allow reading private data members without exposing them directly.
Question bank Tap to reveal →
Which of the following is a potential disadvantage of not using setter methods for private variables?
A · No control over the values assigned to variables
Without setter methods, there is no way to validate or control the data assigned to private variables.
Question bank Tap to reveal →
What is the difference between data hiding and information hiding in encapsulation?
A · Data hiding restricts access to data, information hiding hides implementation details
Data hiding focuses on restricting access to data members; information hiding hides internal implementation details.
Question bank Tap to reveal →
Which of the following best exemplifies information hiding in OOP?
A · Exposing only necessary methods and hiding internal logic
Information hiding means exposing only what is necessary and hiding the internal workings from the user.
Question bank Tap to reveal →
Which of the following statements about data hiding is FALSE?
C · It allows direct access to private variables from outside the class
Data hiding prevents direct access to private variables from outside the class.
Question bank Tap to reveal →
In Java, which keyword is used to declare a member variable that cannot be accessed outside its class?
A · private
The private keyword restricts access to the member variable only within its own class.
Question bank Tap to reveal →
How does encapsulation differ in C++ compared to Java?
A · C++ allows default private access in classes, Java defaults to package-private
In C++, class members are private by default, whereas in Java, members without an access specifier are package-private.
Question bank Tap to reveal →
Which of the following is true about encapsulation in Java?
A · Encapsulation is achieved using classes, access modifiers, and getter/setter methods
Java uses classes, access modifiers (private, public, protected), and getter/setter methods to implement encapsulation.
Question bank Tap to reveal →
In C++, which of the following is NOT an access specifier used for encapsulation?
A · internal
The 'internal' access specifier is not used in C++; it is used in languages like C#.
Question bank Tap to reveal →
Which of the following is NOT an advantage of encapsulation?
C · Direct access to private data members
Encapsulation restricts direct access to private data members to protect data integrity.
Question bank Tap to reveal →
How does encapsulation contribute to code reusability?
A · By hiding implementation details and exposing a clear interface
Encapsulation hides internal details and exposes only necessary interfaces, making code easier to reuse.
Question bank Tap to reveal →
Which of the following is a benefit of encapsulation in software development?
A · Reduces complexity by hiding internal object details
Encapsulation reduces complexity by hiding internal details and exposing only what is necessary.
Question bank Tap to reveal →
Which of the following best distinguishes encapsulation from abstraction?
A · Encapsulation binds data and methods; abstraction hides complexity
Encapsulation is about bundling data and methods, while abstraction focuses on hiding complexity from the user.
Question bank Tap to reveal →
How does encapsulation differ from inheritance?
A · Encapsulation hides data; inheritance allows a class to acquire properties of another
Encapsulation is about data hiding, while inheritance is about acquiring properties and behaviors from a parent class.
Question bank Tap to reveal →
Which of the following statements about encapsulation and polymorphism is TRUE?
A · Encapsulation hides data; polymorphism allows methods to behave differently based on input
Encapsulation is about hiding data, while polymorphism allows methods to have different behaviors based on context.
Question bank Tap to reveal →
Which of the following is an example of encapsulation in practice?
A · Using private variables with public getter and setter methods
Encapsulation is implemented by keeping variables private and providing public methods to access them.
Question bank Tap to reveal →
Which of the following is the correct syntax to declare a private integer variable 'count' in a C++ class?
A · private: int count;
In C++, access specifiers are labels followed by a colon, and variables are declared under them.
Question bank Tap to reveal →
Which of the following code snippets correctly implements a getter method for a private string variable 'name' in Java?
A · public String getName() { return name; }
Getter methods are public, return the type of the variable, and return the variable's value.
Question bank Tap to reveal →
What is the correct way to restrict access to a class member in C++?
A · Declare it under the private access specifier
Members declared under private are accessible only within the class, restricting external access.
Question bank Tap to reveal →
Which of the following is a common error when implementing encapsulation?
A · Making all data members public
Making data members public breaks encapsulation by exposing internal data directly.
Question bank Tap to reveal →
A misconception about encapsulation is that it only involves which of the following?
A · Making variables private without providing access methods
Encapsulation involves both hiding data and providing controlled access via methods; just making variables private is insufficient.
Question bank Tap to reveal →
Which of the following can cause a compilation error related to encapsulation in Java?
A · Accessing a private variable directly from outside its class
Private variables cannot be accessed directly outside their class, causing a compilation error if attempted.
Question bank Tap to reveal →
Which of the following best defines encapsulation in object-oriented programming?
A · Combining data and methods into a single unit and restricting access to some components
Question bank Tap to reveal →
What is the primary purpose of encapsulation in OOP?
B · To protect data from unauthorized access and modification
Encapsulation protects data by restricting direct access to it and only allowing modification through controlled methods, ensuring data integrity and security.
Question bank Tap to reveal →
Which statement best explains how encapsulation improves software maintainability?
B · By grouping related data and methods and hiding internal details
Encapsulation groups related data and methods into a class and hides internal details, making it easier to modify and maintain code without affecting other parts of the program.
Question bank Tap to reveal →
In C++, which access specifier allows class members to be accessible only within the class and its derived classes?
C · protected
The protected access specifier allows members to be accessible within the class itself and by derived classes, but not by other parts of the program.
Question bank Tap to reveal →
Which access specifier in Java allows members to be accessible from any other class in any package?
C · public
The public access specifier allows members to be accessible from any other class regardless of package boundaries.
Question bank Tap to reveal →
Which of the following is TRUE about the 'private' access specifier in Python?
C · Private members can be accessed outside the class using name mangling
In Python, private members are indicated by a double underscore prefix and can be accessed outside the class using name mangling, but this is discouraged.
Question bank Tap to reveal →
Consider a class with a private data member 'age'. Which access specifier should be used for the getter method to allow read access to 'age' from outside the class?
C · public
Getter methods are typically declared public to provide controlled read access to private data members from outside the class.
Question bank Tap to reveal →
Which of the following is a correct reason to use 'protected' access specifier instead of 'private' in a class?
B · To allow access to members from derived classes but not from other classes
Protected members are accessible within the class and by derived classes, enabling controlled inheritance access.
Question bank Tap to reveal →
Which of the following is NOT a typical purpose of getter and setter methods in encapsulation?
C · To allow direct modification of private variables
Getter and setter methods do not allow direct modification of private variables; they provide controlled access to ensure data integrity.
Question bank Tap to reveal →
Which of the following code snippets correctly defines a setter method for a private variable 'salary' in Java?
A · public void setSalary(int salary) { this.salary = salary; }
The setter method should be public to allow external modification and use 'this.salary' to refer to the class variable.
Question bank Tap to reveal →
In Python, which decorator is commonly used to define a getter method for a property to implement encapsulation?
A · @property
The @property decorator is used to define getter methods that allow access to private variables as if they were public attributes.
Question bank Tap to reveal →
Which of the following is a disadvantage of not using getter and setter methods for private data members?
B · Data can be accessed and modified directly, risking data integrity
Without getter and setter methods, private data members can be accessed and modified directly if not properly encapsulated, risking data integrity.
Question bank Tap to reveal →
Which of the following best describes data hiding in encapsulation?
B · Restricting access to internal object details to protect data integrity
Data hiding restricts access to the internal details of an object to protect its data from unauthorized access or modification.
Question bank Tap to reveal →
How does encapsulation contribute to security in software design?
B · By hiding sensitive data and exposing only necessary interfaces
Encapsulation hides sensitive data and exposes only necessary methods to interact with that data, thus enhancing security.
Question bank Tap to reveal →
Which of the following scenarios best illustrates a violation of encapsulation principles?
A · Accessing private data members directly from outside the class
Direct access to private data members from outside the class violates encapsulation and can lead to data corruption.
Question bank Tap to reveal →
Which encapsulation feature is implemented differently in Python compared to C++ and Java?
C · Data hiding through name mangling instead of strict access control
Python uses name mangling to simulate private members, unlike C++ and Java which enforce access control via access specifiers.
Question bank Tap to reveal →
In C++, which keyword is used to specify that class members are accessible from anywhere in the program?
C · public
The public keyword allows class members to be accessed from any part of the program.
Question bank Tap to reveal →
Which of the following is a key difference in encapsulation between Java and Python?
A · Java uses access specifiers; Python relies on naming conventions and name mangling
Java enforces access control using keywords, while Python uses naming conventions and name mangling to indicate private members.
Question bank Tap to reveal →
In Java, which of the following statements about encapsulation is TRUE?
C · Encapsulation is implemented using private variables and public getter/setter methods
Java implements encapsulation by declaring data members private and providing public getter and setter methods to access them.
Question bank Tap to reveal →
Which of the following is NOT a benefit of encapsulation?
C · Increased coupling between classes
Encapsulation reduces coupling by hiding internal details, so increased coupling is not a benefit.
Question bank Tap to reveal →
How does encapsulation support modularity in software design?
B · By hiding internal data and exposing only necessary interfaces
Encapsulation supports modularity by hiding internal data and exposing only necessary interfaces, allowing modules to be developed and tested independently.
Question bank Tap to reveal →
Which of the following best describes how encapsulation improves maintainability?
A · By allowing changes to internal implementation without affecting external code
Encapsulation hides internal implementation details, so changes can be made without impacting other parts of the program, improving maintainability.
Question bank Tap to reveal →
Which of the following statements correctly distinguishes encapsulation from abstraction?
B · Encapsulation bundles data and methods; abstraction focuses on hiding implementation details
Encapsulation bundles data and methods into a single unit, while abstraction focuses on hiding the internal implementation details and showing only relevant features.
Question bank Tap to reveal →
How does encapsulation differ from inheritance in OOP?
B · Encapsulation restricts access to data; inheritance allows a class to acquire properties of another
Encapsulation restricts access to data to protect it, while inheritance allows a class to inherit properties and behaviors from another class.
Question bank Tap to reveal →
Which of the following best explains the relationship between encapsulation and polymorphism?
D · Encapsulation hides data; polymorphism allows objects to behave differently based on type
Encapsulation hides data and implementation details, while polymorphism allows objects to be treated as instances of their parent class but behave differently based on their actual type.
Question bank Tap to reveal →
In a banking application, which encapsulation practice would best protect the account balance from unauthorized modification?
B · Using private access for balance and providing public getter and setter with validation
Declaring balance private and controlling access via getter and setter methods with validation ensures data integrity and security.
Question bank Tap to reveal →
Given a class 'Employee' with a private 'salary' field, which of the following is the best encapsulation practice to update the salary only if the new value is positive?
B · Provide a public setter method that checks if the new salary is positive before assignment
A setter method with validation ensures that only valid salary values are assigned, enforcing encapsulation and data integrity.
Question bank Tap to reveal →
Which of the following encapsulation mistakes can lead to unintended modification of an object's internal state?
B · Providing public setter methods without validation
Public setter methods without validation can allow invalid or harmful data to be assigned, breaking encapsulation principles.
Question bank Tap to reveal →
Which of the following is a common error related to encapsulation in object-oriented programming?
A · Declaring all class members as private and never providing access methods
Declaring all members private without providing any access methods makes it impossible to use or modify the data, which is a practical error.
Question bank Tap to reveal →
What error might occur if a class exposes its data members as public instead of using encapsulation?
B · Data can be modified arbitrarily leading to inconsistent object state
Public data members can be modified directly from outside the class, potentially causing inconsistent or invalid object states.
Question bank Tap to reveal →
Which of the following is an example of a violation of encapsulation principles?
B · Accessing private variables directly from outside the class
Direct access to private variables from outside the class violates encapsulation by exposing internal details.
Question bank Tap to reveal →
What is the primary purpose of abstraction in Object-Oriented Programming?
A · To hide the implementation details and show only functionality
Abstraction focuses on hiding the internal implementation details and exposing only the essential features to the user.
Question bank Tap to reveal →
Which of the following best defines abstraction in OOP?
B · The process of hiding complexity by providing a simplified interface
Abstraction hides the complex reality while exposing only the necessary parts through a simplified interface.
Question bank Tap to reveal →
Which statement correctly describes abstraction in OOP?
B · It hides both data and implementation details to reduce complexity
Abstraction hides both data and implementation details, helping reduce complexity by showing only relevant information.
Question bank Tap to reveal →
Which of the following is NOT a benefit of abstraction in OOP?
B · Increases program complexity
Abstraction reduces program complexity by hiding unnecessary details, so it does not increase complexity.
Question bank Tap to reveal →
Which of the following best describes a key purpose of abstraction in software design?
B · To separate interface from implementation
Abstraction separates the interface from the implementation, allowing users to interact with the interface without knowing the underlying details.
Question bank Tap to reveal →
Which of the following is a significant advantage of using abstraction in OOP?
B · It hides complexity and reduces programming effort
Abstraction hides complexity and reduces programming effort by exposing only necessary details.
Question bank Tap to reveal →
Which of the following statements about abstraction is TRUE at an advanced level?
B · Abstraction allows changing implementation without affecting users
Abstraction allows the implementation to be changed without affecting the interface or users, promoting flexibility and maintainability.
Question bank Tap to reveal →
Which of the following best distinguishes abstraction from encapsulation?
B · Abstraction hides complexity, encapsulation binds data and methods
Abstraction hides complexity by showing only essential features, while encapsulation binds data and methods into a single unit.
Question bank Tap to reveal →
Which of the following statements correctly compares abstraction and encapsulation?
B · Abstraction hides implementation details; encapsulation protects data by restricting access
Abstraction hides implementation details from the user, while encapsulation protects data by restricting direct access to it.
Question bank Tap to reveal →
Which of the following is a key difference between abstraction and encapsulation?
A · Abstraction focuses on what an object does; encapsulation focuses on how it does it
Abstraction emphasizes what an object does by hiding details, while encapsulation focuses on how data and methods are bundled and protected.
Question bank Tap to reveal →
Which of the following statements about abstraction and encapsulation is TRUE at an advanced level?
C · Abstraction hides complexity; encapsulation hides internal object state
Abstraction hides complexity by exposing only relevant details, while encapsulation hides the internal state of an object by restricting access.
Question bank Tap to reveal →
Which of the following is TRUE about abstract classes in OOP?
B · Abstract classes can have both abstract and concrete methods
Abstract classes can contain both abstract methods (without implementation) and concrete methods (with implementation).
Question bank Tap to reveal →
Which of the following statements about abstract classes is CORRECT?
C · They provide a base for subclasses to implement abstract methods
Abstract classes serve as a base class and require subclasses to provide implementations for abstract methods.
Question bank Tap to reveal →
Which of the following is a valid reason to use an abstract class in OOP?
B · To define a common interface with some shared implementation
Abstract classes allow defining a common interface and shared implementation that subclasses can inherit and extend.
Question bank Tap to reveal →
Which of the following statements about abstract classes is TRUE at an advanced level?
A · Abstract classes can be used to partially implement interfaces
Abstract classes can provide partial implementation of interfaces, leaving some methods abstract for subclasses to implement.
Question bank Tap to reveal →
Which of the following is TRUE about abstract methods in OOP?
B · They must be overridden in derived classes
Abstract methods have no implementation and must be overridden in derived classes.
Question bank Tap to reveal →
Which of the following correctly describes an abstract method?
B · A method declared without a body to be implemented by subclasses
An abstract method is declared without a body and must be implemented by subclasses.
Question bank Tap to reveal →
Which of the following is a valid characteristic of abstract methods?
B · They can have access modifiers like public or protected
Abstract methods can have access modifiers such as public or protected to control visibility.
Question bank Tap to reveal →
At an advanced level, which statement about abstract methods is TRUE?
C · Abstract methods enforce a contract for subclasses
Abstract methods enforce a contract that subclasses must provide their own implementation.
Question bank Tap to reveal →
In the context of abstraction, what is the role of interfaces in OOP?
B · To define a contract with method signatures only
Interfaces define a contract by declaring method signatures without implementations, which classes must implement.
Question bank Tap to reveal →
Which of the following statements about interfaces is CORRECT?
B · Interfaces allow multiple inheritance of type
Interfaces allow multiple inheritance of type, enabling a class to implement multiple interfaces.
Question bank Tap to reveal →
Which of the following best describes the difference between abstract classes and interfaces?
A · Abstract classes can have constructors; interfaces cannot
Abstract classes can have constructors and maintain state, whereas interfaces cannot have constructors and traditionally do not hold state.
Question bank Tap to reveal →
At an advanced level, which statement about interfaces is TRUE?
B · Interfaces define a strict contract that implementing classes must follow
Interfaces define a strict contract with method signatures that implementing classes must follow.
Question bank Tap to reveal →
How is abstraction typically implemented in Java?
B · Using abstract classes and interfaces
Java implements abstraction using abstract classes and interfaces to hide implementation details.
Question bank Tap to reveal →
Which of the following is a correct way to implement abstraction in C++?
A · Using abstract classes with pure virtual functions
In C++, abstraction is implemented using abstract classes that contain pure virtual functions.
Question bank Tap to reveal →
Which of the following statements about abstraction implementation in OOP languages is TRUE?
B · C++ uses pure virtual functions to achieve abstraction
C++ uses pure virtual functions in abstract classes to achieve abstraction.
Question bank Tap to reveal →
At an advanced level, which statement about abstraction implementation is CORRECT?
A · Abstract classes in Java can contain implemented and unimplemented methods
Java abstract classes can have both implemented (concrete) and unimplemented (abstract) methods.
Question bank Tap to reveal →
Which of the following is a common use case for abstraction in software development?
B · To separate interface from implementation to allow independent development
Abstraction separates interface from implementation, enabling independent development and easier maintenance.
Question bank Tap to reveal →
Which of the following is an example of abstraction in real-world software systems?
A · Using a database API to perform CRUD operations without knowing internal queries
Using an API abstracts the underlying database implementation, exposing only necessary operations.
Question bank Tap to reveal →
Which of the following best illustrates the use of abstraction in software design?
A · A user interface that hides backend logic from users
A user interface that hides backend logic is an example of abstraction by hiding complexity from users.
Question bank Tap to reveal →
At an advanced level, which scenario best demonstrates abstraction?
A · A framework providing abstract classes and interfaces for developers to extend
Frameworks providing abstract classes and interfaces allow developers to focus on high-level design, hiding implementation details.
Question bank Tap to reveal →
Which of the following best describes high-level abstraction?
B · Focus on general concepts and interfaces
High-level abstraction focuses on general concepts and interfaces, hiding low-level details.
Question bank Tap to reveal →
Which of the following statements correctly contrasts high-level and low-level abstraction?
B · High-level abstraction hides implementation details; low-level abstraction exposes them
High-level abstraction hides implementation details, whereas low-level abstraction exposes them for detailed control.
Question bank Tap to reveal →
At an advanced level, which of the following best illustrates the difference between high-level and low-level abstraction?
B · High-level abstraction focuses on what operations do; low-level focuses on how operations are performed
High-level abstraction focuses on what operations do, hiding details; low-level abstraction focuses on how operations are performed.
Question bank Tap to reveal →
Which of the following best describes the difference between abstraction and data hiding?
A · Abstraction hides implementation details; data hiding restricts access to data members
Abstraction hides implementation details to reduce complexity, while data hiding restricts access to data members to protect integrity.
Question bank Tap to reveal →
Which of the following statements correctly distinguishes abstraction from data hiding?
A · Abstraction is about hiding complexity; data hiding is about restricting access
Abstraction hides complexity by exposing only necessary features, while data hiding restricts access to internal data to protect it.
Question bank Tap to reveal →
Which of the following is TRUE about abstraction and data hiding at a conceptual level?
A · Abstraction focuses on interface design; data hiding focuses on access control
Abstraction focuses on designing interfaces that hide complexity, while data hiding focuses on controlling access to data members.
Question bank Tap to reveal →
At an advanced level, which statement best explains the difference between abstraction and data hiding?
A · Abstraction is a design principle; data hiding is an implementation mechanism
Abstraction is a high-level design principle focusing on hiding complexity, while data hiding is a mechanism to restrict access to data.
Question bank Tap to reveal →
What is the primary purpose of abstraction in Object-Oriented Programming?
A · To hide complex implementation details and show only essential features
Abstraction focuses on hiding the complex implementation details and exposing only the necessary parts to the user.
Question bank Tap to reveal →
Which of the following best defines abstraction in OOP?
A · Separating interface from implementation
Abstraction is about separating the interface (what) from the implementation (how), allowing users to interact with the object without knowing internal details.
Question bank Tap to reveal →
Which statement about abstraction is true?
B · Abstraction hides implementation details and shows only functionality
Abstraction hides the internal implementation details and exposes only the essential functionality to the user.
Question bank Tap to reveal →
Which of the following is NOT a purpose of abstraction in software design?
D · Increasing memory usage
Abstraction aims to reduce complexity, improve reusability, and hide irrelevant details; it does not increase memory usage.
Question bank Tap to reveal →
Which of the following best distinguishes abstraction from encapsulation?
B · Abstraction hides implementation details; encapsulation hides data
Abstraction hides the implementation details from the user, while encapsulation hides the data by restricting access through access modifiers.
Question bank Tap to reveal →
Which scenario best illustrates the difference between abstraction and encapsulation?
A · Abstraction is about what an object does; encapsulation is about how it does it
Abstraction focuses on what an object does (interface), while encapsulation focuses on how it does it (data hiding and access control).
Question bank Tap to reveal →
Which of the following statements is true regarding abstraction and encapsulation?
B · Encapsulation can be achieved without abstraction
Encapsulation can be implemented independently by restricting access to data members, but abstraction requires hiding implementation details, which may or may not involve encapsulation.
Question bank Tap to reveal →
Which of the following is an example of abstraction in OOP?
B · Declaring an abstract class with abstract methods
Declaring an abstract class with abstract methods is a way to enforce abstraction by defining what methods must be implemented without specifying how.
Question bank Tap to reveal →
Which of the following is true about abstract classes in OOP?
B · They may contain both abstract and concrete methods
Abstract classes can contain both abstract methods (without implementation) and concrete methods (with implementation). They cannot be instantiated directly but can have constructors.
Question bank Tap to reveal →
Which of the following statements about abstract classes is correct?
C · An abstract class can provide partial implementation to subclasses
Abstract classes can provide partial implementation and leave some methods abstract for subclasses to implement.
Question bank Tap to reveal →
What happens if a subclass does not implement all abstract methods of its abstract superclass?
A · The subclass becomes abstract and cannot be instantiated
If a subclass does not implement all abstract methods, it must be declared abstract itself and cannot be instantiated.
Question bank Tap to reveal →
Which of the following is NOT true about abstract classes?
C · They can be instantiated directly
Abstract classes cannot be instantiated directly; they are meant to be subclassed.
Question bank Tap to reveal →
Which of the following best describes an abstract method?
B · A method declared without implementation to be overridden by subclasses
An abstract method is declared without implementation and must be overridden by subclasses.
Question bank Tap to reveal →
What is the purpose of declaring a method as abstract in a class?
B · To force subclasses to provide their own implementation
Declaring a method abstract forces subclasses to provide their own implementation of that method.
Question bank Tap to reveal →
Which of the following is true about abstract methods?
C · They must be overridden in non-abstract subclasses
Abstract methods cannot have a body and must be overridden in subclasses that are not abstract.
Question bank Tap to reveal →
Which of the following is NOT allowed for an abstract method in Java?
A · Having a method body
Abstract methods cannot have a method body; they only have a declaration.
Question bank Tap to reveal →
Which of the following statements about interfaces in OOP is correct?
B · Interfaces define a contract that implementing classes must follow
Interfaces define a contract (methods signatures) that implementing classes must provide implementations for.
Question bank Tap to reveal →
How do interfaces support abstraction in OOP?
B · By defining method signatures without implementation
Interfaces provide abstraction by defining method signatures without any implementation, forcing classes to implement them.
Question bank Tap to reveal →
Which of the following is a key difference between abstract classes and interfaces?
A · Abstract classes can have constructors; interfaces cannot
Abstract classes can have constructors and state, whereas interfaces cannot have constructors or instance variables (prior to Java 8 default methods).
Question bank Tap to reveal →
Which of the following is NOT true about interfaces in Java?
D · Interfaces can have instance variables
Interfaces cannot have instance variables; they can only have constants (static final variables).
Question bank Tap to reveal →
In Java, which keyword is used to declare an abstract class?
A · abstract
The keyword 'abstract' is used to declare an abstract class in Java.
Question bank Tap to reveal →
How is abstraction implemented in C++?
A · Using abstract classes with pure virtual functions
In C++, abstraction is implemented using abstract classes that contain pure virtual functions.
Question bank Tap to reveal →
Which of the following is true about pure virtual functions in C++?
B · They make a class abstract
Declaring a pure virtual function makes the class abstract and forces derived classes to override it.
Question bank Tap to reveal →
Which of the following is NOT a benefit of abstraction in OOP?
C · Increased coupling between modules
Abstraction reduces coupling by hiding implementation details, so increased coupling is not a benefit.
Question bank Tap to reveal →
How does abstraction contribute to software development?
A · By allowing developers to focus on high-level design without worrying about low-level details
Abstraction helps developers focus on high-level design by hiding low-level implementation details.
Question bank Tap to reveal →
Which of the following is a real-world example of abstraction?
A · Using a TV remote without knowing its internal circuitry
Using a TV remote without knowing its internal workings is an example of abstraction where only essential functionality is exposed.
Question bank Tap to reveal →
Which of the following software design levels represents the highest level of abstraction?
A · User interface design
User interface design is the highest level of abstraction as it focuses on what the user interacts with, hiding underlying details.
Question bank Tap to reveal →
Which level of abstraction focuses on hiding hardware details from software developers?
A · High-level programming languages
High-level programming languages abstract away hardware details, allowing developers to write code without managing hardware directly.
Question bank Tap to reveal →
Which of the following best describes the relationship between levels of abstraction and software complexity?
B · Higher abstraction levels reduce complexity for developers
Higher levels of abstraction reduce complexity by hiding details and allowing developers to focus on broader concepts.
Question bank Tap to reveal →
Which of the following best exemplifies abstraction in Java?
A · Using the 'abstract' keyword to define a class with some unimplemented methods
Using the 'abstract' keyword to define a class with abstract methods is a direct way to implement abstraction in Java.
Question bank Tap to reveal →
In C++, which syntax defines a pure virtual function to achieve abstraction?
A · virtual void func() = 0;
In C++, a pure virtual function is declared using '= 0' syntax, e.g., virtual void func() = 0;
Question bank Tap to reveal →
Which of the following is NOT a benefit of abstraction in OOP?
C · Increases dependency between modules
Abstraction reduces dependency between modules by hiding implementation details, so increasing dependency is not a benefit.
Question bank Tap to reveal →
Which of the following is an example of abstraction at the software design level?
A · Defining interfaces for modules without specifying implementation
Defining interfaces abstracts the implementation details, allowing modular design and separation of concerns.
Question bank Tap to reveal →
Assertion (A): Abstract classes cannot be instantiated but can have constructors. Reason (R): Constructors in abstract classes are used to initialize common state for subclasses. Choose the correct option:
A · Both A and R are true, and R is the correct explanation of A.
Question bank Tap to reveal →
Which of the following statements about abstraction and interfaces is FALSE when combined with multiple inheritance and default methods in Java 8+?
D · Abstract classes can be instantiated if all abstract methods have default implementations.
Question bank Tap to reveal →
What is the primary purpose of inheritance in Object-Oriented Programming?
A · To enable code reusability and establish a relationship between classes
Inheritance allows a new class to acquire properties and behaviors of an existing class, promoting code reuse and hierarchical relationships.
Question bank Tap to reveal →
Which of the following best defines inheritance in OOP?
A · A process where one class acquires the properties of another class
Inheritance is a mechanism where a new class (derived class) inherits attributes and methods from an existing class (base class).
Question bank Tap to reveal →
Which of the following is NOT a purpose of inheritance?
C · Encapsulation of unrelated data
Encapsulation of unrelated data is not a purpose of inheritance; inheritance focuses on hierarchical relationships and code reuse.
Question bank Tap to reveal →
Which of the following types of inheritance involves a class derived from another derived class?
C · Multilevel Inheritance
Multilevel inheritance occurs when a class is derived from a derived class, forming a chain of inheritance.
Question bank Tap to reveal →
Refer to the diagram below showing a class hierarchy. What type of inheritance is depicted? Refer to the diagram below showing a class A, class B derived from A, and class C derived from A.
C · Hierarchical Inheritance
Hierarchical inheritance is when multiple classes inherit from a single base class, as shown where both B and C inherit from A.
Question bank Tap to reveal →
In C++, which syntax correctly declares class B inheriting publicly from class A?
A · class B : public A {}
In C++, public inheritance is declared using the syntax: class Derived : public Base {}.
Question bank Tap to reveal →
Which keyword is used in Java to inherit a class?
A · extends
Java uses the keyword 'extends' to inherit from a class.
Question bank Tap to reveal →
Refer to the diagram below showing classes in Python. Which syntax correctly represents class B inheriting from class A?
A · class B(A): pass
In Python, inheritance is declared by passing the base class name in parentheses after the derived class name.
Question bank Tap to reveal →
Which of the following access specifiers in C++ allows members of the base class to be accessible in the derived class but not outside?
C · protected
Protected members are accessible within the derived class and its subclasses but not outside these classes.
Question bank Tap to reveal →
In C++, what is the effect of private inheritance on the accessibility of the base class's public members in the derived class?
A · They become private members of the derived class
With private inheritance, public and protected members of the base class become private members of the derived class.
Question bank Tap to reveal →
Which of the following best describes method overriding in inheritance?
A · A derived class provides a new implementation of a base class method with the same signature
Method overriding occurs when a derived class redefines a base class method with the same signature to provide specialized behavior.
Question bank Tap to reveal →
Which of the following is true about method overloading in the context of inheritance?
A · It allows multiple methods with the same name but different parameters within the same class
Method overloading allows multiple methods in the same class with the same name but different parameter lists, enabling polymorphism.
Question bank Tap to reveal →
Which of the following statements about constructors in inheritance is correct?
A · Base class constructor is called before the derived class constructor
In inheritance, the base class constructor is invoked first, followed by the derived class constructor.
Question bank Tap to reveal →
In C++, when an object of a derived class is destroyed, which destructor is called first?
A · Derived class destructor
The derived class destructor is called first, followed by the base class destructor during object destruction.
Question bank Tap to reveal →
Refer to the diagram below showing constructor call order in multilevel inheritance (Class A -> Class B -> Class C). Which is the correct order of constructor calls when creating an object of class C?
A · A, B, C
Constructors are called from the base class down to the most derived class in multilevel inheritance.
Question bank Tap to reveal →
In Java, which keyword is used to call the constructor of the base class from the derived class?
A · super
The 'super' keyword in Java is used to call the base class constructor or access base class members.
Question bank Tap to reveal →
Which of the following statements about the 'super' keyword in Python is correct?
A · It is used to call methods from the base class
'super()' in Python is used to access methods from the base class, especially in overridden methods.
Question bank Tap to reveal →
Which of the following is an advantage of using inheritance in OOP?
A · Promotes code reusability and extensibility
Inheritance promotes code reuse by allowing new classes to use existing code and extend functionality.
Question bank Tap to reveal →
Which of the following is a disadvantage of inheritance?
A · Tight coupling between base and derived classes
Inheritance can cause tight coupling, making changes in the base class potentially affect derived classes adversely.
Question bank Tap to reveal →
Which of the following best describes the relationship between inheritance and polymorphism?
A · Inheritance enables polymorphism by allowing method overriding
Inheritance allows derived classes to override base class methods, enabling polymorphism where objects behave differently based on their class.
Question bank Tap to reveal →
Refer to the diagram below showing a class hierarchy with polymorphism. Which feature is demonstrated when a base class pointer points to derived class objects and calls overridden methods?
A · Runtime polymorphism
Runtime polymorphism allows a base class pointer to invoke derived class methods dynamically at runtime.
Question bank Tap to reveal →
Which of the following describes the diamond problem in multiple inheritance?
A · Ambiguity caused by multiple inheritance of the same base class
The diamond problem occurs when two classes inherit from the same base class and a derived class inherits from both, causing ambiguity.
Question bank Tap to reveal →
In C++, which keyword is used to declare virtual inheritance?
A · virtual
The 'virtual' keyword is used in C++ to specify virtual inheritance to solve the diamond problem.
Question bank Tap to reveal →
Which of the following statements about method overriding is true in the context of inheritance?
A · Overriding allows a derived class to provide a specific implementation of a method already defined in its base class
Method overriding allows a derived class to change or extend the behavior of a base class method with the same signature.
Question bank Tap to reveal →
Which of the following scenarios best illustrates method overloading in inheritance?
A · A class has two methods named 'calculate', one accepting an int and another a float
Method overloading involves multiple methods with the same name but different parameter lists within the same class.
Question bank Tap to reveal →
In C++, which access specifier inheritance type causes all public and protected members of the base class to become private members of the derived class?
A · Private inheritance
Private inheritance makes all inherited public and protected members private in the derived class.
Question bank Tap to reveal →
Which of the following is NOT true about constructors in inheritance?
D · Derived class constructors are called before base class constructors
Derived class constructors are called after base class constructors, not before.
Question bank Tap to reveal →
Refer to the diagram below showing multiple inheritance in C++. Which problem can arise if both base classes have a method with the same name? Class B and Class C inherit from Class A, Class D inherits from B and C.
A · Ambiguity problem
When multiple base classes have methods with the same name, ambiguity arises about which method to call.
Question bank Tap to reveal →
What is the primary purpose of inheritance in object-oriented programming?
A · To allow a class to acquire properties and behaviors of another class
Inheritance enables a class (derived class) to inherit properties and methods from another class (base class), promoting code reuse and hierarchical classification.
Question bank Tap to reveal →
Which of the following best describes inheritance in OOP?
B · A process where one class acquires the properties of another
Inheritance allows a class to inherit attributes and methods from another class, facilitating reuse and extension of existing code.
Question bank Tap to reveal →
Which of the following is NOT a type of inheritance?
C · Parallel inheritance
Parallel inheritance is not a standard type; common types include single, multiple, multilevel, hierarchical, and hybrid inheritance.
Question bank Tap to reveal →
Refer to the diagram below showing a class hierarchy. Which type of inheritance is illustrated?
D · Hierarchical inheritance
The diagram shows one base class with multiple derived classes inheriting directly from it, which is hierarchical inheritance.
Question bank Tap to reveal →
In C++, which syntax correctly shows single inheritance where class B inherits from class A?
A · class B : public A {}
In C++, inheritance is declared using a colon followed by the access specifier and base class name, e.g., class B : public A {}.
Question bank Tap to reveal →
Which of the following is the correct way to inherit a class in Java?
A · class B extends A {}
In Java, inheritance is specified using the keyword 'extends' for classes.
Question bank Tap to reveal →
In Python, how do you indicate that class B inherits from class A?
A · class B(A):
Python uses parentheses after the class name to specify inheritance, e.g., class B(A):
Question bank Tap to reveal →
Refer to the diagram below showing class relationships in C++. Which access specifier is used in the inheritance shown by the arrow from class A to class B?
A · Public inheritance
The arrow with an open arrowhead and label 'public' indicates public inheritance, meaning public and protected members of A retain their access in B.
Question bank Tap to reveal →
Which access specifier in C++ inheritance causes all public and protected members of the base class to become private in the derived class?
B · Private inheritance
Private inheritance makes all inherited public and protected members private in the derived class, restricting access outside the class.
Question bank Tap to reveal →
In C++, if a base class has a protected member, what will be its access level in the derived class if inheritance is protected?
C · Protected
Protected inheritance keeps public and protected members of the base class as protected in the derived class.
Question bank Tap to reveal →
Which of the following statements about method overriding is correct?
A · It allows a derived class to provide a specific implementation of a method already defined in its base class
Method overriding allows a derived class to replace or extend the behavior of a base class method with the same signature.
Question bank Tap to reveal →
In Java, which keyword is used inside a method to call the overridden method of the base class?
A · super
The 'super' keyword in Java is used to refer to the immediate parent class and can be used to call its overridden methods.
Question bank Tap to reveal →
Which of the following best describes the order of constructor calls in multilevel inheritance?
B · Base class constructor is called first, then derived class constructors
In multilevel inheritance, constructors are called starting from the base class up to the most derived class.
Question bank Tap to reveal →
In C++, which of the following statements about destructors in inheritance is true?
A · Derived class destructor is called before base class destructor
When an object is destroyed, the derived class destructor executes first, followed by the base class destructor.
Question bank Tap to reveal →
Refer to the diagram below showing constructor call order in a multilevel inheritance chain (Class A -> Class B -> Class C). Which sequence correctly represents the constructor calls when creating an object of Class C?
B · A → B → C
Constructors are called from the base class (A) to the most derived class (C) in multilevel inheritance.
Question bank Tap to reveal →
Polymorphism in inheritance allows which of the following?
A · Multiple classes to have methods with the same name but different implementations
Polymorphism allows methods with the same name to behave differently depending on the object that invokes them, typically via overriding.
Question bank Tap to reveal →
Which of the following is required to achieve runtime polymorphism in C++ inheritance?
A · Virtual functions
Virtual functions enable runtime polymorphism by allowing the program to decide at runtime which function to invoke.
Question bank Tap to reveal →
Refer to the diagram below showing a base class pointer pointing to derived class objects. Which concept does this illustrate?
B · Runtime polymorphism
Using a base class pointer to refer to derived class objects and invoking overridden methods demonstrates runtime polymorphism.
Question bank Tap to reveal →
Which of the following is a disadvantage of inheritance?
B · Tight coupling between base and derived classes
Inheritance can cause tight coupling, making changes in base classes potentially affect derived classes adversely.
Question bank Tap to reveal →
Which of the following is an advantage of using inheritance?
A · Code reuse and extension
Inheritance promotes code reuse by allowing derived classes to use and extend existing base class functionality.
Question bank Tap to reveal →
Which of the following best describes an abstract class?
A · A class that cannot be instantiated and may contain pure virtual functions
Abstract classes define interfaces with pure virtual functions and cannot be instantiated directly.
Question bank Tap to reveal →
Which keyword is used in C++ to declare a virtual function?
A · virtual
The 'virtual' keyword declares a function as virtual, enabling dynamic dispatch and polymorphism.
Question bank Tap to reveal →
Which of the following statements about pure virtual functions is correct?
A · They have no implementation and must be overridden in derived classes
Pure virtual functions have no body and force derived classes to provide an implementation, making the class abstract.
Question bank Tap to reveal →
Refer to the diagram below illustrating a class with a pure virtual function. What type of class is this?
B · Abstract class
A class with at least one pure virtual function is an abstract class and cannot be instantiated.
Question bank Tap to reveal →
What problem does the diamond problem in multiple inheritance cause?
A · Ambiguity in inherited members from multiple paths
The diamond problem arises when a class inherits from two classes that both inherit from the same base class, causing ambiguity in inherited members.
Question bank Tap to reveal →
Refer to the diamond inheritance diagram below. Which technique is used in C++ to resolve the diamond problem?
A · Virtual inheritance
Virtual inheritance ensures only one copy of the base class is inherited, resolving ambiguity in diamond inheritance.
Question bank Tap to reveal →
Which of the following statements about virtual inheritance in C++ is true?
A · It ensures only one instance of a base class is shared among derived classes
Virtual inheritance solves the diamond problem by sharing a single instance of the base class among multiple derived classes.
Question bank Tap to reveal →
Which of the following is NOT a valid advantage of inheritance?
C · Increases coupling between classes
Inheritance can increase coupling, which is a disadvantage, not an advantage.
Question bank Tap to reveal →
In C++, consider the following classes: ```cpp class A { public: virtual void f(int x = 10) { std::cout << x; } }; class B : public A { public: void f(int x = 20) override { std::cout << x; } }; \nint main() { A* ptr = new B(); ptr->f(); return 0; } ``` What will be the output and why?
A · 10
Question bank Tap to reveal →
In Java, consider the following code: ```java class A { final void show() { System.out.println("A"); } } class B extends A { void show() { System.out.println("B"); } } ``` What will happen when compiling this code?
A · Compilation error because B tries to override final method show()
Question bank Tap to reveal →
In C++, consider the following code snippet: ```cpp class A { public: virtual void f() { std::cout << "A"; } }; class B : public A { public: void f() override { std::cout << "B"; } }; class C : public B { public: void f() { std::cout << "C"; } }; \nint main() { A* ptr = new C(); ptr->f(); return 0; } ``` What will be the output and why?
C · C

Try Practice next.

Marking revisions saves to your dashboard — paywalled in preview.

Test myself in practice →
Ask a doubt
Polymorphism · 10 free messages
Ask me anything about this subtopic. You have 10 free messages this session — chat history isn't saved in preview.