Object Oriented Programming MCQs with Answers
Practice important Object Oriented Programming MCQs with answers and explanations.
Chapter: 24
Q401:
Which casting operator should be used to check the validity of a downcast at runtime?
A. static_cast
B. dynamic_cast
C. reinterpret_cast
D. const_cast
Correct Answer:
B
Chapter: 24
Q402:
In the provided example, which method is used to cast 'this' pointer to base class reference implicitly?
A. static_cast
B. dynamic_cast
C. C-style cast (Person)*this
D. const_cast
Correct Answer:
A
Chapter: 24
Q403:
What is the main difference between copy constructor and assignment operator?
A. Copy constructor initializes new objects; assignment operator copies values to existing objects
B. Both do the same thing
C. Assignment operator initializes new objects; copy constructor copies values
D. There is no difference
Correct Answer:
A
Chapter: 24
Q404:
When overloading the assignment operator in derived classes, why is it necessary to call the base class assignment operator?
A. To ensure base class members are properly copied
B. It is not necessary
C. To improve performance
D. To prevent compilation errors
Correct Answer:
A
Chapter: 24
Q405:
What does the reinterpret_cast operator do in C++?
A. Converts between related types safely
B. Converts between unrelated types without safety checks
C. Removes constness from variables
D. Performs runtime type checking
Correct Answer:
B
Chapter: 24
Q406:
In the context of inheritance, what is 'downcasting'?
A. Casting from base class to derived class
B. Casting from derived class to base class
C. Casting between unrelated classes
D. Casting to void pointers
Correct Answer:
A
Chapter: 24
Q407:
Which casting operator would you use to convert a const int* to an int*?
A. static_cast
B. const_cast
C. dynamic_cast
D. reinterpret_cast
Correct Answer:
B
Chapter: 24
Q408:
What is the result of this code snippet? Person* p = new Student();
A. Compilation error
B. p points to a Person object
C. p points to a Student object treated as Person
D. Runtime error
Correct Answer:
C
Chapter: 24
Q409:
In object-oriented programming, which principle is violated by using protected members extensively?
A. Encapsulation
B. Inheritance
C. Polymorphism
D. Abstraction
Correct Answer:
A
Chapter: 24
Q410:
What does the following code do? Person* p = static_cast<Person*>(&studentObj);
A. Upcasts studentObj to Person pointer
B. Downcasts studentObj to Person pointer
C. Causes a compile-time error
D. Performs runtime type check
Correct Answer:
A
Chapter: 24
Q411:
Which operator is used in C++ for performing explicit type conversions in the old C-style?
A. static_cast
B. (type)expression
C. dynamic_cast
D. reinterpret_cast
Correct Answer:
B
Chapter: 25
Q412:
What does function overriding allow in a derived class?
A. Changing the return type
B. Changing the parameters
C. Overriding the base class method
D. All of the above
Correct Answer:
C
Chapter: 25
Q413:
Function overriding occurs in which scope?
A. Within the same class
B. Between parent and child classes
C. Between functions with different signatures
D. Within unrelated classes
Correct Answer:
B
Chapter: 25
Q414:
Which error occurs when two member functions with the same prototype are declared within the same class?
A. Syntax error
B. Runtime error
C. Duplicate declaration error
D. Logic error
Correct Answer:
C
Chapter: 25
Q415:
In function overloading, what must differ between overloaded functions?
A. Function names
B. Number of parameters
C. Function scope
D. Return types
Correct Answer:
B
Chapter: 25
Q416:
How can a derived class call a base class's overridden method?
A. By using baseClass::methodName()
B. By directly calling methodName()
C. By creating a new instance of the base class
D. By overriding the base class's method
Correct Answer:
A
Chapter: 25
Q417:
What output will the following code produce? Student a(“Ahmad”, “Computer Science”); a.Print();
A. Name: Ahmad, Major: Computer Science
B. Name: Ahmad
C. Major: Computer Science
D. No output due to error
Correct Answer:
A
Chapter: 25
Q418:
What mistake can lead to recursive calls in the overridden method in a derived class?
A. Forgetting to call the base class method
B. Re-declaring variables
C. Incorrect method signature
D. Incorrect return type
Correct Answer:
A
Chapter: 25
Q419:
What will be the output if the Person::Print() method is called from the Student::Print() method?
A. Name: Ahmad
B. Major: Computer Science
C. Name: Ahmad, Major: Computer Science
D. No output
Correct Answer:
C
Chapter: 25
Q420:
What is the static type of a pointer in C++?
A. The type it points to
B. The type of the object it was created from
C. The base class type
D. The derived class type
Correct Answer:
A