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

This set of Object Oriented Programming using C++ online quiz focuses on “Types of Constructors”.

1. How many types of constructors are available, in general, in any language?
a) 2
b) 3
c) 4
d) 5
View Answer

Answer: b
Explanation: There are 3 types of constructors in general, namely, default constructors, parameterized constructors and copy constructors. Default one is called whenever an object is created without arguments.

2. Choose the correct option for the following code.

class student
{
    int marks;
}
student s1;
student s2=2;

a) Object s1 should be passed with argument
b) Object s2 should not be declared
c) Object s2 will not be created, but program runs
d) Program gives compile time error
View Answer

Answer: d
Explanation: The object s2 can be assigned with one value only if a single argument constructor is defined in class, but here, it can’t be done as no constructor is defined. Hence every object must be declare or created without using arguments.
advertisement
advertisement

3. Which constructor is called while assigning some object with another?
a) Default
b) Parameterized
c) Copy
d) Direct assignment is used
View Answer

Answer: c
Explanation: Copy constructor is used while an object is assigned with another. This is mandatory since we can’t decide which member should be assigned to which member value. By using copy constructor, we can assign the values in required form.

4. It’s necessary to pass object by reference in copy constructor because ____________
a) Constructor is not called in pass by reference
b) Constructor is called in pass by reference only
c) It passes the address of new constructor to be created
d) It passes the address of new object to be created
View Answer

Answer: a
Explanation: Object must be passed by reference to copy constructor because constructor is not called in pass by reference. Otherwise, in pass by value, a temporary object will be created which in turn will try to call its constructor that is already being used. This results in creating infinite number of objects and hence memory shortage error will be shown.

5. Which specifier applies only to the constructors?
a) Public
b) Protected
c) Implicit
d) Explicit
View Answer

Answer: d
Explanation: The keyword explicit can be used while defining the constructor only. This is used to suppress the implicit call to the constructor. It ensures that the constructors are being called with the default syntax only (i.e. only by using object and constructor name).
advertisement

6. Which among the following is true?
a) Default constructor can’t be defined by the programmer
b) Default parameters constructor isn’t equivalent to the default constructor
c) Default constructor can be called explicitly
d) Default constructor is and always called implicitly only
View Answer

Answer: c
Explanation: Default constructors can be called explicitly anytime. They are specifically used to allocate memory space for the object in memory, in general. It is not necessary that these should always be called implicitly.

7. Which type of constructor can’t have a return type?
a) Default
b) Parameterized
c) Copy
d) Constructors don’t have a return type
View Answer

Answer: d
Explanation: Constructors don’t return any value. Those are special functions, whose return type is not defined, not even void. This is so because the constructors are meant to initialize the members of class and not to perform some task which can return some value to newly created object.
advertisement

8. Why do we use static constructors?
a) To initialize the static members of class
b) To initialize all the members with static value
c) To delete the static members when not required
d) To clear all the static members initialized values
View Answer

Answer: a
Explanation: Static constructors help in initializing the static members of the class. This is provided because the static members are not considered to be property of the object, rather they are considered as the property of class.

9. When and how many times a static constructor is called?
a) Created at time of object destruction
b) Called at first time when an object is created and only one time
c) Called at first time when an object is created and called with every new object creation
d) Called whenever an object go out of scope
View Answer

Answer: b
Explanation: Those are called at very first call of object creation. That is called only one time because the value of static members must be retained and continued from the time it gets created.

10. Which among the following is true for static constructor?
a) Static constructors are called with every new object
b) Static constructors are used initialize data members to zero always
c) Static constructors can’t be parameterized constructors
d) Static constructors can be used to initialize the non-static members also
View Answer

Answer: c
Explanation: Static constructors can’t be parameterized constructors. Those are used to initialize the value of static members only. And that must be a definite value. Accepting arguments may make it possible that static members loses their value with every new object being created.

11. Within a class, only one static constructor can be created.
a) True
b) False
View Answer

Answer: a
Explanation: Since the static constructors are can’t be parameterized, they can’t be overloaded. Having this case, only one constructor will be possible to be created in a local scope, because the signature will always be same and hence it will not be possible to overload static constructor.

12. Default constructor initializes all data members as ___________
a) All numeric member with some garbage values and string to random string
b) All numeric member with some garbage values and string to null
c) All numeric member with zero and strings to random value
d) All numeric member with zero and strings to null
View Answer

Answer: d
Explanation: Default constructor, which even the programmer doesn’t define, always initialize the values as zero if numeric and null if string. This is done so as to avoid the accidental values to change the conditional statements being used and similar conditions.

13. When is the static constructor called?
a) After the first instance is created
b) Before default constructor call of first instance
c) Before first instance is created
d) At time of creation of first instance
View Answer

Answer: c
Explanation: The static constructor is called before creation of the first instance of that class. This is done so that even the first instance can use the static value of the static members of the class and manipulate as required.

14. If constructors of a class are defined in private access, then __________
a) The class can’t be inherited
b) The class can be inherited
c) Instance can be created only in another class
d) Instance can be created anywhere in the program
View Answer

Answer: a
Explanation: If the constructors are defined in private access, then the class can’t be inherited by other classes. This is useful when the class contains static members only. The instances can never be created.

15. Which among the following is correct, based on the given code below?

class student
{ 
     int marks;
     public : student()
     {  
          cout<<”New student details can be added now”;
     }
};
student s1;

a) Cout can’t be used inside the constructor
b) Constructor must contain only initializations
c) This program works fine
d) This program produces errors
View Answer

Answer: c
Explanation: This program will work fine. This is because it is not mandatory that a constructor must contain only initialization only. If you want to perform a task on each instance being created, that code can be written inside the constructor.

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

To practice all areas of Object Oriented Programming using C++ for online Quizzes, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.