Unit 1: C++ Programming Basics and Functions - Practice Quiz
1 Which OOP concept combines data and the functions that operate on that data into a single unit?
2 Which of the following is commonly used as an object-oriented programming language?
3
Which C++ statement reads a value into the variable age?
cout >> age;
cin >> age;
cout << age;
cin << age;
4
Which C++ statement displays the word Hello?
cout << "Hello";
cin >> "Hello";
cout >> "Hello";
cin << "Hello";
5 Which keyword is used to define a class in C++?
module
define
class
object
6
Given class Student {};, which statement creates an object named s?
Student();
Student s;
object Student;
class s;
7
If age is a public member of object s, how is it accessed?
s::age
s:age
s.age
s->age
8 Which statement correctly describes a C++ union?
9 What is the main purpose of an enumeration in C++?
10 A member function defined inside its class definition is generally treated as what kind of function?
11 How many copies of a static data member are shared by all objects of a class?
12 What is the primary organizational unit in object-oriented programming?
13 Which operator is known as the stream insertion operator in C++?
<<
==
>>
&&
14 What happens when an argument with a default value is omitted in a function call?
15 Which keyword requests that a function be expanded at the place where it is called?
static
friend
inline
extern
16
What does the endl manipulator normally do?
17 What is function overloading in C++?
18 Which keyword allows a non-member function to access a class's private members?
public
inline
friend
virtual
19 What is a reference variable in C++?
20 In call by value, what does a function receive?
21
Consider the following C++ code:
class Shape { public: virtual void draw() { cout << "Shape"; } };
class Circle : public Shape { public: void draw() override { cout << "Circle"; } };
Shape* p = new Circle; p->draw();
Which OOP concept causes Circle to be printed?
Circle object into the Shape pointer
22
The following code reads an integer correctly, but name becomes an empty string when the user enters 21 followed by Asha on the next line:
cin >> age;
getline(cin, name);
Which statement should normally be placed between these two statements?
cin.ignore();
cin.sync();
cin.clear();
cout.flush();
23
What is printed by the following program fragment?
class Point {
int x;
public:
Point(int value) : x(value) {}
int getX() const { return x; }
};
Point p(6);
cout << p.getX();
6
0
24
Consider the following code:
class Box { public: int value; };
Box a; a.value = 5;
Box b = a;
b.value = 9;
cout << a.value << " " << b.value;
What is the output?
9 5
5 9
9 9
5 5
25
Given the following declarations, which statement is valid?
class Base { protected: int x = 4; };
class Derived : public Base { public: int read() { return x; } };
Derived d;
d.Base::x = 8;
cout << Base::x;
cout << d.read();
cout << d.x;
26 Which statement correctly compares C++ structures, unions, enumerations, and classes?
27
Given class Calc { public: int square(int); };, which definition is a non-inline member-function definition?
class Calc { public: int square(int x) { return x * x; } };
int inline Calc::square(int x) { return x * x; }
int Calc::square(int x) { return x * x; }
inline int Calc::square(int x) { return x * x; }
28
What is printed by this code?
class Widget {
public:
static int count;
Widget() { ++count; }
};
int Widget::count = 0;
Widget a, b;
Widget c = a;
cout << Widget::count;
3
2
1
29 A banking program must prevent arbitrary code from changing an account balance and must require all withdrawals to be validated. Which design best follows the object-oriented paradigm?
30
Consider the following code:
istringstream input("17abc");
int number;
char letter;
input >> number >> letter;
What values are stored if both extractions succeed?
number is 17 and letter is 'a'
number is 17 and letter is 'c'
number is 0 and letter is 'a'
31 Which function declaration uses default arguments legally in C++?
void process(int a = 1, int b, int c);
void process(int a, int b = 2, int c = 3);
void process(int a, int b = 2, int c);
void process(int a = 1, int b, int c = 3);
32
Which statement about an inline function in C++ is correct?
main() because its body is processed during compilation.
33
Assume the required formatting header is included. What does the following statement print?
cout << setfill('0') << setw(4) << 7 << " " << setw(2) << 5;
0007 05
0007 005
7000 50
0007 5
34
What is selected by d.show(3) in the following code?
class Base { public: void show(int) { cout << "Base"; } };
class Derived : public Base { public: void show(double) { cout << "Derived"; } };
Derived d;
d.show(3);
Derived::show(double)
Base::show(int)
35
Why can reveal() access v.code in this code?
class Vault {
int code = 42;
friend int reveal(const Vault&);
};
int reveal(const Vault& v) { return v.code; }
Vault by reference automatically changes every private member into a publicly accessible member.
reveal() is explicitly declared as a friend of Vault.
reveal() becomes an inherited member function of Vault.
36
What is the output of the following code?
int x = 4;
int& r = x;
r += 3;
int y = r;
++y;
cout << x << " " << r << " " << y;
4 7 8
8 8 8
7 8 8
7 7 8
37
A function must swap two caller variables and be invoked as swapValues(a, b). Which parameter declaration satisfies this requirement without returning the swapped values?
void swapValues(const int& x, const int& y)
void swapValues(int x, int y)
void swapValues(int& x, int& y)
void swapValues(int* x, int* y)
38
What does digitSum(5024) return?
int digitSum(int n) {
if (n == 0) return 0;
return n % 10 + digitSum(n / 10);
}
7
9
14
11
39
What is printed by the following recursive member-function call?
class Power {
int base;
public:
Power(int b) : base(b) {}
int calculate(int n) const {
if (n == 0) return 1;
return base * calculate(n - 1);
}
};
Power p(3); cout << p.calculate(4);
12
81
27
64
40
What happens when the following call is compiled?
void show(int);
void show(int, int = 0);
show(5);
show(int, int) is selected.
show(int) is selected.
41
Assume C++17. What does the following program print?
#include <iostream>
struct Base {
virtual int value() const { return 1; }
};
struct Derived : Base {
int value() const override { return 2; }
};
int byValue(Base object) { return object.value(); }
int byReference(const Base& object) { return object.value(); }
int main() {
Derived d;
std::cout << byValue(d) << ' ' << byReference(d);
}
2 2
1 1
1 2
42
What does this program print?
#include <iostream>
#include <sstream>
#include <limits>
int main() {
std::istringstream in("12x 34");
int a = -1, b = -1;
in >> a >> b;
in.clear();
in.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
in >> b;
std::cout << a << ' ' << b << ' ' << std::boolalpha << in.fail();
}
12 -1 true
x, so the stream remains failed after clear().
12 0 false
12 34 false
43
Why is the declaration Holder h; ill-formed?
class Holder {
int& value;
public:
Holder() = default;
};
Holder h;
default.
44
Assume each constructor prints C followed by its identifier and each destructor prints D followed by its identifier. Which sequence is produced?
#include <iostream>
struct Trace {
int id;
Trace(int n) : id(n) { std::cout << 'C' << id << ' '; }
~Trace() { std::cout << 'D' << id << ' '; }
};
int main() {
Trace a(1);
{
Trace b(2);
static Trace c(3);
}
Trace d(4);
}
C1 C2 C3 D3 D2 C4 D4 D1
C1 C2 C3 D2 C4 D4 D3 D1
C1 C2 C3 D2 C4 D4 D1 D3
45
Which labeled expression is ill-formed because of the special protected-access rule?
class Base {
protected:
int x = 1;
};
class Derived : public Base {
public:
int inspect(Base& b, Derived& d) {
int p = x; // I
int q = this->x; // II
int r = d.x; // III
int s = b.x; // IV
return p + q + r + s;
}
};
46 Which statement correctly distinguishes C++ structures, unions, scoped enumerations, and classes?
struct cannot have virtual functions; a union can keep every member active; an enum class implicitly converts to int.
struct always uses public inheritance; a union initializes all members; an enum class places enumerators in the surrounding scope.
struct and class mainly differ in default access; a union overlays members; an enum class does not implicitly convert to an integer.
47 A member function is defined inside a class definition placed in a header included by several translation units. Which statement is correct?
inline.
48
Assume C++17. What is printed?
#include <iostream>
class Counter {
inline static int count = 0;
public:
static void bump() { ++count; }
static int value() { return count; }
};
int main() {
Counter a, b;
a.bump();
b.bump();
std::cout << a.value() << ' ' << Counter::value();
}
1 2
a and b operate on separate hidden copies, while the qualified call accesses the class-wide copy.
2 2
1 1
49 Consider a closed set of operations and an evolving set of shape types. Which comparison best describes the usual maintenance tradeoff between a procedural design using type-based dispatch and an object-oriented design using virtual methods?
50
Immediately after the extraction below, which stream-state description is normally correct?
std::istringstream in("10");
int value;
in >> value;
eof() is true, fail() is true, and good() is false.
eof() remains false until another extraction is attempted, because successful formatted extraction never observes the end of the buffer.
eof() is true, fail() is false, and good() is false.
eof() is false, fail() is false, and good() is true.
51
Assume all declarations occur in the same namespace scope. What is the result?
#include <iostream>
void total(int a, int b = 2);
void total(int a = 1, int b);
void total(int a, int b) { std::cout << a + b; }
int main() {
total();
}
3.
52 An external-linkage inline function is identically defined in multiple translation units and contains a function-local static variable. Which statement is required by the C++ language?
53
What does this program print?
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::hex
<< std::setfill('0')
<< std::setw(4) << 26
<< ' '
<< std::setw(2) << 10;
}
0026 10 because std::hex, std::setfill, and std::setw all reset after one formatted insertion.
001a 10
001a 000a
001a 0a
54
What does the first call print, and what does the second call print?
#include <iostream>
struct Base {
void f(int) { std::cout << "Base"; }
};
struct Derived : Base {
void f(double) { std::cout << "Derived"; }
};
int main() {
Derived d;
d.f(1);
std::cout << ' ';
struct Exposed : Derived {
using Base::f;
} e;
e.f(1);
}
Derived Derived
Base Base
Derived Base
55
Consider this hidden friend definition:
class Number {
int value;
public:
explicit Number(int v) : value(v) {}
friend Number operator+(Number a, Number b) {
return Number(a.value + b.value);
}
};
Which use is ill-formed if no separate namespace-scope declaration of operator+ is provided?
Number c = operator+(Number(1), Number(2));
auto pointer = &operator+;
Number c = Number(1) + Number(2);
56 Which declaration creates a reference that can be safely read in the following statement?
auto&& r = std::move(3);, because an rvalue reference returned through std::move always extends temporary lifetime
const int& r = identity(3);, where identity returns its const int& parameter
const int& r = 1 + 2;
const int& r = std::max(1, 2);
57
What does this program print?
#include <iostream>
void modify(int value, int* address, int& reference) {
value += 1;
*address += 2;
reference += 4;
}
int main() {
int n = 1;
modify(n, &n, n);
std::cout << n;
}
7
8
4
58
Why does this recursive function fail to reach its base case for an initial argument greater than zero?
unsigned countdown(unsigned n) {
if (n == 0)
return 0;
return countdown(n--);
}
n - 1.
59
Given the declarations below, which statement is correct?
class Vault {
int code = 42;
friend class Auditor;
};
class Auditor {
public:
int read(const Vault& v) { return v.code; }
};
class SeniorAuditor : public Auditor {
public:
int inspect(const Vault& v) { return v.code; }
};
Auditor::read is valid, but SeniorAuditor::inspect is invalid.
SeniorAuditor::inspect is valid only because public inheritance transfers every private-access privilege from the base class.
60
A class declares void process(); in a header, and the function is defined outside the class in that same header without inline. The header is included by two translation units. What is the principal consequence?
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →