MCQ 2024
MCQ 2024
#include <bits/stdc++.h>
class Shape{
public:
virtual void describe(){}
}
public:
Circle(){}
Circle(int ipR){
this->radius = ipR;
}
void describe(){
cout << "radius: " << this->radius << endl;
}
};
int main(){
Circle s1(25);
Circle s2(14);
Circle s3 = s1+s2;
s3.describe();
return 0;
}
A. radius: 39
B. 39
C. radius: 0
D. Compilation error
Question 2. What is the output of this program?
#include <bits/stdc++.h>
using namespace std;
class Shape{
public:
virtual void area(){};
};
public:
Square(int inputSide){
this->side = inputSide;
cout << "Square's generated!" << endl;
this->area();
}
void area(){
cout << side*side << endl;
}
~Square(){
cout << "This obj (Square) is deleted!" << endl;
}
};
public:
Rectangle(int w, int h=8){
this->width = w;
this->height = h;
cout << "Rectangle's generated!" << endl;
this->area();
}
void area(){
cout << width*height << endl;
}
~Rectangle(){
cout << "This obj (Rectangle) is deleted!" << endl;
}
};
~FourEdgeShape(){
cout << "This obj (FourEdgeShape) is deleted!" << endl;
}
};
int main(){
FourEdgeShape S1(80);
return 0;
}
A.
Rectangle's generated! C.
960 Rectangle's generated!
Square's generated! 640
6400 Square's generated!
This is a four-edge shape! 6400
This obj (Rectangle) is deleted! This is a four-edge shape!
This obj (Square) is deleted! This obj (FourEdgeShape) is deleted!
This obj (FourEdgeShape) is deleted! This obj (Square) is deleted!
This obj (Rectangle) is deleted!
B.
Rectangle's generated! D.
960 Rectangle's generated!
Square's generated! 640
6400 Square's generated!
This is a four-edge shape! 6400
This obj (FourEdgeShape) is deleted! This is a four-edge shape!
This obj (Square) is deleted! This obj (Rectangle) is deleted!
This obj (Rectangle) is deleted! This obj (Square) is deleted!
This obj (FourEdgeShape) is deleted!
Question 3. What is the output of this program?
#include <bits/stdc++.h>
using namespace std;
class Shape{
public:
virtual void area(){};
};
public:
Square(int inputSide){
this->side = inputSide;
this->area();
}
void area(){
cout << side*side << endl;
}
~Square(){
cout << "This obj (Square) is deleted!" << endl;
}
};
public:
Rectangle(int w, int h){
this->width = w;
this->height = h;
this->area();
}
void area(){
cout << width*height << endl;
}
~Rectangle(){
cout << "This obj (Rectangle) is deleted!" << endl;
}
};
class Things{
public:
void showArea(Shape* something){
something->area();
}
};
int main(){
Square Yard(20);
Rectangle Ruler(2,14);
Things myThings;
Things myThings2;
myThings.showArea(&Yard);
myThings2.showArea(&Ruler);
return 0;
}
A B C D
28 400 400 28
400 28 28 400
400 400 400 400
28 28 28 28
This obj (Rectangle) is This obj (Square) is This obj (Rectangle) is This obj (Square) is
deleted! deleted! deleted! deleted!
This obj (Square) is This obj (Rectangle) is This obj (Square) is This obj (Rectangle) is
deleted! deleted! deleted! deleted!
Question 4. What is the output of this program?
#include <bits/stdc++.h>
using namespace std;
#define pi M_PI
class Cone{
protected:
double radius;
double height;
public:
Cone(double r, double h=9){
this->radius = r;
this->height = h;
}
void area(){
cout << "The area (cm^3): " << setprecision(2) << fixed<<
(1.0/3.0) * pi * pow(radius, 2) * height << endl;
}
};
int main(){
Cone IceCream(3.75);
Cone Hat(20,30);
IceCream.area();
Hat.area();
return 0;
}
A B C D
The area (cm^3): The area (cm^3): The area (cm^3): Compilation error
132.54 132.54 3769.91
The area (cm^3): The area (cm^3): The area (cm^3):
12566.37 3769.91 132.54
Question 5. What is the output of this program?
#include <bits/stdc++.h>
class Shape{
public:
virtual void describe(){}
};
public:
Circle(){}
Circle(int ipR){
this->radius = ipR;
}
void describe(){
cout << "radius: " << this->radius << endl;
}
};
int main(){
Circle s1(25);
Circle s2(14);
Circle s3 = s1+s2;
s3.describe();
return 0;
}
A. radius: 39
B. 39
C. radius: 0
D. Compilation error
Question 6. What is the output of this program?
#include <iostream>
using namespace std;
class ThreeEdgeShape {
protected:
double side;
public:
ThreeEdgeShape(int side){
this->side = side;
}
void perimeter(){
cout << this->side*3+1 << endl;
}
};
int main() {
EquilateralTriangle Paper(5);
ThreeEdgeShape Euler(5);
Paper.perimeter();
Euler.perimeter();
return 0;
}
A B C D
15 15 16 16
15 16 15 16
Question 7. Fill the most suitable code in the comment section so that the code will execute the desired
output.
#include <bits/stdc++.h>
using namespace std;
class Person{
private:
string name;
int yearOfBirth;
public:
Person(){
this -> name = "Peter";
this -> yearOfBirth = 2000;
}
void describe(){
cout << "name: " << this->name << " and yearOfBirth: " <<
this->yearOfBirth << endl;
}
};
int main(){
Person Peter;
//the comment section
PeterFake.describe();
return 0;
}
Desired output:
#include <bits/stdc++.h>
using namespace std;
class Cat{
private:
string name;
int age;
public:
Cat(string name, int age){
this->name = name;
this->age = age;
}
void operator++(){
this->age+=5;
}
void operator++(int){
this->age+=2;
}
void describe(){
cout << "My cat is " << this->name << " and " << this->age <<
" years old. \n";
}
};
int main(){
Cat Bella("Bella",2);
//the comment section
return 0;
}
Desired output:
A B C D
Bella.describe();
Bella.describe(); Bella.describe(); Bella.describe(); Bella++;
++Bella; Bella++; ++Bella; Bella.describe();
++Bella;
Bella.describe(); Bella.describe(); Bella.describe(); Bella.describe();
++Bella; Bella++; Bella++;
#include <bits/stdc++.h>
using namespace std;
class Person{
private:
string name;
int yearOfBirth;
public:
Person(){
this -> name = "Peter";
this -> yearOfBirth = 2000;
}
void describe(){
cout << "name: " << this->name << " and yearOfBirth: " <<
this->yearOfBirth << endl;
}
};
int main(){
Person Peter;
Person PeterFake("Peter",2007);
Person Mike("Mike",2007);
Mike = Peter;
Mike.describe();
return 0;
}
A
B
C
D Compilation error
Question 10. Fill the most suitable code in the comment section so that the code will execute the
desired output.
#include <bits/stdc++.h>
using namespace std;
class Student{
private:
string name;
int credit;
public:
Student(){
this -> name = "Peter";
this -> credit = 2;
}
void operator++(int x) {
this->credit+=x;
}
void describe(){
cout << "Name: " << this->name << " - Total credit: " <<
this->credit << endl;
}
};
int main(){
Student Peter;
Student Mike("Mike",5);
Peter = Mike;
//the comment section
return 0;
}
Desired output:
A for(int i=1;i<=20;i++){
Mike.operator++(i);
}
Mike.describe();
B for(int i=1;i<=21;i++){
Peter.operator++(i);
}
Peter.describe();
C for(int i=1;i<=20;i++){
++Mike(i);
}
Mike.describe();
D for(int i=1;i<=21;i++){
++Mike(i);
}
Mike.describe();