Unit 6: Handling Exceptions, Templates and STL - Practice Quiz
1 What is the main purpose of exception handling in C++?
2 Which C++ feature is commonly used to handle exceptional conditions?
3 Which block contains code that may generate an exception?
4
What happens when an exception is thrown in a try block?
5 Which keyword is used to explicitly generate an exception in C++?
6 Which statement correctly throws the integer value ?
7 Which keyword introduces an exception handler?
8
What does catch(...) match in C++?
9 Which statement re-throws the currently handled exception?
10 Why might a function re-throw an exception?
11 What is the main purpose of a function template?
12 Which keyword commonly declares a template parameter in C++?
13 What does a class template provide?
14
Which syntax creates an object of a class template using int as its type?
15 What can a class template be used as in inheritance?
16 What is the Standard Template Library mainly known for providing?
17 What is an STL container used for?
18 Which STL component provides operations such as sorting and searching?
19 What is the main role of an STL iterator?
20 Which statement best describes an STL vector?
21 What happens to local automatic objects when an exception leaves the function in which those objects were created?
22
Given class Derived : public Base {};, which ordering of handlers correctly allows a Derived exception to receive specialized handling?
catch(...) before catch(Derived& d)
catch(Base& b) before catch(Derived& d)
catch(Base b) followed by catch(Base& b)
catch(Derived& d) before catch(Base& b)
23
Consider double x = -2.0; if (x < 0) throw x;. Which handler directly matches the type of the thrown exception?
catch(double value)
catch(float value)
catch(long value)
catch(int value)
24
Why is catch(const std::exception& e) generally preferred to catch(std::exception e)?
std::exception
25
Inside a catch block, which statement rethrows the currently handled exception while preserving its original exception object?
throw;
catch;
return;
throw e;
26
For template<class T> T maximum(T a, T b);, what happens when maximum(4, 6.5) is called without explicit template arguments?
T is deduced as double through automatic numeric promotion
T is deduced as int because the first argument determines it
27
Given template<class T> T square(T x) { return x * x; }, what is the type of the result of square<double>(3)?
float
double
int
long
28
A class template Counter<T> contains static int count;. What is true of Counter<int>::count and Counter<double>::count?
29
For template<class T> class Box { T value; };, which declaration creates an object whose value member has type std::string?
Box<string()> item;
Box item<std::string>;
Box<std::string> item;
template Box<std::string> item;
30
Suppose template<class T> class Base {};. Which declaration correctly defines a class template Derived<T> that publicly inherits from Base<T>?
class Derived<T> : public Base<T> {};
template<class T> class Derived : public Base<T> {};
template<class T> class Derived : public Base {};
template<class T> class Derived : Base<class T> {};
31
Which STL design feature most directly allows one algorithm such as std::find to work with several different container types?
void*
32 A program needs unique keys maintained in sorted order with efficient key-based lookup. Which standard container best matches these requirements?
std::set
std::vector
std::list
std::multiset
33
Which call correctly sorts every element of std::vector<int> values in ascending order?
std::sort(values.begin(), values.end() - 1);
std::sort(values.begin(), values.end());
std::sort(values.end(), values.begin());
std::sort(values.front(), values.back());
34
Why must v.end() not be dereferenced for a nonempty vector v?
v.back()
35
An iterator points to an element of a vector. A later push_back causes the vector to reallocate its storage. What happens to the old iterator?
36
After std::vector<int> v; v.reserve(20);, which statement is guaranteed before additional elements are inserted?
v.size() is 20 and all elements contain 0
v.size() is 0 and v.capacity() is at least 20
v.size() is 0 and v.capacity() remains 0
v.size() is 20 and v.capacity() is exactly 20
37
What is the main effect of destination.splice(destination.end(), source) for two compatible std::list<int> objects?
source unchanged
source to destination
38
Why is std::sort(items.begin(), items.end()) unsuitable when items is a std::list<int>?
std::sort accepts only vector iterators
std::sort cannot compare integer elements
std::sort requires contiguous element storage
std::sort requires random-access iterators
39
Function f() throws an exception, g() calls f() without a matching handler, and main() calls g() inside a matching try block. Where is the exception handled?
f()
main()
g()
40
Where should a catch(...) handler be placed when it follows typed handlers for the same try block?
try and the first handler
41
Consider the following program:
struct Guard {
~Guard() { throw 2; }
};
int main() {
try {
Guard g;
throw 1;
} catch (...) {
std::cout << "caught";
}
}
Assuming the default exception specifications generated by modern C++, what is the program's outcome?
caught after discarding the exception thrown by the destructor.
throw expression.
std::terminate when the destructor throws during stack unwinding.
2 because the destructor's exception replaces integer 1.
42
What is printed by the following program?
struct X {
char id;
~X() { std::cout << id << ' '; }
};
int main() {
try {
X a{'A'};
try {
X b{'B'};
throw 7;
} catch (double) {
std::cout << "D ";
}
} catch (int) {
std::cout << "C";
}
}
B D A C
B A C
D B A C
A B C
43
What does this code print?
struct Base {
virtual const char* name() const { return "Base"; }
virtual ~Base() = default;
};
struct Derived : Base {
const char* name() const override { return "Derived"; }
};
int main() {
try {
throw Derived{};
} catch (Base b) {
std::cout << b.name();
}
}
Derived object cannot match a Base handler.
Derived, because virtual dispatch preserves the thrown object's type.
Base, because the handler parameter slices the caught object.
44
Given the handlers below, which one handles throw Derived{};?
struct Base { virtual ~Base() = default; };
struct Derived : Base {};
try {
throw Derived{};
} catch (Base& e) {
std::cout << "base";
} catch (Derived& e) {
std::cout << "derived";
} catch (...) {
std::cout << "other";
}
catch (...) handler handles it because polymorphic objects require ellipsis.
Derived& handler handles it because exact matches always take priority.
Base& handler handles it because handlers are tested in source order.
45
Assume Derived publicly inherits from polymorphic Base. Compare these two handlers:
void g() {
try { throw Derived{}; }
catch (Base& e) { throw e; }
}
void h() {
try { throw Derived{}; }
catch (Base& e) { throw; }
}
If each function is called inside handlers for Derived& followed by Base&, which pair is selected?
g: Derived&; h: Base&
g: Base&; h: Base&
g: Base&; h: Derived&
g: Derived&; h: Derived&
46
Given the function template below, what happens at the marked call?
template<class T>
T combine(T a, T b) { return a + b; }
auto x = combine(1, 2.5); // marked call
int and double instantiations.
T is deduced as double, and x becomes 3.5.
T is deduced as int, and x becomes 3.
T.
47
For the forwarding-reference template below, what are the deduced types of T?
template<class T>
void inspect(T&& value);
int n = 0;
inspect(n); // call 1
inspect(0); // call 2
int; call 2: int&&
int&; call 2: int&&
const int&; call 2: int
int&; call 2: int
48
What does the call f(p) print?
template<class T>
void f(T) { std::cout << "primary"; }
template<class T>
void f(T*) { std::cout << "pointer"; }
template<>
void f<int*>(int*) { std::cout << "special"; }
int* p = nullptr;
f(p);
primary, because explicit specializations disable template ordering.
pointer, because overload resolution selects the pointer primary template.
special, because an explicit specialization always wins overload resolution.
49
Which replacement makes the following template well-formed?
template<class T>
struct Wrapper {
using Container = T;
void process() {
/* replacement */ item{};
}
};
The replacement must declare item using the nested type Container::value_type.
50
What is printed by this class-template partial specialization?
template<class T>
struct Kind {
static constexpr const char* name = "ordinary";
};
template<class T>
struct Kind<T*> {
static constexpr const char* name = "pointer";
};
std::cout << Kind<const int*>::name;
pointer, with T deduced as const int.
ordinary, because partial specializations ignore cv-qualified types.
const may qualify either level.
ordinary, because const int* is not exactly T*.
51
Why does return value; fail in the template below, and which replacement fixes it?
template<class T>
struct Base {
int value = 42;
};
template<class T>
struct Derived : Base<T> {
int get() const { return value; }
};
Base<T>::get(value).
Base<T>::value.
Derived::value.
this->value.
52
What mechanism causes Algorithm<Fast>::run() to call Fast::execute()?
template<class Derived>
struct Algorithm {
void run() {
static_cast<Derived*>(this)->execute();
}
};
struct Fast : Algorithm<Fast> {
void execute() { std::cout << "fast"; }
};
Fast class namespace
run is called
53
Which design property most directly allows one STL algorithm such as std::find to operate on arrays, vectors, and lists without those containers sharing a common base class?
54
An insertion into a std::unordered_map triggers a rehash but does not erase any element. Which statement correctly describes invalidation?
55
After executing the code below, which property is guaranteed?
std::vector<int> v{1, 2, 1, 3, 1};
auto new_end = std::remove(v.begin(), v.end(), 1);
v.size() is 5, and the vector's complete contents remain unchanged.
v.size() is 5, and [v.begin(), new_end) equals {2, 3}.
v.size() is 2, and the complete vector equals {2, 3}.
v.size() is 3, and [new_end, v.end()) contains only 1s.
56
What is the logical range produced by std::unique in this example?
std::vector<int> v{1, 2, 1, 1, 2};
auto e = std::unique(v.begin(), v.end());
[v.begin(), e) is {1, 2, 1}, because the final repeated value is discarded.
[v.begin(), e) is {1, 2}, because all duplicate values are removed.
[v.begin(), e) is {1, 2, 1, 2}, because only adjacent duplicates collapse.
[v.begin(), e) is {1, 2, 2}, because equal values become adjacent first.
57
Let v be a std::vector<int> and l a std::list<int>, each containing elements. What are the standard complexity characteristics of std::distance(v.begin(), v.end()) and std::distance(l.begin(), l.end())?
std::distance increments every iterator.
58
Assume sufficient memory is available. What is guaranteed by this code?
std::vector<int> v;
v.reserve(4);
v.push_back(10);
int* p = &v[0];
v.push_back(20);
std::cout << *p;
10 because the second insertion cannot exceed the reserved capacity.
p is undefined because every push_back invalidates pointers.
20 because push_back may relocate the first element in place.
reserve provides only a size guarantee.
59
A std::vector<int> has spare capacity, and an element is inserted into its middle without reallocation. Which iterators are invalidated?
60
What is true after executing this code?
std::list<int> a{1, 2};
std::list<int> b{3, 4};
auto it = std::next(b.begin()); // points to 4
a.splice(std::next(a.begin()), b, it);
a is {4, 1, 2}, b is {3}, and all iterators into both lists are invalidated.
a is {1, 2, 4}, b is {3}, and it is invalidated by the transfer.
a is {1, 4, 2}, b is {3}, and it still refers to 4 in a.
a is {1, 4, 2}, b is {3}, and it still belongs to b.
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 →