Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 6 MCQsException Handling, Templates and Standard Template Library (STL)
What is the primary purpose of exception handling in C++?
Not quite. Try another option.
Answer: To handle run-time errors and abnormal conditions
Exception handling provides a way to transfer control from one part of a program to another when a run-time error or an anomalous condition occurs.
Question 2 of 50
Which keyword is used to raise an exception?
Not quite. Try another option.
Answer: throw
The 'throw' keyword is used to signal (raise) an exception when an error occurs.
Question 3 of 50
What happens during 'stack unwinding' in exception handling?
Not quite. Try another option.
Answer: Destructors of automatic objects are called as control leaves scopes
When an exception is thrown, the runtime looks for a handler. As it exits scopes, it calls destructors for all fully constructed local objects. This is stack unwinding.
Question 4 of 50
Which block contains the code that might generate an exception?
Not quite. Try another option.
Answer: try
The 'try' block encloses the code that is monitored for exceptions.
Question 5 of 50
How do you define a catch block that handles all types of exceptions?
Not quite. Try another option.
Answer: catch(...)
The syntax 'catch(...)' (ellipsis) is used to catch any type of exception that was thrown.
Question 6 of 50
When using multiple catch blocks, how should they be ordered?
Not quite. Try another option.
Answer: Specific to general
Catch blocks are checked sequentially. If a general catch (like a base class) comes first, it will shadow specific derived class catches. Therefore, specific types must come first.
Question 7 of 50
What is the syntax to rethrow an exception currently being handled?
Not quite. Try another option.
Answer: throw;
Using 'throw;' without an operand inside a catch block rethrows the current exception to the next enclosing try/catch scope.
Question 8 of 50
If an exception is thrown but not caught anywhere in the program, which function is called by default?
Not quite. Try another option.
Answer: terminate()
If an exception is not caught, the C++ runtime calls std::terminate(), which usually calls abort().
Question 9 of 50
Can a destructor throw an exception safely?
Not quite. Try another option.
Answer: No, it generally terminates the program if thrown during stack unwinding
Throwing exceptions from destructors is dangerous. If a destructor is called during stack unwinding and throws another exception, std::terminate() is called immediately.
Question 10 of 50
Which header file is required for standard exception classes like std::exception?
Not quite. Try another option.
Answer: <exception>
The <exception> header defines the base class std::exception.
Question 11 of 50
What is a C++ Template?
Not quite. Try another option.
Answer: A blueprint for creating a family of classes or functions
Templates allow writing generic code where the type is a parameter, acting as a blueprint for generating specific functions or classes.
Question 12 of 50
Which keyword is used to declare a template?
Not quite. Try another option.
Answer: template
The 'template' keyword introduces a template declaration.
Question 13 of 50
In the syntax template <typename T>, what does T represent?
Not quite. Try another option.
Answer: A placeholder for a data type
T acts as a generic placeholder that gets replaced by a specific data type (like int, double, or a custom class) during instantiation.
Question 14 of 50
Can keywords class and typename be used interchangeably in a template parameter list?
Not quite. Try another option.
Answer: Yes, they are equivalent in this context
In a template parameter declaration (e.g., template <class T> or template <typename T>), both keywords mean the same thing.
Question 15 of 50
What is function template overloading?
Not quite. Try another option.
Answer: Defining multiple templates with the same name but different parameters
Function templates can be overloaded by other templates or by non-template functions with the same name.
Question 16 of 50
When a class template inherits from a non-template class, it is called:
Not quite. Try another option.
Answer: Standard inheritance behavior
A template class can inherit from a regular (non-template) class. The base class remains fixed, while the derived class is generic.
Question 17 of 50
Which of the following is a valid Non-Type Template Parameter?
Not quite. Try another option.
Answer: int value
Non-type template parameters are restricted to integral types (int, char, long, etc.), pointers, or references. Floating-point numbers are generally not allowed (prior to C++20).
Question 18 of 50
What is 'Template Specialization'?
Not quite. Try another option.
Answer: Defining a specific implementation for a particular data type
Specialization allows defining a distinct implementation of a template when a specific type (e.g., char*) is passed.
Question 19 of 50
What happens at compile time when a template is instantiated?
Not quite. Try another option.
Answer: The compiler generates the code for the specific type
Templates rely on compile-time instantiation. The compiler generates a new version of the function/class for every unique type used.
Question 20 of 50
Can a template class have static data members?
Not quite. Try another option.
Answer: Yes, each instantiation has its own copy of the static member
Each instantiation (e.g., MyClass<int> and MyClass<double>) is a distinct type, so they have their own separate static members.
Question 21 of 50
What does STL stand for?
Not quite. Try another option.
Answer: Standard Template Library
STL stands for Standard Template Library, a set of C++ template classes to provide common programming data structures and functions.
Question 22 of 50
What are the three main components of STL?
Not quite. Try another option.
Answer: Containers, Algorithms, Iterators
The core components of STL are Containers (data storage), Algorithms (procedures to process data), and Iterators (connectors).
Question 23 of 50
Which component acts as a bridge between containers and algorithms?
Not quite. Try another option.
Answer: Iterator
Iterators provide a uniform way to traverse containers, allowing algorithms to process data without knowing the internal details of the container.
Question 24 of 50
Which of the following is a Sequence Container?
Not quite. Try another option.
Answer: vector
Sequence containers store elements in a linear order. Vector, list, and deque are sequence containers. Map and set are associative.
Question 25 of 50
Which header file is needed to use generic algorithms like sort()?
Not quite. Try another option.
Answer: <algorithm>
The standard header <algorithm> contains the definitions for algorithmic functions like sort, search, count, etc.
Question 26 of 50
What is the underlying data structure of a std::vector?
Not quite. Try another option.
Answer: Dynamic Array
A vector is a dynamic array that can resize itself automatically when elements are added or removed.
Question 27 of 50
What is the underlying data structure of a std::list?
Not quite. Try another option.
Answer: Doubly Linked List
std::list is implemented as a doubly linked list, allowing fast insertion and deletion at any point.
Question 28 of 50
Which iterator type does std::vector support?
Not quite. Try another option.
Answer: Random Access Iterator
Vectors support random access iterators, meaning you can jump to any element instantly using arithmetic (e.g., it + 5).
Question 29 of 50
What is the time complexity for random access in a std::list?
Not quite. Try another option.
Answer: O(n)
Lists are linked lists. To access the Nth element, you must iterate through the links from the start, taking linear time O(n).
Question 30 of 50
Which function adds an element to the end of a vector?
Not quite. Try another option.
Answer: push_back()
The push_back() function appends an element to the end of a vector.
Question 31 of 50
Which function removes the last element of a vector?
Not quite. Try another option.
Answer: pop_back()
The pop_back() function removes the last element of the container.
Question 32 of 50
Which of the following operations is faster in std::list compared to std::vector?
Not quite. Try another option.
Answer: Insertion in the middle
Insertion in a list only involves changing pointers (O(1) once the position is found), whereas vector insertion requires shifting elements (O(n)).
Question 33 of 50
What does the begin() function return?
Not quite. Try another option.
Answer: An iterator to the first element
begin() returns an iterator pointing to the first element of the container.
Question 34 of 50
What does the end() function return?
Not quite. Try another option.
Answer: An iterator to the position just past the last element
end() returns an iterator referring to the 'past-the-end' element, which acts as a placeholder and does not point to valid data.
Question 35 of 50
How do you access the element at index 3 in a vector v safely with bounds checking?
Not quite. Try another option.
Answer: v.at(3)
The at() function performs bounds checking and throws an out_of_range exception if the index is invalid. The [] operator does not check bounds.
Question 36 of 50
What happens to the capacity of a vector when it gets full?
Not quite. Try another option.
Answer: It automatically increases, typically doubling its size
Vectors manage memory dynamically. When capacity is reached, they allocate a larger block of memory (often 2x) and copy elements over.
Question 37 of 50
Which header file is required to use std::vector?
Not quite. Try another option.
Answer: <vector>
The standard header is <vector>.
Question 38 of 50
Can std::vector store elements of different data types?
Not quite. Try another option.
Answer: No, it is a homogeneous container
STL containers are homogeneous; they store elements of a single specific type defined at declaration.
Question 39 of 50
Which operator is used to dereference an iterator to get the value it points to?
Not quite. Try another option.
Answer: *
The asterisk (*) operator is used to dereference an iterator, just like a pointer, to access the value.
Question 40 of 50
In std::list, what type of iterator is provided?
Not quite. Try another option.
Answer: Bidirectional
Lists provide bidirectional iterators, allowing traversal forward and backward, but not random jumps.
Question 41 of 50
What is the result of v.size() on an empty vector v?
Not quite. Try another option.
Answer: 0
size() returns the number of elements currently in the container. For an empty vector, it is 0.
Question 42 of 50
What is the difference between size() and capacity() in a vector?
Not quite. Try another option.
Answer: Size is used memory, capacity is allocated memory
Size is the number of actual elements. Capacity is the amount of memory allocated before the vector needs to resize.
Question 43 of 50
How do you check if a container v is empty?
Not quite. Try another option.
Answer: v.empty()
The empty() function returns a boolean: true if size is 0, false otherwise.
Question 44 of 50
Which function clears all elements from a vector?
Not quite. Try another option.
Answer: clear()
The clear() function removes all elements from the container, leaving its size at 0.
Question 45 of 50
In a template, what is 'Class Template Inheritance'?
Not quite. Try another option.
Answer: Deriving a class template from a template or non-template base class
Class templates can take part in inheritance relationships just like regular classes.
Question 46 of 50
What keyword allows an iterator to move to the next element?
Not quite. Try another option.
Answer: ++ (Increment operator)
Iterators overload the increment (++) operator to traverse to the next element in the container.
Question 47 of 50
Does std::list support the [] operator?
Not quite. Try another option.
Answer: No
std::list does not support random access via subscript operator [] because it is a linked list.
Question 48 of 50
In exception handling, which logic applies to derived/base class catches?
Not quite. Try another option.
Answer: Derived class catch must appear before Base class catch
Because a Derived object 'is-a' Base object, catching Base first would catch Derived exceptions too, making the Derived catch unreachable.
Question 49 of 50
Which method inserts elements into a vector at a specific position?
Not quite. Try another option.
Answer: insert()
The insert(iterator, value) function places an element at the specified iterator position, shifting subsequent elements.
Question 50 of 50
What is the return type of the bad_alloc exception?
Not quite. Try another option.
Answer: It is a type thrown, usually when new fails
std::bad_alloc is the exception type thrown by the new operator when memory allocation fails. It doesn't 'return' a type in the function sense.
That's unit 6
Answer a few questions and your score shows up here.
Nearly through. Did unit 6 of CSE202 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 6 of CSE202 save you a night before the exam?
Then you can help keep it free for the next batch. 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.