C++ MCQs with Explanation

POINTERS, VIRTUAL FUNCTIONS AND POLYMORPHISM

1. A pointer is

Explanation ≡ Correct answer: option b) a variable for storing addresses
In C++, a pointer is a variable that stores the memory address of another variable. It allows us to indirectly access and manipulate the data stored at that memory location.

2. The declaration statement
int* p1, p2 ;

Explanation ≡ Correct answer: option c) declares p1 as a pointer variable and p2 as an integer variable
In this declaration, p1 is declared as a pointer variable to int (int*), and p2 is declared as an integer variable. The * indicates that p1 is a pointer variable, and the absence of * for p2 indicates it is a regular integer variable.

3. The statement
int * p ;
can be interpreted as

Explanation ≡ Correct answer: option b) the variable pointed to by p is an integer
In this statement, int* p; declares a pointer variable p that points to an integer (int). The * indicates that p is a pointer, and it is designed to store the memory address of an integer variable.

4. For the statement
p = *m;
which of the following statements is TRUE?

Explanation ≡ Correct answer: option b) The value of the variable pointed to by m is assigned to p
In this statement, *m dereferences the pointer m and gives us the value stored at the memory address pointed to by m. This value is then assigned to the pointer p, which means p now points to the same value as m.

5. Identify the error in the following code segment

int *ptr, m = 100; // line 1 cout << *ptr; //line 2
Explanation ≡ Correct answer: option b) In line 2, *ptr should be written as &ptr
In line 1, the variable ptr is declared as a pointer to int, but it is not initialized. It contains a garbage value. When you try to print the value at the address pointed by ptr (i.e., *ptr) in line 2, it results in undefined behavior. To fix this, you need to assign the address of m to the pointer ptr before accessing its value. The correct code should be: int *ptr, m = 100; // line 1 ptr = &m; cout << *ptr; //line 2

6. Compile-time polymorphism is accomplished by using

Explanation ≡ Correct answer: options a) function overloading and b) Operator overloading
Compile-time polymorphism is achieved through function overloading and operator overloading. Function overloading allows defining multiple functions with the same name but different parameter lists, while operator overloading allows redefining the behavior of operators for user-defined data types.

7. The mechanism of using virtual functions to achieve polymorphism is known as

Explanation ≡ Correct answer: options a) run-time polymorphism, b) dynamic binding, and c) late binding
When virtual functions are used, the function to be executed is determined at runtime based on the actual type of the object (not the type of the pointer). This mechanism is known as run-time polymorphism. It enables dynamic binding of functions to objects, which means the function to be called is determined dynamically at runtime based on the actual object type. This mechanism is also known as late binding. So, all the above options are correct as they refer to different aspects of the same mechanism - using virtual functions to achieve polymorphism.

8. Consider the following class definition:

class Test { public : void show (void) {cout <<"Test";} };

If x is an object of Test and ptr is a pointer to x, then which one of the following statements can correctly invoke the function show ( )?

Explanation ≡ Correct answer: options a) x.show ( ), b) ptr -> show ( ), and c) (*ptr). show ( )
In C++, all the above options are valid and equivalent ways to invoke the function show() for an object x of class Test using a pointer ptr to x. a) x.show ( ) ; // Directly calling the function using the object b) ptr -> show ( ) ; // Calling the function through the pointer using the arrow operator c) (*ptr). show ( ); // Calling the function through the pointer using the dereference operator All these options will correctly invoke the show() function for the object x.

9. If x is a private data member of a class, then it is legal to assign a value to x inside a member function using

Explanation ≡ Correct answer: options b) this ->x = 100; and d) all the above
If x is a private data member of a class, you cannot assign a value to it directly from outside the class. However, you can access and modify private data members using member functions of the class. b) this -> x = 100; // Here, this is a pointer to the current object, and we can use it to access the private data member x and assign it a value. d) all the above; // This option is correct because both options b and c are valid and equivalent ways to access and modify the private data member x using member functions.

10. The statement
return * this ;
inside a member function of a class returns

Explanation ≡ Correct answer: option a) a copy of the object that invoked the function
The statement return *this; inside a member function of a class returns a copy of the object that invoked the function. The this pointer is a special pointer that points to the current object (i.e., the object that called the member function). Dereferencing the this pointer with *this gives us the current object, and using return will return a copy of that object.

