Question: 16 -
Which object will be created first?class student
{
int marks;
};
student s1, s2, s3;
-
s3 then s2 then s1
-
s1 then s2 then s3
-
all are created at same time
-
s2 then s3 then s1
Answer:
s1 then s2 then s3
Solution:
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.
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.
Question: 17 -
Which among the following helps to create a temporary instance?
-
Implicit call to a parameterized constructor
-
Implicit call to a default constructor
-
Explicit call to a copy constructor
-
Explicit call to a constructor
Answer:
Explicit call to a constructor
Solution:
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.
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.
Question: 18 -
Which of the following statement is correct about the program given below?#include<iostream.h>
class MasterQuiz
{
public:
MasterQuiz()
{
cout<< "Master";
}
~MasterQuiz()
{
cout<< "Quiz";
}
};
int main()
{
MasterQuiz objQuiz;
return 0;
}
-
The program will print the output MasterQuiz.
-
The program will print the output Master.
-
The program will report compile time error.
-
The program will print the output Quiz.
Answer:
The program will print the output MasterQuiz.
Solution not available.
Question: 19 -
For constructor overloading, each constructor must differ in ___________ and __________
-
Return type and definition
-
Return type and type of arguments
-
Number of arguments and type of arguments
-
Number of arguments and return type
Answer:
Number of arguments and type of arguments
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.
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.
Question: 20 -
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;
}
-
Only object s1 and s2 will be created
-
Program runs and all objects are created
-
Program will give compile time error
-
Object s3, syntax error
Answer:
Program runs and all objects are created
Solution:
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.
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.