Unit 1: Principles of OOP and C++ Essentials - Practice Quiz
1 What is the primary focus of object-oriented programming?
2 Which programming paradigm primarily organizes a program as a sequence of functions?
3 Which C++ stream object is commonly used to display output on the console?
cerr
clog
cout
cin
4
Which operator is used with cin to read input in C++?
==
<<
&&
>>
5 What is a class in C++?
6 What is an object in C++?
7 How are the data members of a union stored?
8 Which statement correctly describes the members of a structure?
9 What does an enumeration define in C++?
10 Which keyword declares a scoped enumeration in modern C++?
static enum
public class
enum class
union enum
11 How many copies of a static data member are shared by all objects of a class?
12 Which type of class function can be called using the class name without creating an object?
13 What is a user-defined function?
14 Which keyword requests that a function be treated as an inline function?
typedef
inline
virtual
extern
15 Which keyword declares a non-member function that can access a class's private members?
private
friend
operator
protected
16 What privilege does a friend class receive?
17 Which symbol is used to declare a reference variable in C++?
&
#
%
*
18 What is passed to a function in call by value?
19 Which kind of parameter is normally used in call by address?
20 What is recursion?
21 A banking application must represent accounts with their balances and restrict balance updates to validated deposit and withdrawal operations. Which design best follows the object-oriented paradigm?
Account class
22
What does the following code print?
int x = 7;
cout << setfill('0') << setw(4) << x;
7000
7777
7
0007
23
After cin >> age;, a program must read a full name using getline(cin, name);. However, name becomes empty. Which statement should normally be placed before getline?
cout.ignore();
cin.flush();
cin.clear();
cin.ignore();
24
Consider the following class:
class Box { int width; public: void setWidth(int w) { width = w; } };
Which statement about width is correct?
setWidth
25
What is printed by this program fragment?
class Item { public: Item() { cout << "C"; } };
Item items[3];
CCC
CC
C
26 A program must store an employee's integer ID, floating-point salary, and character grade simultaneously. Which declaration is most appropriate?
27
Given enum class Status { Idle, Running, Stopped };, which statement correctly creates a variable initialized to Running?
int s = Status::Running;
Status s = Status::Running;
Status s = Running;
Status::Running s;
28
What is printed by the following code?
class Counter { public: static int count; Counter() { ++count; } };
int Counter::count = 0;
Counter a[3]; Counter b; cout << Counter::count;
4
0
1
3
29
A class contains a non-static member value and a static member count. Which operation is valid inside a static member function without receiving an object?
count directly
value directly
this->value directly
30
Given int power(int base, int exponent = 2);, which call uses the default argument?
power(5, 3)
power(exponent)
power()
power(5)
31 Which pair of function declarations forms a valid overload based on parameter types?
int compute(int); and static int compute(int);
int compute(int); and double compute(int);
int compute(int); and int compute(double);
int compute(int); and int compute(int);
32
Consider inline int square(int x) { return x * x; }. If int i = 3; cout << square(i++) << " " << i; is executed, what is printed?
9 5
9 4
16 4
12 4
33
A non-member function must compare private values stored in two objects of class Box. Which declaration inside Box grants the required access?
friend bool compare(Box, Box);
virtual bool compare(Box, Box);
static bool compare(Box, Box);
inline class compare(Box, Box);
34
If class A declares class B as a friend, which statement is correct?
A automatically becomes a friend of B
B become friends of A
B can access private members of A
B becomes a friend
35
What is printed by this code?
int x = 4, y = 9;
int& larger = (x > y) ? x : y;
larger = 0;
cout << x << " " << y;
4 0
4 9
0 0
0 9
36
Which declaration correctly creates a reference variable that aliases score?
int score = 10; int& ref = 10;
int score = 10; int ref& = score;
int score = 10; int& ref = score;
int score = 10; int& ref;
37
What is printed after this function call?
void update(int v, int* p, int& r) { ++v; ++(*p); ++r; }
int a = 1, b = 1, c = 1; update(a, &b, c); cout << a << b << c;
222
111
122
212
38 Which function signature allows two integers to be swapped without pointer syntax at the call site?
void swap(int& a, int& b);
void swap(int* a, int* b);
void swap(const int a, const int b);
void swap(int a, int b);
39
What value is returned by sumOdd(5)?
int sumOdd(int n) { if (n <= 1) return 1; return n + sumOdd(n - 2); }
6
9
15
8
40
What does the following call print?
void show(int n) { if (n == 0) return; cout << n; show(n - 1); cout << n; }
show(3);
123321
123123
321321
321123
41
A program represents bank accounts as records and provides separate functions such as deposit(Account&, double) and withdraw(Account&, double). Which redesign most strongly demonstrates object-oriented encapsulation rather than merely reorganizing the procedural code?
Account methods that enforce balance invariants
42
Given int x; while (std::cin >> x) { /* process x */ }, what is the most accurate reason the loop terminates?
43
What is the key language-level distinction between these declarations: class A { int x; }; and struct B { int x; };?
x is private in A and public in B
A supports inheritance but B does not
A objects cannot be dynamically allocated
B objects cannot contain member functions
44
Consider union U { int i; float f; }; U u; u.i = 10; u.f = 2.5f;. Which statement is correct?
u.i always restores its former value
45
Which initialization is rejected when enum class State { Idle, Busy }; is declared?
State s = Idle;
State s = State::Busy;
State s{State::Busy};
State s = State::Idle;
46
For class Counter { static int n; };, which definition is required in a traditional pre-C++17 translation unit if n is odr-used?
int Counter::n = 0;
Counter::static int n = 0;
int n::Counter = 0;
static int Counter::n = 0;
47
Inside a static member function of class Logger, which access is invalid unless an object is explicitly supplied?
48
Given void f(int); void f(double = 1.0);, what happens for the call f()?
double overload is selected
int overload is selected
49
Which statement best describes the inline specifier in modern C++?
50
If class Box { friend void inspect(const Box&); private: int value; }; is declared, which statement is correct?
inspect becomes a member function
Box objects can call inspect
inspect may access value
51
Class A declares friend class B;. Which conclusion is valid?
52
After int a = 3, b = 4; int& r = a; r = b;, what are the final values?
r is rebound from a to b
a is 4 and b is 4
a is 3 and b is 4
a is 3 and b is 3
53 Which declaration is valid and safely extends the lifetime of the temporary bound to it?
int&& r = 5; only as a named reference
const int& r = 5;
int& r = 5;
int& const r = 5;
54
For void f(int x, int* p, int& r) { x++; (*p)++; r++; }, called as int a=1,b=1; f(a,&a,b);, what are a and b afterward?
a = 3, b = 2
a = 1, b = 1
a = 2, b = 2
a = 2, b = 3
55
Which call can be valid for void change(int* p) while still allowing the function to represent "no object supplied"?
change(NULL); always selects an integer overload
change(&nullptr);
change(0);
change(nullptr);
56
What is the principal defect in int sum(int n) { return n + sum(n - 1); } when called with a positive integer?
57
What does this function return for f(4)? int f(int n) { if (n <= 1) return 1; return f(n - 1) + f(n - 2); }
58
After std::cin >> n; std::getline(std::cin, line);, why can line initially be empty when valid input is entered on one line?
getline
getline ignores all whitespace
59
For enum Color { Red = 1, Green, Blue = 1 };, which statement is correct?
Green has value 2
Blue cannot equal Red
Green has value 1
60
A class has a non-static member int value; but no user-declared constructor. What happens for Record r; when Record is an aggregate and default initialization is used?
value is value-initialized automatically
value is guaranteed to be zero
value has an indeterminate value
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 →