Unit 6: Exception Handling, Templates and Standard Template Library - Practice Quiz
1 What is an exception in C++?
2 Which three C++ keywords are primarily used for exception handling?
try, throw, and catch
for, while, and do
new, delete, and sizeof
if, else, and switch
3
What is the purpose of a try block in C++?
4
What happens when an exception is thrown inside a try block?
catch block executes in sequence
catch block
try block starts again automatically
5 Which keyword is used to signal an exception in C++?
except
raise
throw
signal
6
What does the statement throw 10; do?
10
10
10
7
Which handler can catch an exception thrown by throw 5;?
catch(double value)
catch(std::string value)
catch(int value)
catch(char value)
8
What does catch(...) mean in C++?
std::exception
9
How is the current exception rethrown from inside a catch block?
catch;
rethrow;
throw;
throw();
10
Why might a catch block rethrow an exception?
11 What is the main purpose of a function template?
12
In template <class T> T maximum(T a, T b);, what does T represent?
13 What does a class template allow a programmer to create?
14
Which declaration creates an object from a class template named Box using int?
Box object<int>;
Box<int> object;
int<Box> object;
template Box object;
15 Can a class template inherit from another class in C++?
16
Given template <class T> class Derived : public Base<T> {};, what does Base<T> represent?
T
T
17 What is an STL container?
18 What is the primary role of an STL iterator?
19
Which std::vector member function adds an element to the end?
pop_back()
push_back()
insert_front()
push_front()
20
Which feature is provided by std::list?
21
What is printed by the following C++ code?
try {
throw 7;
} catch (double x) {
std::cout << "D";
} catch (int x) {
std::cout << x;
}
7
D7
D
22 A function creates two local objects and then throws an exception. The exception is caught by its caller. What happens to the local objects?
23
Given int value = 10;, what is true after executing throw value;?
value to double
value
value
24
A polymorphic exception hierarchy has BaseError and FileError : public BaseError. Which handler order correctly allows specialized handling?
BaseError by value
FileError before BaseError
BaseError before FileError
25
Inside catch (const BaseError& e), which statement rethrows the current exception while preserving its original dynamic type?
throw;
throw BaseError();
return;
throw e;
26
Consider template<class T> T larger(T a, T b);. Which call fails template argument deduction?
larger(2.0, 5.0)
larger(2, 5.0)
larger(2, 5)
larger('a', 'z')
27
Both template<class T> void show(T); and void show(int); are visible. Which overload is selected by show(4)?
show(int)
show<int>(int)
28
For template<class T> class Box { T item; };, which declarations create two objects from different specializations?
Box<T> a; Box<T> b;
Box a<int>; Box b<double>;
Box<int> a; Box<double> b;
Box(int) a; Box(double) b;
29
How should a member function T Box<T>::get() const normally be defined outside the class template?
template<class T> T Box::get<T>() { return item; }
T Box::get() const { return item; }
class<T> T Box<T>::get() const { return item; }
template<class T> T Box<T>::get() const { return item; }
30
Which declaration defines Derived<T> as publicly inheriting from the corresponding Base<T> specialization?
template<class T> class Derived : public Base<T> {};
template<class T> class Derived : public Base {};
template<class T> class Derived<T> : Base {};
class Derived<T> : public Base<T> {};
31
Inside a class template derived from Base<T>, an unqualified call to an inherited member process() is not found because the base is dependent. Which expression correctly makes the lookup dependent?
super.process();
this->process();
T::process();
base.process();
32
Which statement correctly sorts all elements of std::vector<int> values in ascending order?
std::sort(values.begin(), values.end());
values.sort();
values.sort(values.begin(), values.end());
std::sort(values.front(), values.back());
33
Why can std::sort(items.begin(), items.end()) be used with a std::vector but not with a std::list?
std::sort requires contiguous allocation
std::sort modifies container capacity
std::sort accepts arithmetic values only
std::sort requires random-access iterators
34
After auto it = std::find(v.begin(), v.end(), 12);, how should the program test whether 12 was absent?
if (it == nullptr)
if (it == v.end())
if (*it == 0)
if (it == v.begin())
35
A vector contains {10, 20, 30, 40}. What remains after v.erase(v.begin() + 1);?
{10, 20, 40}
{10, 30, 40}
{10, 20, 30}
{20, 30, 40}
36 Which operation can invalidate every iterator and reference to elements of a vector because it may reallocate storage?
push_back beyond current capacity
front on a non-empty vector
empty on any valid vector
size on any valid vector
37
A vector has size() == 4 and capacity() == 10. What does v.reserve(8) guarantee immediately afterward?
4
8
8
18
38
A list contains {2, 4, 6} and it points to 4. After items.insert(it, 3), what is the list?
{2, 4, 6, 3}
{2, 3, 4, 6}
{2, 4, 3, 6}
{3, 2, 4, 6}
39
Which operation transfers elements from one std::list to another without copying the element values?
merge
assign
insert
splice
40
What is the main advantage of catching a polymorphic exception as const BaseError& instead of BaseError?
41
Consider the following C++ code:
struct X {
int id;
~X() { std::cout << id; }
};
void f() {
X a{1};
try {
X b{2};
throw 7;
} catch (double) {
std::cout << "D";
}
std::cout << "F";
}
int main() {
try { f(); }
catch (int) { std::cout << "I"; }
}
What is printed?
12I
2I1
21I
2F1I
42
Given the hierarchy and handler order below, which handler processes the exception?
struct Base { virtual ~Base() = default; };
struct Derived : Base {};
try {
throw Derived{};
} catch (const Base&) {
std::cout << "B";
} catch (const Derived&) {
std::cout << "D";
} catch (...) {
std::cout << "A";
}
A
Derived handler prints D
Base handler prints B
43
Assume C++17 or later. What is the essential lifetime property of the object created by this statement?
throw Widget{};
44
What does the following program print?
struct Base {
virtual const char* name() const { return "Base"; }
};
struct Derived : Base {
const char* name() const override { return "Derived"; }
};
try {
throw Derived{};
} catch (Base b) {
std::cout << b.name();
}
Base
Derived
45
Inside catch (const Base& e), how do throw; and throw e; differ when the currently handled object has dynamic type Derived?
throw e; preserves Derived, while throw; creates a sliced Base exception
Derived exception object
throw; preserves Derived, while throw e; creates a sliced Base exception
Base&
46
A function declared void g() noexcept allows an exception to escape its body. Which behavior is required by C++?
std::exception
std::terminate() is invoked
47
During stack unwinding for exception E1, a local object's destructor throws E2, and E2 escapes that destructor. What happens?
E2 replaces E1 and continues unwinding
std::terminate() is invoked
E1 replaces E2 and continues unwinding
48
What is printed by the following code?
template<class T, std::size_t N>
constexpr std::size_t extent(T (&)[N]) { return N; }
int a[7];
int* p = a;
std::cout << extent(a);
Assume the shown call is the only call to extent.
8
7
49
Given the function template below, which call is well-formed without changing the template?
template<class T>
T combine(T a, T b) { return a + b; }
combine<double>(1, 2.5)
combine(1, 2.5)
combine("a", "b")
combine<>(1L, 2.5F)
50
Which overload is selected by show(10)?
template<class T>
void show(T) { std::cout << "T"; }
void show(int) { std::cout << "I"; }
TI
I
T
51
What is printed by this program?
template<class T>
struct Counter {
static int value;
};
template<class T>
int Counter<T>::value = 0;
Counter<int>::value = 3;
Counter<double>::value = 8;
std::cout << Counter<int>::value << Counter<double>::value;
38
11
33
88
52
Why is typename required in the declaration below?
template<class C>
void f() {
typename C::value_type x{};
}
typename forces C to be a standard container
typename delays construction of x until runtime
C::value_type is dependent and must be identified as a type
value_type is always a static data member of C
53
Why can Derived<T>::call() fail to find execute() in this code?
template<class T>
struct Base {
void execute();
};
template<class T>
struct Derived : Base<T> {
void call() { execute(); }
};
Base<T> before inheriting
execute() is private because no access label appears in Base
54
Which declaration correctly derives a generic stack from Container<T> and forwards the type argument to the base class template?
template<class T> class Stack : public Container<T> {};
template<class T> class Stack : public Container {};
class Stack<T> : public Container<T> {};
template<class T> class Stack : Container<class T> {};
55
Why is the following call ill-formed for a std::list<int> values?
std::sort(values.begin(), values.end());
std::sort cannot compare values stored in node-based containers
std::sort accepts only iterators returned by std::vector
std::sort requires contiguous iterators, but list iterators are forward-only
std::sort requires random-access iterators, but list iterators are bidirectional
56
After executing the code below, what are the elements of v?
std::vector<int> v{1, 2, 3, 2, 4};
auto new_end = std::remove(v.begin(), v.end(), 2);
No call to erase is made.
1, 3, 4, while the size remains 5
1, 2, 3, 2, 4
1, 3, while the size remains 5
1, 3, 4, and its size becomes 3
57
A std::vector<int> v has size() == 4 and capacity() == 10. An iterator it points to v[1]. After v.push_back(9), which statement is correct?
it is invalid because size() changes even without reallocation
it remains valid because no reallocation is required
it remains valid only if 9 is smaller than v[1]
it is invalid because every insertion invalidates all iterators
58
For a type T that is copy-constructible and has a potentially throwing move constructor, which strategy may std::vector<T> use during reallocation to preserve its strong exception guarantee?
std::memcpy in all cases
59
Let it point to an element of list a. What happens to it after this operation moves that element into list b?
b.splice(b.end(), a, it);
it remains valid and now refers to the element in b
it becomes equal to b.end() after the transfer
it remains valid but still associates the element with a
it is invalidated because the element changes containers
60
Assuming both lists use compatible allocators, what is the complexity of transferring all elements from source to the end of target using the overload below?
target.splice(target.end(), source);
target.size()
source.size()
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 →