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 optimize memory usage
B. To improve compilation speed
C. To handle run-time errors and abnormal conditions
D. To allow syntax errors to pass

2 Which keyword is used to raise an exception?

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

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

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

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

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

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

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

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. throw;
B. return;
C. rethrow;
D. throw e;

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

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

9 Can a destructor throw an exception safely?

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

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

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

11 What is a C++ Template?

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

12 Which keyword is used to declare a template?

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

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

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

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

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

15 What is function template overloading?

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

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

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

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

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

18 What is 'Template Specialization'?

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

19 What happens at compile time when a template is instantiated?

A. The compiler generates the code for the specific type
B. The compiler ignores the template
C. The compiler creates a void pointer implementation
D. Nothing, it happens at runtime

20 Can a template class have static data members?

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

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. Files, Streams, Buffers
C. Classes, Objects, Methods
D. Inputs, Processes, Outputs

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

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

24 Which of the following is a Sequence Container?

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

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

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

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

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

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

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

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(log n)
B. O(1)
C. O(n)
D. O(n*n)

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

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

31 Which function removes the last element of a vector?

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

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

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

33 What does the begin() function return?

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

34 What does the end() function return?

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

35 How 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)

36 What happens to the capacity of a vector when it gets full?

A. It stops accepting elements
B. It throws an exception
C. It automatically increases, typically doubling its size
D. It deletes old elements

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

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

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

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

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. None
B. Forward only
C. Bidirectional
D. Random Access

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

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

42 What 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. Size is in bytes, capacity is in elements
D. Capacity is used memory, size is allocated memory

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

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

44 Which function clears all elements from a vector?

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

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

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

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

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

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

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

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. add()
C. put()
D. push()

50 What is the return type of the bad_alloc exception?

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