Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ MCQ : Q01. The address of a variable temp of type int is (A) *temp (B) &temp

ID: 3592416 • Letter: C

Question

C++ MCQ :

Q01. The address of a variable temp of type int is (A) *temp (B) &temp (C) int& temp (D) int temp&

Q02. If a class C is derived from class B, which is derived from class A, all through public inheritance, then a class C member function can access (A) protected and public data only in C and B. (B) protected and public data only in C. (C) private data in A and B. (D) protected data in A and B.

Q03. Usually a pure virtual function (A) has a complete function body. (B) will never be called. (C) will be called only to delete an object. (D) is defined only in derived class.

Q04. Run-Time Polymorphism is achieved by ______ (A) friend function (B) virtual function (C) operator overloading (D) function overloading

Q05. The keyword friend does not appear in (A) the class allowing access to another class. (B) the class desiring access to another class. (C) the private section of a class. (D) the public section of a class.

Q06. Use of virtual functions implies (A) overloading. (B) overriding. (C) static binding. (D) dynamic binding.

Q07. Data members which are static (A) cannot be assigned a value (B) can only be used in static functions (C) cannot be defined in a Union (D) can be accessed outside the class

Q08. Declaration of a pointer reserves memory space (A) for the object. (B) for the pointer. (C) both for the object and the pointer. (D) none of these.

Q09. Consider the following statements char *ptr; ptr = “hello”; cout << *ptr; What will be printed? (A) first letter (B) entire string (C) it is a syntax error (D) last letter

Q10. In which case is it mandatory to provide a destructor in a class? (A) Almost in every class (B) Class for which two or more than two objects will be created (C) Class for which copy constructor is defined (D) Class whose objects will be created dynamically

Q11. The members of a class, by default, are (A) public (B) protected (C) private (D) mandatory to specify

Q12. Which of the statements is true in a protected derivation of a derived class from a base class? (A) Private members of the base class become protected members of the derived class (B) Protected members of the base class become public members of the derived class (C) Public members of the base class become protected members of the derived class (D) Protected derivation does not affect private and protected members of the derived class.

Q13. A pointer to the base class can hold address of (A) only base class object (B) only derived class object (C) base class object as well as derived class object (D) None of the above

Q14. If there is a pointer p to object of a base class and it contains the address of an object of a derived class and both classes contain a virtual member function abc(), then the statement p->abc(); will cause the version of abc() in the __________class to be executed. (A) Base Class (B) Derived class (C) Produces compile time error (D) Produces runtime error

Q15. A static function (A) should be called when an object is destroyed. (B) is closely connected with and individual object of a class. (C) can be called using the class name and function name. (D) is used when a dummy object must be created.

Q16. What is the output of the following program? #include void main() { char *ptr=“abcd” char ch; ch = ++*ptr++; cout<

Q17. A copy constructor is a member function that takes (A) no arguments (B) 1 argument (C) 2 arguments (D) an arbitrary no. of arguments

Q18. If we store the address of a derived class object into a variable whose type is a pointer to the base class, then the object, when accessed using this pointer.. (A) Continues to act like a derived class object. (B) Continues to act like a derived class object if virtual functions are called. (C) Acts like a base class object. (D) Acts like a base class, if virtual functions are called.

Q19. If a base class destructor is not virtual, then (A) It cannot have a function body. (B) It cannot be called. (C) It cannot be called when accessed from pointer. (D) Destructor in derived class cannot be called when accessed through a pointer to the base class.

Q20. Member functions, when defined within the class specification: (A) are always inline. (B) are not inline. (C) are inline by default, unless they are too big or too complicated. (D) are not inline by default.

Q21. Cross-out all the lines that will not compile in the main function of the following program:

#include
class A{ public: A(){} virtual int output() = 0;
private:
  int i; };

class B: public A{ private: int j; };

class C{ public: int f(int a){return x*a;} protected:
  void setX(int a){x=a;}
  int getX(){return x;}
private:
  int x; };

class D: public C{ private: int z; };

int main(){
A objA;
B objB;
  C objC;
D objD;  
C.setX(2);
  cout<   D.setX(1);  
D.f(3);
  return 0; }

