Object Oriented Programming using C++ Questions and Answers – Constructors

«
»

This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Constructors”.

1. Which among the following is called first, automatically, whenever an object is created?
a) Class
b) Constructor
c) New
d) Trigger
View Answer

Answer: b
Explanation: Constructors are the member functions which are called automatically whenever an object is created. It is a mandatory functions to be called for an object to be created as this helps in initializing the object to a legal initial value for the class.
advertisement

2. Which among the following is not a necessary condition for constructors?
a) Its name must be same as that of class
b) It must not have any return type
c) It must contain a definition body
d) It can contains arguments
View Answer

Answer: c
Explanation: Constructors are predefined implicitly, even if the programmer doesn’t define any of them. Even if the programmer declares a constructor, it’s not necessary that it must contain some definition.

3. Which among the following is correct?
a) class student{ public: int student(){} };
b) class student{ public: void student (){} };
c) class student{ public: student{}{} };
d) class student{ public: student(){} };
View Answer

Answer: d
Explanation: The constructors must not have any return type. Also, the body may or may not contain any body. Defining default constructor is optional, if you are not using any other constructor.
advertisement
advertisement

4. In which access should a constructor be defined, so that object of the class can be created in any function?
a) Public
b) Protected
c) Private
d) Any access specifier will work
View Answer

Answer: a
Explanation: Constructor function should be available to all the parts of program where the object is to be created. Hence it is advised to define it in public access, so that any other function is able to create objects.

5. How many types of constructors are available for use in general (with respect to parameters)?
a) 2
b) 3
c) 4
d) 5
View Answer

Answer: a
Explanation: Two types of constructors are defined generally, namely, default constructor and parameterized constructor. Default constructor is not necessary to be defined always.
advertisement

6. If a programmer defines a class and defines a default value parameterized constructor inside it.
He has not defined any default constructor. And then he try to create the object without passing arguments, which among the following will be correct?
a) It will not create the object (as parameterized constructor is used)
b) It will create the object (as the default arguments are passed)
c) It will not create the object (as the default constructor is not defined)
d) It will create the object (as at least some constructor is defined)
View Answer

Answer: b
Explanation: It will create the object without any problem, because the default arguments use the default value if no value is passed. Hence it is equal to default constructor with zero parameters. But it will not create the object if signature doesn’t match.

7. Default constructor must be defined, if parameterized constructor is defined and the object is to be created without arguments.
a) True
b) False
View Answer

Answer: a
Explanation: If the object is create without arguments and only parameterized constructors are used, compiler will give an error as there is no default constructor defined. And some constructor must be called so as to create an object in memory.
advertisement

8. If class C inherits class B. And B has inherited class A. Then while creating the object of class C, what will be the sequence of constructors getting called?
a) Constructor of C then B, finally of A
b) Constructor of A then C, finally of B
c) Constructor of C then A, finally B
d) Constructor of A then B, finally C
View Answer

Answer: d
Explanation: While creating the object of class C, its constructor would be called by default. But, if the class is inheriting some other class, firstly the parent class constructor will be called so that all the data is initialized that is being inherited.

9. In multiple inheritance, if class C inherits two classes A and B as follows, which class constructor will be called first?

advertisement
class A{ };
class B{ };
class C: public A, public B{  };

a) A()
b) B()
c) C()
d) Can’t be determined
View Answer

Answer: a
Explanation: Constructor of class A will be called first. This is because the constructors in multiple inheritance are called in the sequence in which they are written to be inherited. Here A is written first, hence it is called first.

10. Which among the following is true for copy constructor?
a) The argument object is passed by reference
b) It can be defined with zero arguments
c) Used when an object is passed by value to a function
d) Used when a function returns an object
View Answer

Answer: b
Explanation: It can’t be defined with zero number of arguments. This is because to copy one object to another, the object must be mentioned so that compiler can take values from that object.

11. If the object is passed by value to a copy constructor?
a) Only public members will be accessible to be copied
b) That will work normally
c) Compiler will give out of memory error
d) Data stored in data members won’t be accessible
View Answer

Answer: c
Explanation: Compiler runs out of memory. This is because while passing the argument by value, a constructor of the object will be called. That in turn called another object constructor for values, and this goes on. This is like a constructor call to itself, and this goes on infinite times, hence it must be passed by reference, so that the constructor is not called.

12. Which object will be created first?

class student 
{  
    int marks;
};
student s1, s2, s3;

a) s1 then s2 then s3
b) s3 then s2 then s1
c) s2 then s3 then s1
d) all are created at same time
View Answer

Answer: a
Explanation: The objects are created in the sequence of how they are written. This happens because the constructors are called in the sequence of how the objects are mentioned. This is done in sequence.

13. Which among the following helps to create a temporary instance?
a) Implicit call to a default constructor
b) Explicit call to a copy constructor
c) Implicit call to a parameterized constructor
d) Explicit call to a constructor
View Answer

Answer: d
Explanation: Explicit call to a constructor can let you create a temporary instance. This is because the temporary instances doesn’t have any name. Those are deleted from memory as soon as their reference is removed.

14. Which among the following is correct for the class defined below?

class student
{
    int marks;
    public: student(){}
    student(int x)
    { 
         marks=x; 
    }
};
main()
{
    student s1(100);
    student s2();
    student s3=100;
    return 0;
}

a) Object s3, syntax error
b) Only object s1 and s2 will be created
c) Program runs and all objects are created
d) Program will give compile time error
View Answer

Answer: c
Explanation: It is a special case of constructor with only 1 argument. While calling a constructor with one argument, you are actually implicitly creating a conversion from the argument type to the type of class. Hence you can directly specify the value of that one argument with assignment operator.

15. For constructor overloading, each constructor must differ in ___________ and __________
a) Number of arguments and type of arguments
b) Number of arguments and return type
c) Return type and type of arguments
d) Return type and definition
View Answer

Answer: a
Explanation: Each constructor must differ in the number of arguments it accepts and the type of arguments. This actually defines the constructor signature. This helps to remove the ambiguity and define a unique constructor as required.

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter