Unit 1: Foundational Concepts - Practice Quiz
1 Which feature is supported by C++ but not directly by C?
2 Which data type is commonly used to store a single character in C and C++?
int
float
char
double
3 What is a variable in a program?
4 Which C++ keyword can be used to declare a constant?
return
const
static
void
5 Which object is commonly used to display output in C++?
sizeof
scanf
cin
cout
6 Which control structure repeatedly executes code while a condition remains true?
else clause
while loop
switch label
if statement
7 What does a function declaration tell the compiler?
8 What is passed to a function during call by value?
9 What is recursion?
10 Which concept combines data and related functions within one unit?
11 Which keyword defines a structure in C++?
namespace
struct
typedef
template
12 What is an object in C++?
13 When is a constructor normally called in C++?
14 What is the main purpose of a data structure?
15 Which of the following is a linear data structure?
16 What does time complexity describe?
17 What does a time-space trade-off mean?
18 What does Big-O notation commonly express?
19 What does Big-Omega notation commonly express?
20 What does indicate about an algorithm's growth?
21
A C++ program defines int process(int) and double process(double). Which feature allows process(4) and process(4.5) to select different functions?
22
What does cout << fixed << setprecision(1) << 7 / 2; display when the required headers are included?
4.0
3.0
3.2
3.5
23
What is printed by int x = 5; { int x = 8; cout << x << " "; } cout << x;?
5 8
8 8
8 5
5 5
24
Given int a = 1, b = 2; const int* p = &a;, which statement is valid?
p = &b;
(*p)++;
*p = 3;
*p = b;
25
Which pair correctly reads an int n and a double x using C input and then using C++ input?
scanf("%d %f", &n, &x); and cin << n << x;
scanf("%d %lf", n, x); and cin >> x >> n;
scanf("%d %lf", &n, &x); and cin >> n >> x;
scanf("%f %lf", &n, &x); and cout >> n >> x;
26
What is the final value of sum? int sum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) continue; sum += i; if (sum > 5) break; }
21
4
16
9
27
Given the declaration int combine(int, int = 2); and definition int combine(int a, int b) { return a + b; }, what does combine(5) return?
7
5
2
28
What is printed by void update(int a, int& b) { a += 2; b += 2; } int x = 3, y = 3; update(x, y); cout << x << " " << y;?
5 5
5 3
3 5
3 3
29
How many total function invocations occur for f(4), including the initial call, if f(n) returns immediately when and otherwise calls f(n-1) and f(n-2)?
12
7
8
9
30
If Base declares virtual void show() and Derived overrides it, which implementation runs for Base* p = new Derived; p->show();?
Derived::show()
Base::show()
31
Consider struct S { int x; }; and class C { int x; };. Without adding access specifiers, which statement is correct?
S::x is private and C::x is public
S::x is public and C::x is private
32
A Counter class has a non-static member value initialized to 0, an increment() method, and a get() method. If object a is incremented twice and object b once, what do a.get() and b.get() return?
1 and 2
2 and 2
3 and 3
2 and 1
33
A Derived object contains a member object Member and inherits from Base. Which order is used for construction and destruction?
Derived, Base, Member; destroy Member, Base, Derived
Member, Base, Derived; destroy Derived, Base, Member
Base, Derived, Member; destroy Member, Derived, Base
Base, Member, Derived; destroy Derived, Member, Base
34 A browser must implement a Back operation that returns to the most recently visited page first. Which data structure best matches this requirement?
35 For a sparse graph with vertices and relatively few edges , which representation usually uses space?
36
What are the time and auxiliary-space complexities of for (int i = 0; i < n; i++) for (int j = 1; j < n; j *= 2) count++;?
37 A program repeatedly computes an expensive deterministic result for the same inputs. Which change most directly trades additional space for reduced execution time?
38 What is the tightest Big-O bound among the choices for ?
39
A loop performs one constant-time operation for every integer from 1 through n. What is its tightest listed asymptotic lower bound?
40
What is the Big-Theta complexity of for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) count++;?
41
Assume the following source is compiled once as C and once as C++:
#include <stdlib.h>
int main(void) { int *p = malloc(sizeof *p); free(p); }
Which statement is correct?
malloc.
void * to int *.
malloc automatically returns the requested pointer type.
sizeof *p dereferences an uninitialized pointer.
42
What does the following C++ expression print?
int i = -1; unsigned int u = 1; std::cout << (i < u);
0, because i is converted to a large unsigned value before comparison.
0, because every comparison between signed and unsigned values is false.
1, because the signed value -1 is numerically smaller than unsigned 1.
43
Given the following C++ program fragment, what is printed?
int x = 1;
void f() {
static int x = 2;
{ int x = 7; ++x; }
std::cout << ++x << ' ';
}
int main() { f(); f(); std::cout << x; }
3 3 1
3 4 4
3 4 1
8 8 1
44
For the declarations below, which statement is well-formed C++?
int a = 1, b = 2;
const int *p = &a;
int * const q = &a;
q = &b;
constexpr int r = a + 1;
*p = 3;
p = &b;
45
The input consists of 42 followed immediately by a newline. What values result from this C statement?
int n = 0; char c = 'X'; int r = scanf("%d%c", &n, &c);
r is 2, n is 42, and c contains the newline.
r is 1, n is 4, and c contains the character 2.
r is 2, n is 42, and c contains the next non-whitespace character.
r is 1, n is 42, and c remains equal to X.
46
Let , where , and assume arithmetic does not overflow. How many times is work() called?
for (int i = 1; i <= n; i *= 2)
for (int j = i; j >= 1; j /= 2)
work();
47 Which pair of C++ declarations introduces two distinct overloads rather than conflicting declarations or repeated declarations of the same function?
void f(int *); and void f(const int *);
void f(int); and void f(const int);
int f(int); and double f(int);
void f(int *); and void f(int * const);
48
What is printed by this C++ code?
void transform(int &a, int &b) { a += 2; b *= 3; }
int x = 4;
transform(x, x);
std::cout << x;
12
18
6
49
For powers of two, what is the time complexity of this function if each addition, comparison, and division costs constant time?
int solve(int n) {
if (n <= 1) return 1;
return solve(n / 2) + solve(n / 2) + n;
}
50
What is printed when a Derived object is constructed?
struct Base {
Base() { identify(); }
virtual void identify() { std::cout << "Base"; }
};
struct Derived : Base {
void identify() override { std::cout << "Derived"; }
};
Derived object;
Derived
Base
BaseDerived
51
Consider these C++ definitions:
struct S { int x; };
class C { int x; };
struct D : S {};
class E : S {};
From an unrelated function, for which objects is direct access through object.x permitted?
C and D objects only
S and E objects only
D and E objects only
S and D objects only
52
Assuming allocation succeeds, what is printed before scope exit?
class Box {
int *p;
public:
Box(int value) : p(new int(value)) {}
void set(int value) { *p = value; }
int get() const { return *p; }
};
Box a(5);
Box b = a;
b.set(9);
std::cout << a.get() << ' ' << b.get();
5 5
5 9
9 9
53
Suppose each constructor and destructor records its object's label. For the class below, which sequence is recorded when a D object is created and then destroyed?
struct D : B2, B1 {
M m2;
M m1;
D() : m1("m1"), m2("m2") { record("D"); }
~D() { record("~D"); }
};
B1, B2, m2, m1, D, ~D, ~m1, ~m2, ~B2, ~B1
B2, B1, m2, m1, D, ~D, ~m1, ~m2, ~B1, ~B2
B2, B1, m2, m1, D, ~D, ~m2, ~m1, ~B1, ~B2
B2, B1, m1, m2, D, ~D, ~m2, ~m1, ~B1, ~B2
54 A collection must support insertion, removal of the most recently inserted value, and retrieval of the current minimum, all in worst-case time. Which design meets every requirement?
55 Which classification correctly describes all four data structures under their conventional implementations?
56
Assume work() uses time and space. What are the time and auxiliary-space complexities of this code?
for (int i = 1; i <= n; ++i)
for (int j = i; j <= n; j += i)
work();
57 A static set of arbitrary keys must answer membership queries. The key universe is too large for direct addressing. Which approach uses extra space and achieves expected total time?
58
Define
Which asymptotic statement is correct?
59 Let for . Which claim is true?
60
For any fixed logarithm base greater than , determine the tight asymptotic bound of
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 →