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

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

1 What is the primary purpose of exception handling in C++?

A. To allow syntax errors to pass
B. To optimize memory usage
C. To handle run-time errors and abnormal conditions
D. To improve compilation speed

2 Which keyword is used to raise an exception?

A. throw
B. try
C. raises
D. catch

3 What happens during 'stack unwinding' in exception handling?

A. The program restarts from main()
B. The stack memory is increased
C. Destructors of automatic objects are called as control leaves scopes
D. Global variables are reset

4 Which block contains the code that might generate an exception?

A. try
B. template
C. throw
D. catch

5 How do you define a catch block that handles all types of exceptions?

A. catch(...)
B. catch(Exception e)
C. catch(void)
D. catch(all)

6 When using multiple catch blocks, how should they be ordered?

A. Specific to general
B. Alphabetical order
C. Random order
D. General to specific

7 What is the syntax to rethrow an exception currently being handled?

A. return;
B. rethrow;
C. throw;
D. throw e;

8 If an exception is thrown but not caught anywhere in the program, which function is called by default?

A. terminate()
B. stop()
C. abort()
D. exit()

9 Can a destructor throw an exception safely?

A. No, syntax error
B. Yes, always
C. Yes, but only integer exceptions
D. No, it generally terminates the program if thrown during stack unwinding

10 Which header file is required for standard exception classes like std::exception?

A. <stdexcept>
B. <trycatch>
C. <error>
D. <exception>

11 What is a C++ Template?

A. A type of inheritance
B. A library of graphics
C. A mechanism to hide data
D. A blueprint for creating a family of classes or functions

12 Which keyword is used to declare a template?

A. type
B. typename
C. template
D. generic

13 In the syntax template <typename T>, what does T represent?

A. A specific integer value
B. The return type
C. The class name
D. A placeholder for a data type

14 Can keywords class and typename be used interchangeably in a template parameter list?

A. No, class is for classes and typename is for primitives
B. Yes, they are equivalent in this context
C. No, typename is deprecated
D. Yes, but class is preferred for functions

15 What is function template overloading?

A. Cannot overload templates
B. Calling a template function recursively
C. Defining multiple templates with the same name but different parameters
D. Hiding a template function

16 When a class template inherits from a non-template class, it is called:

A. Mixed inheritance
B. Standard inheritance behavior
C. Template inheritance
D. Invalid inheritance

17 Which of the following is a valid Non-Type Template Parameter?

A. dynamic object
B. float value
C. string literal
D. int value

18 What is 'Template Specialization'?

A. Defining a specific implementation for a particular data type
B. Using a template for integers only
C. Creating a template inside another template
D. Deleting a template

19 What 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 generates the code for the specific type
D. The compiler ignores the template

20 Can a template class have static data members?

A. Yes, each instantiation has its own copy of the static member
B. No
C. Yes, shared by all instances of all types
D. Yes, but they cannot be initialized

21 What does STL stand for?

A. Standard Type List
B. Standard Template Library
C. System Template Library
D. Structured Template Language

22 What are the three main components of STL?

A. Containers, Algorithms, Iterators
B. Inputs, Processes, Outputs
C. Files, Streams, Buffers
D. Classes, Objects, Methods

23 Which component acts as a bridge between containers and algorithms?

A. Functor
B. Adapter
C. Allocator
D. Iterator

24 Which of the following is a Sequence Container?

A. map
B. set
C. stack
D. vector

25 Which header file is needed to use generic algorithms like sort()?

A. <algo>
B. <algorithm>
C. <vector>
D. <stl>

26 What is the underlying data structure of a std::vector?

A. Hash Table
B. Dynamic Array
C. Doubly Linked List
D. Linked List

27 What is the underlying data structure of a std::list?

A. Binary Tree
B. Dynamic Array
C. Stack
D. Doubly Linked List

28 Which iterator type does std::vector support?

A. Random Access Iterator
B. Input Iterator only
C. Forward Iterator
D. Bidirectional Iterator

29 What is the time complexity for random access in a std::list?

A. O(n)
B. O(1)
C. O(log n)
D. O(n*n)

30 Which function adds an element to the end of a vector?

A. push_back()
B. insert()
C. add()
D. enqueue()

31 Which function removes the last element of a vector?

A. delete()
B. remove_last()
C. extract()
D. pop_back()

32 Which of the following operations is faster in std::list compared to std::vector?

A. Memory usage
B. Sorting
C. Random access
D. Insertion in the middle

33 What does the begin() function return?

A. A pointer to the memory
B. An iterator to the first element
C. An iterator to the last element
D. The first value

34 What does the end() function return?

A. An iterator to the position just past the last element
B. NULL
C. The last element
D. An iterator to the last element

35 How do you access the element at index 3 in a vector v safely with bounds checking?

A. v.get(3)
B. v.at(3)
C. *(v+3)
D. v[3]

36 What 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 throws an exception
D. It deletes old elements

37 Which header file is required to use std::vector?

A. <list>
B. <array>
C. <vec>
D. <vector>

38 Can std::vector store elements of different data types?

A. No, it is a homogeneous container
B. Yes, it is loosely typed
C. Only if defined as vector<var>
D. Yes, using void pointers

39 Which operator is used to dereference an iterator to get the value it points to?

A. ->
B. *
C. ::
D. &

40 In std::list, what type of iterator is provided?

A. Bidirectional
B. Forward only
C. Random Access
D. None

41 What is the result of v.size() on an empty vector v?

A. Garbage value
B. 0
C. 1
D. Exception

42 What 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

43 How do you check if a container v is empty?

A. v.size() == null
B. v.null()
C. v.checkEmpty()
D. v.empty()

44 Which function clears all elements from a vector?

A. erase()
B. remove()
C. delete()
D. clear()

45 In a template, what is 'Class Template Inheritance'?

A. Copying code manually
B. Creating a class inside a function
C. Deriving a class template from a template or non-template base class
D. Inheritance is not allowed in templates

46 What keyword allows an iterator to move to the next element?

A. ++ (Increment operator)
B. next()
C. move()
D. >> (Shift operator)

47 Does std::list support the [] operator?

A. Yes
B. Only for read access
C. Only if the list is sorted
D. No

48 In exception handling, which logic applies to derived/base class catches?

A. Derived class catch must appear before Base class catch
B. Base class catch must appear before Derived class catch
C. Order does not matter
D. They cannot exist in the same try-catch block

49 Which method inserts elements into a vector at a specific position?

A. insert()
B. push()
C. put()
D. add()

50 What is the return type of the bad_alloc exception?

A. Memory failure
B. It is a type thrown, usually when new fails
C. void
D. NULL