1What is the primary purpose of exception handling in C++?
A.To handle run-time errors and abnormal conditions
B.To allow syntax errors to pass
C.To improve compilation speed
D.To optimize memory usage
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.throw
B.raises
C.try
D.catch
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.The program restarts from main()
C.Destructors of automatic objects are called as control leaves scopes
D.Global variables are reset
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.try
B.catch
C.throw
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(...)
B.catch(void)
C.catch(all)
D.catch(Exception e)
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.Alphabetical order
D.Random 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.return;
B.throw e;
C.rethrow;
D.throw;
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.exit()
B.terminate()
C.abort()
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.No, it generally terminates the program if thrown during stack unwinding
B.Yes, always
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.<error>
B.<stdexcept>
C.<exception>
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 type of inheritance
B.A library of graphics
C.A blueprint for creating a family of classes or functions
D.A mechanism to hide data
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.typename
C.type
D.template
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.The class name
C.A placeholder for a data type
D.A specific integer value
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.No, typename is deprecated
B.Yes, they are equivalent in this context
C.No, class is for classes and typename is for primitives
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.Cannot overload templates
B.Hiding a template function
C.Defining multiple templates with the same name but different parameters
D.Calling a template function recursively
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.Standard inheritance behavior
B.Template inheritance
C.Mixed inheritance
D.Invalid inheritance
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.int value
B.float value
C.dynamic object
D.string literal
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.Using a template for integers only
B.Deleting a template
C.Defining a specific implementation for a particular data type
D.Creating a template inside another template
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 creates a void pointer implementation
C.The compiler ignores the template
D.The compiler generates the code for the specific type
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, but they cannot be initialized
D.Yes, each instantiation has its own copy of the static member
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.Structured Template Language
B.Standard Type List
C.Standard Template Library
D.System Template Library
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.Inputs, Processes, Outputs
D.Classes, Objects, Methods
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.Adapter
B.Functor
C.Allocator
D.Iterator
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.stack
B.map
C.vector
D.set
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.<algorithm>
B.<vector>
C.<stl>
D.<algo>
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.Dynamic Array
B.Doubly Linked List
C.Hash Table
D.Linked List
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.Doubly Linked List
B.Stack
C.Binary Tree
D.Dynamic Array
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.Input Iterator only
B.Bidirectional Iterator
C.Random Access Iterator
D.Forward Iterator
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(n)
B.O(n*n)
C.O(log n)
D.O(1)
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.push_back()
B.add()
C.insert()
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.extract()
B.delete()
C.pop_back()
D.remove_last()
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.Memory usage
D.Insertion in the middle
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.A pointer to the memory
B.An iterator to the last element
C.The first value
D.An iterator to the first 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.An iterator to the position just past the last element
B.An iterator to the last element
C.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.get(3)
B.v[3]
C.*(v+3)
D.v.at(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 automatically increases, typically doubling its size
B.It stops accepting elements
C.It deletes old elements
D.It throws an exception
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.<list>
B.<array>
C.<vec>
D.<vector>
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.Only if defined as vector<var>
D.Yes, using void pointers
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.None
C.Bidirectional
D.Forward only
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.Garbage value
B.1
C.Exception
D.0
Correct Answer: 0
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.Size is in bytes, capacity is in elements
B.Capacity is used memory, size is allocated memory
C.Size is used memory, capacity is allocated memory
D.They are the same
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.null()
C.v.checkEmpty()
D.v.empty()
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.delete()
B.clear()
C.erase()
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.Copying code manually
B.Creating a class inside a function
C.Inheritance is not allowed in templates
D.Deriving a class template from a template or non-template base class
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.>> (Shift operator)
B.++ (Increment operator)
C.move()
D.next()
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.No
B.Only if the list is sorted
C.Yes
D.Only for read access
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.They cannot exist in the same try-catch block
B.Derived class catch must appear before Base class catch
C.Base class catch must appear before Derived class catch
D.Order does not matter
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.It is a type thrown, usually when new fails
B.void
C.NULL
D.Memory failure
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.