11. Which of the following statements is FALSE?

Explanation ≡ Correct answer: option a) Address of a derived class object can be assigned to a base class pointer
The statement in option a) is TRUE. In C++, we can assign the address of a derived class object to a base class pointer. This is because a derived class object is a type of a base class object, and the base class pointer can point to any object of a derived class or any object of a class that is derived from the base class. Options b), c), and d) are all TRUE statements and correctly describe the behavior of inheritance in C++.

12. In C++, virtual functions are used to

Explanation ≡ Correct answer: options b) enable the use of the same function call to invoke functions from different classes, and c) make a base class abstract
In C++, virtual functions are used to achieve run-time polymorphism. When a function is declared as virtual in the base class, it allows the use of the same function call to invoke functions from different classes in the inheritance hierarchy. The correct function to be called is determined at runtime based on the actual type of the object. Additionally, when a function is declared as pure virtual (using "= 0") in the base class, it makes the class abstract, and objects of that class cannot be created. However, derived classes must provide definitions for the pure virtual functions; otherwise, they also become abstract. Options a) and d) are not correct descriptions of the use of virtual functions in C++.

13. Which of the following statements is TRUE about virtual functions?

Explanation ≡ Correct answer: options a) A constructor cannot be a virtual function, b) A virtual function in the base class must be defined, even though it may not be used, and c) They cannot be declared static
a) A constructor cannot be a virtual function: Constructors cannot be declared as virtual functions in C++. This is because constructors are responsible for initializing the object, and the virtual mechanism may not work as expected during object construction. b) A virtual function in the base class must be defined, even though it may not be used: If a function is declared as virtual in the base class, it must be defined in the base class, even if it is not used. This ensures that the derived classes have a valid function to override. c) They cannot be declared static: Virtual functions cannot be declared as static because static functions belong to the class itself and are not associated with any specific object. Virtual functions depend on the type of the object at runtime, so they cannot be static. Therefore, all the above options are correct statements about virtual functions.

14. A pure virtual function is a virtual function that

Explanation ≡ Correct answer: option a) makes the class abstract
A pure virtual function is a virtual function that is declared in the base class with "= 0" at the end of its declaration. It indicates that the function has no implementation in the base class and is meant to be overridden in derived classes. When a class has at least one pure virtual function, it becomes an abstract class. An abstract class cannot be instantiated, i.e., objects of an abstract class cannot be created. It is meant to serve as a base class for other classes and provides a common interface that derived classes must implement. Options b), c), and d) are not correct descriptions of a pure virtual function.

15. Which of the following statements is TRUE?

Explanation ≡ Correct answer: options a) All virtual functions in an abstract base class must be declared as pure virtual functions and c) If a base class declares a pure virtual function, all derived classes must define the function
a) All virtual functions in an abstract base class must be declared as pure virtual functions: When a base class is designed to be abstract (i.e., meant to serve as a base for other classes and not instantiated itself), all its virtual functions should be declared as pure virtual functions (functions with "= 0" at the end of their declaration). c) If a base class declares a pure virtual function, all derived classes must define the function: If a base class declares a pure virtual function, all the derived classes must provide definitions for that function. If a derived class fails to provide a definition for a pure virtual function, it also becomes abstract and cannot be instantiated. Options b) and d) are not correct statements about abstract classes and pure virtual functions.


Your Score is 10

THE STUDENT FRIENDLY BOOK TO LEARN C++. THE EXPLANATIONS ARE IN SIMPLE ENGLISH AND THEY PROVIDE EXERCISES TO MAKE YOUR LEARNING EXCELLENT.

Topic wise C++ MCQs Index ≡
Click on particular topic to visit that page
  1. Principles of Object-Oriented Programming 
  2. Beginning with C++
  3. Tokens, Expressions, and Control Structures
  4. Functions in C++
  5. Classes and Objects
  6. Constructors and Destructors
  7. Operator Overloading
  8. Inheritance
  9. Pointers, Virtual Functions, and Polymorphism
  10. Managing Console I/O Operations
  11. Working with Files
  12. Templates
  13. Exception Handling
  14. Standard Template Library
  15. Manipulating Strings
  16. New Features in ANSI C++ Standards
  17. Object-Oriented System Development

Post a Comment

0 Comments