C++
Start
Congratulations - you have completed C++.
You scored %%SCORE%% out of %%TOTAL%%.
Your performance has been rated as %%RATING%%
Your answers are highlighted below.
Question 1 |
What is the output of the code below?
Assume all necessary header files and namespaces are included.
class state{
public:
~state(){set();}
void set() { cout << "state ";}
};
class tired : public state{
public:
~tired(){set();}
void set() { cout << "tired ";}
};
int main(int argc){
state o1;
tired o2;
}
state tired tired | |
state tired state | |
tired state state | |
tired state tired |
Question 2 |
Pick the most suitable option for the code below. Assume inclusion of all necessary headers and namespaces
class BaseClass
{
private:
virtual inline void FunOne()
{
cout <<" BaseClass::FunOne ";
}
};
class DerivedClass : public BaseClass
{
public:
void FunOne()
{
cout <<" DerivedClass::FunOne ";
}
};
int main()
{
BaseClass *pObj = new DerivedClass();
pObj->FunOne();
delete pObj;
return 0;
}
Compilation error: cannot access private member of BaseClass | |
DerivedClass::FunOne | |
Compilation error: virtual member of BaseClass cannot be private | |
BaseClass::FunOne |
Question 3 |
What is the output of the code shown below? Assume inclusion of all necessary headers and namespaces
class B {
public:
int f(int i) { cout << "int"; return i+1; }
};
class D : public B {
public:
double f(double d) { cout << "double"; return d+1.3; }
};
int main(){
D* pd = new D;
pd->f(2);
pd->f(2.3);
}
intdouble | |
doubleint | |
doubledouble | |
intint |
Question 4 |
In the code below,Which of the following statements will be ill-formed?
struct A{
explicit A(int x){}
operator int(){
int x = 0;
return x;
}
};
int main(){
A a1(2); // Line 1
A a2 = 3; // Line 2
A a3 = 2.21; // Line 3
}
None of the above | |
Only Line 2 | |
Both Line 2 and Line 3 | |
Only Line 3 |
Question 5 |
The C++ standard states:
"An lvalue of a non-function, non-array type T can be converted to an rvalue….
If T is a non-class type, the type of the rvalue is the cv-unqualified version of T. Otherwise, the type of the rvalue is T"
Consider the code below
struct A{
int x;
A(int i) : x(i) { }
operator int(){return x;}
};
int main(){
int x;
const int y = 2;
A a(1);
const A ac(2);
x = y; // Line1
x = ac; // Line2
y = ac; // Line3
}
Which of the above statements are illegal with reference to the quote from the standard?
Line1
| |
Only Line2
| |
None of the above | |
Line2 and Line3
|
Question 6 |
Predict the output of the code shown below.
struct A{
operator int(){cout << "1"; return 0;}
operator unsigned int(){cout << "2"; return 0;}
};
int main(){
int x = 2, y = 3;
unsigned int z = 2;
x = (x < y) ? A() : y+z;
}
This code is ill-formed: ambiguous function call
| |
2 | |
1 | |
This code is ill-formed: syntax error in conditonal statement |
Question 7 |
What is the last digit printed in the output of the code shown below?
Assume inclusion of all necessary headers and standard namespaces
struct A {
A(int x = 0) : mx(x) { }
A& operator=(int x) { cout << "1"; mx = x; return *this; }
A& operator=(long l) { cout << "2"; mx = (int)l; return *this; }
A& operator=(bool b) { cout << "3"; mx = b; return *this; }
int mx;
};
struct B {
B(long l = 0L) : ml(l) { }
operator long() { return ml; }
long ml;
};
struct C {
C(bool b = false) : mb(b) { }
operator bool() { return mb; }
bool mb;
};
int main(){
C c;
A a;
B b;
a = c + b;
}
No output as the code does not build: no overloaded operator + in struct A | |
2 | |
1 | |
3 |
Question 8 |
What is the output of the following program #includeusing namespace std; class AB{ public: AB(double){} }; void func(int a, double b) {cout << 1;} void func(double a, int b) {cout << 2;} void func(AB a, int b) {cout << 3;} void func(int ...) {cout << 4;} void func(AB ...) {cout << 5;} int main(){ AB c(5.5); func(2.0, 2); func(c, 2.0); func(c, "Hello"); }
135 | |
155 | |
235 | |
255 |
Question 9 |
Consider the code shown below.
class B {};
class BB : public B{};
class BBB : public BB{};
void f(B *){}
void f(BB *){}
void f(void *){}
int main(){
B *pb;
BB* pbb;
___ *pbbb;
f(pb);
f(pbb);
f(pbbb);
}
If you are told that the call f(pbbb) calls function "void f(BB *)",
then which of the following types are applicable to pbbb other than BB*?
void*
| |
BBB*
| |
Either of a or b above
| |
None of the above |
Question 10 |
What is the output of the code below?
Assume the inclusion of all necessary headers and standard namespaces.
void Fn(const char *a){
cout << "Fn1 ";
}
template void Fn(const T a){
cout << "Fn2 ";
}
int main()
{
char *str = "Mango";
char str1[] = "Tree";
const char *str2 = str1;
Fn(str);
Fn(str1);
Fn(str2);
}
Will print "Fn1 Fn2 Fn2 "
| |
Will print "Fn2 Fn2 Fn2 " | |
Will print "Fn1 Fn1 Fn1 "
| |
Will print "Fn2 Fn2 Fn1 "
|
Question 11 |
void Fn(const int &x){
cout << "Fn1" << " ";
}
void Fn(int &x){
cout << "Fn2" << " ";
}
template void Fn(const T a){
cout << "Fn3" << " ";
}
void main(){
const int nMax = 255;
int nVar = 100;
Fn(nMax);
Fn(nVar);
}
Fn1 Fn3 | |
Fn1 Fn2
| |
Fn2 Fn2
| |
Fn1 Fn1
|
Question 12 |
What is your opinion on the code below?
int Fn(int a, int b){
return a;
}
double Fn(double a, double b){
return a;
}
template T Fn(T a, T b){
return a;
}
void main(){
double a = 2.0;
int d = 1;
double e = Fn(d, a);
}
Compilation Error - call to overloaded Fn is ambiguous | |
template function Fn will be called
| |
int Fn(int,int) will be called
| |
double Fn(double, double) will be called
|
Question 13 |
As responsible class designers, which of the following would you additionally implement in the RollNumbers class shown below with respect to the subscript overloaded operator?
class RollNumbers{
public:
RollNumbers(){ members[0] = ""; members[1] = "";}
string& operator[](const int index){return members[index];}
private:
string members[2];
};
No, the current class design is good enough. This class does not need any other design change | |
string& operator[](const int index) const {return members[index];} as a public member function
| |
Either of a or b
| |
const string& operator[](const int index) const {return members[index];} as a public member function
|
Question 14 |
For the code shown below, how many functions are part of the candidate function set for the addition statement in the main function?
class mydata{
int mx;
public:
mydata(int x = 0) {mx = x;}
mydata operator+(const mydata& rhs){
mydata temp(rhs);
temp.mx += mx;
return temp;
}
mydata operator+(int x){
mydata temp(*this);
temp.mx += x;
return temp;
}
operator int() const{
return mx;
}
};
int main(){
mydata d(3);
mydata r = d + 5;
}
1 | |
2 | |
3 | |
4 |
Question 15 |
Which of the following options is correct about the code shown below?
class AClass{
private:
AClass(const AClass&rhs){
mx = rhs.mx;
}
public:
AClass(int x) : mx(x){}
AClass& operator+=(const AClass& rhs){
mx += rhs.mx;
return *this;
}
AClass operator+(const AClass& rhs){
AClass temp(*this);
temp += rhs;
return temp;
}
private:
int mx;
};
int main(){
AClass a(3);
AClass b(5);
b = b + a; // Call 1
b += a; // Call 2
b + a; // Call 3
}
None of the calls will give compilation error. This code will compile and run fine | |
All of the calls Call 1, Call 2 and Call 3 will give compilation error
| |
Call 1 will give compilation error
| |
Both Call 1 and Call 3 will give compilation error
|
Question 16 |
Consider the set of statements given below for a overloaded operator member function of class X. Which of the following could be correct? X ret (*this); ++*this; return ret;
This code is a good candidate to appear in the member operator++ of class X | |
This code is a good candidate to appear in the member operator++(int) of class X | |
This code is illegal since you cannot declare 'ret' as illustrated | |
None of the above |
Question 17 |
What is the output of the following program
class CB{
private:
int m_nState;
public:
CB(int nState = 0): m_nState(nState){cout << 1;}
CB(const CB& obj){ m_nState = obj.GetState();}
~CB(){}
operator int(){return m_nState;}
int GetState() const {return m_nState;}
CB& operator= (const CB& obj){
m_nState = obj.GetState();
return *this;
}
};
int main(void){
CB obj1 = 2;
CB obj2;
obj2 = obj1 + 3;
}
111 | |
No output | |
1 | |
11 |
Question 18 |
If the following code has to compile without changing the main function, which of the following options is correct ?
Assume all header files and standard namespaces are included.
class CA{
private:
int m_Var;
public:
int GetState() const {return m_Var;}
void SetState(int nVar) {m_Var = nVar;}
bool operator > (const CA &obj2) {
return (m_Var > obj2.m_Var) ? true : false;
}
};
int main(void) {
CA obj1;
obj1.SetState(2);
if(obj1 > 3){cout << "In If block" << endl;}
}
No, it cannot be done. | |
Either of b or c | |
Provide an operator int()in the class that will return the value of m_Var | |
Provide a constructor in the class that takes a default int parameter |
Once you are finished, click the button below. Any items you have not completed will be marked incorrect.
Get Results
There are 18 questions to complete.
|
List |
Return
Shaded items are complete.
| 1 | 2 | 3 | 4 | 5 |
| 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | End |
Return
You have completed
questions
question
Your score is
Correct
Wrong
Partial-Credit
You have not finished your quiz. If you leave this page, your progress will be lost.
Correct Answer
You Selected
Not Attempted
Final Score on Quiz
Attempted Questions Correct
Attempted Questions Wrong
Questions Not Attempted
Total Questions on Quiz
Question Details
Results
Date
Score
Time allowed
minutes
seconds
Time used
Answer Choice(s) Selected
Question Text
All done
Need more practice!
Keep trying!
Not bad!
Good work!
Perfect!
$gf_formid_number