Unit 6: Exception Handling, Templates and Standard Template Library - Practice Quiz

CSE202 — Object Oriented Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 What is an exception in C++?

Basics of exception handling Easy
A. A function that always returns no value
B. A variable that stores only integer values
C. A loop that repeats a block of code
D. An event that disrupts normal program execution

2 Which three C++ keywords are primarily used for exception handling?

Basics of exception handling Easy
A. try, throw, and catch
B. for, while, and do
C. new, delete, and sizeof
D. if, else, and switch

3 What is the purpose of a try block in C++?

Exception handling mechanism Easy
A. To contain code that may throw an exception
B. To declare global variables
C. To repeatedly execute code until a condition eventually becomes false
D. To define a class template

4 What happens when an exception is thrown inside a try block?

Exception handling mechanism Easy
A. Every catch block executes in sequence
B. Control moves to a matching catch block
C. The compiler removes the throwing statement
D. The try block starts again automatically

5 Which keyword is used to signal an exception in C++?

Throwing mechanism Easy
A. except
B. raise
C. throw
D. signal

6 What does the statement throw 10; do?

Throwing mechanism Easy
A. Throws an integer exception with value 10
B. Returns the value 10
C. Creates ten exception objects and stores them for later use
D. Prints the value 10

7 Which handler can catch an exception thrown by throw 5;?

Catching mechanism Easy
A. catch(double value)
B. catch(std::string value)
C. catch(int value)
D. catch(char value)

8 What does catch(...) mean in C++?

Catching mechanism Easy
A. It catches only integer exceptions
B. It catches exceptions of any type
C. It catches only exceptions explicitly inherited from std::exception
D. It ignores all exceptions permanently

9 How is the current exception rethrown from inside a catch block?

Rethrowing an exception Easy
A. catch;
B. rethrow;
C. throw;
D. throw();

10 Why might a catch block rethrow an exception?

Rethrowing an exception Easy
A. To declare the exception as a template
B. To convert every exception into a loop
C. To prevent the exception from leaving the current handler under any circumstances
D. To let another handler process it

11 What is the main purpose of a function template?

Function template Easy
A. To create a separate unrelated function manually for every possible data type
B. To define one function for multiple data types
C. To restrict a function to integer data
D. To execute a function without calling it

12 In template <class T> T maximum(T a, T b);, what does T represent?

Function template Easy
A. A generic type parameter
B. A runtime exception object
C. A standard container iterator
D. A fixed integer value

13 What does a class template allow a programmer to create?

Class template Easy
A. A class that works with different data types
B. A class that handles only compile-time errors
C. A class that cannot contain member functions
D. A class whose objects must all store exactly the same predefined integer value

14 Which declaration creates an object from a class template named Box using int?

Class template Easy
A. Box object<int>;
B. Box<int> object;
C. int<Box> object;
D. template Box object;

15 Can a class template inherit from another class in C++?

Class template with inheritance Easy
A. Only when the derived class has no members
B. Only when every base-class function is static and all data members are public
C. No, templates cannot participate in inheritance
D. Yes, inheritance can be used with class templates

16 Given template <class T> class Derived : public Base<T> {};, what does Base<T> represent?

Class template with inheritance Easy
A. A base-class template specialization
B. A function returning T
C. An exception handler for T
D. An iterator that traverses all derived objects and automatically converts their values

17 What is an STL container?

Introduction to STL containers, algorithms and iterators Easy
A. A keyword used to throw exceptions
B. A compiler setting for template expansion
C. A function that can execute only mathematical operations on one fixed value
D. An object used to store a collection of elements

18 What is the primary role of an STL iterator?

Introduction to STL containers, algorithms and iterators Easy
A. To permanently sort every container immediately when an element is inserted
B. To define the container's data type
C. To traverse elements in a container
D. To catch errors from a container

19 Which std::vector member function adds an element to the end?

Vector container Easy
A. pop_back()
B. push_back()
C. insert_front()
D. push_front()

20 Which feature is provided by std::list?

List container Easy
A. Efficient insertion and deletion at known positions
B. Storage of elements in one contiguous memory block
C. Direct random access using the subscript operator
D. Automatic conversion of every stored element into a dynamically allocated character array

21 What is printed by the following C++ code?

try {
throw 7;
} catch (double x) {
std::cout << "D";
} catch (int x) {
std::cout << x;
}

Basics of exception handling Medium
A. 7
B. Nothing
C. D7
D. 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?

Exception handling mechanism Medium
A. They are destroyed only if explicitly deleted
B. They remain alive until program termination
C. They are destroyed in reverse construction order
D. They are destroyed in construction order

23 Given int value = 10;, what is true after executing throw value;?

Throwing mechanism Medium
A. The exception remains valid only in the function
B. The exception converts value to double
C. The exception stores a copy of value
D. The exception stores a reference to value

24 A polymorphic exception hierarchy has BaseError and FileError : public BaseError. Which handler order correctly allows specialized handling?

Catching mechanism Medium
A. Catch only BaseError by value
B. Catch FileError before BaseError
C. Catch both types in either order
D. Catch BaseError before FileError

25 Inside catch (const BaseError& e), which statement rethrows the current exception while preserving its original dynamic type?

Rethrowing an exception Medium
A. throw;
B. throw BaseError();
C. return;
D. throw e;

26 Consider template<class T> T larger(T a, T b);. Which call fails template argument deduction?

Function template Medium
A. larger(2.0, 5.0)
B. larger(2, 5.0)
C. larger(2, 5)
D. larger('a', 'z')

27 Both template<class T> void show(T); and void show(int); are visible. Which overload is selected by show(4)?

Function template Medium
A. The non-template show(int)
B. Both overloads, causing ambiguity
C. The template show<int>(int)
D. Neither overload, causing an error

28 For template<class T> class Box { T item; };, which declarations create two objects from different specializations?

Class template Medium
A. Box<T> a; Box<T> b;
B. Box a<int>; Box b<double>;
C. Box<int> a; Box<double> b;
D. Box(int) a; Box(double) b;

29 How should a member function T Box<T>::get() const normally be defined outside the class template?

Class template Medium
A. template<class T> T Box::get<T>() { return item; }
B. T Box::get() const { return item; }
C. class<T> T Box<T>::get() const { return item; }
D. 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?

Class template with inheritance Medium
A. template<class T> class Derived : public Base<T> {};
B. template<class T> class Derived : public Base {};
C. template<class T> class Derived<T> : Base {};
D. 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?

Class template with inheritance Medium
A. super.process();
B. this->process();
C. T::process();
D. base.process();

32 Which statement correctly sorts all elements of std::vector<int> values in ascending order?

Introduction to STL containers, algorithms and iterators Medium
A. std::sort(values.begin(), values.end());
B. values.sort();
C. values.sort(values.begin(), values.end());
D. 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?

Introduction to STL containers, algorithms and iterators Medium
A. std::sort requires contiguous allocation
B. std::sort modifies container capacity
C. std::sort accepts arithmetic values only
D. 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?

Introduction to STL containers, algorithms and iterators Medium
A. if (it == nullptr)
B. if (it == v.end())
C. if (*it == 0)
D. if (it == v.begin())

35 A vector contains {10, 20, 30, 40}. What remains after v.erase(v.begin() + 1);?

Vector container Medium
A. {10, 20, 40}
B. {10, 30, 40}
C. {10, 20, 30}
D. {20, 30, 40}

36 Which operation can invalidate every iterator and reference to elements of a vector because it may reallocate storage?

Vector container Medium
A. push_back beyond current capacity
B. front on a non-empty vector
C. empty on any valid vector
D. size on any valid vector

37 A vector has size() == 4 and capacity() == 10. What does v.reserve(8) guarantee immediately afterward?

Vector container Medium
A. Its size remains 4
B. Its size becomes 8
C. Its capacity becomes 8
D. Its capacity becomes 18

38 A list contains {2, 4, 6} and it points to 4. After items.insert(it, 3), what is the list?

List container Medium
A. {2, 4, 6, 3}
B. {2, 3, 4, 6}
C. {2, 4, 3, 6}
D. {3, 2, 4, 6}

39 Which operation transfers elements from one std::list to another without copying the element values?

List container Medium
A. merge
B. assign
C. insert
D. splice

40 What is the main advantage of catching a polymorphic exception as const BaseError& instead of BaseError?

Catching mechanism Medium
A. It avoids copying and object slicing
B. It prevents stack unwinding from occurring
C. It permits modification of the exception
D. It converts all errors to the base type

41 Consider the following C++ code:

CPP
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?

Exception handling mechanism Hard
A. 12I
B. 2I1
C. 21I
D. 2F1I

42 Given the hierarchy and handler order below, which handler processes the exception?

CPP
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";
}

Catching mechanism Hard
A. No handler matches the exception
B. The catch-all handler prints A
C. The Derived handler prints D
D. The Base handler prints B

43 Assume C++17 or later. What is the essential lifetime property of the object created by this statement?

CPP
throw Widget{};

