A. Copy constructor
B. Default constructor
C. Dynamic constructor
D. Create constructor
A copy constructor is a special constructor in C++ for creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed (const or non-const), which might be followed by parameters of any type (all having default values).
A. Passing by value
B. Object initialization
C. Dynamic initialization
D. Copy initialization
In dynamic initialization, initial values are provided during runtime. Objects can also be initialized at the runtime. The benefit of dynamic initialization is that it allows different initialization modes using overloaded constructors.
A. Destructors do not accept any arguments, nor do they return values
B. Member functions can be called from within a destructor
C. Destructors can be inherited
D. Destructor functions are called automatically when an object is destroyed
A destructor is a special member function which is called when the lifetime of an object ends. The purpose of destructor is to free the resources that the object may have acquired during its lifetime. It never takes any argument, nor does it return any value.
A. an object is created of that class.
B. it is a global function.
C. it calls the copy constructor.
D. it can access the constructor.
A constructor is a member function of a class that is automatically called, when an object is created of that class.
A. Destructor A at work
Destructor B at work
Destructor C at work
Constructor A at work
Constructor B at work
Constructor C at work
B. Constructor A at work
Constructor B at work
Constructor C at work
Destructor A at work
Destructor B at work
Destructor C at work
C. Constructor A at work
Destructor A at work
Constructor B at work
Destructor B at work
Constructor C at work
Destructor C at work
D. Constructor A at work
Constructor B at work
Constructor C at work
Destructor C at work
Destructor B at work
Destructor A at work
In the given program, the constructor A is invoked first, followed by constructors of classes B and C. The destructors however are invoked in the reverse order of derivation. The destructor of class C, which is the most derived class, is invoked first and then destructors of B and A are invoked respectively.
Output:
A. A constructor has a return type
B. A constructor cannot contain a function call
C. A constructor has a void return type
D. A constructor has no return type
A constructor is a member method with the same name as its class. It is used to initialize the objects of that class type with a legal initial values. A constructor is not called like other functions so they don't have return types like other functions.
A. Destructor is missing
B. Constructor cannot assign values to data members
C. Object cannot be created in main() as constructor is private
D. The object in main() does not call any function
We generally want the constructor and destructor to be made public so that the class can be created. The constructor is called when an object is created but if the constructor is private, it cannot be called so the object cannot be constructed. This will cause the compiler to complain.
A. Two
B. Only one
C. Three
D. Unlimited
Default constructor is a constructor with no arguments. Only one constructor is required to initialize the object.
A. 3
B. 0
C. 10
D. 1
While returning from a function, destructor is the last method to be executed. The destructor for the object 'ob' is called after the value of 'x' is copied to the return value of the function. Thus, before destructor could change the value of 'x' to 10, the current value of 'x' gets copied and hence the output is 'x = 3'.
Output:
A. Inline constructor
B. Explicit call to the constructor
C. Implicit constructor
D. Default constructor
A default constructor (non-parameterized constructor) does not contain any arguments.
A. can accept a number of arguments
B. can accept only one argument
C. accepts no arguments
D. always accept reference to an object
Parameterized constructors allow us to create a new instance of a class while simultaneously passing arguments to the new instance.
A. private final Student(){}
B. void final Student(){}
C. Student(Student s){}
D. abstract Student(){}
A constructor cannot specify any return type not even void. A constructor cannot be final, static or abstract.
A. (&Student)
B. (Student&)
C. Student(&Student)
D. Student(Student&)
A copy constructor is used to copy an object to a newly created object. That is, it is used during initialization, including passing function arguments by value and not during ordinary assignment. A copy constructor for a class normally has this prototype:
class_name (const class_name&);
Therefore, the copy constructor for class Student would be Student(Student&)
A. constructors having same structure as that of the class.
B. constructors having same name as that of the class, but different parameters.
C. constructors having same type as that of the class.
D. constructors having same declaration as that of the class.
Overloading constructors, like overloading other function names, is just the practice of defining more than one constructor for a class, each taking a different set of parameters.
A. inheritance.
B. file handling.
C. pointers.
D. constructor.
Constructor in a class is a special member function that is called when an object is created.
A. beginning of the program.
B. end of the program.
C. execution of the program.
D. deletion of the program.
A destructor is called for a class object when that object passes out of scope or is explicitly deleted. So, it is called at the end of the program.
A. beginning of the program.
B. end of the program.
C. execution of the program.
D. deletion of the program.
A constructor is called at the beginning of the program when the object is created.
A. declaration of both is different.
B. objectives are different.
C. constructor cannot be defined in a program.
D. constructor does not have return type.
Constructor does not have a return type, whereas a normal function has a return type. If a function is not returning any value, then "void" must act as its return type.
A. public.
B. private.
C. protected.
D. friend.
Generally, a constructor should be defined under the public section of a class, so that its objects can be created in any function.
A. one.
B. five.
C. seven.
D. unlimited.
A class can define numerous constructors. It can be of the following types : a) default constructor, b) parameterized constructor c) copy constructor.
A. function inheritance.
B. data hiding.
C. data generalization.
D. function overloading.
Constructor overloading is similar to function overloading as it can also be loaded for various combinations of parameter types. The only difference is that an overloaded function can return a value but an overloaded constructor cannot return a value.
A. one.
B. two.
C. three.
D. infinite.
Parameterized constructors can pass the values in two ways: by implicit call and by explicit call.
A. copy constructor.
B. parameterized constructor.
C. default constructor.
D. destructor.
Parameterized constructors take values as arguments into the constructor function when the objects are created.
A. static initialization.
B. dynamic initialization.
C. public initialization.
D. private initialization.
In dynamic initialization, at the time of execution, the values are assigned to the variables.
A. overloading.
B. encapsulation.
C. data hiding.
D. abstraction.
Default arguments might not work for all possible combinations of arguments whereas a function may be overloaded for all possible combination of the arguments.
A. member functions only.
B. friend functions only.
C. member and friend function.
D. public function.
Constructor functions obey the usual access rules. That is, the private and protected constructors are available only for class members and friend functions, however public constructors are available for all the functions.
A. it can return any value.
B. an argument can be provided to it.
C. it cannot be inherited.
D. it is possible to print the address of a destructor.
Destructors cannot be inherited, though a derived class can call the base class constructor.
A. ~.
B. !.
C. ^.
D. $.
A destructor is a member function with the same name as that of its class and is preceded by a tilde(~).
A. destroyer.
B. destructor.
C. deinitializer.
D. constructor.
Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members, when the object is destroyed. A destructor is called for a class object, when that object passes out of scope or is explicitly deleted.
A. virtual constructor.
B. copy constructor.
C. default constructor.
D. same constructor.
A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object. This constructor takes a single argument ,i.e., a reference to the object to be copied.
A. helper function.
B. class.
C. friend function.
D. main function.
A constructor is a member function which is always declared with the same name as its class.
A. constructor object.
B. temporary object.
C. permanent object.
D. public object.
Temporary objects are unnamed objects created on the stack by the compiler. They are used during reference initialization and during evaluation of expressions including standard type conversions, argument passing, function returns, and evaluation of the throw expression.
A. when the object is not initialized with other object of the same type.
B. when a function returns an object.
C. when an object is passed without value to a function.
D. when the object is initialized with object of different type.
When a function returns an object, the copy constructor creates a temporary object to hold the return value of the function returning an object.
A. Member function that destroys the class
B. Member function that destroys constructors
C. Member function that destroys objects
D. Member function that destroys both constructors and objects
A destructor is a function with the same name as that of class name preceded by a tilde sign (~). It is a member function which is used to destroy the objects that have been created by a constructor.
A. The constructor will be FSH and destructor will be ~FSH
B. The constructor will be ~FSH and destructor will be FSH~
C. The constructor will be FSH( )and destructor will be ~FSH
D. The constructor will be FSH( ) and destructor will be ~FSH( )
A constructor is a member method with the same name as its class. A destructor is a function with the same name as that of class name preceded by a tilde sign (~). Thus, if a class is named FSH: the constructor will be FSH () and the destructor will be ~FSH()
A. When an object is passed as a parameter to a function by reference
B. When a function returns an object by value
C. When one object is assigned to another object
D. When a function does not return an object by value
The copy constructor is invoked when an object is passed by value to a function. It creates the copy that the function operates on.
A. Destructor called
B. Constructor called
C. Runtime error
D. Compile time error
Only one object t1 is constructed here. t2 is just a pointer variable and not an object.
A. Objects cannot be created for it
B. Objects will be created but not initialized
C. Compiler will be display an error message
D. Compiler creates one
Compiler generates an implicit default constructor when explicit default constructor is not specified.
A. Implicitly
B. Explicitly
C. Implicitly and explicitly
D. As a copy
A constructor can be called implicitly and explicitly.
A. Overloaded constructor
B. Copy constructor
C. Multiple constructors
D. Default constructor
A constructor with no arguments is called default constructor or non-parameterized constructor. They are automatically called when object of the class is created.
A. multiple
B. 1
C. 2
D. 3
There can be multiple constructors of the same class, provided they have different signatures.
A. Name of the object
B. Name of the class
C. Name of the public member functions
D. Name of the private data members
Constructor is a member method with the same name as its class. It is used to initialize the objects of that class type with a legal initial values.
A. Arguments
B. Only one argument
C. No arguments
D. Reference to an object
The constructor that takes arguments is called a parameterized constructor. A parameterized constructor contains arguments to assign the values to the class elements.
A. Objects will be created only in non member functions
B. Only global objects will be created
C. Objects will be created only in member functions
D. It will not be possible to create objects
When we want that a user of the class should not be able to create an object of a class but the member functions of the class should be able to create it, then the constructors should be made private.
A. Return only integer values
B. Return an object of the same class
C. Return any value
D. Never return a value
A constructor is a special function that can be used once and never returns a value. It is used to initialize the objects of that class type with a legal initial values.
A. Can take any number of arguments
B. Can take only one argument
C. Contains no arguments
D. Can take as many arguments as the objects
A default constructor (non - parameterized constructor) does not contain any arguments. It is called when an object is declared but is not initialized with any arguments.
A. creates class members
B. initializes objects
C. initializes classes
D. creates default constructs
constructor is a special member function which is used to initialize the member of the class.
A. the constructor will be FSH and destructor will be ~FSH
B. the constructor will be ~FSH and destructor will be FSH~
C. the constructor will be FSH( )and destructor will be ~FSH
D. the constructor will be FSH( ) and destructor will be ~FSH( )
Constructors and destructors are special member functions of classes that are used to construct and destroy class objects respectively. Construction may involve memory allocation and initialization for objects. Destruction may involve cleanup and deallocation of memory occupied by objects.
A. can take any number of arguments
B. can take only one argument
C. cannot take any arguments
D. can take as many arguments as the objects
A default constructor is a constructor that accepts no parameter.If a class has no explicit constructor defined, the compiler will supply a default constructor.
A. creates class members.
B. initializes objects.
C. initializes classes.
D. creates default constructs.
Constructor is a special member function used to initialize members of an object.
A. objects cannot be created for it.
B. objects will be created but not initialized.
C. the compiler will display an error message.
D. the compiler creates one.
This is called default constructor, which is automatically created by compiler.
A. implicitly.
B. explicitly.
C. implicitly or explicitly.
D. as a copy.
We call a constructor explicitly by giving different number of parameters, but otherwise if we provide no parameters or arguments, default constructor is automatically called.
A. passing by value
B. objects initialization
C. dynamic initialization
D. copy initialization
values could be assigned at compile-time as well as run-time. If assignment will be done at run-time then it is known as dynamic initialization.
A. class mt(a).
B. class mt(10).
C. Match mt(10).
D. Time mt(a).
Match is the name of the class and mt is the name of the object. Integer value i.e. 10 passed to the object.
A. constructor and destroyer.
B. constructor and destructor.
C. destructor and constructor.
D. public and private.
function1 is a constructor which initializes the class members when it is invoked. function2 is a destructor which destroys the object created by the destructor.
A. 5.
B. 9.
C. 14.
D. 19.
Here x = 9, y = 5*2 = 10, total = 9+ 10 = 19. So 19 will be displayed.
A. constructor.
B. destructor.
C. destroyer.
D. structure.
function1 is referred to as destructor. Destructors are usually used to deallocate memory as the object goes out of scope.
A. public; and int num().
B. n = 9; and void display().
C. class num and n = 9;.
D. public; and n = 9;.
After public, there must be colon not semicolon. Constructor does not have any return type.
A. sample obj(20,100).
B. sample obj(100,20).
C. class obj(20,100).
D. class obj(100,20).
An object can be declared with the same name as the class name. Here class name is sample and the values passed are 100 and 20.
A. weight() and ~weight().
B. x() and ~x().
C. y() and ~y().
D. x() and ~y().
constructor and destructor have the same name as that of a class. Destructor is always preceded by tilde(~) symbol.
A. constructor should not be defined publicly.
B. constructor should have arguments.
C. constructor should not have any return type.
D. constructor should be hidden.
double sample(); is not correct as constructor does not have any return type.
A. Time, distance, distance, Time, figure.
B. Distance, distance, Time, Time, figure.
C. Time, distance, Time, distance, figure.
D. Figure, distance, Time, distance, Time.
At first main() will be executed. Inside main(), Time is called then distance, then distance under class figure is called, then Time and finally figure.
A. use.
B. class.
C. scope.
D. call.
Destructor is always executed automatically whenever the class object goes out of scope.
A. parameterized constructor.
B. copy constructor.
C. default constructor.
D. mutual constructors.
Constructors that take arguments are known as parameterized constructors.
A. if the class designer wants to allow creation of objects using zero-argument, one-argument and two-argument constructors.
B. if the class designer does not want to allow creation of objects using zero-argument, one-argument and two-argument constructors.
C. if the class designer wants to allow allocation of memory using zero-argument, one-argument and two-argument constructors.
D. if the class designer does not want to allow creation of objects using zero-argument, one-argument and two-argument constructors.
If the class designer wants to allow creation of objects using zero-argument, one-argument and two-argument constructors, then the statement is true, otherwise false.
A. global.
B. local.
C. public.
D. private.
When we want that a user of the class should not be able to create an object of a class but the member functions of the class should be able to create it then the constructors should be made private.
A. ~weight( ) and weight( ).
B. weight( ) and ~weight( ).
C. #weight( ) and weight( ).
D. weight( ) and #weight( ).
class weight
{
int x, y;
public:
weight( ) { }
~weight( ) { }
};
A. class pt(a)
B. class pt(10)
C. Pilot pt(10)
D. num pt(a)
Pilot is the name of the class. pt is the name of the object and the integer value passed inside it is 10.
A. Interview In;
B. Interview In();
C. Interview (10);
D. Interview In(10);
Interview is the name of the class and In is the name of the object. Inside the brackets, integer value is passed.
A. constructor.
B. private member.
C. destructor.
D. public member.
function1 is destructor and it will get invoked as the scope of object gets over.
A. destructor.
B. destroyer.
C. constructor.
D. temporary object.
In object-oriented programming, a constructor is a special block of statements that initializes the values of members of a class. It always declared with the same name as the class is.
A. parameterized constructor.
B. copy constructor.
C. default constructor.
D. mutual constructors.
A constructors that accepts no parameter is called the default constructors. Here, Teacher() is a default constructor, as it accepts no parameters.
A. Earth, Universe, Universe, Earth, India.
B. Earth, India, Universe, Universe, Earth.
C. Earth, Universe, India, Universe, Earth.
D. Earth, Universe, Universe, India, Earth.
At first main() function will be executed. Inside main(),Earth is called then Universe, then Universe under class India is called, then Earth and finally India.
A. the class.
B. constructors.
C. objects.
D. pointers
A destructor is a method that is automatically invoked, when an object is destroyed. Its main purpose is to deallocate the memory space occupied by objects when it goes out of scope.
A. cannot take arguments
B. cannot have default values
C. can be inherited
D. cannot have a return type
Constructor is a function whose name is same as the object, with no return type (not even void).
A. objects will be created only in non member functions.
B. only global objects will be created.
C. objects will be created only in member functions.
D. it is not possible to create objects.
Generally, a constructor should be defined under the public section of a class, so that its object can be created in any function. If a constructor is defined privately then objects will only be created in member functions.
A. returns only integer values
B. returns an object of the same class
C. returns float value
D. never return a value
Constructor is a special member function that never return a value.
A. parameterized constructor
B. default constructor
C. empty constructor
D. null constructor
A constructor with default arguments is equivalent to a default constructor.
A. is equivalent to an overloaded constructor
B. is equivalent to a copy constructor
C. is equivalent to multiple constructors
D. is equivalent to a default constructor
A default constructor is a constructor that either has no parameters, or if it has parameters, all the parameters have default values.
A. objects will be created in non member functions
B. objects will be created only in friend functions
C. objects will be created only in member functions
D. it will not be possible to create objects
The constructor is usually called when the Object is instantiated/created.
The constructor is a special function of the class. Some of the features that differentiate it from others functions -
It is the one invoked automatically when the object is created.
It has the same name as the class.
It cannot return a value.
Destructors are special member functions that are invoked automatically when an object is to be destroyed. The name of the destructor is same as the class, preceded by a ~(tilde) character.
Initializing objects through the copy constructor is called copy initialization.
In the example, for a class ABC, the statement below creates object ob2 initialized with the object ob.
A temporary instance is the one that lives in the memory as long as it is being used or referenced in an expression and after this it dies.
A constructor that takes arguments is a parameterized constructor.
Example :
class ABC{
int a;
int b;
public:
ABC( int x, int y){a=x; b=y;} // parameterized constructor
……..
};
void main(){
ABC ob2(2,3); // parameterized constructor invoked
……..
A constructor that does not accept any argument is a default constructor.
Example: Default constructor for STUDENT class -
STUDENT( ) {rno=0;}
The constructor provides some valid initial values to the created object.
A constructor is a member function of a class that is invoked automatically when an object of that class is created. It has the same name as the class and initializes the object with legal initial values.
The compiler provides its own values, which are legal. By defining the constructor, we can provide values according to our requirement.
A function can create objects of a class if it can access the constructors.
An object that is existing must be scrapped off when it is no more needed. The task of scrapping off an object is carried out by a destructor. A destructor deinitializes an object and deallocates all allocated resources.
The copy constructor is called 8 times -
1. object a passed by value to func( )
2. object y initialized in func ( ) with x
3. object z initialized in func ( ) with y
4. object z returned
5. object b passed by value to func( )
6. object y initialized in func ( ) with x
7. object z initialized in func ( ) with y
In both the statements, an object, s1 is created. In statement 1, the constructor is called implicitly. In statement 2, the constructor is called explicitly, by name.
An object created in a program must be destroyed when it is no more needed. This task is done by the destructor. It deallocates the memory space and resources acquired by an object.
Dynamic initialization means that initial values for the object may be provided at runtime. In the example, values to be passed as arguments are received at run time.
class ABC { int a;
int b;
public:
ABC( int x, int y) { a=x; b=y; }
..
};
void main( ){
int x, y;
cout<<”Enter integer values for x and y “;
cin>>x>>y;
ABC ob ( x, y);
…
The order of invocation will be –
1. X :: X( ) for x1
2. Y :: Y( ) for y1
3. X :: X( ) for ox in z1
4. Y :: Y( ) for oy in z1
This is because, when an argument is passed by value, its copy is made. To make a copy of an object, the copy constructor is called. Thus, it calls itself again. In fact, it calls itself over and over again until the compiler, goes out of memory. So, the object is passed by reference which doesn't create copy.
A copy constructor is a constructor of the form classname( classname &). the compiler will use the copy constructor whenever you initialize an instance using values of another instance of same type.
For Example -
Sample s1; //default constructor
Sample s2=s1; //copy constructor
Overloaded function refer to the same function performing different activities depending on the values passed to it. Constructor too is a function, and it can be overloaded. Multiple constructors can be defined and different number and types of values can be sent for initialization. Example :
class A{
int a;
int b;
public:
A ( ) {a=0; b=0;} //default constructor
A( int x, int y){a=x;b=y;} //constructor with 2 arguments
A(int x){a=x; b=2*x;} //constructor with 1 arguments
};
void main(){
A ob; // will invoke the default constructor
A ob2(2,3); // constructor with two arguments invoked
A ob3(6); // constructor with one arguments invoked
Destructors too are special member functions. Their characteristics are –
1. They have the same name as the class, preceded by the ‘~’ character (tilde).
2. They are invoked automatically when objects are destroyed.
3. No return type is specified for destructors
4. Destructors obey the access rules.
5. They cannot be static.
6. They cannot be inherited.
7. Arguments cannot be passed to a destructor.