Explanation / Answer

Solutions :

1. (B) &temp
Explanation

A variable is a memory location and it's address can be accessed using ampersand (&) operator which is an address in memory.
For example : Address of int temp will be &temp , Address of char temp will be &temp

2. (D) protected data in A and B
Explanation

C++ has 3 access specifiers :
Public : All the class members declared under public will be available to everyone
Private : No one can access the class members declared private outside that class.
Protected : It is similar to private but with one difference that protected data can be accessed by subclass.
Looking at the definition of protected it is clear that class c can access protected data in A and B.

3.(D) is defined only in derived class
Explanation

A pure virtual function is required to be implemented by the derived class as the classes containing pure virtual functions are termed
as abstract and abstract classes are used as base class.

4.(B) virtual function
Explanation

Run time polymorphism is deciding which object's method should be invoked at Runtime instead of compile time.
Virtual functions are expected to be overridden in the derived class. By using Virtual function we can call functions of a derived class using pointer of the base class which is run time polymorphism.

5.(C) the private section of a class
Explanation

A friend function will be friendly with a class even though it is not a member of that class and can access the private members of the class. It is used to give access to other class and is not a member of a class.

6. D) dynamic binding
Explanation

It can be seen in the explanation of question 4. (Deciding which object's method should be invoked at Runtime instead of compile time which is dynamic binding)

7.(B) can only be used in static functions
Explanation

For static memmbers only single copy of the variable is created for all objects.
And by definition a static member function can access the static data members only.

8.(B) for the pointer
Explanation

Declaration of a pointer reserves memory space for the pointer as we are just declaring a pointer not intializing any object.

9.(A) first letter
Explanation

char *ptr;
ptr = “hello”;
cout << *ptr -> ptr points to the first char of the string , therefore *ptr will print value of the first char.

10.(D) Class whose objects will be created dynamically
Explanation

When objects are created dynamically then the default constructor just makes the pointer itself go away without clearing the memory it is pointing to. So it is mandatory to have a destructor when we create objects dynamically.


11.(C) private
Explanation

In C++ by default members are private

12. (C) Public members of the base class become protected members of the derived class
Explanation

When inheritance is protected :
-Private members of base class are not accessible by derived class.
-Protected members of base class remain protected in derived class.
-Public members of base class become protected in derived class.

13.(C) base class object as well as derived class object
Explanation

A derived class includes everything that is in the base class. But a base class does not include everything that is in the derived class , so a base class object can point to both derived as well as base class object but not vise versa.

14.(B) Derived class
Explanation

A virtual function which is declared in base class is overriden by derived class.When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.

15. (C) can be called using the class name and function name
Explanation

Just like the static data members or static variables inside the class, static member functions also does not depend on object of class.
It can be called using the class name and function name.

16. Question not complete

17.(B) 1 argument
Explanation

In C++ copy constructor is a special constructor for creating a new object as a copy of an existing object.
So it takes one argument as the object of same class.

18.(B) Continues to act like a derived class object if virtual functions are called
Explanation

A virtual function which is declared in base class is overriden by derived class.When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.

19.(D) Destructor in derived class cannot be called when accessed through a pointer to the base class
Explanation

As explained previously ,when you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function.
So if base class destructor is not virtual then destructor in derived class cannot be called when accessed through a pointer to the base class.

20.(A) are always inline
Explanation

class Car {
public:
string type;
int getSpeed(void); ---> Member function are always inline in class specification
};

21.
class A{ public: A(){} virtual int output() = 0;
private:
int i; };

class B: public A{ private: int j; };

class C{ public: int f(int a){return x*a;} protected:
void setX(int a){x=a;}
int getX(){return x;}
private:
int x; };

class D: public C{ private: int z; };

int main(){
A objA; -------> A is abstract class so it will object can't be created
B objB; -------> B is abstract class so it will object can't be created
C objC;
D objD;  
C.setX(2); -----> setX is protected and is not static
cout< D.setX(1); ----> same as above , It doesn't return any value
D.f(3);
return 0; }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote