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 variable that stores only integer values
B. A loop that repeats a block of code
C. An event that disrupts normal program execution
D. A function that always returns no value

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

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

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

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

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

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

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

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

6 What does the statement throw 10; do?

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

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

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

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

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

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

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

10 Why might a catch block rethrow an exception?

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

11 What is the main purpose of a function template?

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

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

Function template Easy
A. A runtime exception object
B. A generic type parameter
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 cannot contain member functions
B. A class that works with different data types
C. A class whose objects must all store exactly the same predefined integer value
D. A class that handles only compile-time errors

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

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

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

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

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

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

17 What is an STL container?

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

18 What is the primary role of an STL iterator?

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

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

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

20 Which feature is provided by std::list?

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

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. D
C. D7
D. Nothing

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 in reverse construction order
B. They are destroyed only if explicitly deleted
C. They are destroyed in construction order
D. They remain alive until program termination

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

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

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

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

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

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

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

Function template Medium
A. larger(2, 5.0)
B. larger(2, 5)
C. larger(2.0, 5.0)
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 template show<int>(int)
B. Neither overload, causing an error
C. Both overloads, causing ambiguity
D. The non-template show(int)

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

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

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

Class template Medium
A. class<T> T Box<T>::get() const { return item; }
B. template<class T> T Box::get<T>() { return item; }
C. template<class T> T Box<T>::get() const { return item; }
D. T Box::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<T> : Base {};
B. template<class T> class Derived : public Base<T> {};
C. template<class T> class Derived : public 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. this->process();
B. T::process();
C. super.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. values.sort();
B. std::sort(values.front(), values.back());
C. values.sort(values.begin(), values.end());
D. std::sort(values.begin(), values.end());

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 random-access iterators
B. std::sort accepts arithmetic values only
C. std::sort requires contiguous allocation
D. std::sort modifies container capacity

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 == v.begin())
B. if (*it == 0)
C. if (it == nullptr)
D. if (it == v.end())

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

Vector container Medium
A. {10, 30, 40}
B. {10, 20, 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. empty on any valid vector
B. push_back beyond current capacity
C. front on a non-empty 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, 3, 4, 6}
B. {2, 4, 3, 6}
C. {3, 2, 4, 6}
D. {2, 4, 6, 3}

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

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

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

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

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 Derived handler prints D
C. The Base handler prints B
D. The catch-all handler prints A

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 is destroyed before stack unwinding begins
B. A separate exception object persists until exception handling finishes
C. The object persists until the entire program terminates
D. The local temporary persists until the throwing function returns

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. Derived
B. Nothing, because the handler does not match
C. Base
D. The program has undefined behavior

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. Both statements preserve the original Derived exception object
B. throw; preserves Derived, while throw e; creates a sliced Base exception
C. throw e; preserves Derived, while throw; 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 caller may catch the exception normally
B. The function automatically returns without a value
C. The exception is converted to std::exception
D. 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?

Exception handling mechanism Hard
A. Both exceptions reach separate outer handlers
B. std::terminate() is invoked
C. E2 replaces E1 and continues unwinding
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 call is ambiguous
B. 7
C. 8
D. The program is ill-formed

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("a", "b")
B. combine<double>(1, 2.5)
C. combine<>(1L, 2.5F)
D. combine(1, 2.5)

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. Both overloads, printing TI
B. The non-template overload, printing I
C. Neither overload because the call is ambiguous
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 delays construction of x until runtime
B. typename forces C to be a standard container
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. execute() is private because no access label appears in Base
C. A class template cannot inherit from another class template
D. The base is dependent, so unqualified lookup does not inspect it during definition

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 : Container<class T> {};
B. template<class T> class Stack : public Container<T> {};
C. template<class T> class Stack : public Container {};
D. class Stack<T> : public Container<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 random-access iterators, but list iterators are bidirectional
D. std::sort requires contiguous iterators, but list iterators are forward-only

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 vector remains exactly 1, 2, 3, 2, 4
B. The first two elements are 1, 3, while the size remains 5
C. The vector is exactly 1, 3, 4, and its size becomes 3
D. The first three elements are 1, 3, 4, while the size remains 5

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. Default-construct replacements without preserving values
B. Move existing elements and ignore any exception
C. Relocate elements with std::memcpy in all cases
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 but still associates the element with a
B. it remains valid and now refers to the element in b
C. it becomes equal to b.end() after the transfer
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. Constant time
B. Linear in source.size()
C. Logarithmic in both list sizes
D. Linear in target.size()