Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Object-oriented programming (OOP) has been around for decades. If you have a basic understanding of C++ and are interested in leveling up your skills, this class will help you do just that.
Starting with an overview of the basics, you’ll dive into understanding the time-honored technique for implementing complex applications using user-defined classes. Followed up by discussing classes and objects, and then building up to the high-level topics including inheritance and polymorphism.
Throughout the course, you’ll be fully immersed in OOP for C++, with illustrations, exercises, quizzes, and hands-on challenges every step of the way. You’ll walk away with an understanding of classes and objects behavior and be able to easily create simple, efficient, reusable and secure code.
Q1. Which of the following represents correct syntax of a function call in C++?
Q2. The following declaration of an integer array is best explained by which of the given statement?
int arr[4][3];
Q3. A static integer array is a constant pointer in C++. Which of the following is the correct explanation of this statement?
Q4. What is the difference between a reference variable and a pointer in C++?
Q5. The following line of the code will generate a syntax error because:
static int temp;
Q1. The declaration of a function consists of the following parts:
Q2. The definition of a function is contained inside:
{ }
curly brackets[ ]
squared brackets( )
parenthesesQ3. What is the output of the following program?
void square(int num){
num = num * num;
return num;
}
int main(){
int x = 5;
square(5);
cout << x << endl;
}
Q4. A variable declared inside a function cannot be used outside the function’s scope.
Q5. What is the output of the follow code?
bool foo(int n){
if(n > 20){
return true;
}
else{
return false;
}
}
int main(){
bool b;
b = foo(20);
cout << b;
}
Answer:
int sumAllOdds(int arr[], int size) {
int sum = 0;
for(int i = 0 ; i < size; i++) {
if(arr[i] % 2 != 0) { // checks if number is odd
sum += arr[i];
}
}
return sum;
}
Answer:
int secondMinimum(int arr[], int size) {
int min = 214748364;
int secondmin = 214748364;
for (int i = 0; i < size; i++) {
if (arr[i] < min) {
secondmin = min;
min = arr[i];
}
else if (arr[i] < secondmin) {
secondmin = arr[i];
}
}
return secondmin;
}
Answer:
float squareSum(float num1, float num2, float num3) {
float sum = 0;
num1 = num1 * num1;
num2 = num2 * num2;
num3 = num3 * num3;
sum = num1 + num2 + num3;
return sum;
}
Answer:
int squareSum(int num1, int num2, int num3) {
int sum = 0;
num1 = num1 * num1;
num2 = num2 * num2;
num3 = num3 * num3;
sum = num1 + num2 + num3;
return sum;
}
Q1. Which operator can be used to dereference a pointer, p
?
Q2. Pointers allows us to point to objects in:
Q3. What will be the value of *p
at the end of this code?
int *p;
int var = 10;
p = &var;
*p = 15;
var = 20;
Q4. What will be the value of *x
and y
after the foo
function call in the code below?
void foo(int *p, int q){
*p = *p + q;
q = *p * q;
}
int main(){
int *x = new int(5);
int y = 7;
foo(x, y);
}
Q5. What command can be used to free dynamic memory reserved by a pointer?
Answer:
void halve(double* n){
*n = *n/2;
}
Answer:
void swapVals(int *p, int *q){
int temp = *p;
*p = *q;
*q = temp;
}
Q1. The attributes of a class can be divided into the following two parts:
Q2. What is the output of the code below?
class Score {
int num;
public:
Score(){
num = 0;
}
Score(int n){
num = n;
}
int getNum(){
return num;
}
};
int main() {
Score sc(5);
cout << sc.num;
}
Q3. The scope resolution operator can be used to define member functions outside the class definition in the following syntax:
returntype memberFunction::className
Q4. Data members are usually kept:
Q5. Member functions are usually kept:
Answer:
class Rectangle {
public:
float length, height;
Rectangle(float l, float h) {
length = l;
height = h;
}
float perimeter() {
return length*2 + height*2;
}
float area() {
return length*height;
}
};
Answer:
class Student
{
private:
float mark1;
float mark2;
string name;
public:
// Constructor with no arguments (default constructor)
Student() {
name = "";
mark1=0;
mark2=0;
}
// Constructor with three arguments
Student(string na, float ma1,float ma2) {
name=na;
mark1=ma1;
mark2=ma2;
}
int GetMarks(int marknumber) {
if(marknumber == 1){
return mark1;
}
else{
return mark2;
}
}
float calc_total() {
return (mark1+mark2);
}
};
Answer:
class calculator{
float num1, num2;
public:
calculator() {
num1 = 0;
num2 = 0;
}
int add(float n1, float n2){
num1 = n1;
num2 = n2;
return num1 + num2;
}
float subtract(float n1, float n2){
num1 = n1;
num2 = n2;
return num2 - num1;
}
float multiply(float n1, float n2){
num1 = n1;
num2 = n2;
return num1 * num2;
}
float divide(float n1, float n2){
num1 = n1;
num2 = n2;
return num2 / num1;
}
};
Answer:
#include <iostream>
#include <cmath>
using namespace std;
class Point {
// Private fields
private:
int x;
int y;
public:
// Default Constructor
Point() {
x = 0;
y = 0;
}
// Parameterized Constructor
Point(int x, int y) {
this->x = x;
this->y = y;
}
double distance() {
double distance = sqrt(x*x + y*y);
return distance;
}
double distance(int x2, int y2) {
double distance = sqrt(((x2-x)*(x2-x))+((y2-y)*(y2-y)));
return distance;
}
};
Q1. ______ is a set of statements that performs operations in a program.
Q2. What will be the output of the following code?
#include <iostream>
using namespace std;
int add(int a, int b)
{
return (a + b);
}
float add(float a, float b)
{
return (a + (-b));
}
int main()
{
int x1 = 2, y1 = 2;
float x2 = 4.0, y2 = 3.0;
cout << add(x1, y1)<< endl;
cout << add(x2, y2);
return 0;
}
Q3. How will you declare a pointer that holds an integer type?
Q4. What is the correct syntax for de-allocating the following array?
int *arr = new int[100];
Q5. What will be the output of the following code?
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 5, 10, 8, 4 };
int* p = (arr + 1);
cout << *p + 2 <<endl;
cout << *arr + 10;
return 0;
}
Q6. How many access modifiers are present in class?
Q7. What will happen when you run the following code?
class ExmpClass {
public:
int x;
private:
int y;
};
int main() {
ExmpClass obj;
obj.x = 25;
obj.y = 50;
}
Q8. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class Rectangle
{
int length, width;
public:
Rectangle(){
length = 0;
width = 0;
}
Rectangle(int l, int w){
length = l;
width = w;
}
int area ()
{
return (length * width);
}
};
int main ()
{
Rectangle rectangle(3,4);
rectangle.area();
cout << rectangle.area();
return 0;
}
Q9. Which of the following statements are wrong about a friend function of a class?
Q10. An object is an entity with some data and operations.
Q11. Pass-by-Value method passes the address of a variable as an argument instead of the variable itself.
Q12. The output of the code below will be 8.
#include <iostream>
using namespace std;
void cube(int *a){
if(a != NULL){
*a = (*a) * (*a) * (*a);
}
}
int main() {
int *p = new int(2);
cube(p);
cout << *p;
}
Q13. A C++ class holds only member functions.
Q14. Match the terms in the left column with their respective definitions on the right side:
Answers:
Q15. In this challenge, you need to write a function isPrime
that takes an integer as an argument and displays whether it’s prime or not. It should display yes
if the number is prime and no
otherwise.
Note: Remember a positive integer which is only divisible by 1 and itself is known as prime number. 0 and 1 are not prime numbers!
isPrime(10)
isPrime(3)
no
yes
Answer:
Q16. In this coding challenge, you need to define a Circle
class that has an int
type variable radius
, a default constructor that initializes the radius
to 0
, another constructor that initializes the value of radius
to a given a value that it takes as an argument, and one member function print_area
that prints the area of the circle.
If a circle object with a radius of 2 is created and the function print_area
is called:
Circle obj(2);
obj.print_area();
Then it should print the circle’s area which in this case would be 12.57.
12.57
Note: You can consider the value of pi to be 3.14.
Answer:
Q1. What are the two main components of data hiding?
Q2. A header file contains:
Q3. The point of data hiding is to reveal the relevant functions needed to interact with a class and keeping the inner implementation hidden.
Q4. Apart from header files, abstraction can be implemented using:
Q5. According to the conventions of data hiding, class member should be:
Q1. What will be the output of following piece of code?
class Base {
public:
Base(){
cout << "Base constructor!" << endl;
}
};
class Derived: public Base {
public:
Derived(){
cout << "Derived constructor!" << endl;
}
};
int main(){
Derived d;
}
Q2. Figure out the sequence in which destructors will be called:
class Base {
public:
~Base() {
cout << " Base destructor!" << endl;
}
};
class Derived: public Base {
public:
~Derived() {
cout << " Derived destructor!" << endl;
}
};
int main() {
Derived d;
}
Q3. What is the output of the following code?
class grandParent{
public:
void GPprint() {
cout << "Grandparent print" << endl;
}
};
class Parent: public grandParent {
public:
void Pprint() {
cout << "Parent print" << endl;
GPprint();
}
};
class Child : public Parent {
public:
void Cprint() {
cout << "Child print" << endl;
Pprint();
}
};
int main() {
Child c;
c.Cprint();
}
Q4. What will the following program display?
#include <iostream>
using namespace std;
class B {
public:
void disp() {
cout << "B";
}
};
class D : public B {
public:
void disp() {
cout << "D";
}
};
class DD : public D {
};
int main() {
DD dd;
dd.disp();
return 0;
}
Q5. What will be displayed by the following program when it is run?
#include <iostream>
using namespace std;
class B {
private:
int count = 0;
public:
void inc() {
cout << count++;
}
};
class D : public B {
public:
void inc() {
cout << count++;
}
};
int main() {
D d;
d.inc();
return 0;
}
Q6. What will be the output of the following program?
#include <iostream>
using namespace std;
class B {
public:
void greet(int c) {
cout << c;
}
void greet() {
cout << "Hello B";
}
};
class D : public B {
public:
void greet() {
cout << "Hello D";
}
};
int main() {
D d;
d.greet(3);
return 0;
}
Q7. What will be the output of the following program?
#include<iostream>
using namespace std;
class B
{
protected:
int count;
public:
B() {count = 0;}
};
class D1: public B
{
public:
int sep;
};
class D2: public B
{
public:
int set;
};
class DD: public D1, public D2
{
public:
void show() { cout << count; }
};
int main(void)
{
DD d;
d.show();
return 0;
}
Q8. What will be the output of the following program?
#include<iostream>
using namespace std;
class B
{
protected:
int count;
public:
B() {count = 0;}
int getCount() { return count; }
};
class D : protected B
{
public:
void disp() { cout << getCount(); }
};
int main(void)
{
D d;
d.disp();
return 0;
}
Q9. What will be the output of the following program?
#include<iostream>
using namespace std;
class B
{
private:
const int ID = 10;
protected:
int count;
public:
B() {count = 0;}
int getCount() { return count; }
};
class D : protected B
{
public:
void disp() { cout << getCount(); }
int getID() { return ID; }
};
int main(void)
{
D d;
d.getID();
return 0;
}
Q10. What will be the output of the following program?
#include<iostream>
using namespace std;
class B
{
private:
const int ID = 10;
protected:
int count;
public:
B() {count = 0;}
int getCount() { return count; }
};
class D : private B
{
public:
void disp() { cout << getCount(); }
int getID() { return ID; }
};
int main(void)
{
D d;
d.getID();
return 0;
}
Answer:
// Derived Class
class Car : public Vehicle {
string name; // Name of a Car
public:
Car() { // Default Constructor
name = "";
}
// This function sets the name of the car
void setDetails(string name) { // Setter Function
this->name = name;
}
// This function calls the Base class functions and appends the result with the input
string getDetails(string carName) {
string details = carName + ", " + getModel() + ", " + getSpeed(); // calling Base Class Function
return details;
}
};
Q1. What is the output of the following piece of code?
#include <iostream>
using namespace std;
class Base{
public:
virtual void print() {
cout <<" Base Print";
}
};
class Derived: public Base{
public:
void print() {
cout <<"Derived Print" ;
}
};
int main(){
Base * bp;
Derived obj;
bp = &obj;
bp->print();
}
Q2. Does the code run successfully?
#include<iostream>
using namespace std;
class Base{
public:
virtual void show() = 0;
};
int main(){
Base b;
Base *bp;
}
Q3. What does the following piece of code do?
#include <iostream>
using namespace std;
class Shape {
public:
virtual float getArea() = 0;
};
class Rectangle : public Shape {
private:
float width;
float height;
public:
Rectangle(float wid, float heigh) {
width = wid;
height = heigh;
}
float getArea(){
return width * height;
}
};
int main() {
Shape * shp = new Rectangle(4, 5);
float num = shp->getArea();
cout << "Area of rectagle is " << num << endl;
}
Q1. In a part-of relationship between two classes:
Q2. What kind of relationship does composition use?
Q3. In aggregation, a class can reference the object of another class using:
Q4. Two associated classes cannot exist independently.
Q5. What kind of relationship does association follow?
Q1. Which option correctly describes encapsulation?
Q2. Which option correctly describes abstraction?
Q3. What will be the output of the following code?
#include <iostream>
using namespace std;
class A {
public:
void print() { cout <<"Inside A"; }
};
class B : public A {
public:
void print() { cout <<"Inside B"; }
};
class C: public B { };
int main()
{
C c;
c.print();
return 0;
}
Q4. What will be the output of the following code?
#include <iostream>
using namespace std;
class A {
public:
A(){
cout << "A's constructor called" << endl;
}
~A() { cout << "A's destructor called" << endl; }
};
class B : public A {
public:
B(){
cout << "B's constructor called" << endl;
}
~B() { cout << "B's destructor called" << endl; }
};
int main() {
B b;
return 0;
}
Q5. ____________ members of base class are inaccessible to the derived class.
Q6. What will be the output of the following code?
#include <iostream>
using namespace std;
class Shape {
public:
float getArea(){}
};
class Square : public Shape {
private:
float width;
float length;
public:
Square(float w, float l) {
width = w;
length = l;
}
float getArea(){
return width * length;
}
};
int main() {
Square sq(2, 6);
Shape* shape = &sq;
cout << sq.getArea() << endl;
cout << shape->getArea() << endl <<endl;
}
Q7. What will be the output of the following code?
#include<iostream>
using namespace std;
class A
{
public:
virtual void show() { cout<<"In A"<<endl; }
};
class B: public A
{
public:
void show() { cout<<"In B"<<endl; }
};
class C: public B
{
public:
void show() { cout<<"In C"<<endl; }
};
int main(void)
{
B b;
A* a = &b;
a->show();
C c;
a = &c;
a->show();
return 0;
}
Q8. What type of relationship (composition, aggregation, or association) does the following describe?
A House class that contains member objects of type Door, Window, and Wood class.
Q9. What type of relationship (composition, aggregation, or association) does the following describe?
A dance class led by a teacher, and the students can join or drop out of it.
Q10. Encapsulation makes classes easier to change and maintain as well as provides more flexibility as we decide which variables have read/write privileges.
Q11. The advantages of inheritance include data hiding, avoiding code duplication, and extensibility.
Q12. Overriding needs inheritance, however, the function in derived class/es should not have the same declaration as the function as of the base class.
Q13. In aggregation, the lifetime of the owned object does not depend on the lifetime of the owner.
Q14. Match the terms in the left column with their respective definitions on the right side:
Answers:
Q15. In this challenge, you need to define a class Average
. It should contain the data members num1
, num2
, and num3
of type int
. Next, you need to define a constructor that initializes these numbers with the values it takes as arguments. You also need to define a function print_average
that prints the average of the three numbers.
Average obj(1,2,3);
obj.print_average();
2
Answer:
Q16. In this challenge, you have to implement inheritance between the classes Shape
and Square
. The Shape
class has a private
member color
. You need to define the setColor
and getColor
functions to be able to access this private member.
The Square
class derives from Shape
. It has the length
and width
data members. You need to initialize the constructor and define the following two functions:
display_area
: calculates and displays the area of the squaredisplayColor
: displays the color of the squareHint: You can access the
getColor
function in theSquare
class as it derives from theShape
class.
Square sq(4,3);
sq.setColor("red");
sq.display_area();
sq.displayColor();
"The area of the square is: 12"
"The color of the square is: red"
Answer:
I hope this Learn Object-Oriented Programming in C++ Educative Quiz Answers would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>