Polymorphism

  1. A pointer is
    • the data type of an address variable
    • a variable for storing addresses
    • a special type of integer variable
    • a special type of operator
  2. The declaration statement int* p1, p2 ;
    • declares two pointer variables p1 and p2
    • declares two integer variables p1 and p2
    • declares p1 as a pointer variable and p2 as an integer variable
    • is illegal because of blank space between * and p1
  3. The statement int * p ; can be interpreted as
    • the variable whose address is stored in p is an integer
    • the variable pointed to by p is an integer
    • p points to an integer
    • All the above
  4. For the statement p = *m; which of the following statements is TRUE?
    • The value of m is assigned to p
    • The value of the variable pointed to by m is assigned to p
    • p is a pointer variable
    • Address of m is assigned to p
  5. Identify the error in the following code segment
    1. int *ptr, m = 100;    // line 1
    2. cout << *ptr;  //line 2
      • Declaring ptr and m in one line is wrong
      • In line 2, *ptr should be written as &ptr
      • The address of m should be assigned to ptr before it is accessed
      • No error
  6. Compile-time polymorphism is accomplished by using
    • function overloading
    • Operator overloading
    • a and b
    • neither a nor b
  7. The mechanism of using virtual functions to achieve polymorphism is known as
    • run-time polymorphism
    • dynamic binding
    • late binding
    • all the above
  8. consider the following class definition: 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 ( )?
    1. class Test
    2. {
    3. public : void show (void) {cout <<”Test”;}
    4. };
      • x.show ( ) ;
      • ptr -> show ( ) ;
      • (*ptr). show ( );
      • All of the above
  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
    • x = 100;
    • this ->x = 100;
    • (*this).x = 100;
    • all the above
  10. The statement return * this ; inside a member function of a class returns ?
    • a copy of the object that invoked the function
    • address of the member function
    • address of this pointer
    • None of the above
  11. Which of the following statements is FALSE?
    • Address of a derived class object can be assigned to a base class pointer
    • Treating the base class object as a derived class object is wrong
    • Address of a base class object can be assigned to a derived class pointer
    • A base class pointer cannot be used to access all the members of the derived class
  12. In C++, virtual functions are used to
    • create functions that can never be accessed
    • to make a base class abstract
    • to create an array of base class pointers that can point to different functions
    • enable the use of the same function call to invoke functions from different classes.
  13. Which of the following statements is TRUE about virtual functions?
    • A constructor cannot be a virtual function
    • A virtual function in the base class must be defined, even though it may not be used
    • They cannot be declared static
    • All the above
  14. A pure virtual function is a virtual function that
    • makes the class to be abstract
    • is used in derived classes
    • is used to transform the base class into a virtual base class
    • takes no argument and returns nothing
  15. Which of the following statements is TRUE?
    • All virtual functions in an abstract base class must be declared as pure virtual functions
    • A class is made abstract by declaring it virtual
    • A derived class cannot contain a pure virtual function
    • If a base class declares a pure virtual function, all derived classes must define the function.

Answers:

  1. a variable for storing addresses
  2. declares p1 as a pointer variable and p2 as an integer variable
  3. All the above
  4. The value of the variable pointed to by m is assigned to p
  5. The address of m should be assigned to ptr before it is accessed
  6. a and b
  7. all the above
  8. all the three
  9. all the above
  10. a copy of the object that invoked the function
  11. Treating the base class object as a derived class object is wrong
  12. enable the use of the same function call to invoke functions from different classes
  13. All the above
  14. makes the class to be abstract
  15. If a base class declares a pure virtual function, all derived classes must define the function

Related posts