Test Series - oops

Test Number 10/78

Q: Which among the following is called first, automatically, whenever an object is created?
A. Class
B. Constructor
C. New
D. Trigger
Solution: 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.
Q: 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
Solution: 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.
Q: 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(){} };
Solution: 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.
Q: 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
Solution: 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.
Q: How many types of constructors are available for use in general (with respect to parameters)?
A. 2
B. 3
C. 4
D. 5
Solution: Two types of constructors are defined generally, namely, default constructor and parameterized constructor. Default constructor is not necessary to be defined always.
Q: 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)
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.

You Have Score    /10