C++ MCQ Questions - Classes & Objects
11. What is the output of this program?
Note:Includes all required header files
using namespace std;
class Empty {};
int main()
{
cout << sizeof(Empty);
return 0;
}
View Answer
12. What is the output of this program?
Note:Includes all required header files
using namespace std;
class Empty { };
class Derived: Empty { int a; };
int main()
{
cout << sizeof(Derived);
return 0;
}
View Answer
13. What is the output of this program?
Note:Includes all required header files
class Test
{
int x;
};
int main()
{
Test t;
cout << t.x;
return 0;
}
View Answer
14. Assume that an integer and a pointer each takes 4 bytes. Also, assume that there is no alignment in objects. Predict the output following program.
Note:Includes all required header files
using namespace std;
class Test
{
static int x;
int *ptr;
int y;
};
int main()
{
Test t;
cout << sizeof(t) << " ";
cout << sizeof(Test *);
}
View Answer
15. Which of the following is true about the following program
#include <iostream>
using namespace std;
class LFC
{
public:
int i;
void get();
};
void LFC::get()
{
std::cout << "Enter the value of i: ";
std::cin >> i;
}
LFC t;
int main()
{
LFC t;
t.get();
std::cout << "value of i in local t: "<<t.i<<'\n';
::t.get();
std::cout << "value of i in global t: "<<::t.i<<'\n';
return 0;
}
View Answer
16. What will be the output of this program?
Note:Includes all required header files
using namespace std;
class sample
{
int x;
}
int main()
{
sample obj;
obj.x=100;
cout<<"x="<< obj.x;
}
View Answer
17. What will be the output of this program?
Note:Includes all required header files
using namespace std;
//Empty class
class test
{
};
int main()
{
test testObj;
cout<<"size ="<< sizeof(testObj);
return 0;
}
View Answer
18. What will be the output of the following program?
#include <iostream>
using namespace std;
class LFC
{
public:
int x;
};
int main()
{
LFC *p = new LFC();
(*p).x = 5;
cout<< (*p).x << " " << p->x << " " ;
p->x = 10;
cout<< (*p).x << " " << p->x ;
return 0;
}
View Answer
19. Which of the following statements is correct when a class is inherited publicly?
View Answer
20. What does the cerr represent?
View Answer
Also check :