1What is the primary purpose of exception handling in C++?
A.To improve compilation speed
B.To handle run-time errors and abnormal conditions
C.To optimize memory usage
D.To allow syntax errors to pass
Correct Answer: To handle run-time errors and abnormal conditions
Explanation: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.
Incorrect! Try again.
2Which keyword is used to raise an exception?
A.try
B.catch
C.throw
D.raises
Correct Answer: throw
Explanation:The 'throw' keyword is used to signal (raise) an exception when an error occurs.
Incorrect! Try again.
3What happens during 'stack unwinding' in exception handling?
A.The stack memory is increased
B.Destructors of automatic objects are called as control leaves scopes
C.Global variables are reset
D.The program restarts from main()
Correct Answer: Destructors of automatic objects are called as control leaves scopes
Explanation: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.
Incorrect! Try again.
4Which block contains the code that might generate an exception?
A.catch
B.throw
C.try
D.template
Correct Answer: try
Explanation:The 'try' block encloses the code that is monitored for exceptions.
Incorrect! Try again.
5How do you define a catch block that handles all types of exceptions?
A.catch(all)
B.catch(Exception e)
C.catch(...)
D.catch(void)
Correct Answer: catch(...)
Explanation:The syntax 'catch(...)' (ellipsis) is used to catch any type of exception that was thrown.
Incorrect! Try again.
6When using multiple catch blocks, how should they be ordered?
A.General to specific
B.Specific to general
C.Random order
D.Alphabetical order
Correct Answer: Specific to general
Explanation: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.
Incorrect! Try again.
7What is the syntax to rethrow an exception currently being handled?
A.throw e;
B.throw;
C.rethrow;
D.return;
Correct Answer: throw;
Explanation:Using 'throw;' without an operand inside a catch block rethrows the current exception to the next enclosing try/catch scope.
Incorrect! Try again.
8If an exception is thrown but not caught anywhere in the program, which function is called by default?
A.abort()
B.exit()
C.terminate()
D.stop()
Correct Answer: terminate()
Explanation:If an exception is not caught, the C++ runtime calls std::terminate(), which usually calls abort().
Incorrect! Try again.
9Can a destructor throw an exception safely?
A.Yes, always
B.No, it generally terminates the program if thrown during stack unwinding
C.Yes, but only integer exceptions
D.No, syntax error
Correct Answer: No, it generally terminates the program if thrown during stack unwinding
Explanation:Throwing exceptions from destructors is dangerous. If a destructor is called during stack unwinding and throws another exception, std::terminate() is called immediately.
Incorrect! Try again.
10Which header file is required for standard exception classes like std::exception?
A.<exception>
B.<stdexcept>
C.<error>
D.<trycatch>
Correct Answer: <exception>
Explanation:The <exception> header defines the base class std::exception.
Incorrect! Try again.
11What is a C++ Template?
A.A library of graphics
B.A blueprint for creating a family of classes or functions
C.A mechanism to hide data
D.A type of inheritance
Correct Answer: A blueprint for creating a family of classes or functions
Explanation:Templates allow writing generic code where the type is a parameter, acting as a blueprint for generating specific functions or classes.
Incorrect! Try again.
12Which keyword is used to declare a template?
A.generic
B.type
C.template
D.typename
Correct Answer: template
Explanation:The 'template' keyword introduces a template declaration.
Incorrect! Try again.
13In the syntax template <typename T>, what does T represent?
A.The return type
B.A placeholder for a data type
C.A specific integer value
D.The class name
Correct Answer: A placeholder for a data type
Explanation:T acts as a generic placeholder that gets replaced by a specific data type (like int, double, or a custom class) during instantiation.
Incorrect! Try again.
14Can keywords class and typename be used interchangeably in a template parameter list?
A.Yes, they are equivalent in this context
B.No, class is for classes and typename is for primitives
C.No, typename is deprecated
D.Yes, but class is preferred for functions
Correct Answer: Yes, they are equivalent in this context
Explanation:In a template parameter declaration (e.g., template <class T> or template <typename T>), both keywords mean the same thing.
Incorrect! Try again.
15What is function template overloading?
A.Defining multiple templates with the same name but different parameters
B.Calling a template function recursively
C.Hiding a template function
D.Cannot overload templates
Correct Answer: Defining multiple templates with the same name but different parameters
Explanation:Function templates can be overloaded by other templates or by non-template functions with the same name.
Incorrect! Try again.
16When a class template inherits from a non-template class, it is called:
A.Invalid inheritance
B.Template inheritance
C.Mixed inheritance
D.Standard inheritance behavior
Correct Answer: Standard inheritance behavior
Explanation:A template class can inherit from a regular (non-template) class. The base class remains fixed, while the derived class is generic.
Incorrect! Try again.
17Which of the following is a valid Non-Type Template Parameter?
A.float value
B.int value
C.string literal
D.dynamic object
Correct Answer: int value
Explanation: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).
Incorrect! Try again.
18What is 'Template Specialization'?
A.Creating a template inside another template
B.Defining a specific implementation for a particular data type
C.Deleting a template
D.Using a template for integers only
Correct Answer: Defining a specific implementation for a particular data type
Explanation:Specialization allows defining a distinct implementation of a template when a specific type (e.g., char*) is passed.
Incorrect! Try again.
19What happens at compile time when a template is instantiated?
A.Nothing, it happens at runtime
B.The compiler generates the code for the specific type
C.The compiler creates a void pointer implementation
D.The compiler ignores the template
Correct Answer: The compiler generates the code for the specific type
Explanation:Templates rely on compile-time instantiation. The compiler generates a new version of the function/class for every unique type used.
Incorrect! Try again.
20Can a template class have static data members?
A.No
B.Yes, shared by all instances of all types
C.Yes, each instantiation has its own copy of the static member
D.Yes, but they cannot be initialized
Correct Answer: Yes, each instantiation has its own copy of the static member
Explanation:Each instantiation (e.g., MyClass<int> and MyClass<double>) is a distinct type, so they have their own separate static members.
Incorrect! Try again.
21What does STL stand for?
A.Standard Template Library
B.System Template Library
C.Standard Type List
D.Structured Template Language
Correct Answer: Standard Template Library
Explanation:STL stands for Standard Template Library, a set of C++ template classes to provide common programming data structures and functions.
Incorrect! Try again.
22What are the three main components of STL?
A.Files, Streams, Buffers
B.Containers, Algorithms, Iterators
C.Classes, Objects, Methods
D.Inputs, Processes, Outputs
Correct Answer: Containers, Algorithms, Iterators
Explanation:The core components of STL are Containers (data storage), Algorithms (procedures to process data), and Iterators (connectors).
Incorrect! Try again.
23Which component acts as a bridge between containers and algorithms?
A.Allocator
B.Functor
C.Iterator
D.Adapter
Correct Answer: Iterator
Explanation:Iterators provide a uniform way to traverse containers, allowing algorithms to process data without knowing the internal details of the container.
Incorrect! Try again.
24Which of the following is a Sequence Container?
A.map
B.set
C.vector
D.stack
Correct Answer: vector
Explanation:Sequence containers store elements in a linear order. Vector, list, and deque are sequence containers. Map and set are associative.
Incorrect! Try again.
25Which header file is needed to use generic algorithms like sort()?
A.<algo>
B.<algorithm>
C.<stl>
D.<vector>
Correct Answer: <algorithm>
Explanation:The standard header <algorithm> contains the definitions for algorithmic functions like sort, search, count, etc.
Incorrect! Try again.
26What is the underlying data structure of a std::vector?
A.Linked List
B.Doubly Linked List
C.Dynamic Array
D.Hash Table
Correct Answer: Dynamic Array
Explanation:A vector is a dynamic array that can resize itself automatically when elements are added or removed.
Incorrect! Try again.
27What is the underlying data structure of a std::list?
A.Dynamic Array
B.Doubly Linked List
C.Binary Tree
D.Stack
Correct Answer: Doubly Linked List
Explanation:std::list is implemented as a doubly linked list, allowing fast insertion and deletion at any point.
Incorrect! Try again.
28Which iterator type does std::vector support?
A.Forward Iterator
B.Bidirectional Iterator
C.Random Access Iterator
D.Input Iterator only
Correct Answer: Random Access Iterator
Explanation:Vectors support random access iterators, meaning you can jump to any element instantly using arithmetic (e.g., it + 5).
Incorrect! Try again.
29What is the time complexity for random access in a std::list?
A.O(1)
B.O(n)
C.O(log n)
D.O(n*n)
Correct Answer: O(n)
Explanation:Lists are linked lists. To access the Nth element, you must iterate through the links from the start, taking linear time O(n).
Incorrect! Try again.
30Which function adds an element to the end of a vector?
A.insert()
B.add()
C.push_back()
D.enqueue()
Correct Answer: push_back()
Explanation:The push_back() function appends an element to the end of a vector.
Incorrect! Try again.
31Which function removes the last element of a vector?
A.remove_last()
B.delete()
C.pop_back()
D.extract()
Correct Answer: pop_back()
Explanation:The pop_back() function removes the last element of the container.
Incorrect! Try again.
32Which of the following operations is faster in std::list compared to std::vector?
A.Random access
B.Sorting
C.Insertion in the middle
D.Memory usage
Correct Answer: Insertion in the middle
Explanation:Insertion in a list only involves changing pointers (O(1) once the position is found), whereas vector insertion requires shifting elements (O(n)).
Incorrect! Try again.
33What does the begin() function return?
A.The first value
B.A pointer to the memory
C.An iterator to the first element
D.An iterator to the last element
Correct Answer: An iterator to the first element
Explanation:begin() returns an iterator pointing to the first element of the container.
Incorrect! Try again.
34What does the end() function return?
A.The last element
B.An iterator to the last element
C.An iterator to the position just past the last element
D.NULL
Correct Answer: An iterator to the position just past the last element
Explanation:end() returns an iterator referring to the 'past-the-end' element, which acts as a placeholder and does not point to valid data.
Incorrect! Try again.
35How do you access the element at index 3 in a vector v safely with bounds checking?
A.v[3]
B.v.at(3)
C.v.get(3)
D.*(v+3)
Correct Answer: v.at(3)
Explanation:The at() function performs bounds checking and throws an out_of_range exception if the index is invalid. The [] operator does not check bounds.
Incorrect! Try again.
36What happens to the capacity of a vector when it gets full?
A.It throws an exception
B.It stops accepting elements
C.It automatically increases, typically doubling its size
D.It deletes old elements
Correct Answer: It automatically increases, typically doubling its size
Explanation:Vectors manage memory dynamically. When capacity is reached, they allocate a larger block of memory (often 2x) and copy elements over.
Incorrect! Try again.
37Which header file is required to use std::vector?
A.<vector>
B.<vec>
C.<array>
D.<list>
Correct Answer: <vector>
Explanation:The standard header is <vector>.
Incorrect! Try again.
38Can std::vector store elements of different data types?
A.Yes, it is loosely typed
B.No, it is a homogeneous container
C.Yes, using void pointers
D.Only if defined as vector<var>
Correct Answer: No, it is a homogeneous container
Explanation:STL containers are homogeneous; they store elements of a single specific type defined at declaration.
Incorrect! Try again.
39Which operator is used to dereference an iterator to get the value it points to?
A.&
B.->
C.*
D.::
Correct Answer: *
Explanation:The asterisk (*) operator is used to dereference an iterator, just like a pointer, to access the value.
Incorrect! Try again.
40In std::list, what type of iterator is provided?
A.Random Access
B.Bidirectional
C.Forward only
D.None
Correct Answer: Bidirectional
Explanation:Lists provide bidirectional iterators, allowing traversal forward and backward, but not random jumps.
Incorrect! Try again.
41What is the result of v.size() on an empty vector v?
A.1
B.Garbage value
C.
D.Exception
Correct Answer:
Explanation:size() returns the number of elements currently in the container. For an empty vector, it is 0.
Incorrect! Try again.
42What is the difference between size() and capacity() in a vector?
A.They are the same
B.Size is used memory, capacity is allocated memory
C.Capacity is used memory, size is allocated memory
D.Size is in bytes, capacity is in elements
Correct Answer: Size is used memory, capacity is allocated memory
Explanation:Size is the number of actual elements. Capacity is the amount of memory allocated before the vector needs to resize.
Incorrect! Try again.
43How do you check if a container v is empty?
A.v.size() == null
B.v.checkEmpty()
C.v.empty()
D.v.null()
Correct Answer: v.empty()
Explanation:The empty() function returns a boolean: true if size is 0, false otherwise.
Incorrect! Try again.
44Which function clears all elements from a vector?
A.erase()
B.delete()
C.clear()
D.remove()
Correct Answer: clear()
Explanation:The clear() function removes all elements from the container, leaving its size at 0.
Incorrect! Try again.
45In a template, what is 'Class Template Inheritance'?
A.Inheritance is not allowed in templates
B.Deriving a class template from a template or non-template base class
C.Creating a class inside a function
D.Copying code manually
Correct Answer: Deriving a class template from a template or non-template base class
Explanation:Class templates can take part in inheritance relationships just like regular classes.
Incorrect! Try again.
46What keyword allows an iterator to move to the next element?
A.++ (Increment operator)
B.next()
C.move()
D.>> (Shift operator)
Correct Answer: ++ (Increment operator)
Explanation:Iterators overload the increment (++) operator to traverse to the next element in the container.
Incorrect! Try again.
47Does std::list support the [] operator?
A.Yes
B.No
C.Only for read access
D.Only if the list is sorted
Correct Answer: No
Explanation:std::list does not support random access via subscript operator [] because it is a linked list.
Incorrect! Try again.
48In exception handling, which logic applies to derived/base class catches?
A.Base class catch must appear before Derived class catch
B.Derived class catch must appear before Base class catch
C.Order does not matter
D.They cannot exist in the same try-catch block
Correct Answer: Derived class catch must appear before Base class catch
Explanation:Because a Derived object 'is-a' Base object, catching Base first would catch Derived exceptions too, making the Derived catch unreachable.
Incorrect! Try again.
49Which method inserts elements into a vector at a specific position?
A.add()
B.push()
C.insert()
D.put()
Correct Answer: insert()
Explanation:The insert(iterator, value) function places an element at the specified iterator position, shifting subsequent elements.
Incorrect! Try again.
50What is the return type of the bad_alloc exception?
A.Memory failure
B.NULL
C.void
D.It is a type thrown, usually when new fails
Correct Answer: It is a type thrown, usually when new fails
Explanation: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.
Incorrect! Try again.
Give Feedback
Help us improve by sharing your thoughts or reporting issues.