C++ quiz with instant feedback

This quiz contains 8 questions and tests your C++ programming skills on:

The questions are a bit tricky, don't rush, take your time. The correct answer is displayed immediately after you select one of the options.Good luck!

Question 1 - Data hiding

Consider the following header and main file. What is the result of this program?
#ifndef PERSON_H
#define PERSON_H

class Person
{
public:
    double getHeight() {return this->height;}
    void setHeight(double height) {this->height = height;}
private:
    double height;
};

#endif // PERSON_H
#include <iostream>
#include "person.h"

int main()
{
    Person p;
    p.height = 1.85;
    std::cout << p.getHeight() << '\n';
}
This program will:
a) output 1.85 to the console
b) output 0 to the console
c) not compile
d) throw an exception

c) is correct. This program will not compile because double height is declared private. The value of the person's height can only be changed through the function setHeight(double height).

Question 2 - Constructor

Consider the following header and main file. What is the result of this program?
#ifndef EARTHQUAKE_H
#define EARTHQUAKE_H

class Earthquake {
public:
    double getMagnitude() const {return this->magnitude;}
private:
    double magnitude;
};

#endif // EARTHQUAKE_H
#include <iostream>
#include "earthquake.h"

int main()
{
    Earthquake e1();
    std::cout << e1.getMagnitude() << '\n';
}
This program will:
a) not compile
b) output 0 to the console
c) output undetermined result to the console
d) throw an exception

a) is correct. This program will not compile. The compiler thinks this is a function declaration with the name e1 and return type Earthquake. The correct call to the default constructor should be Earthquake e1, i.e. without parentheses.

Question 3 - Implicit conversion

Consider the following header and main file. What is the result of this program?
#ifndef EARTHQUAKE_H
#define EARTHQUAKE_H

class Earthquake {
public:
    Earthquake() {this->magnitude = 5.0;}
    Earthquake(double magnitude){this->magnitude = magnitude;}
    double getMagnitude() const {return this->magnitude;}
private:
    double magnitude;
};

#endif // EARTHQUAKE_H
#include <iostream>
#include "earthquake.h"

int main()
{
    Earthquake e1 = 4;
    std::cout << e1.getMagnitude() << '\n';
}
This program will:
a) not compile
b) output 0 to the console
c) output 4 to the console
d) output 5 to the console

c) is correct. The program will output 4 to the console. The statement "Earthquake e1 = 4;" will call the one parameter constructor. One parameter constructors in C++ perform implicit type conversion.

Question 4 - Copy constructor

Consider the following header and main file. What is the result of this program?
#ifndef DVD_H
#define DVD_H

class DVD
{
public:
    DVD(std::string name){this->name = name;}
    std::string getName() const {return this->name;}
    DVD(const DVD& otherDVD){this->name = "Kill Bill 1";}
private:
    std::string name;
};
#endif // DVD_H
#include <iostream>
#include "dvd.h"

int main()
{
    DVD d1("Iron Man 3");
    DVD d2 = d1;
    std::cout << d2.getName() << '\n';
    return 0;
}
This program will:
a) output Iron Man 3 to the console
b) output Kill Bill 1 to the console
c) output an empty string to the console
d) not compile

b) is correct. This program will output Kill Bill 1 to the console. The statement "DVD d2 = d1" will call the corresponding copy constructor.

Question 5 - Polymorphism

Consider the following header and main file. What is the result of this program?
#ifndef TEMPMETHOD_H
#define TEMPMETHOD_H
#include <iostream>

class TempMethod {
public:
    virtual double computeT(double p) = 0;
    virtual void print() const = 0;
};

class XMethod : public TempMethod {
public:
    double computeT(double p) {return 3*p;}
    void print() const {std::cout << "x method" << '\n';}
};

class YMethod : public TempMethod {
public:
    double computeT(double p) {return p*p;}
};

#endif // TEMPMETHOD_H
#include <iostream>
#include "tempmethod.h"

int main()
{
    TempMethod* tm = new YMethod;
    std::cout << tm->computeT(4) << '\n';
    return 0;
}
This program will:
a) output 4 to the console
b) output 12 to the console
c) output 16 to the console
d) not compile

d) is correct. This program will not compile. The print() method was declared as pure virtual in the base class TempMethod and has to be implemented by the subclass YMethod for the program to compile.

Question 6 - Static members

Consider the following header, source and main file. What is the result of this program?
#ifndef SHAPE_H
#define SHAPE_H

class Shape {
public:
    Shape(double width);
    double getWidth() const {return this->width;}
    static void update();
private:
    double width;
};

#endif // SHAPE_H
#include "shape.h"

Shape::Shape(double width){
    this->width = width;
}

void Shape::update(){
    width++;
}
#include "shape.h"

int main()
{
    Shape sh(10);
    std::cout << sh.getWidth() << '\n';
    return 0;
}
This program will:
a) output 10 to the console
b) output 11 to the console
c) not compile
d) throw an exception

c) is correct. This program will not compile. Static function update() cannot access non-static members of a class.

Question 7 - Const members

Consider the following header, source and main file. What is the result of this program?
#ifndef PRODUCT_H
#define PRODUCT_H
#include<iostream>

class Product {
public:
    Product(double xPrice);
    void print() const;
private:
    double price;
    const double vat;
};

#endif // PRODUCT_H
#include "product.h"

Product::Product(double xPrice){
    this->price = xPrice;
    this->vat = 0.15;
}

void Product::print() const {
    std::cout << this->price << ", " << this->vat << '\n';
}

#include <iostream>
#include "product.h"

int main()
{
    Product p(15.3);
    p.print();
    return 0;
}
This program will:
a) output 15.3, 0.15 to the console
b) output 15.3, 0 to the console
c) not compile
d) output 0, 0 to the console

c) is correct. This program will not compile. This is because const member 'vat' was initialized through assignment. Only non-const members can be initialized through assignment. Const members must be initialized through an initialization list.

Question 8 - Operator overloading

Consider the following header, source and main file. What is the result of this program?
#ifndef POINT_H
#define POINT_H
#include<iostream>

class Point
{
public:
    Point(double x, double y = 1.0);
    Point operator+ (const Point& p);
    void print() const;
private:
    double x;
    double y;
};

#endif // POINT_H
#include "point.h"

Point::Point(double x, double y)
{
    this->x = x;
    this->y = y;
}

Point Point::operator+(const Point& p)
{
    return Point (x + p.x, y + p.y);
}

void Point::print() const
{
    std::cout << x << " " << y << '\n';
}
#include <iostream>
#include "point.h"

int main()
{
    Point p(0.0,0.0);
    Point r(3.0);
    Point s(5.0,1.0);
    Point m = p + r + s;
    m.print();
    return 0;
}
This program will:
a) output 8, 1 to the console
b) output 8, 2 to the console
c) throw an exception
d) not compile

b) is correct. This program will output 8, 2 to the console. The constructor has a default y parameter 1.0 and the addition operator has been overloaded.


Latest update: 22.04.2016
Created: 2014
© Walletfox.com, 2017