Unit 6 - Practice Quiz

CSE202

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

2 Which keyword is used to raise an exception?

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

3 What 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()

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

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

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

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

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

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

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

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

8 If 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()

9 Can 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

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

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

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

12 Which keyword is used to declare a template?

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

13 In 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

14 Can 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

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

16 When 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

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

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

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

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

20 Can 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

21 What does STL stand for?

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

22 What are the three main components of STL?

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

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

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

24 Which of the following is a Sequence Container?

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

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

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

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

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

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

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

28 Which iterator type does std::vector support?

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

29 What 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)

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

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

31 Which function removes the last element of a vector?

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

32 Which 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

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

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

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

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

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

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

38 Can 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>

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

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

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

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

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

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

44 Which function clears all elements from a vector?

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

45 In 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

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. 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. 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

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

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

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