0% found this document useful (0 votes)
3 views

MCQ 2024

mcqs questions on c++

Uploaded by

Bao Huynh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

MCQ 2024

mcqs questions on c++

Uploaded by

Bao Huynh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Question 1. What is the output of this program?

#include <bits/stdc++.h>

using namespace std;

class Shape{
public:
virtual void describe(){}
}

class Circle: public Shape{


private:
int radius=0;

public:
Circle(){}

Circle(int ipR){
this->radius = ipR;
}

Circle operator+(const Circle &obj){


Circle result(this->radius + obj->radius);
return result;
}

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(){};
};

class Square: public Shape{


protected:
int side;

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;
}

};

class Rectangle: public Shape{


protected:
int width;
int height;

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;
}
};

class FourEdgeShape: public Rectangle, public Square {


public:
FourEdgeShape(int side): Square(side), Rectangle(side,12){
this->side = side;
cout <<"This is a four-edge shape!\n";
}

~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(){};
};

class Square: public Shape{


protected:
int side;

public:
Square(int inputSide){
this->side = inputSide;
this->area();
}

void area(){
cout << side*side << endl;
}

~Square(){
cout << "This obj (Square) is deleted!" << endl;
}

};

class Rectangle: public Shape{


protected:
int width;
int height;

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>

using namespace std;

class Shape{
public:
virtual void describe(){}
};

class Circle: public Shape{


private:
int radius=0;

public:
Circle(){}

Circle(int ipR){
this->radius = ipR;
}

Circle operator+(const Circle &obj){


Circle result(this->radius + obj.radius);
return result;
}

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;
}

virtual void perimeter(){


cout << this->side*3 << endl;
}
};

class EquilateralTriangle: public ThreeEdgeShape {


protected:
double side;
public:
EquilateralTriangle(double side): ThreeEdgeShape(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;
}

Person(string name, int yob){


this -> name = name;
this -> yearOfBirth = yob;
}

void describe(){
cout << "name: " << this->name << " and yearOfBirth: " <<
this->yearOfBirth << endl;
}

};

int main(){
Person Peter;
//the comment section
PeterFake.describe();
return 0;
}

Desired output:

A Person PeterFake = Peter;


B Person PeterFake("Peter",2007);
C Person PeterFake(Peter,2007);
D Person PeterFake = Peter(2007);
Question 8. 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 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++;

Bella.describe(); Bella.describe(); Bella.describe();


Question 9. What is the output of this program?

#include <bits/stdc++.h>
using namespace std;

class Person{
private:
string name;
int yearOfBirth;

public:
Person(){
this -> name = "Peter";
this -> yearOfBirth = 2000;
}

Person(const Person& someone){


this -> name = someone.name;
this -> yearOfBirth = someone.yearOfBirth;
}

Person(string name, int yob){


this -> name = name;
this -> yearOfBirth = yob;
}

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;
}

Student(string name, int credit){


this -> name = name;
this -> credit = credit;
}

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();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy