Docsity

Object-Oriented Programming (OOP) Exam Core Principles, Inheritance, Polymorphism,, Exams of Object Oriented Programming

Toggle sidebar
bg1
Object-Oriented Programming (OOP) Exam Core Principles,
Inheritance, Polymorphism, Encapsulation, and C++ Classes
Questions and Answers (Q&A) with Correct Answers and
Rationales
Main Areas Covered Latest Exam 2026/2027 by Brian
Verified Answers A+
1. Which OOP principle allows a subclass to provide a specific implementation
of a method already defined in its superclass?
A. Encapsulation
B. Inheritance
C. Polymorphism (overriding)
D. Abstraction
Correct Answer: C
Rationale: Method overriding is a form of polymorphism where a subclass
provides a specific implementation of a method declared in its superclass.
Encapsulation hides data; inheritance creates an "is-a" relationship; abstraction
simplifies complex reality.
2. What is the primary purpose of encapsulation?
A. To allow a class to inherit from multiple classes
B. To hide internal state and require interaction through methods
C. To enable method overloading
D. To create abstract classes
Correct Answer: B
Rationale: Encapsulation bundles data and methods and restricts direct access to
internal state, typically through private members and public getters/setters.
3. Which relationship does inheritance model?
A. "Has-a" relationship
B. "Is-a" relationship
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Object-Oriented Programming (OOP) Exam Core Principles, Inheritance, Polymorphism, and more Exams Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming (OOP) Exam Core Principles,

Inheritance, Polymorphism, Encapsulation, and C++ Classes

Questions and Answers (Q&A) with Correct Answers and

Rationales

Main Areas Covered Latest Exam 2026/2027 by Brian

Verified Answers A+

1. Which OOP principle allows a subclass to provide a specific implementation of a method already defined in its superclass? A. Encapsulation B. Inheritance C. Polymorphism (overriding) D. Abstraction Correct Answer: C Rationale: Method overriding is a form of polymorphism where a subclass provides a specific implementation of a method declared in its superclass. Encapsulation hides data; inheritance creates an "is-a" relationship; abstraction simplifies complex reality. 2. What is the primary purpose of encapsulation? A. To allow a class to inherit from multiple classes B. To hide internal state and require interaction through methods C. To enable method overloading D. To create abstract classes Correct Answer: B Rationale: Encapsulation bundles data and methods and restricts direct access to internal state, typically through private members and public getters/setters. 3. Which relationship does inheritance model? A. "Has-a" relationship B. "Is-a" relationship

C. "Uses-a" relationship D. "Part-of" relationship Correct Answer: B Rationale: Inheritance models an "is-a" relationship (e.g., a Car is-a Vehicle). Composition models "has-a" or "part-of" relationships.