Throwing mechanism Hard
A. The object persists until the entire program terminates
B. The object is destroyed before stack unwinding begins
C. The local temporary persists until the throwing function returns
D. A separate exception object persists until exception handling finishes

44 What does the following program print?

CPP
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();
}

Catching mechanism Hard
A. Nothing, because the handler does not match
B. Base
C. The program has undefined behavior
D. Derived

45 Inside catch (const Base& e), how do throw; and throw e; differ when the currently handled object has dynamic type Derived?

Rethrowing an exception Hard
A. throw e; preserves Derived, while throw; creates a sliced Base exception
B. Both statements preserve the original Derived exception object
C. throw; preserves Derived, while throw e; creates a sliced Base exception
D. Both statements create a new exception whose type is Base&

46 A function declared void g() noexcept allows an exception to escape its body. Which behavior is required by C++?

Basics of exception handling Hard
A. The exception is converted to std::exception
B. The function automatically returns without a value
C. std::terminate() is invoked
D. The caller may catch the exception normally

47 During stack unwinding for exception E1, a local object's destructor throws E2, and E2 escapes that destructor. What happens?

Exception handling mechanism Hard
A. E2 replaces E1 and continues unwinding
B. std::terminate() is invoked
C. Both exceptions reach separate outer handlers
D. E1 replaces E2 and continues unwinding

48 What is printed by the following code?

CPP
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.

Function template Hard
A. The program is ill-formed
B. 8
C. The call is ambiguous
D. 7

49 Given the function template below, which call is well-formed without changing the template?

CPP
template<class T>
T combine(T a, T b) { return a + b; }

Function template Hard
A. combine<double>(1, 2.5)
B. combine(1, 2.5)
C. combine("a", "b")
D. combine<>(1L, 2.5F)

50 Which overload is selected by show(10)?

CPP
template<class T>
void show(T) { std::cout << "T"; }

void show(int) { std::cout << "I"; }

Function template Hard
A. Neither overload because the call is ambiguous
B. Both overloads, printing TI
C. The non-template overload, printing I
D. The function template, printing T

51 What is printed by this program?

CPP
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;

Class template Hard
A. 38
B. 11
C. 33
D. 88

52 Why is typename required in the declaration below?

CPP
template<class C>
void f() {
    typename C::value_type x{};
}

Class template Hard
A. typename forces C to be a standard container
B. typename delays construction of x until runtime
C. C::value_type is dependent and must be identified as a type
D. value_type is always a static data member of C

53 Why can Derived<T>::call() fail to find execute() in this code?

CPP
template<class T>
struct Base {
    void execute();
};

template<class T>
struct Derived : Base<T> {
    void call() { execute(); }
};

Class template with inheritance Hard
A. The derived class must specialize Base<T> before inheriting
B. A class template cannot inherit from another class template
C. The base is dependent, so unqualified lookup does not inspect it during definition
D. 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?

Class template with inheritance Hard
A. template<class T> class Stack : public Container<T> {};
B. template<class T> class Stack : public Container {};
C. class Stack<T> : public Container<T> {};
D. template<class T> class Stack : Container<class T> {};

55 Why is the following call ill-formed for a std::list<int> values?

CPP
std::sort(values.begin(), values.end());

Introduction to STL containers, algorithms and iterators Hard
A. std::sort cannot compare values stored in node-based containers
B. std::sort accepts only iterators returned by std::vector
C. std::sort requires contiguous iterators, but list iterators are forward-only
D. std::sort requires random-access iterators, but list iterators are bidirectional

56 After executing the code below, what are the elements of v?

CPP
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.

Introduction to STL containers, algorithms and iterators Hard
A. The first three elements are 1, 3, 4, while the size remains 5
B. The vector remains exactly 1, 2, 3, 2, 4
C. The first two elements are 1, 3, while the size remains 5
D. The vector is exactly 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?

Vector container Hard
A. it is invalid because size() changes even without reallocation
B. it remains valid because no reallocation is required
C. it remains valid only if 9 is smaller than v[1]
D. 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?

Vector container Hard
A. Move existing elements and ignore any exception
B. Relocate elements with std::memcpy in all cases
C. Default-construct replacements without preserving values
D. Copy existing elements into the new storage

59 Let it point to an element of list a. What happens to it after this operation moves that element into list b?

CPP
b.splice(b.end(), a, it);

List container Hard
A. it remains valid and now refers to the element in b
B. it becomes equal to b.end() after the transfer
C. it remains valid but still associates the element with a
D. 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?

CPP
target.splice(target.end(), source);

List container Hard
A. Linear in target.size()
B. Linear in source.size()
C. Logarithmic in both list sizes
D. Constant time