4. What is an abstract class in C++? A. A class with no methods B. A class that cannot be instantiated and contains at least one pure virtual function C. A class with only static methods D. A class that inherits from multiple classes Correct Answer: B Rationale: An abstract class has at least one pure virtual function (declared with =

  1. and cannot be instantiated directly. 5. Which access specifier allows a member to be accessed only within the class itself? A. public B. protected C. private D. friend Correct Answer: C Rationale: Private members are accessible only within the class. Protected members are accessible in derived classes. Public members are accessible anywhere. 6. What is the purpose of a virtual function in C++? A. To prevent inheritance B. To enable runtime polymorphism (dynamic dispatch) C. To make a function static D. To hide a function from derived classes Correct Answer: B Rationale: Virtual functions enable dynamic dispatch, so the correct function is called based on the actual object type at runtime.

Correct Answer: A Rationale: A pure virtual function is declared with = 0 and makes the class abstract. Section 2: C++ Fundamentals (Q26–Q50)

11. What is the output of int x = 7 / 2; in C++? A. 3. B. 3 C. 4 D. Error Correct Answer: B Rationale: Integer division truncates the fractional part, yielding 3. 12. What is the output of 7 % 2 in C++? A. 3 B. 1 C. 0 D. 3. Correct Answer: B Rationale: The modulus operator returns the remainder: 7 divided by 2 is 3 with a remainder of 1. 13. Which operator is used to access a member through a pointer? A.. B. - > C. :: D. * Correct Answer: B Rationale: The - > operator accesses members through a pointer. The. operator accesses members through an object. 14. What does the new operator do? A. Deallocates memory B. Allocates memory from the heap and returns a pointer

C. Creates a stack variable D. Copies an object Correct Answer: B Rationale: new allocates dynamic memory and returns a pointer to the allocated object.

15. What must be used to release memory allocated with new? A. free() B. delete C. remove() D. destroy() Correct Answer: B Rationale: delete releases memory allocated with new. delete[] is used for arrays. 16. What is a null pointer? A. A pointer that points to address 0 B. A pointer that points to a valid object C. A pointer that has not been declared D. A pointer to a constant Correct Answer: A Rationale: A null pointer stores the value 0 (or nullptr in modern C++) and does not point to a valid object. 17. What is the purpose of the const keyword? A. To make a variable mutable B. To make an entity read-only C. To allocate memory D. To create a pointer Correct Answer: B Rationale: const tells the compiler the entity is read-only and cannot be modified. 18. What is the difference between struct and class in C++? A. struct members are public by default; class members are private by default B. struct cannot have methods; class can

22. What is the primary purpose of QList in Qt? A. To store a collection of key-value pairs B. To manage a list of integers only C. To create a dynamic array of objects D. To manage a list of objects with different data types Correct Answer: C Rationale: QList is a dynamic array-like container for objects of the same type. 23. In Qt, what is the key difference between QList and QVector? A. QList is for objects; QVector is for primitives only B. QList provides faster random access C. QVector automatically resizes when elements are added D. QList is a linked list; QVector is a dynamic array Correct Answer: D Rationale: Historically, QList was array-based for large types; QVector is a dynamic array. The distinction has evolved, but QVector is the dynamic array class . 24. Which statement about QListIterator is true? A. QListIterator directly modifies elements B. QListIterator is more efficient than a regular iterator C. QListIterator allows both forward and backward traversal D. QListIterator creates a copy of a QList Correct Answer: C Rationale: QListIterator provides forward and backward traversal of a QList. 25. What is the main purpose of QStringList? A. To store a list of integers B. To store a list of objects C. To store a list of strings D. To store a list of floating-point numbers Correct Answer: C Rationale: QStringList is a convenience class for storing lists of strings.

26. Which of the following is NOT a valid Qt container class for managing lists? A. QList B. QLinkedList C. QArrayList D. QVector Correct Answer: C Rationale: QArrayList is not a Qt class. QList, QLinkedList, and QVector are valid Qt containers. 27. What is the primary advantage of using iterators with QList? A. Iterators allow direct modification of the QList's internal structure B. Iterators provide a safe and efficient way to traverse and modify elements C. Iterators are always faster than for loops D. Iterators prevent all bugs Correct Answer: B Rationale: Iterators provide a safe, efficient way to traverse and modify container elements. 28. Which Qt class should you inherit from to create a custom list class? A. QList B. QVector C. QStringList D. QListWidget Correct Answer: A Rationale: QList is the base class suitable for inheritance when creating a custom list with additional functionality. 29. What does QMap store? A. A list of strings B. A collection of key-value pairs C. A dynamic array of integers D. A list of iterators Correct Answer: B Rationale: QMap is an associative container that stores key-value pairs.

Correct Answer: C Rationale: Composition uses a solid line with a filled diamond at the parent (whole) end.

34. What is the key difference between aggregation and composition? A. Aggregation is strong ownership; composition is weak B. Aggregation allows the child to exist without the parent; composition enforces the parent's existence C. Aggregation is represented by a diamond; composition is not D. Aggregation is always one-to-one Correct Answer: B Rationale: In aggregation, the child can exist independently. In composition, the child's lifetime is tied to the parent. 35. Which of the following is an example of an association relationship? A. A car is composed of an engine, wheels, and seats B. A university has multiple departments, and each department has multiple students C. A person inherits properties from a parent class D. A computer contains a keyboard and mouse Correct Answer: B Rationale: Association represents a general relationship between classes without ownership implications. 36. In UML, how is inheritance represented? A. A solid line with a filled diamond B. A dashed line with an arrow C. A solid line with a hollow triangle pointing to the parent D. A straight line without decoration Correct Answer: C Rationale: Inheritance (generalization) uses a solid line with a hollow triangle pointing to the superclass. 37. What does a multiplicity notation like 1..* mean in UML? A. Exactly one B. Zero or one

C. One or more D. Zero or more Correct Answer: C Rationale: 1..* means one to many (one or more).

38. Which relationship best models "a Department has Professors, and Professors can exist without the Department"? A. Composition B. Aggregation C. Inheritance D. Dependency Correct Answer: B Rationale: Aggregation models a "has-a" relationship where the part can exist independently of the whole. 39. Which relationship best models "a House has Rooms, and Rooms cannot exist without the House"? A. Aggregation B. Composition C. Association D. Inheritance Correct Answer: B Rationale: Composition models strong ownership where the part's lifetime is tied to the whole. 40. What is a UML class diagram used for? A. To model database schemas only B. To visualize the static structure of a system, including classes, attributes, methods, and relationships C. To model runtime behavior only D. To write code automatically Correct Answer: B Rationale: UML class diagrams show the static structure of a system.

5. What is a reference in C++? A. A pointer that can be reassigned B. An alias for an existing variable C. A copy of a variable D. A null pointer Correct Answer: B Rationale: A reference is an alias (alternative name) for an existing variable and must be initialized when declared. 6. Which statement about constructors is correct? A. Constructors have a return type B. Constructors can be overloaded C. Constructors cannot be called explicitly D. Constructors must be private Correct Answer: B Rationale: Constructors can be overloaded (multiple constructors with different parameter lists). They have no return type. 7. What is a destructor used for? A. To initialize objects B. To clean up resources when an object is destroyed C. To copy objects D. To overload operators Correct Answer: B Rationale: A destructor (declared with ~ClassName()) is called automatically when an object goes out of scope or is deleted. 8. What is the purpose of the copy constructor? A. To create a new object as a copy of an existing object B. To destroy objects C. To overload assignment D. To initialize static members Correct Answer: A Rationale: The copy constructor creates a new object as a copy of an existing object of the same class.

9. When is a copy constructor called? A. When an object is passed by value B. When an object is returned by value C. When an object is initialized with another object D. All of the above Correct Answer: D Rationale: The copy constructor is called when an object is passed by value, returned by value, or initialized with another object. 10. What is the Rule of Three in C++? A. Three constructors are required B. If a class defines a destructor, copy constructor, or copy assignment operator, it should define all three C. Three inheritance levels are required D. Three virtual functions are required Correct Answer: B Rationale: The Rule of Three states that if a class needs a custom destructor, copy constructor, or copy assignment operator, it likely needs all three (due to resource management). 11. What does the static keyword do when applied to a class member? A. Makes it belong to the class rather than instances B. Makes it constant C. Makes it virtual D. Makes it private Correct Answer: A Rationale: Static members belong to the class itself, not to individual objects. They are shared by all instances. 12. What is a friend function? A. A function that is a member of the class B. A non-member function granted access to private members C. A virtual function D. A static function

Correct Answer: B Rationale: Multiplication has higher precedence than addition: 5 + (3 * 2) = 5 + 6 = 11.

17. Which header file is required for cout and cin? A. B. C. D. Correct Answer: B Rationale: declares cout, cin, cerr, and clog. 18. What is the difference between cout and cerr? A. cout is unbuffered; cerr is buffered B. cout is buffered; cerr is unbuffered C. They are identical D. cerr is for input Correct Answer: B Rationale: cout is buffered; cerr is unbuffered and typically used for error messages. 19. What does endl do? A. Ends the program B. Outputs a newline and flushes the buffer C. Ends a string D. Deletes a line Correct Answer: B Rationale: endl inserts a newline character and flushes the output buffer. 20. What is the correct way to declare a constant in C++? A. constant int x = 5; B. const int x = 5; C. final int x = 5; D. fixed int x = 5;

Correct Answer: B Rationale: The const keyword makes a variable read-only.

21. What is the output of int x = 10; x += 5;? A. 10 B. 5 C. 15 D. 20 Correct Answer: C Rationale: x += 5 is equivalent to x = x + 5, yielding 15. 22. What is the difference between = and == in C++? A. They are identical B. = is assignment; == is comparison C. = is comparison; == is assignment D. == is invalid in C++ Correct Answer: B Rationale: = assigns a value; == compares two values for equality. 23. Which loop is guaranteed to execute at least once? A. for B. while C. do-while D. foreach Correct Answer: C Rationale: A do-while loop executes the body first, then checks the condition. 24. What is the output of for (int i = 0; i < 3; i++) cout << i;? A. 123 B. 012 C. 0123 D. 1234 Correct Answer: B Rationale: The loop prints 0, 1, 2 (i < 3), concatenated as "012".

29. What is a vector in C++? A. A fixed-size array B. A dynamic array that can grow and shrink C. A linked list D. A hash table Correct Answer: B Rationale: std::vector is a dynamic array that automatically resizes. 30. What is the difference between struct and class in C++? A. struct members are public by default; class members are private by default B. struct cannot have methods; class can C. struct cannot inherit; class can D. They are identical Correct Answer: A Rationale: The only difference is default access: struct members are public by default, class members are private. 31. What is the purpose of the virtual keyword? A. To enable runtime polymorphism B. To make a function static C. To make a function private D. To prevent inheritance Correct Answer: A Rationale: virtual enables dynamic dispatch, so the correct function is called based on the actual object type. 32. What is a pure virtual function? A. A function with no body, declared with = 0 B. A function that cannot be overridden C. A static function D. A function that returns void Correct Answer: A Rationale: A pure virtual function is declared with = 0 and makes the class abstract.

33. What is the output of cout << sizeof(int); on most systems? A. 1 B. 2 C. 4 D. 8 Correct Answer: C Rationale: On most systems, int is 4 bytes. 34. What is the difference between new and malloc? A. new calls constructors; malloc does not B. malloc calls constructors; new does not C. They are identical D. new is for arrays only Correct Answer: A Rationale: new allocates memory and calls constructors; malloc only allocates raw memory. 35. What is the difference between delete and free? A. delete calls destructors; free does not B. free calls destructors; delete does not C. They are identical D. delete is for arrays only Correct Answer: A Rationale: delete calls destructors; free only releases raw memory. 36. What is a memory leak? A. Memory that is allocated but never freed B. Memory that is freed twice C. Memory that is read before initialization D. Memory that is written out of bounds Correct Answer: A Rationale: A memory leak occurs when allocated memory is never deallocated, causing memory exhaustion.