Cs301mid Term MCQ File
Cs301mid Term MCQ File
---
**4. Which of the following is NOT typically a result of a poorly chosen data structure?**
---
---
---
**7. In what scenario might buying a faster computer not resolve a problem?**
---
**8. Which of the following is a factor that can determine if a solution is efficient?**
---
**1. What is the first step in selecting an appropriate data structure for solving a problem?**
---
**2. What should be done if the available disk space is insufficient for the data?**
---
**3. When selecting a data structure, why is it important to consider the basic operations that must be
supported?**
---
**4. What is an example of a basic operation that might be considered when selecting a data
structure?**
d) Writing documentation
---
**5. Why might inserting data into the beginning or middle of an array require extra steps?**
---
**6. What challenge is associated with deleting data from a specific position in an array?**
---
d) Only the first and last elements of the structure can be accessed
**Correct Answer:** b) Data can be accessed at any position without following a sequence
---
**8. Why is it important to consider whether data will be accessed in a well-defined order or
randomly?**
---
---
**10. What should you consider if your program is not efficient enough for the client?**
---
**1. Why is it important to choose the right data structure for a given problem?**
b) Because the right data structure can improve efficiency and performance
**Correct Answer:** b) Because the right data structure can improve efficiency and performance
---
**2. What is a key skill this course aims to teach regarding data structures?**
b) How to select and replace data structures without affecting the rest of the program
**Correct Answer:** b) How to select and replace data structures without affecting the rest of the
program
---
**3. Which of the following is NOT one of the three basic requirements of a data structure?**
c) Programming effort
**4. What should a programmer do if they find two different data structures suitable for the same
problem?**
---
**5. What does the course emphasize about the costs and benefits of data structures?**
---
a) Programming languages
d) Software libraries
**Correct Answer:** b) By measuring and comparing the performance of both in similar scenarios
---
**8. Why might a data structure that is suitable for small data not be suitable for large data?**
**Correct Answer:** a) Small data structures do not scale well for large data
---
**9. What challenge does a programmer face when deciding which data structure to use for a problem
where data size may change over time?**
---
**10. What is a key factor in determining the better data structure when dealing with large datasets?**
---
---
---
---
---
**15. What happens if you try to store a different data type in an array?**
---
**16. How does the array index range relate to the size of the array?**
**Correct Answer:** b) It ranges from 0 to one less than the size of the array
---
**17. Why might a programmer prefer to use arrays over other data structures in certain scenarios?**
---
---
---
**Correct Answer:** b) To have a variety of tools available for solving different problems
**1. What is the reason that `x` in the array `int x[6];` is not an lvalue?**
c) Because it is a pointer
**Correct Answer:** b) Because it is not a variable but a name for the array
---
**2. Why is the statement `x = 3;` not allowed for the array `int x[6];`?**
**Correct Answer:** a) Because it tries to assign a single value to the entire array
---
**3. What does the statement `x[0] = 5;` do in the context of an array `int x[6];`?**
d) It is an invalid statement
**Correct Answer:** b) Assigns the value 5 to the first element of the array
---
**Correct Answer:** a) Because `x` cannot be assigned the sum of `a` and `b`
---
**5. What would happen if you tried to execute the statement `x = &n;` in the array `int x[6];`?**
a) The program would assign the memory address of `n` to the array `x`
---
**6. Why can't the name of an array (like `x` in `int x[6];`) be changed?**
a) Because it is a constant representing multiple memory locations
---
**7. What problem arises when you declare a very large array but use only a small portion of it?**
---
**Correct Answer:** b) When the size of the array is only known at runtime
---
**9. Which of the following statements is true regarding array names in C/C++?**
---
**10. What is a potential solution to the problem of wasting memory when using arrays?**
a) Allocates 20 memory locations and stores the address of the first location in `y`
b) Creates a new integer array of size 20 and initializes all elements to zero
c) Allocates 20 contiguous memory locations and assigns the last address to `y`
**Correct Answer:** a) Allocates 20 memory locations and stores the address of the first location in `y`
---
**2. Which of the following statements about the variable `y` after executing `int* y = new int[20];` is
true?**
---
**3. Why is the statement `y = x;` allowed when `x` is an array and `y` is a pointer?**
b) Because `x` holds the address of its first element and `y` can store addresses
**Correct Answer:** b) Because `x` holds the address of its first element and `y` can store addresses
---
**4. Which of the following operations is not allowed for the array `x` declared as `int x[6];`?**
a) `x[0] = 5;`
b) `x[1] = x[2];`
c) `x = &n;`
d) `x[5] = 10;`
---
**5. What is the purpose of the `delete[] y;` statement in dynamic memory management?**
**Correct Answer:** b) Releases the memory allocated to the array to avoid memory leaks
---
**6. What is the primary difference between an array and a list data structure?**
a) Arrays store elements of different types, while lists store elements of the same type
b) Arrays are fixed in size, while lists can dynamically grow and shrink
c) Arrays allow insertion and deletion at any position, while lists do not
**Correct Answer:** b) Arrays are fixed in size, while lists can dynamically grow and shrink
---
**7. Which operation would you use to create an exact copy of a list?**
a) `copy()`
b) `clone()`
c) `duplicate()`
d) `replicate()`
---
---
**9. What does the `insert(X, position)` function do in a list?**
**Correct Answer:** c) Inserts the element `X` at the specified `position` in the list
---
**10. Which function would you use to check if a specific element `X` is present in a list?**
a) `find(X)`
b) `contains(X)`
c) `search(X)`
d) `locate(X)`
b) Moves the "current" pointer to the very first element of the list
**Correct Answer:** b) Moves the "current" pointer to the very first element of the list
---
**2. Which function moves the "current" pointer to the last element of the list?**
a) `end()`
b) `back()`
c) `tail()`
d) `final()`
---
---
**4. How does the `back()` function affect the "current" pointer in a list?**
---
**5. If you want to navigate to the first element in a list after moving forward and backward, which
function should you use?**
a) `start()`
b) `tail()`
c) `back()`
d) `next()`
Chapter#2
### Quiz: Implementing and Understanding List Data Structures in C++
---
**1. When implementing a list using an array in C++, why might you start indexing from 1 instead of
0?**
a) To avoid using the zeroth position for simplicity
b) To utilize all positions in the array
c) To improve memory management
d) To follow standard C++ practices
**Correct Answer:** a) To avoid using the zeroth position for simplicity
---
**2. What happens when you call the `add(9)` method when the current position in the list is 3?**
a) The element 9 replaces the current element at position 3
b) The element 9 is added to the end of the list
c) The element 9 is inserted at position 4, and all subsequent elements are shifted one position to the
right
d) The element 9 is inserted at the beginning of the list
**Correct Answer:** c) The element 9 is inserted at position 4, and all subsequent elements are shifted
one position to the right
---
**3. After adding an element to the list using `add()`, what changes occur in the array representation of
the list?**
a) The size of the array is decreased by one
b) The current position is moved forward by one, and the size is increased by one
c) The array is reallocated to accommodate the new element
d) The first element of the list is replaced
**Correct Answer:** b) The current position is moved forward by one, and the size is increased by one
---
**4. What is the function of the `next()` method in the context of list manipulation?**
a) It adds a new element to the list
b) It moves the current position one position forward in the list
c) It removes the last element of the list
d) It resets the current position to the start of the list
**Correct Answer:** b) It moves the current position one position forward in the list
---
**5. How do you handle boundary conditions in a list implemented using an array?**
a) By using dynamic memory allocation to expand the array when needed
b) By writing additional code to check if the current position is at the start or end of the list before
moving it
c) By ensuring that the array size is always large enough to accommodate new elements
d) By resetting the list when the boundary is reached
**Correct Answer:** b) By writing additional code to check if the current position is at the start or end
of the list before moving it
---
**6. What happens if you attempt to move the "current" pointer forward when it is already at the last
element of the array?**
a) The pointer wraps around to the first element
b) An error occurs as the pointer cannot move beyond the last element
c) The list is automatically expanded to add more elements
d) The pointer remains at the last element
**Correct Answer:** b) An error occurs as the pointer cannot move beyond the last element
---
**7. In a list implemented using an array of size 100, what happens if you attempt to add the 101st
element?**
a) The element is added, and the array size is automatically increased
b) The list is cleared and starts from the first element again
c) An error occurs as there is no space available in the array
d) The 101st element replaces the first element
**Correct Answer:** c) An error occurs as there is no space available in the array
---
**8. Why is it important to consider boundary conditions when working with lists in C++?**
a) To ensure that the list operates efficiently
b) To avoid runtime errors and out-of-bounds access
c) To allow for dynamic resizing of the list
d) To maintain the order of elements in the list
**Correct Answer:** b) To avoid runtime errors and out-of-bounds access
---
**9. Which method would you use to move the current position backward in a list?**
a) `start()`
b) `previous()`
c) `back()`
d) `reverse()`
**Correct Answer:** c) `back()`
---
**10. If the current position is at index 1 in a list, what happens if you call the `back()` method?**
a) The current position moves to the last element
b) An error occurs because there is no previous element
c) The current position moves to the zeroth element
d) The list is reversed
**Correct Answer:** b) An error occurs because there is no previous element
**1. What is the result of calling the `remove()` method when the current position is at index 5 and the
element at that position is 7?**
a) The element 7 is removed and the remaining elements are shifted to the right
b) The element 7 is removed and the size of the list is increased by one
c) The element 7 is removed, and all elements to the right are shifted one position to the left
d) The element 7 is replaced with 0 and the size of the list remains unchanged
**Correct Answer:** c) The element 7 is removed, and all elements to the right are shifted one position
to the left
---
**2. After removing an element from the list, what happens to the `current` pointer?**
a) The `current` pointer moves to the next element
b) The `current` pointer moves to the previous element
c) The `current` pointer remains at the same index, pointing to the new element that occupies the
removed element's place
d) The `current` pointer is reset to the start of the list
**Correct Answer:** c) The `current` pointer remains at the same index, pointing to the new element
that occupies the removed element's place
---
**3. How does the `remove()` method affect the size of the list?**
a) It increases the size of the list by one
b) It decreases the size of the list by one
c) It leaves the size of the list unchanged
d) It doubles the size of the list
**Correct Answer:** b) It decreases the size of the list by one
---
---
**5. If the `find(x)` function does not locate the element `x` in the list, what does it return?**
a) 1 (true)
b) 0 (false)
c) -1
d) The index of the element
**Correct Answer:** b) 0 (false)
---
**6. When using the `remove()` method, which of the following operations is performed to maintain the
integrity of the list?**
a) Reallocate the list to a larger size
b) Shift elements on the right of the removed element one position to the right
c) Shift elements on the right of the removed element one position to the left
d) Insert a placeholder element at the removed position
**Correct Answer:** c) Shift elements on the right of the removed element one position to the left
---
**7. In the context of the `find(x)` function, what does the statement `if (j < size + 1)` check?**
a) It verifies if the element `x` is within the list boundaries
b) It checks if the index `j` is within the valid range of the list
c) It ensures that the element `x` was found and `j` is a valid position
d) It validates if `x` is the last element in the list
**Correct Answer:** c) It ensures that the element `x` was found and `j` is a valid position
---
**8. If the `current` pointer is at index 5 and you call `remove()`, what will be the new index of the
`current` pointer if element 7 is removed?**
a) It will point to index 6
b) It will point to index 4
c) It will point to index 5
d) It will be reset to the start of the list
**Correct Answer:** c) It will point to index 5
---
---
**10. After the `find(x)` function is executed and the element `x` is found, what happens to the `current`
pointer?**
a) It is reset to the beginning of the list
b) It points to the position of the found element `x`
c) It moves to the next element after `x`
d) It remains unchanged
**Correct Answer:** b) It points to the position of the found element `x`
**1. In the `find(x)` function, what does the `break` statement do when an element `x` is found?**
a) It terminates the function immediately
b) It exits the for loop
c) It sets the `current` pointer to the start of the list
d) It continues the loop to the next iteration
**Correct Answer:** b) It exits the for loop
---
**2. What value does `find(x)` return if the element `x` is found in the list?**
a) 0
b) -1
c) 1
d) The index of the element `x`
**Correct Answer:** c) 1
---
**3. What does the `get()` method do in a list implemented with an array?**
a) Adds a new element to the list
b) Retrieves the element at the current position
c) Removes the element at the current position
d) Updates the value at a specified position
**Correct Answer:** b) Retrieves the element at the current position
---
---
---
---
---
**8. How does the `end()` method affect the `current` pointer?**
a) It sets the `current` pointer to the beginning of the list
b) It moves the `current` pointer one position to the end of the list
c) It sets the `current` pointer to the last element
d) It sets the `current` pointer to a specified index
**Correct Answer:** c) It sets the `current` pointer to the last element
---
**9. What is the worst-case time complexity for the `add()` method when inserting an element at the start
of the list?**
a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)
**Correct Answer:** c) O(n)
---
**10. In the context of the `remove()` method, what is the worst-case scenario for removing an
element?**
a) Removing an element from the end of the list
b) Removing an element from the middle of the list
c) Removing an element from the beginning of the list
d) Removing an element when the list is empty
**Correct Answer:** c) Removing an element from the beginning of the list
---
**11. When using a linked list, what does each node contain?**
a) The value of the element and the index of the next element
b) The value of the element and a pointer to the next node
c) The index of the element and the value of the next node
d) The value of the element and the size of the list
**Correct Answer:** b) The value of the element and a pointer to the next node
---
**12. How do you know if you have reached the end of a linked list?**
a) The `current` pointer points to NULL
b) The `head` pointer is NULL
c) The last node’s `next` pointer is NULL
d) The `current` pointer is equal to the size of the list
**Correct Answer:** c) The last node’s `next` pointer is NULL
---
**13. In a linked list, what role does the `head` pointer serve?**
a) Points to the last node of the list
b) Points to the current node being processed
c) Points to the first node of the list
d) Points to a specific element in the list
**Correct Answer:** c) Points to the first node of the list
---
**14. What happens when you remove an element from a linked list?**
a) The element is deleted, and the list is reindexed
b) The element is replaced with NULL
c) The nodes are rearranged, and the link to the next node is updated
d) The linked list is reset to its initial state
**Correct Answer:** c) The nodes are rearranged, and the link to the next node is updated
---
**15. What is the advantage of using a linked list over an array for dynamic data storage?**
a) Faster access to elements
b) Constant time complexity for element access
c) Dynamic size and no need for resizing
d) Simpler memory management
**Correct Answer:** c) Dynamic size and no need for resizing
---
**16. How do you traverse a linked list from the beginning to the end?**
a) Access elements by their index
b) Use a for loop with the index of each element
c) Follow the `next` pointers from the `head` node to the end
d) Reverse the list and access elements from the end
**Correct Answer:** c) Follow the `next` pointers from the `head` node to the end
---
---
**18. In the context of a linked list, what does the `next` field in a node indicate?**
a) The position of the next node in the list
b) The memory address of the next node
c) The value of the next element in the list
d) The size of the linked list
**Correct Answer:** b) The memory address of the next node
---
---
**20. How do you add a new node to the end of a linked list?**
a) Update the `head` pointer to the new node
b) Traverse the list to find the last node and update its `next` pointer to the new node
c) Insert the new node at the beginning and adjust the `head` pointer
d) Replace the `current` node with the new node
**Correct Answer:** b) Traverse the list to find the last node and update its `next` pointer to the new
node
Chapter#3
Here’s a comprehensive quiz based on array and linked list operations, complete with multiple-choice
questions and the correct answers:
---
**2. What value does `find(x)` return if the element `x` is found in the list?**
a) 0
b) -1
c) 1
d) The index of the element `x`
**Correct Answer:** c) 1
**3. What does the `get()` method do in a list implemented with an array?**
a) Adds a new element to the list
b) Retrieves the element at the current position
c) Removes the element at the current position
d) Updates the value at a specified position
**Correct Answer:** b) Retrieves the element at the current position
**8. How does the `end()` method affect the `current` pointer?**
a) It sets the `current` pointer to the beginning of the list
b) It moves the `current` pointer one position to the end of the list
c) It sets the `current` pointer to the last element
d) It sets the `current` pointer to a specified index
**Correct Answer:** c) It sets the `current` pointer to the last element
**9. What is the worst-case time complexity for the `add()` method when inserting an element at the start
of the list?**
a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)
**Correct Answer:** c) O(n)
**10. In the context of the `remove()` method, what is the worst-case scenario for removing an
element?**
a) Removing an element from the end of the list
b) Removing an element from the middle of the list
c) Removing an element from the beginning of the list
d) Removing an element when the list is empty
**Correct Answer:** c) Removing an element from the beginning of the list
**11. When using a linked list, what does each node contain?**
a) The value of the element and the index of the next element
b) The value of the element and a pointer to the next node
c) The index of the element and the value of the next node
d) The value of the element and the size of the list
**Correct Answer:** b) The value of the element and a pointer to the next node
**12. How do you know if you have reached the end of a linked list?**
a) The `current` pointer points to NULL
b) The `head` pointer is NULL
c) The last node’s `next` pointer is NULL
d) The `current` pointer is equal to the size of the list
**Correct Answer:** c) The last node’s `next` pointer is NULL
**13. In a linked list, what role does the `head` pointer serve?**
a) Points to the last node of the list
b) Points to the current node being processed
c) Points to the first node of the list
d) Points to a specific element in the list
**Correct Answer:** c) Points to the first node of the list
**14. What happens when you remove an element from a linked list?**
a) The element is deleted, and the list is reindexed
b) The element is replaced with NULL
c) The nodes are rearranged, and the link to the next node is updated
d) The linked list is reset to its initial state
**Correct Answer:** c) The nodes are rearranged, and the link to the next node is updated
**15. What is the advantage of using a linked list over an array for dynamic data storage?**
a) Faster access to elements
b) Constant time complexity for element access
c) Dynamic size and no need for resizing
d) Simpler memory management
**Correct Answer:** c) Dynamic size and no need for resizing
**16. How do you traverse a linked list from the beginning to the end?**
a) Access elements by their index
b) Use a for loop with the index of each element
c) Follow the `next` pointers from the `head` node to the end
d) Reverse the list and access elements from the end
**Correct Answer:** c) Follow the `next` pointers from the `head` node to the end
**20. How do you add a new node to the end of a linked list?**
a) Update the `head` pointer to the new node
b) Traverse the list to find the last node and update its `next` pointer to the new node
c) Insert the new node at the beginning and adjust the `head` pointer
d) Replace the `current` node with the new node
**Correct Answer:** b) Traverse the list to find the last node and update its `next` pointer to the new
node
Here's a twenty-question quiz based on the information provided about linked lists and their
implementation in C++:
2. **In the linked list memory representation, what does the value `0` in the pointer part of a node
signify?**
- A) The node is the first in the list
- B) The node is the last in the list
- C) The node has no data
- D) The node is invalid
3. **When a new node is added to a linked list, where is it typically inserted in relation to the current
node?**
- A) Before the current node
- B) After the current node
- C) At the beginning of the list
- D) At the end of the list
4. **In the provided C++ code, what does the `new Node(9)` statement do?**
- A) Deletes a node with the value `9`
- B) Creates a new node with the value `9`
- C) Updates the existing node with the value `9`
- D) Finds a node with the value `9`
7. **Which part of the `add()` method in the `List` class ensures that the new node points to the next node
after it?**
- A) `newNode->setNext(currentNode->getNext());`
- B) `currentNode->setNext(newNode);`
- C) `currentNode = newNode;`
- D) `size++;`
9. **In the `add()` method, what does the `else` block handle?**
- A) Inserting a new node when the list is not empty
- B) Inserting a new node when the list is empty
- C) Removing a node from the list
- D) Retrieving data from the list
10. **What does the `get()` method of the `List` class return?**
- A) The value of the last node
- B) The pointer to the current node
- C) The data value of the current node
- D) The total number of nodes in the list
11. **Which file typically contains the implementation of methods for a class in C++?**
- A) .h file
- B) .cpp file
- C) .txt file
- D) .cpp and .h file
13. **What does the `next()` method return if the `currentNode` is `NULL`?**
- A) `true`
- B) `false`
- C) `0`
- D) `1`
14. **In the `List` class constructor, why is `headNode` set to a new `Node` with `setNext(NULL)`?**
- A) To initialize `headNode` as an empty node
- B) To prepare `headNode` for future node additions
- C) To delete existing nodes
- D) To create a node with data
15. **What is the main function of the `traverse()` friend function in the `List` class?**
- A) To add nodes to the list
- B) To delete nodes from the list
- C) To display the elements of the list
- D) To retrieve the current node
17. **What is the role of the `size` variable in the `List` class?**
- A) To keep track of the node values
- B) To store the memory address of nodes
- C) To count the total number of nodes in the list
- D) To store the next node's address
19. **Which method is used to move the `currentNode` pointer to the next node in the `List` class?**
- A) `add()`
- B) `get()`
- C) `next()`
- D) `traverse()`
**Correct Answer: C**
20. **What is the main use of `Node *` pointers in the linked list implementation?**
- A) To store node data
- B) To keep track of node addresses
- C) To increment the size of the list
- D) To initialize the list
**Correct Answer: B
Chapter#4
Here’s a 20-question multiple-choice quiz based on the provided lecture on linked lists. Each question is
followed by the correct answer for reference.
---
**2. In the `remove()` method, which line of code updates the `next` pointer of the node before the node
to be removed?**
A) `delete currentNode;`
B) `currentNode = lastCurrentNode;`
C) `lastCurrentNode->setNext(currentNode->getNext());`
D) `size--;`
**Answer:** C) `lastCurrentNode->setNext(currentNode->getNext());`
**4. What is the purpose of the `delete currentNode;` statement in the `remove()` method?**
A) To reassign `currentNode` to the next node
B) To free the memory allocated for `currentNode`
C) To increment the size of the list
D) To move `currentNode` to `lastCurrentNode`
**Answer:** B) To free the memory allocated for `currentNode`
**5. After calling the `remove()` method, what does `currentNode = lastCurrentNode;` do?**
A) Updates the `currentNode` to point to the node that was before the removed node
B) Deletes the node pointed to by `lastCurrentNode`
C) Sets `currentNode` to NULL
D) Sets `lastCurrentNode` to NULL
**Answer:** A) Updates the `currentNode` to point to the node that was before the removed node
**8. Which method would you use to traverse through the list after calling `start()`?**
A) `length()`
B) `remove()`
C) `next()`
D) `add()`
**Answer:** C) `next()`
**9. What will happen if the `remove()` method is called on the `headNode`?**
A) It will remove the `headNode` and update `headNode` to the next node
B) It will do nothing
C) It will set the `headNode` to NULL
D) It will only decrement the size of the list
**Answer:** A) It will remove the `headNode` and update `headNode` to the next node
**11. Which of the following is a new method added in the doubly-linked list node class?**
A) `getNext()`
B) `setNext()`
C) `getPrev()`
D) `setPrev()`
**Answer:** C) `getPrev()`
**12. In a doubly-linked list, what does the `setPrev(Node* prevNode)` method do?**
A) Sets the value of the node
B) Points to the next node in the list
C) Sets the `prevNode` pointer
D) Deletes the previous node
**Answer:** C) Sets the `prevNode` pointer
**15. In the Josephus Problem, what does the `remove()` method do?**
A) Adds a node to the list
B) Removes the node at a specific position
C) Moves the `currentNode` to the next node
D) Updates the size of the list
**Answer:** B) Removes the node at a specific position
**16. When using the `add` method in a linked list, what operation is performed?**
A) A new node is inserted after the current node
B) The list is sorted
C) The head node is deleted
D) The list size is decreased
**Answer:** A) A new node is inserted after the current node
**17. In a doubly-linked list, how do you update the pointers to insert a new node between two existing
nodes?**
A) Update `nextNode` of the previous node and `prevNode` of the next node
B) Only update the `nextNode` of the new node
C) Only update the `prevNode` of the new node
D) Delete both adjacent nodes and add the new node
**Answer:** A) Update `nextNode` of the previous node and `prevNode` of the next node
**18. What happens when you call the `back()` method in a singly-linked list?**
A) It moves the `currentNode` one step back
B) It is not supported in singly-linked lists
C) It moves the `currentNode` to the start of the list
D) It deletes the previous node
**Answer:** B) It is not supported in singly-linked lists
**20. In the code provided for the Josephus Problem, what does the `while (list.length() > 1)` loop do?**
A) It counts the total number of nodes in the list
B) It continues removing nodes until only one node remains
C) It adds new nodes to the list
D) It prints all nodes in the list
**Answer:** B) It continues removing nodes until only one node remains
#### Question 1
What does the statement `list.next()` do in the given context?
- A) Moves the pointer to the previous node
- B) Moves the pointer forward by a specified number of nodes
- C) Removes the current node
- D) Displays the value of the current node
#### Question 2
If the value of `M` is 3, how many times will `list.next()` be called before calling the `remove` method?
- A) 1
- B) 2
- C) 3
- D) 4
**Answer:** C) 3
#### Question 3
After removing a node, how does the position of the pointer change in the given problem?
- A) It stays at the same node
- B) It moves to the previous node
- C) It moves to the next node
- D) It moves to the start of the list
#### Question 4
In the while loop described, what happens when the length of the list becomes less than or equal to 1?
- A) The while loop continues indefinitely
- B) The while loop is terminated
- C) The pointer moves to the start of the list
- D) A new node is added to the list
#### Question 5
How does the list behavior change after a node is removed?
- A) The list resets to the beginning
- B) The counting starts from the next node
- C) The list is sorted
- D) The pointer moves to the previous node
#### Question 6
What is the purpose of incrementing the value of `M` in different iterations?
- A) To make the list shorter
- B) To vary the step count for removing nodes
- C) To reset the list to its original state
- D) To double the number of nodes
#### Question 7
What is the final result when the length of the list is one?
- A) The list is cleared
- B) The node is re-inserted at the start
- C) The single remaining node's value is displayed
- D) The list starts again from the beginning
#### Question 8
If `M` is initially set to 3 and the list length is 10, which node will be removed first?
- A) 1st node
- B) 4th node
- C) 7th node
- D) 10th node
#### Question 9
If the length of the list is 9 and `M` is 3, which node will be pointed to after three calls to `list.next()`
starting from the 4th node?
- A) 6th node
- B) 7th node
- C) 8th node
- D) 9th node
**Answer:** C) 8th node
#### Question 10
What is the significance of the Josephus problem in the context described?
- A) It determines the leader based on a fixed step count
- B) It calculates the total number of nodes
- C) It sorts the nodes in ascending order
- D) It resets the list position
Chapter#5
Here's a twenty-question multiple-choice quiz based on the provided material on circular linked lists,
abstract data types, and stack implementations:
### 1. **What is the primary advantage of using a circular linked list in the Josephus problem?**
a) Faster insertion and deletion operations
b) Simplicity in removing elements in a circular manner
c) Easier to access elements randomly
d) Reduced memory usage
**Answer:** b) Simplicity in removing elements in a circular manner
### 2. **Which method in the `CList` class starts the traversal from the beginning of the list?**
a) `add()`
b) `start()`
c) `next()`
d) `remove()`
**Answer:** b) `start()`
### 3. **In the Josephus problem program, what does the `remove()` method do?**
a) Moves to the next element
b) Adds a new element to the list
c) Removes the current element from the list
d) Retrieves the current element
**Answer:** c) Removes the current element from the list
### 4. **What is the primary benefit of using encapsulation in data structures like `CList`?**
a) Faster execution of code
b) Hiding the internal implementation details from the user
c) Reducing memory usage
d) Allowing multiple inheritance
**Answer:** b) Hiding the internal implementation details from the user
### 5. **Which method of the stack data structure adds an element to the top?**
a) `pop()`
b) `top()`
c) `push()`
d) `isEmpty()`
**Answer:** c) `push()`
### 8. **In the provided stack implementation, what should the user do before calling `pop()`?**
a) Call `isFull()`
b) Call `top()`
c) Call `isEmpty()`
d) Call `push()`
**Answer:** c) Call `isEmpty()`
### 9. **Which data structure is used to solve the Josephus problem in the provided program?**
a) Array
b) Singly Linked List
c) Doubly Linked List
d) Circular Linked List
**Answer:** d) Circular Linked List
### 10. **What does the `add()` method in the `CList` class do?**
a) Adds a new element to the list
b) Retrieves the current element
c) Removes the current element
d) Moves to the next element
**Answer:** a) Adds a new element to the list
### 12. **Which method checks if the stack is full in the provided stack implementation?**
a) `isEmpty()`
b) `top()`
c) `isFull()`
d) `push()`
**Answer:** c) `isFull()`
### 13. **In a stack, what is the time complexity for both `push()` and `pop()` operations when
implemented with an array?**
a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)
**Answer:** a) O(1)
### 14. **What happens if you try to pop an element from an empty stack in the provided
implementation?**
a) The stack will be reset
b) An exception will be thrown
c) The stack will display an error message
d) The stack will crash
**Answer:** c) The stack will display an error message
### 15. **What is the main disadvantage of using an array to implement a stack?**
a) It is slow for insertion and deletion
b) It requires dynamic memory allocation
c) It has a fixed size and can become full
d) It does not support the `top()` method
**Answer:** c) It has a fixed size and can become full
### 16. **In the context of the stack implementation, what does the `push()` method do?**
a) Removes the top element
b) Checks if the stack is full
c) Adds a new element to the top
d) Retrieves the top element without removing it
**Answer:** c) Adds a new element to the top
### 17. **Which method in a circular linked list helps in moving to the next element?**
a) `start()`
b) `get()`
c) `remove()`
d) `next()`
**Answer:** d) `next()`
### 18. **What is the time complexity of the `length()` method in a circular linked list?**
a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)
**Answer:** b) O(n)
### 19. **What does the `isEmpty()` method of a stack return if the stack has elements?**
a) True
b) False
c) Null
d) An exception
**Answer:** b) False
### 20. **What is a common real-life example of a stack?**
a) A list of books on a shelf
b) A circular queue
c) A stack of plates in a cafeteria
d) A doubly linked list
**Answer:** c) A stack of plates in a cafeteria
Chapter #6
Here’s a 30-question multiple-choice quiz based on the lecture on Stack data structures, including both
array-based and linked list implementations:
2. **In a stack implemented using an array, where are elements typically pushed and popped from?**
- A) The beginning of the array
- B) The middle of the array
- C) The end of the array
- D) Random positions in the array
**Answer:** B) isFull()
4. **What is the time complexity of the `push()` operation in a stack implemented using a linked list?**
- A) O(1)
- B) O(n)
- C) O(log n)
- D) O(n^2)
**Answer:** A) O(1)
5. **In a stack implemented with a singly-linked list, where are new elements inserted?**
- A) At the end of the list
- B) At the beginning of the list
- C) In the middle of the list
- D) At random positions
6. **Which method in a stack implemented with a linked list retrieves the top element without removing
it?**
- A) push()
- B) pop()
- C) top()
- D) isEmpty()
**Answer:** C) top()
7. **Which method is responsible for removing the top element from a stack implemented using a linked
list?**
- A) push()
- B) pop()
- C) top()
- D) isFull()
**Answer:** B) pop()
8. **What is the purpose of the `isEmpty()` method in a stack implemented using a linked list?**
- A) To check if the stack is full
- B) To check if the stack has no elements
- C) To get the top element
- D) To remove the top element
9. **Why might a stack implemented with a linked list be preferred over one implemented with an
array?**
- A) It supports random access
- B) It has no fixed size limitation
- C) It uses less memory
- D) It is easier to implement
10. **What is the time complexity of the `pop()` operation in a stack implemented using a linked list?**
- A) O(1)
- B) O(n)
- C) O(log n)
- D) O(n^2)
**Answer:** A) O(1)
11. **In the context of stacks, what does LIFO stand for?**
- A) Last-In-First-Out
- B) Least-In-First-Out
- C) Last-In-First-Ordered
- D) Largest-In-First-Out
**Answer:** A) Last-In-First-Out
12. **In a linked list implementation of a stack, what does the `head` pointer represent?**
- A) The last node in the list
- B) The first node in the list
- C) The middle node in the list
- D) A null value
13. **When implementing a stack with a linked list, which pointer needs to be updated during the
`push()` operation?**
- A) The `next` pointer of the new node
- B) The `head` pointer
- C) The `previous` pointer of the current node
- D) The `last` pointer
14. **Which method should be called before performing a `pop()` operation to ensure that the stack is not
empty?**
- A) isFull()
- B) top()
- C) push()
- D) isEmpty()
**Answer:** D) isEmpty()
15. **What does the `top()` method return in a stack implemented with a linked list?**
- A) The value of the node at the end of the list
- B) The value of the node at the beginning of the list
- C) The value of the node that was just added
- D) The number of nodes in the stack
16. **Which of the following is NOT a reason to prefer a linked list implementation of a stack over an
array?**
- A) Dynamic memory allocation
- B) Avoidance of fixed size limitation
- C) Lower memory overhead for pointers
- D) Simplicity of implementation
17. **What happens in a stack when the computer's address space becomes full?**
- A) The stack implementation itself fails
- B) The stack can continue to grow indefinitely
- C) The stack becomes empty
- D) The stack operations are unaffected
**Answer:** A) The stack implementation itself fails
18. **In postfix notation, how would you write the expression (A + B) * C?**
- A) A B + C *
- B) A + B C *
- C) (A B +) C *
- D) A + B * C
**Answer:** A) A B + C *
20. **Which of the following is NOT a method of a stack implemented using a linked list?**
- A) push()
- B) pop()
- C) top()
- D) isFull()
**Answer:** D) isFull()
21. **What is the time complexity of checking if a stack is empty using a linked list implementation?**
- A) O(1)
- B) O(n)
- C) O(log n)
- D) O(n^2)
**Answer:** A) O(1)
22. **How does a stack's implementation affect its memory usage when using an array?**
- A) Memory is allocated dynamically as needed
- B) Memory is allocated all at once, potentially wasting space
- C) Memory is allocated in chunks of fixed size
- D) Memory is never wasted
23. **In a singly-linked list stack, where does the `next` pointer of each node point?**
- A) To the previous node in the list
- B) To the next node in the list
- C) To a random node in the list
- D) To a null value
24. **Which of the following is a disadvantage of using a linked list to implement a stack?**
- A) Requires additional memory for pointers
- B) Cannot handle large amounts of data
- C) Does not support LIFO operations
- D) Requires fixed-size memory allocation
25. **What is the result of converting the infix expression A + B to postfix notation?**
- A) A B +
- B) A + B
- C) + A B
- D) A B
**Answer:** A) A B +
26. **What does the `isFull()` method check in a stack implemented with an array?**
- A) If the stack has reached its capacity
- B) If the stack has no elements
- C) If the stack has more than one element
- D) If the stack can accept more elements without resizing
27. **When using a stack for expression evaluation, what is the postfix form of the expression 12 + 60 -
23?**
- A) 12 60 + 23 -
- B) 12 60 - 23 +
- C) 12 + 60 - 23
- D) + 12 60 - 23
**Answer:** A) 12 60 + 23 -
28. **What is a key advantage of using a linked list over an array for stack implementation?**
- A) More complex pointer management
- B) Fixed memory allocation
- C) Dynamic size adjustment
- D) Faster memory access
30. **Which of the following operations is not constant time (O(1)) in a stack implemented with an
array?**
- A) push()
- B) pop()
- C) top()
- D) isFull()
**Answer:** D) isFull()
Chapter#7
Here’s a 30-question quiz based on your lecture notes about infix and postfix expressions, including their
conversion and evaluation. Each question has multiple choice answers, with the correct answers indicated.
---
**Answer:** A) `AB+`
**3. Which of the following is a valid postfix expression for the infix expression `(4 + 3) * 5`?**
- A) `43+5*`
- B) `4 3 + 5 *`
- C) `4 + 3 5 *`
- D) `4 3 5 * +`
**Answer:** A) `43+5*`
**5. Which operation would you perform first in the postfix expression `4 3 2 * +`?**
- A) Addition
- B) Subtraction
- C) Multiplication
- D) Division
**Answer:** C) Multiplication
**Answer:** B) Pop two operands from the stack, apply the operator, and push the result
**7. What does the `push()` operation do in the context of stack implementation?**
- A) Retrieves the top element
- B) Adds an element to the top of the stack
- C) Removes an element from the top of the stack
- D) Checks if the stack is empty
**8. Which of the following operations is not constant time (O(1)) in a stack implemented with an
array?**
- A) push()
- B) pop()
- C) top()
- D) isFull()
**Answer:** D) isFull()
**9. What is the postfix notation of the infix expression `12 + 60 - 23`?**
- A) `12 60 23 - +`
- B) `12 60 + 23 -`
- C) `12 60 23 + -`
- D) `12 60 23 - +`
**Answer:** B) `12 60 + 23 -`
**10. For the infix expression `A * B + C`, which operation has the highest precedence?**
- A) Addition
- B) Multiplication
- C) Subtraction
- D) Division
**Answer:** B) Multiplication
**Answer:** A) `AB*C+`
**12. How are operators with higher precedence handled in postfix notation?**
- A) They are placed before lower precedence operators.
- B) They are placed after lower precedence operators.
- C) They are ignored.
- D) Their precedence is determined by parentheses.
**Answer:** A) They are placed before lower precedence operators.
**13. In evaluating the postfix expression `6 2 3 + 5 *`, what is the first step?**
- A) Apply addition
- B) Apply multiplication
- C) Push the numbers onto the stack
- D) Pop the top two numbers from the stack
**14. What does the function `prcd(op1, op2)` do in the context of infix to postfix conversion?**
- A) Checks if `op1` has precedence over `op2`
- B) Determines if `op1` is an operand
- C) Compares `op1` and `op2` for equality
- D) Converts `op1` and `op2` to postfix
**15. What happens when encountering a closing parenthesis `)` in the infix to postfix conversion
algorithm?**
- A) Push it onto the stack
- B) Pop operators from the stack until encountering an opening parenthesis
- C) Discard it
- D) Add it to the postfix string
**Answer:** B) Pop operators from the stack until encountering an opening parenthesis
**16. Which algorithm step is responsible for converting the infix expression `A + B * C` to postfix?**
- A) Pushing all operands onto the stack
- B) Applying all operators immediately
- C) Pushing operators with lower precedence onto the stack
- D) Using parentheses to enforce operator precedence
**Answer:** D) 14
**19. When converting infix to postfix, what does encountering an operator `op` while the stack contains
operators with higher or equal precedence mean?**
- A) Push `op` onto the stack immediately
- B) Pop operators from the stack to the postfix string until an operator of lower precedence is
encountered
- C) Ignore the operator `op`
- D) Discard the top operator from the stack
**Answer:** B) Pop operators from the stack to the postfix string until an operator of lower
precedence is encountered
**Answer:** C) Subtraction
**22. What does the `pop()` operation do in the context of stack implementation?**
- A) Adds an element to the stack
- B) Retrieves and removes the top element from the stack
- C) Checks if the stack is empty
- D) Peeks at the bottom element of the stack
**Answer:** B) Retrieves and removes the top element from the stack
**Answer:** A) 23
**25. Which operator has the highest precedence in most programming languages?**
- A) Addition
- B) Subtraction
- C) Multiplication
- D) Division
**Answer:** C) Multiplication
**26. In the infix expression `(A + B) * (C - D)`, how are the parentheses used?**
- A) To enforce precedence of operations
- B) To change the order of operands
- C) To separate operators
- D) To denote unary operations
**Answer:** A) `ABC+*D-`
**Answer:** C) 20
**Answer:** A) `A B C - / D +`
**30. What is the final result of evaluating the postfix expression `7 8 + 3 2 + /`?**
- A) 2
- B) 5
- C) 6
- D) 4
**Answer:** C) 6
Chapter#8
Sure! Here’s a 30-question multiple-choice quiz on the conversion of infix expressions to postfix notation,
C++ templates, and stack usage:
---
### Quiz on Infix to Postfix Conversion, C++ Templates, and Stack Usage
**2. How is the expression `A + B * C` converted to postfix notation if addition has higher precedence
than multiplication?**
A) `AB+C*`
B) `A B C * +`
C) `(A B C * +)`
D) `AB C * +`
**3. In the conversion process from infix to postfix, what happens to opening parentheses?**
A) They are discarded
B) They are converted to closing parentheses
C) They are pushed onto the stack
D) They are placed in the postfix expression
**4. When encountering a closing parenthesis in infix to postfix conversion, what should be done?**
A) Discard the closing parenthesis
B) Push the closing parenthesis onto the stack
C) Pop operators from the stack until an opening parenthesis is encountered
D) Convert the closing parenthesis to a subtraction operator
*Correct Answer: C) Pop operators from the stack until an opening parenthesis is encountered*
**5. What is the postfix notation for the infix expression `(A + B) * C`?**
A) `AB+C*`
B) `A B + C *`
C) `A + B C *`
D) `A B C * +`
**7. What data structure is commonly used to convert an infix expression to postfix notation?**
A) Queue
B) Stack
C) List
D) Array
**9. Which of the following is NOT a member function of the Stack class template?**
A) `push()`
B) `pop()`
C) `peek()`
D) `sort()`
**10. What does the `push()` function of the Stack class do?**
A) Removes the top element from the stack
B) Returns the top element of the stack
C) Adds an element to the top of the stack
D) Checks if the stack is empty
*Correct Answer: A) 5*
**12. What is the purpose of `#define MAXSTACKSIZE 50` in the Stack implementation?**
A) To define the maximum size of the stack
B) To create a constant named `MAXSTACKSIZE`
C) To allocate memory for the stack
D) To initialize the stack
**13. What happens if you attempt to push an element onto a full stack?**
A) The element is added and the stack size is increased
B) The stack overflows and an error message is shown
C) The element is discarded
D) The stack is automatically resized
**14. How is memory managed for the stack in the provided code?**
A) Static memory allocation
B) Dynamic memory allocation using `new` and `delete`
C) Automatic memory management
D) Memory is not allocated
**15. What is the purpose of the `peek()` function in the Stack class?**
A) To add an element to the stack
B) To remove and return the top element of the stack
C) To return the top element without removing it
D) To check if the stack is full
**16. In the context of function calls, what does the stack contain?**
A) Only the return address
B) Only the function arguments
C) Return address, function arguments, and local variables
D) Only the local variables
**19. In the function call stack, where is the return address stored?**
A) At the bottom of the stack
B) At the top of the stack
C) In a register
D) In the function's local variables
**21. What does the `pop()` function of the Stack class do?**
A) Adds an element to the stack
B) Removes and returns the top element of the stack
C) Returns the top element without removing it
D) Checks if the stack is full
*Correct Answer: B) Removes and returns the top element of the stack*
**22. How can you ensure that the Stack class template can be used with different data types?**
A) Use a different class for each data type
B) Write separate code for each data type
C) Use C++ templates to create a generic class
D) Use only one specific data type for the stack
**23. What is the effect of the `delete nodes;` statement in the Stack destructor?**
A) Frees the memory allocated for the stack
B) Allocates more memory for the stack
C) Resets the stack size to zero
D) Reinitializes the stack
**24. How does the stack handle function return values in assembly language?**
A) By storing the value in a global variable
B) By placing the return value on the stack
C) By storing the return value in a register
D) By discarding the return value
**26. What is the role of `T* nodes;` in the Stack class definition?**
A) To store the size of the stack
B) To point to the array of stack elements
C) To hold the top index of the stack
D) To manage memory allocation for the stack
**27. How does the `empty()` function determine if the stack is empty?**
A) By checking if the stack size is zero
B) By checking if the top index is less than zero
C) By checking if the stack pointer is null
D) By checking if the stack array is full
**28. What is the initial value of the `top` variable in the Stack constructor?**
A) 0
B) 1
C) -1
D) MAXSTACKSIZE
Chapter # 9
Here is a 30-question multiple-choice quiz based on the concepts discussed in your lecture. Each question
includes the correct answer.
**1. In the context of memory organization for processes, which section is used for function calls?**
A) Code
B) Static Data
C) Stack
D) Heap
**3. When a function is called, where are its parameters and local variables stored?**
A) In the code section
B) In the static data section
C) On the stack
D) In the heap
**4. What happens to the local variables of a function once it finishes execution?**
A) They are stored in the heap
B) They are moved to static data
C) They are removed from the stack
D) They are kept in the code section
**10. In a queue implemented using a linked list, where are elements added?**
A) At the front
B) At the rear
C) In the middle
D) Randomly
**12. In a circular array implementation of a queue, what does the `enqueue()` operation do?**
A) Adds an element at the front
B) Moves the rear pointer and adds an element
C) Removes an element from the rear
D) Checks if the queue is full
**13. When using a circular array for a queue, how is the `rear` pointer updated in `enqueue()`?**
A) It is incremented by 1 and wrapped around using modulo operation
B) It is decremented by 1
C) It is set to the front pointer
D) It is set to a fixed value
**14. What does the `dequeue()` function do in a circular array implementation of a queue?**
A) Increments the front pointer
B) Decrements the rear pointer
C) Removes an element from the front and updates the front pointer
D) Adds an element to the rear
*Correct Answer: C) Removes an element from the front and updates the front pointer*
**15. Which data structure allows both insertion and deletion at one end?**
A) Queue
B) Stack
C) Linked List
D) Array
**17. In the stack layout, what is the correct order of items stored when a function is called?**
A) Local variables, parameters, return address
B) Parameters, return address, local variables
C) Return address, parameters, local variables
D) Parameters, local variables, return address
**19. What is the purpose of the `isFull()` function in a queue implemented with an array?**
A) To check if there are elements in the queue
B) To determine if the array has reached its maximum capacity
C) To remove an element from the queue
D) To add an element to the queue
*Correct Answer: B) To determine if the array has reached its maximum capacity*
**20. Which data structure is better suited for scenarios where elements need to be processed in the order
they arrive?**
A) Stack
B) Queue
C) Linked List
D) Array
**22. In a queue, which operation retrieves the element at the front without removing it?**
A) enqueue()
B) dequeue()
C) front()
D) isEmpty()
**23. What happens to the front pointer in a queue when `dequeue()` is called?**
A) It is incremented
B) It is decremented
C) It remains unchanged
D) It is reset to zero
**24. How does a queue implemented using an array handle the wrap-around problem?**
A) By resizing the array
B) By shifting elements
C) By using a circular array
D) By using a linked list
*Correct Answer: C) By using a circular array*
**25. Which section of memory is used to store global variables and static data?**
A) Code section
B) Stack
C) Heap
D) Static data section
**26. What is the time complexity of the `enqueue()` and `dequeue()` operations in a queue implemented
using a linked list?**
A) O(1) for both operations
B) O(n) for both operations
C) O(n) for `enqueue()` and O(1) for `dequeue()`
D) O(1) for `enqueue()` and O(n) for `dequeue()`
**27. In a queue using an array, what does the `front` index represent?**
A) The position where new elements are added
B) The position of the last element
C) The position of the first element to be removed
D) The position of the middle element
**29. In a stack, what operation retrieves but does not remove the top element?**
A) push()
B) pop()
C) peek()
D) front()
Chapter#10
Here's a quiz based on the content provided about queues, simulation models, and priority queues in the
context of a banking simulation:
#### 1. What is the primary purpose of using a queue in the bank simulation described?
A) To store transaction history
B) To manage the order of customer service
C) To record customer complaints
D) To calculate daily profit
#### 2. In the provided bank simulation, what happens when a new customer arrives?
A) The customer immediately leaves the bank
B) The customer is inserted into the shortest queue
C) The customer is randomly assigned to any queue
D) The customer is served immediately without waiting
#### 4. How does the event-based simulation differ from the time-based simulation?
A) It uses a continuous clock to track events
B) It processes events based on their occurrence time rather than waiting for the clock to tick
C) It does not use queues
D) It ignores the arrival and departure times of customers
**Correct Answer: B) It processes events based on their occurrence time rather than waiting for the clock
to tick**
#### 5. What is a priority queue?
A) A queue where the first item is served first
B) A queue where items are processed based on their priority rather than their arrival time
C) A queue where items are processed randomly
D) A queue where items are processed based on their arrival time
**Correct Answer: B) A queue where items are processed based on their priority rather than their arrival
time**
#### 6. In the context of the bank simulation, what information is stored in the `customer.dat` file?
A) Bank account details
B) Customer arrival time and transaction duration
C) Teller shift timings
D) Daily bank transactions
**Correct Answer: B) A departure event is created and added to the priority queue**
**Correct Answer: C) It handles the arrival of a new customer and assigns them to the shortest queue**
**Correct Answer: C) To process a customer's departure and manage the next customer in the queue**
#### 11. What kind of data structure is used to manage customer events in the simulation?
A) Stack
B) Queue
C) Priority Queue
D) Array
#### 12. In the given C++ code, what does the `while` loop in the `main` function do?
A) It processes customer transactions directly
B) It continuously runs the simulation until all events are processed
C) It initializes the simulation
D) It reads customer data from the file
**Correct Answer: B) It continuously runs the simulation until all events are processed**
**Correct Answer: B) Reads new customer data from the file and adds an arrival event to the priority
queue**
#### 14. What is the purpose of the `Event` class in the simulation?
A) To store customer details
B) To manage the order of customer service
C) To represent a specific point in time when an event occurs, such as arrival or departure
D) To handle queue operations
**Correct Answer: C) To represent a specific point in time when an event occurs, such as arrival or
departure**
**Correct Answer: B) To ensure that items are processed in the order of their priority**
#### 16. What happens if the queue for a teller is empty when a customer arrives?
A) The customer waits in the queue
B) The customer leaves the bank
C) The customer is served immediately at the teller
D) The customer is assigned to a different teller
#### 17. How is the total wait time for all customers calculated?
A) By summing the time each customer spends at the teller
B) By calculating the time difference between arrival and departure for each customer
C) By counting the number of customers served
D) By measuring the total simulation time
**Correct Answer: B) By calculating the time difference between arrival and departure for each
customer**
**Correct Answer: B) The total time spent by all customers and the average time spent per customer**
#### 19. What does the `totalTime` variable represent in the simulation?
A) The total number of customers served
B) The cumulative time all customers have spent in the bank
C) The total time the bank is open
D) The time taken to process each transaction
**Correct Answer: B) The cumulative time all customers have spent in the bank**
#### 20. What is the role of the `Customer` class in the simulation?
A) To manage the tellers
B) To represent a customer and their details, such as arrival time and transaction duration
C) To handle the priority queue operations
D) To maintain the simulation clock
**Correct Answer: B) To represent a customer and their details, such as arrival time and transaction
duration**
#### 22. What is the purpose of incrementing the clock variable in the time-based simulation?
A) To move to the next customer in the queue
B) To simulate the passage of time and process events accordingly
C) To read new customer data
D) To update the queue lengths
**Correct Answer: B) To simulate the passage of time and process events accordingly**
**Correct Answer: C) It removes and returns the customer at the front of the queue**
**Correct Answer: B) They are created and added to the priority queue after a customer completes their
transaction**
#### 25. What determines the priority of an event in the priority queue?
A) The type of event
B) The arrival time of the customer
C) The event's occurrence time
D) The customer's transaction duration
#### 27. In the bank simulation, what is the role of the `while` loop in the `main` function?
A) To read new customer data from the file
B) To continuously process events until the priority queue is
empty
C) To initialize the simulation parameters
D) To print the results of the simulation
**Correct Answer: B) To continuously process events until the priority queue is empty**
**Correct Answer: C) Customers are served based on the length of the queue they are assigned to**
#### 29. What is the key advantage of using a priority queue in the simulation?
A) It processes events in the order they are added
B) It ensures that events are processed based on their priority, allowing efficient handling of customer
service
C) It simplifies the simulation code
D) It reduces the time complexity of the simulation
**Correct Answer: B) It ensures that events are processed based on their priority, allowing efficient
handling of customer service**
#### 30. How does the simulation ensure that the time is accurately tracked?
A) By using a fixed time increment
B) By using a continuous clock and updating the time with each event
C) By randomly adjusting the time based on customer arrivals
D) By manually setting the time for each event
**Correct Answer: B) By using a continuous clock and updating the time with each event**
Chapter# 11
Here's a 30-question multiple-choice quiz based on the content from your lecture on priority queues and
binary trees. Each question is followed by the correct answer.
**1. What is the primary characteristic of a priority queue compared to a regular queue?**
- A. FIFO order
- B. LIFO order
- C. Elements are retrieved based on priority
- D. Elements are sorted alphabetically
**2. In the provided C++ code for the `PriorityQueue`, what does the `full()` method check?**
- A. If the queue is empty
- B. If the queue size is greater than 0
- C. If the queue size is equal to `PQMAX`
- D. If the queue is not initialized
**4. What is the time complexity of removing an element from the beginning of an array in the priority
queue implementation?**
- A. O(1)
- B. O(n)
- C. O(log n)
- D. O(n^2)
**6. What is the purpose of the `insert()` method in the `PriorityQueue` class?**
- A. To check if the queue is full
- B. To remove an element from the queue
- C. To add an element to the queue and sort it
- D. To clear the queue
*Correct Answer: C. To add an element to the queue and sort it*
**7. How does the `PriorityQueue` implementation handle the situation when the queue is full?**
- A. It throws an exception
- B. It returns a NULL pointer
- C. It displays an error message and returns 0
- D. It automatically expands the queue
**8. In a binary tree, what is the term for the node at the top of the tree?**
- A. Child
- B. Leaf
- C. Root
- D. Subtree
*Correct Answer: B. A binary tree where each non-leaf node has exactly two children*
**11. In the provided binary tree example, what is the level of the root node?**
- A. 1
- B. 2
- C. 0
- D. 3
*Correct Answer: C. 0*
**15. What does the `length()` method of the `PriorityQueue` class return?**
- A. The maximum size of the queue
- B. The number of elements in the queue
- C. The index of the last element
- D. The priority of the first element
**17. What is the time complexity of sorting elements in the `PriorityQueue` after an insertion?**
- A. O(1)
- B. O(n)
- C. O(log n)
- D. O(n^2)
**19. What happens when you try to insert an element into a full priority queue?**
- A. The queue expands automatically
- B. The element is added but not sorted
- C. An error message is displayed, and the operation fails
- D. The oldest element is removed to make space
**20. Which data structure was discussed as an alternative to arrays for implementing priority queues?**
- A. Linked list
- B. Stack
- C. Graph
- D. Hash table
**21. What is the main advantage of using a tree over a linear data structure?**
- A. It allows faster searching and hierarchical data representation
- B. It uses less memory
- C. It has a simpler implementation
- D. It guarantees sorted data
*Correct Answer: A. It allows faster searching and hierarchical data representation*
**22. What does the `remove()` method return if the queue is empty?**
- A. An integer value indicating failure
- B. A NULL pointer
- C. The last inserted element
- D. A default event object
**23. In the binary tree example, what is the depth of the tree shown?**
- A. 1
- B. 2
- C. 3
- D. 4
*Correct Answer: C. 3*
**24. What is the main difference between a binary tree and a strictly binary tree?**
- A. The number of nodes
- B. The level of nodes
- C. The requirement for each non-leaf node to have exactly two children
- D. The use of sorting
*Correct Answer: C. The requirement for each non-leaf node to have exactly two children*
**25. In the binary tree, what is the term for nodes that have no children?**
- A. Internal nodes
- B. Parent nodes
- C. Leaf nodes
- D. Root nodes
**27. What term describes the process of organizing nodes in a binary tree by priority?**
- A. Sorting
- B. Balancing
- C. Hierarchical structuring
- D. Indexing
**28. What is a key advantage of using a priority queue in simulations, such as banking systems?**
- A. Efficient handling of events based on priority
- B. Automatically sorting data
- C. Limiting the number of elements
- D. Preventing duplication of data
*Correct Answer: A. All levels, except possibly the last, are completely filled*
**30. Which traversal method is commonly used for binary trees to visit nodes in a specific order?**
- A. Pre-order, in-order, and post-order
- B. Linear search
- C. Depth-first search
- D. Breadth-first search
Chapter # 12
Sure! Here's a 30-question multiple-choice quiz based on the information from your lecture notes on
binary trees. The correct answers are indicated in parentheses.
---
**9. What is the time complexity of finding duplicates in a list using a binary tree?**
a) O(n)
b) O(log n)
c) O(n log n) (Correct)
d) O(n^2)
**10. What is the primary advantage of using a binary tree over a linked list for searching duplicates?**
a) Binary trees use less memory
b) Binary trees are easier to implement
c) Binary trees reduce the number of comparisons (Correct)
d) Linked lists are faster for searching
**11. What is the initial value of the left and right pointers in a newly created TreeNode object in C++?**
a) NULL (Correct)
b) 0
c) -1
d) Undefined
**14. What does the `getLeft()` method return in the `TreeNode` class?**
a) The value stored in the left child
b) The left sub-tree pointer (Correct)
c) The parent node pointer
d) The right sub-tree pointer
**16. What is the purpose of the `insert()` function in the provided C++ code?**
a) To delete a node from the tree
b) To add a new node to the tree (Correct)
c) To print the tree
d) To search for a node
**18. What condition terminates the while loop in the `insert()` function?**
a) When the new node is smaller than the current node
b) When the new node is greater than the current node
c) When a duplicate value is found
d) When `q` becomes NULL (Correct)
**19. What is the role of the `p` pointer in the `insert()` function?**
a) It points to the new node
b) It traverses the tree to find the correct position (Correct)
c) It initializes the tree
d) It sets the value of the node
**23. What type of data is the `object` member in the `TreeNode` class?**
a) Integer
b) String
c) Object pointer (Correct)
d) Boolean
**24. How is a new node added as the right child in the `insert()` function?**
a) `p->setRight(node);` (Correct)
b) `p->setLeft(node);`
c) `q->setRight(node);`
d) `q->setLeft(node);`
**26. In the `TreeNode` class, what does the `setInfo()` method do?**
a) It sets the value of the left child
b) It sets the value of the right child
c) It sets the value stored in the node (Correct)
d) It sets the parent node
**28. What type of object does the `TreeNode` class template handle?**
a) Integer
b) String
c) Any type specified by the template parameter (Correct)
d) Float
**29. What is the delimiter used to indicate the end of the list in the C++ code?**
a) 0
b) -1 (Correct)
c) 1
d) NULL
**30. What is the expected output when attempting to insert a duplicate value using the `insert()`
function?**
a) The duplicate value is added to the tree
b) The tree is restructured
c) A message is printed and the new node is deleted (Correct)
d) The existing node value is updated
Chapter # 13
Here’s a thirty-question quiz based on the lecture content you provided. The correct answers are
indicated.
### Quiz
1. **What is the primary advantage of using a binary tree over a linked list for searching operations?**
- A) Linked lists are always faster.
- B) Binary trees require more memory.
- C) Binary trees allow for faster search operations.
- D) Linked lists are easier to implement.
2. **In a binary tree, what is the maximum number of comparisons required to find a number in a tree of
depth \( d \)?**
- A) \( d \)
- B) \( d \times 2 \)
- C) \( 2^d \)
- D) \( d^2 \)
**Answer: A) \( d \)**
4. **In a binary search tree (BST), where are the nodes with values less than the root node located?**
- A) Right sub-tree
- B) Left sub-tree
- C) Root node
- D) Not in the tree
**Answer: A) Preorder**
6. **Which traversal method prints nodes in the order: left subtree, root, right subtree?**
- A) Preorder
- B) Inorder
- C) Postorder
- D) Level order
**Answer: B) Inorder**
7. **What traversal method prints nodes in the order: left subtree, right subtree, root?**
- A) Preorder
- B) Inorder
- C) Postorder
- D) Level order
**Answer: C) Postorder**
9. **How does a binary tree generally compare in terms of search time to a linked list with the same
number of nodes?**
- A) Binary trees are slower.
- B) Binary trees are faster.
- C) They have the same search time.
- D) Linked lists are faster.
11. **In the `inorder` traversal of a binary tree, when is the root node printed?**
- A) Before traversing its subtrees
- B) After traversing the left subtree but before the right subtree
- C) After traversing both subtrees
- D) Only if it is the only node in the tree
**Answer: B) After traversing the left subtree but before the right subtree**
14. **What is the output of the `inorder` traversal method for a BST if the tree nodes are in ascending
order?**
- A) Sorted order of nodes
- B) Reverse sorted order of nodes
- C) Random order of nodes
- D) All nodes in the left subtree followed by the right subtree
15. **Which traversal method does not produce a sorted list of nodes in a BST?**
- A) Preorder
- B) Inorder
- C) Postorder
- D) Level order
**Answer: C) Postorder**
16. **What is the depth of a binary tree with 100,000 nodes?**
- A) 14
- B) 15
- C) 20
- D) 25
**Answer: C) 20**
17. **Which of the following is true about the `find` method in a binary tree?**
- A) It inserts a new node if the number is not found.
- B) It returns true if the number is not found.
- C) It traverses the tree to determine if a number is present.
- D) It only prints the number if found.
19. **What will the `postorder` traversal method print first in a given binary tree?**
- A) The root node
- B) The left subtree
- C) The right subtree
- D) None of the above
**Answer: B) The left subtree**
20. **What is the result of traversing a BST with preorder method on the tree shown in the example?**
- A) 14 4 3 9 7 5 15 18 16 17 20
- B) 3 4 5 7 9 14 15 16 17 18 20
- C) 14 15 16 17 18 20 4 3 9 7 5
- D) 20 18 17 16 15 14 9 7 5 4 3
**Answer: A) 14 4 3 9 7 5 15 18 16 17 20**
21. **What is the maximum number of levels you need to traverse in a binary tree with 1 billion
nodes?**
- A) 20
- B) 25
- C) 30
- D) 40
**Answer: D) 40**
22. **In a binary search tree (BST), where are the nodes with values greater than the root node located?**
- A) Left sub-tree
- B) Right sub-tree
- C) Root node
- D) Not in the tree
23. **Which function would you use to print nodes in a binary tree in the order: left subtree, root, right
subtree?**
- A) Preorder
- B) Inorder
- C) Postorder
- D) Level order
**Answer: B) Inorder**
24. **What is the complexity of finding a number in a binary search tree with \( n \) nodes in the worst
case?**
- A) \( O(1) \)
- B) \( O(n) \)
- C) \( O(\log n) \)
- D) \( O(n^2) \)
25. **Which traversal method ensures that nodes are printed in ascending order for a binary search
tree?**
- A) Preorder
- B) Inorder
- C) Postorder
- D) Level order
**Answer: B) Inorder**
26. **What is the output order for a `preorder` traversal on the following tree with root 10, left child 5,
and right child 15?**
- A) 10 5 15
- B) 5 10 15
- C) 15 10 5
- D) 5 15 10
**Answer: A) 10 5 15**
27. **Which traversal method is useful for evaluating expressions stored in a binary
tree?**
- A) Preorder
- B) Inorder
- C) Postorder
- D) Level order
**Answer: C) Postorder**
28. **In a `preorder` traversal of a binary tree, which node is visited last?**
- A) Leftmost node
- B) Rightmost node
- C) Root node
- D) All nodes are visited simultaneously
29. **What is the depth of a binary tree with a maximum of 1,000 nodes if it is a perfect binary tree?**
- A) 8
- B) 9
- C) 10
- D) 11
**Answer: B) 9**
Chapter # 14
Here’s a twenty-question quiz based on the information provided about tree traversal techniques
(preorder, inorder, and non-recursive traversal). The correct answers are indicated as well.
2. **In inorder traversal, which node is printed after visiting the left subtree?**
- A) The root node
- B) The right subtree
- C) The left subtree
- D) The right subtree before the root node
- **Correct Answer: A) The root node**
3. **Given the tree structure with root 14, which node comes immediately after node 4 in inorder
traversal?**
- A) Node 15
- B) Node 9
- C) Node 7
- D) Node 3
- **Correct Answer: B) Node 9**
4. **In a binary tree, what does the inorder traversal sequence for the tree with nodes [3, 4, 5, 7, 9, 14, 15,
16, 17, 18, 20] signify?**
- A) The order of nodes visited in postorder traversal
- B) The order of nodes visited in preorder traversal
- C) The order of nodes visited in inorder traversal
- D) The order of nodes visited in level-order traversal
- **Correct Answer: C) The order of nodes visited in inorder traversal**
5. **In a non-recursive inorder traversal using a stack, when does the stack become empty?**
- A) After all nodes are pushed onto the stack
- B) After the root node is processed
- C) After all nodes are popped from the stack
- D) After the left subtree is processed
- **Correct Answer: C) After all nodes are popped from the stack**
6. **In preorder traversal, which node is processed last in the tree with root node 14?**
- A) Node 14
- B) Node 20
- C) Node 17
- D) Node 5
- **Correct Answer: B) Node 20**
7. **For a binary tree, the inorder traversal visits nodes in which sequence?**
- A) Left subtree, root node, right subtree
- B) Root node, left subtree, right subtree
- C) Right subtree, root node, left subtree
- D) Root node, right subtree, left subtree
- **Correct Answer: A) Left subtree, root node, right subtree**
9. **In the non-recursive inorder traversal of a binary tree, what happens when a node’s left subtree is
NULL?**
- A) The node is skipped.
- B) The node is pushed onto the stack.
- C) The node’s right subtree is immediately processed.
- D) The stack is emptied.
- **Correct Answer: C) The node’s right subtree is immediately processed.**
10. **Which traversal method requires an explicit stack for non-recursive implementation?**
- A) Postorder traversal
- B) Inorder traversal
- C) Preorder traversal
- D) Level-order traversal
- **Correct Answer: B) Inorder traversal**
11. **Which traversal method processes nodes in the order of left subtree, right subtree, and root node?**
- A) Preorder traversal
- B) Inorder traversal
- C) Postorder traversal
- D) Level-order traversal
- **Correct Answer: C) Postorder traversal**
12. **In the given tree with root 14, what is the sequence of nodes printed in preorder traversal?**
- A) 14, 4, 3, 9, 7, 5, 15, 18, 16, 17, 20
- B) 14, 4, 9, 3, 7, 5, 15, 18, 20, 16, 17
- C) 14, 15, 4, 9, 3, 7, 5, 18, 16, 20, 17
- D) 14, 4, 3, 9, 7, 5, 15, 18, 20, 16, 17
- **Correct Answer: A) 14, 4, 3, 9, 7, 5, 15, 18, 16, 17, 20**
13. **What is the first step in the inorder traversal of a binary tree using recursion?**
- A) Print the root node
- B) Traverse the right subtree
- C) Traverse the left subtree
- D) Check if the node is NULL
- **Correct Answer: C) Traverse the left subtree**
14. **What is the result of the level-order traversal of a binary tree with nodes [14, 4, 15, 3, 9, 18, 7, 16,
20, 5, 17]?**
- A) 14 4 15 3 9 18 7 16 20 5 17
- B) 14 15 4 18 3 9 7 16 20 5 17
- C) 14 4 15 9 3 18 7 16 20 5 17
- D) 14 4 15 7 9 3 18 16 20 5 17
- **Correct Answer: A) 14 4 15 3 9 18 7 16 20 5 17**
15. **In the non-recursive preorder traversal using a stack, what is the order of processing nodes starting
from the root node?**
- A) Right subtree first, then left subtree
- B) Left subtree first, then right subtree
- C) Root node first, then left subtree, then right subtree
- D) Left subtree first, then root node, then right subtree
- **Correct Answer: C) Root node first, then left subtree, then right subtree**
16. **Which traversal method visits nodes in the order: left subtree, right subtree, root node?**
- A) Level-order traversal
- B) Preorder traversal
- C) Inorder traversal
- D) Postorder traversal
- **Correct Answer: D) Postorder traversal**
17. **In a recursive inorder traversal, what action follows the visit to the left subtree?**
- A) Print the root node
- B) Visit the right subtree
- C) Return to the parent node
- D) Visit the left subtree again
- **Correct Answer: A) Print the root node**
19. **In a binary tree, what is the key difference between preorder and inorder traversal?**
- A) Preorder visits the root node first, while inorder visits it after the left subtree.
- B) Inorder visits the root node first, while preorder visits it after the left subtree.
- C) Preorder and inorder traversals visit nodes in the same order.
- D) Preorder and inorder traversals use different methods to visit nodes.
- **Correct Answer: A) Preorder visits the root node first, while inorder visits it after the left subtree.**
20. **In the context of tree traversal, what does level-order traversal primarily focus on?**
- A) Visiting nodes in a depth-first manner
- B) Visiting nodes by level from top to bottom
- C) Visiting nodes in a left-to-right manner within each level
- D) Both B and C
- **Correct Answer: D) Both B and C**
Chapter# 15
Sure! Here's a 30-question multiple-choice quiz based on the concepts discussed in the lecture about tree
traversal and binary search trees (BST). The correct answers are indicated.
1. **What traversal method prints the root node first in a binary tree?**
- A) Inorder
- B) Postorder
- C) Preorder *(Correct Answer)*
- D) Level-order
6. **What is the time complexity of level-order traversal in terms of the number of nodes, `n`?**
- A) O(log n)
- B) O(n) *(Correct Answer)*
- C) O(n^2)
- D) O(1)
7. **In a binary search tree (BST), where are values less than the current node's value located?**
- A) On the right subtree
- B) On the left subtree *(Correct Answer)*
- C) At the root node
- D) Randomly in the tree
8. **What type of data does the `wordTree` function in the provided code handle?**
- A) Integers
- B) Floats
- C) Characters *(Correct Answer)*
- D) Booleans
9. **How does the `insert` function in the code handle duplicate values?**
- A) It ignores the duplicate
- B) It overwrites the existing value
- C) It prints an error message and deletes the new node *(Correct Answer)*
- D) It merges the duplicate values
10. **Which traversal method would you use to print the elements of a BST in sorted order?**
- A) Preorder
- B) Postorder
- C) Inorder *(Correct Answer)*
- D) Level-order
11. **In the `insert` function, what does `strcmp(info, p->getInfo())` compare?**
- A) Two integers
- B) Two floating-point numbers
- C) Two strings *(Correct Answer)*
- D) Two boolean values
12. **When deleting a node with two children in a BST, which node typically replaces the deleted
node?**
- A) The right child
- B) The left child
- C) The smallest node in the right subtree *(Correct Answer)*
- D) The largest node in the left subtree
13. **What does the `inorder` function do in the provided code for a BST?**
- A) Deletes nodes
- B) Inserts nodes
- C) Traverses and prints nodes in sorted order *(Correct Answer)*
- D) Searches for nodes
14. **In a queue used for level-order traversal, which end is used for insertion?**
- A) The front end
- B) The rear end *(Correct Answer)*
- C) The middle
- D) The top end
15. **What will be the output of a level-order traversal for the tree in Figure 15.1?**
- A) 14 4 15 3 9 18 7 16 20 5 17 *(Correct Answer)*
- B) 14 15 4 3 9 18 7 20 16 17 5
- C) 14 4 3 9 15 18 7 16 20 5 17
- D) 14 15 4 18 9 3 7 20 16 5 17
16. **In the level-order traversal algorithm, what is checked before adding a node's children to the
queue?**
- A) If the node has siblings
- B) If the node has a parent
- C) If the node has left and right children *(Correct Answer)*
- D) If the node is a leaf
17. **Which function is used to insert elements into a queue in the level-order traversal code?**
- A) dequeue()
- B) push()
- C) add()
- D) enqueue() *(Correct Answer)*
18. **What will the queue contain after the first iteration in the level-order traversal function?**
- A) Only the root node
- B) Root node and its children *(Correct Answer)*
- C) All nodes in the tree
- D) Only the leftmost child
19. **What happens if a node has no children in the level-order traversal?**
- A) The node is skipped
- B) The node is printed and nothing is added to the queue *(Correct Answer)*
- C) The node's parent is added to the queue
- D) The node causes an error
20. **How are elements in a binary search tree (BST) typically arranged?**
- A) Randomly
- B) Sorted in increasing order by their values *(Correct Answer)*
- C) Sorted in decreasing order
- D) In a circular manner
21. **Which function is responsible for maintaining the binary search tree properties during insertion?**
- A) delete()
- B) insert() *(Correct Answer)*
- C) levelorder()
- D) inorder()
22. **In the `insert` function, what happens if `info` is equal to the current node's data?**
- A) The node is inserted as a duplicate
- B) The insertion operation is stopped
- C) An error message is displayed and the new node is deleted *(Correct Answer)*
- D) The new node is merged with the existing node
23. **What is the primary advantage of using a queue over a stack for level-order traversal?**
- A) Stack provides faster access
- B) Queue maintains the order of nodes from top to bottom *(Correct Answer)*
- C) Stack uses less memory
- D) Queue is simpler to implement
24. **In level-order traversal, what does `cout << *(treeNode->getInfo()) << " ";` do?**
- A) Enqueues the node's value
- B) Dequeues the node
- C) Prints the value of the node *(Correct Answer)*
- D) Deletes the node
25. **Which traversal method is used to visit nodes in the order of left subtree, root, and then right
subtree?**
- A) Preorder
- B) Postorder
- C) Inorder *(Correct Answer)*
- D) Level-order
26. **What does the `levelorder` function print as it traverses the tree?**
- A) The values of nodes in a preorder manner
- B) The values of nodes in a postorder manner
- C) The values of nodes level by level *(Correct Answer)*
- D) The values of nodes in an inorder manner
27. **In the code provided, how are the nodes added to the queue?**
- A) By pushing
- B) By enqueuing *(Correct Answer)*
- C) By appending
- D) By inserting
28. **Which traversal method is best suited for processing nodes level by level in a binary tree?**
- A) Preorder
- B) Postorder
- C) Inorder
- D) Level-order *(Correct Answer)*
29. **What must be true for a node to be inserted into the right subtree of a given node in a BST?**
- A) The new node's value must be smaller
- B) The new node's value must be equal
- C) The new node's value must be larger *(Correct Answer)*
- D) The new node's value must be the same
Chapter # 16
Here's a 30-question quiz based on the content from your lecture notes about binary trees and their
traversals:
**1. What is the primary data structure used for level-order traversal of a binary tree?**
- A) Stack
- B) Queue
- C) Array
- D) Linked List
**3. In the level-order traversal algorithm, what does the `dequeue()` function do?**
- A) Adds a node to the queue.
- B) Removes a node from the front of the queue.
- C) Inserts a node at the end of the queue.
- D) Clears all nodes from the queue.
**4. In the provided `levelorder()` function, what happens if the input `treeNode` is NULL?**
- A) The function prints an error message.
- B) The function does nothing and returns immediately.
- C) The function inserts a dummy node into the queue.
- D) The function continues execution with the next node.
**7. In the given `levelorder()` function, what is the role of the `if` statement checking `treeNode-
>getLeft() != NULL`?**
- A) To enqueue the left subtree if it exists.
- B) To print the left subtree node.
- C) To dequeue the left subtree node.
- D) To terminate the traversal if the left subtree is NULL.
**8. Which of the following traversals would visit the nodes in the order: left subtree, root, right
subtree?**
- A) Preorder Traversal
- B) Inorder Traversal
- C) Postorder Traversal
- D) Level-order Traversal
**9. What is the complexity of level-order traversal of a binary tree in terms of time?**
- A) O(log n)
- B) O(n)
- C) O(n log n)
- D) O(n^2)
**Correct Answer: B) O(n)**
**10. Which tree traversal method would be most suitable for printing a binary tree from top to bottom,
level by level?**
- A) Preorder
- B) Inorder
- C) Postorder
- D) Level-order
**11. If the root node of a binary tree contains the value 14, what will be the first element printed by the
`levelorder()` function?**
- A) 14
- B) 4
- C) 15
- D) 3
**12. In the provided code, which function is responsible for inserting nodes into the queue?**
- A) `dequeue()`
- B) `enqueue()`
- C) `getInfo()`
- D) `printTree()`
**13. What is the output of the `levelorder()` function for the tree with root 14 and the following levels:
[4, 15], [3, 9, 18], [7, 16, 20], [5, 17]?**
- A) 14 4 15 3 9 18 7 16 20 5 17
- B) 14 3 9 15 7 18 16 20 5 17
- C) 14 4 15 9 3 18 7 20 16 5 17
- D) 14 4 15 7 9 3 18 16 20 5 17
**14. When performing level-order traversal, which of the following operations is performed when the
queue is empty?**
- A) The traversal continues.
- B) The function terminates.
- C) The queue is refilled.
- D) The root node is reinserted.
**15. In the provided code for binary search trees, what is the purpose of the `strcmp` function?**
- A) To compare two integer values.
- B) To compare two string values lexicographically.
- C) To sort nodes in the tree.
- D) To convert strings to integers.
**16. What will be the result of deleting a leaf node from a binary search tree?**
- A) The tree will be rebalanced.
- B) The node is removed and its parent's pointer is updated.
- C) The node is simply marked as deleted.
- D) The node is moved to the root.
**Correct Answer: B) The node is removed and its parent's pointer is updated.**
**17. When deleting a node with one child from a binary search tree, what happens?**
- A) The node is replaced by its child.
- B) The child is inserted into a new node.
- C) The node is moved to the root.
- D) The child is removed from the tree.
**18. In the given code for inserting nodes into a binary search tree, what does the `insert()` function do if
a duplicate value is encountered?**
- A) It inserts the duplicate value anyway.
- B) It deletes the existing node with the same value.
- C) It displays an error message and does not insert the duplicate.
- D) It replaces the existing node with the new value.
**Correct Answer: C) It displays an error message and does not insert the duplicate.**
**19. Which traversal method would visit the nodes in the order: left subtree, right subtree, root?**
- A) Preorder Traversal
- B) Inorder Traversal
- C) Postorder Traversal
- D) Level-order Traversal
**20. What does the `remove()` function do in the binary search tree context?**
- A) It adds a new node to the tree.
- B) It deletes a node from the tree.
- C) It prints the nodes of the tree.
- D) It balances the tree.
**21. If a node to be deleted in a binary search tree has two children, which strategy is used to delete
it?**
- A) Replace it with its right child.
- B) Replace it with its left child.
- C) Replace it with the smallest node in its right subtree.
- D) Replace it with the largest node in its left subtree.
**Correct Answer: C) Replace it with the smallest node in its right subtree.**
**22. What type of binary tree traversal will produce a sorted sequence of elements?**
- A) Preorder Traversal
- B) Inorder Traversal
- C) Postorder Traversal
- D) Level-order Traversal
**23. Which function is used to print the values of nodes in the binary search tree in the provided
code?**
- A) `insert()`
- B) `remove()`
- C) `printTree()`
- D) `levelorder()`
**26. What is the purpose of the `getInfo()` function in the binary search tree context?**
- A) To insert a new node.
- B) To retrieve the value of the node.
- C) To remove a node from the tree.
- D) To print the value of the node.
**28. How is the binary search tree balanced after deletion of a node?**
- A) The tree is automatically balanced by the `remove()` function.
- B) The tree is manually balanced by the user.
- C) The tree requires no balancing as it is always balanced.
- D) The `remove()` function does not balance the tree.
**Correct Answer: D) The `remove()` function does not balance the tree.**
**29. What will be the output of the `printTree()` function after removing all even numbers from a tree
containing numbers 0 to 30?**
- A) All even numbers are printed.
- B) Only odd numbers from 1 to 29 are printed.
- C) Only even numbers from 0 to 30 are printed.
- D) No numbers are printed.
**30. What is the significance of the constant `ITEM_NOT_FOUND` in the `BinarySearchTree` class?**
- A) It represents the default value for new nodes.
- B) It indicates that a search operation did not find a result.
- C) It is used to initialize the tree.
- D) It is a placeholder for node values.
**Correct Answer: B) It indicates that a search operation did not find a result.**
Chapter # 17
Certainly! Here's a 30-question quiz on reference variables and their use in C++ programming,
particularly with binary search trees and function arguments.
1. **What does the `&` symbol represent when used in a function parameter list?**
- A) Address of the variable
- B) Reference to the variable
- C) Dereference the variable
- D) Logical AND operator
**Answer: B) Reference to the variable**
2. **In the function `void insert(const EType& x);`, what does `const EType& x` signify?**
- A) `x` is a constant pointer to `EType`
- B) `x` is a constant reference to `EType`
- C) `x` is a mutable reference to `EType`
- D) `x` is a pointer to a constant `EType`
**Answer: B) `x` is a constant reference to `EType`**
5. **In the function `intMinus1(int oldVal)`, how is the argument `oldVal` passed?**
- A) By reference
- B) By pointer
- C) By value
- D) By address
**Answer: C) By value**
9. **In the function `int intMinus3(int& oldVal)`, what does `oldVal` refer to?**
- A) A copy of the argument
- B) The address of the argument
- C) The argument itself by reference
- D) A new variable with the same name
**Answer: C) The argument itself by reference**
11. **What is the output of `cout << intMinus1(myInt);` where `myInt` is initialized to 31?**
- A) 30
- B) 31
- C) 32
- D) Error
**Answer: A) 30**
12. **What effect does `intMinus2(&myInt);` have on `myInt` if `myInt` is 31 and `intMinus2`
decrements its argument by 2?**
- A) `myInt` becomes 29
- B) `myInt` becomes 30
- C) `myInt` remains 31
- D) `myInt` becomes 32
**Answer: A) `myInt` becomes 29**
13. **In the `intMinus3` function, if `myInt` is passed by reference and is 31, what will `myInt` be after
`intMinus3` is called and decrements it by 3?**
- A) 31
- B) 28
- C) 30
- D) 27
**Answer: B) 28**
14. **Which memory area is used for storing local variables during function execution?**
- A) Heap
- B) Static Data
- C) Code
- D) Stack
**Answer: D) Stack**
15. **What is the main advantage of using reference variables over pointers for function arguments?**
- A) They require less memory.
- B) They are simpler and avoid pointer syntax.
- C) They are faster in execution.
- D) They prevent modifications to the argument.
**Answer: B) They are simpler and avoid pointer syntax.**
16. **In a function that uses call by reference, what happens to the original argument in the caller
function?**
- A) It remains unchanged.
- B) It is duplicated.
- C) It is modified.
- D) It is hidden from the called function.
**Answer: C) It is modified.**
17. **What does `*oldVal` in `intMinus2(int* oldVal)` represent?**
- A) The address of `oldVal`
- B) The value at the address pointed to by `oldVal`
- C) The value of `oldVal`
- D) The pointer itself
**Answer: B) The value at the address pointed to by `oldVal`**
18. **What is the purpose of the `const` keyword when used with reference variables, such as `const int&
x`?**
- A) To allow modification of the referenced value
- B) To prevent modification of the referenced value
- C) To change the data type of the reference
- D) To pass the reference by pointer
**Answer: B) To prevent modification of the referenced value**
19. **In the context of call stacks, where are function arguments and local variables stored?**
- A) Heap
- B) Static Data
- C) Stack
- D) Code
**Answer: C) Stack**
20. **Which function call would not modify the value of `myInt`?**
- A) `intMinus1(myInt)`
- B) `intMinus2(&myInt)`
- C) `intMinus3(myInt)`
- D) None of the above
**Answer: A) `intMinus1(myInt)`**
21. **When using `intMinus3(int& oldVal)`, how does the caller function's variable `myInt` relate to
`oldVal` in `intMinus3`?**
- A) They are independent variables.
- B) `oldVal` is a copy of `myInt`.
- C) `oldVal` is a reference to `myInt`.
- D) `oldVal` is an address of `myInt`.
**Answer: C) `oldVal` is a reference to `myInt`.**
22. **If `intMinus2` is called with `&myInt` and `myInt` is 31, what will the function return?**
- A) 31
- B) 29
- C) 30
- D) 28
**Answer: B) 29**
23. **What happens to the `retVal` variable in the caller function after calling `intMinus1`?**
- A) It will be the result of `intMinus1`, but `myInt` remains unchanged.
- B) It will be unchanged.
- C) It will be the same as `myInt`.
- D) It will cause a compilation error.
**Answer: A) It will be the result of `intMinus1`, but `myInt` remains unchanged.**
24. **In `intMinus3`, why is there no need to use `&` inside the function body?**
- A) Because `oldVal` is a pointer.
- B) Because `oldVal` is already a reference.
- C) Because `oldVal` is a copy of the value.
- D) Because `oldVal` is constant.
**Answer: B) Because `oldVal` is already a reference.**
27. **What will be the result if `intMinus2` is called with a pointer to a variable `myInt` which is initially
5, and the function decrements the value by 5?**
- A) `myInt` remains 5
- B) `myInt` becomes 0
- C) `myInt` becomes -5
- D) Compilation error
**Answer: C) `myInt` becomes -5**
28. **What does the `const` keyword do when used in `const int& x`?**
- A) Allows modification of `x`
- B) Disables modification of `x`
- C) Changes `x` to a pointer type
- D) Changes `x` to a reference type
**Answer: B) Disables modification of `x`**
29. **Which type of parameter passing would be most efficient for large objects in C++?**
- A) Pass by value
- B) Pass by pointer
- C) Pass by reference
- D) Pass by address
**Answer: C) Pass by reference**
30. **In C++, if `intMinus2` is defined as `void intMinus2(int* ptr)` and you want to pass `myInt` by
reference, what is the correct call?**
- A) `intMinus2(myInt);`
- B) `intMinus2(&myInt);`
- C) `intMinus2(*myInt);`
- D) `intMinus2(ptr(myInt));`
**Answer: B) `intMinus2(&myInt);`**
Chapter # 18
Here’s a 30-question quiz based on the lecture notes you provided, with multiple-choice answers and the
correct answers indicated:
2. **In which memory area are local variables created when a function is called?**
- A) Heap
- B) Code segment
- C) Stack
- D) Static data
- **Correct Answer: C**
3. **Which of the following languages uses call by reference for all parameters?**
- A) C++
- B) Java
- C) FORTRAN
- D) PASCAL
- **Correct Answer: C**
4. **What is the main difference between call by value and call by reference?**
- A) Call by value uses pointers, call by reference does not.
- B) Call by reference does not copy the actual value, while call by value does.
- C) Call by value passes a reference, call by reference passes a copy of the value.
- D) Call by value stores variables on the heap, call by reference stores them on the stack.
- **Correct Answer: B**
5. **In Java, how are native data types like int and float passed?**
- A) By reference
- B) By value
- C) By pointer
- D) By name
- **Correct Answer: B**
9. **In the `loadCustomer()` function, what is the purpose of the `enqueue()` method?**
- A) To add objects to the heap.
- B) To remove objects from the queue.
- C) To add objects to the queue.
- D) To initialize the customer objects.
- **Correct Answer: C**
10. **When using reference variables in C++, which of the following is true?**
- A) They are used to directly access memory addresses.
- B) They must be initialized when declared.
- C) They can be reassigned to point to different variables.
- D) They can be null.
- **Correct Answer: B**
11. **What does the `const` keyword do when used with a function parameter?**
- A) It allows the parameter to be modified.
- B) It ensures the parameter cannot be modified within the function.
- C) It makes the parameter a global variable.
- D) It converts the parameter to a reference type.
- **Correct Answer: B**
13. **What is the main reason for using pointers to manage objects on the heap?**
- A) To avoid stack overflow.
- B) To manage memory dynamically.
- C) To automatically handle memory deallocation.
- D) To simplify object creation.
- **Correct Answer: B**
15. **Which of the following statements is true about the heap and stack memory layout?**
- A) The stack grows upwards and the heap grows downwards.
- B) The stack grows downwards and the heap grows upwards.
- C) Both the stack and heap grow in the same direction.
- D) The stack and heap do not affect each other.
- **Correct Answer: B**
16. **In the `serviceCustomer()` function, what does the `delete` keyword do?**
- A) It deallocates memory for a pointer.
- B) It removes an object from the queue.
- C) It initializes a new object.
- D) It adds an object to the heap.
- **Correct Answer: A**
17. **Which keyword is used in C++ to define a constant value that cannot be modified?**
- A) static
- B) final
- C) const
- D) immutable
- **Correct Answer: C**
22. **When using dynamic memory allocation, which operator is used to allocate memory?**
- A) `malloc`
- B) `free`
- C) `new`
- D) `delete`
- **Correct Answer: C**
23. **What happens if you access a memory location after the object has been deleted?**
- A) The object is reinitialized.
- B) The program may crash or produce undefined behavior.
- C) The object’s memory is automatically reclaimed.
- D) The pointer is automatically reset to null.
- **Correct Answer: B**
24. **What is the purpose of the `getName()` method in the `Customer` class?**
- A) To set the name of the customer.
- B) To retrieve and return the name of the customer.
- C) To delete the customer object.
- D) To add the customer to a queue.
- **Correct Answer: B**
26. **In C++, why should you avoid returning references to local variables from functions?**
- A) They cannot be assigned to global variables.
- B) The reference may become a dangling reference when the function exits.
- C) Local variables cannot be passed to other functions.
- D) They increase the function execution time.
- **Correct Answer: B**
27. **What is the role of the `&` sign when passing parameters in C++?**
- A) To indicate call by value.
- B) To indicate call by reference.
- C) To indicate call by pointer.
- D) To indicate a constant parameter.
- **Correct Answer: B**
Chapter #19
Here's a ten-question quiz based on the concepts covered in the lecture about the `const` keyword,
reference variables, and AVL trees:
### Quiz
**1. What does the `const` keyword do when used with a reference parameter in a function?**
- A) Allows the function to modify the referenced object.
- B) Prevents the function from modifying the referenced object.
- C) Creates a copy of the referenced object.
- D) Ensures the function can only read from the referenced object but not modify it.
**2. When `const` is used at the end of a member function signature, what does it signify?**
- A) The function can modify the class's member variables.
- B) The function cannot modify the class's member variables.
- C) The function is private to the class.
- D) The function is a static member of the class.
**5. In an AVL tree, what is the maximum allowed difference in height between the left and right subtrees
of any node?**
- A) 2
- B) 1
- C) 0
- D) 3
**8. How does returning by reference rather than by value benefit function performance?**
- A) It creates a new copy of the object each time.
- B) It avoids the overhead of copying large objects, improving performance.
- C) It ensures the object cannot be modified.
- D) It simplifies the function’s implementation.
**10. In the context of AVL trees, what does the term "balance factor" refer to?**
- A) The number of nodes in the left and right subtrees.
- B) The difference in height between the left and right subtrees of a node.
- C) The total number of levels in the tree.
- D) The depth of the tree from the root to the deepest leaf.
**Correct Answer: B**
Chapter # 20
Here's a ten-question quiz based on the lecture content, including multiple-choice answers and the correct
answers indicated:
1. **What is the primary benefit of using the `const` keyword with reference parameters in C++
functions?**
- A) It allows the function to modify the parameter.
- B) It ensures that the parameter is read-only within the function.
- C) It eliminates the need for function overloading.
- D) It increases the speed of the function.
**Correct Answer:** B) It ensures that the parameter is read-only within the function.
2. **What does the `const` keyword signify when used at the end of a member function's signature?**
- A) The function is allowed to modify member variables.
- B) The function does not modify member variables.
- C) The function is a static member.
- D) The function has no return type.
3. **When returning an object by reference with `const` in C++, what is the effect?**
- A) The object can be modified by the caller.
- B) The object is returned as a copy.
- C) The caller cannot modify the object.
- D) The reference is always null.
**Correct Answer:** C) The caller cannot modify the object.
4. **In AVL trees, what is the maximum allowed difference in height between the left and right subtrees
of any node?**
- A) 0
- B) 1
- C) 2
- D) 3
**Correct Answer:** B) 1
**Correct Answer:** B) To balance the tree and maintain the AVL property.
**Correct Answer:** A) The right child of the rotated node becomes the new root of the subtree.
7. **In an AVL tree, which of the following statements is true about the inorder traversal of the tree?**
- A) The inorder traversal always results in a balanced tree.
- B) The inorder traversal results in a sorted sequence of values.
- C) The inorder traversal can be used to detect AVL violations.
- D) The inorder traversal always returns the same result regardless of tree balance.
Chapter # 21
Based on the content from your lecture, here’s a ten-question quiz with multiple-choice answers and the
correct answers indicated:
2. **After inserting node 4 into the AVL tree shown in Fig 21.3, what is the balance factor of node 2?**
- A) 0
- B) -1
- C) 1
- D) -2
3. **In Fig 21.4, after inserting node 5, what is the balance factor of node 3?**
- A) 0
- B) -1
- C) -2
- D) 1
4. **Which type of rotation is needed if the balance factor of a node is -2 and the insertion was in the
right subtree of the right child of that node?**
- A) Single left rotation
- B) Single right rotation
- C) Double left-right rotation
- D) Double right-left rotation
5. **In the AVL tree of Fig 21.5, which node became the new root after performing the rotation?**
- A) Node 2
- B) Node 3
- C) Node 4
- D) Node 5
6. **What is the result of performing an inorder traversal on the AVL tree shown in Fig 21.7?**
- A) 1 2 3 4 5 6
- B) 1 2 3 4 5 6 7
- C) 1 2 3 4 5 6 7 16
- D) 1 2 3 4 5 6 7 15 16
7. **In Fig 21.10, after inserting node 16, what is the balance factor of node 7?**
- A) 0
- B) 1
- C) -1
- D) -2
8. **What is the balance factor of node 7 after inserting node 15 in Fig 21.11?**
- A) 0
- B) -1
- C) 1
- D) -2
9. **Which type of rotation is required if the balance factor of a node is -2 and the insertion was in the
left subtree of the right child of that node?**
- A) Single left rotation
- B) Single right rotation
- C) Double left-right rotation
- D) Double right-left rotation
10. **In Fig 21.12, after rotating node 7, what is the new position of node 7?**
- A) It becomes the root of the tree.
- B) It becomes the left child of node 16.
- C) It becomes the right child of node 16.
- D) It remains in the same position.
Chapter # 22
2. **In which case does a single rotation successfully balance the AVL tree?**
- A) When a new node is inserted into the right subtree of the left child.
- B) When a new node is inserted into the left subtree of the right child.
- C) When a new node is inserted into the left subtree of the left child.
- D) When a new node is inserted into the right subtree of the right child.
4. **What type of double rotation is performed when a new node is inserted into the left subtree of the
right child of the AVL tree?**
- A) Left-right rotation.
- B) Right-left rotation.
- C) Right-right rotation.
- D) Left-left rotation.
5. **What does the height difference between the left and right subtrees of a node indicate in an AVL
tree?**
- A) The node’s balance factor.
- B) The node’s level in the tree.
- C) The total number of nodes in the tree.
- D) The height of the entire tree.
6. **When a new node is inserted in the right subtree of the left child of an AVL tree, which rotation is
performed first?**
- A) Right rotation.
- B) Left rotation.
- C) Left-right rotation.
- D) Right-left rotation.
7. **In the AVL tree balancing process, what is the result of performing a right rotation followed by a left
rotation?**
- A) Right-left double rotation.
- B) Left-right double rotation.
- C) Single right rotation.
- D) Single left rotation.
8. **Which of the following is NOT a case where single rotation can fix the AVL tree imbalance?**
- A) Insertion in the left subtree of the left child.
- B) Insertion in the right subtree of the right child.
- C) Insertion in the left subtree of the right child.
- D) Insertion in the right subtree of the left child.
9. **What is the key difference between single and double rotations in AVL trees?**
- A) Single rotations involve two nodes, while double rotations involve more.
- B) Double rotations consist of two single rotations performed in a specific order.
- C) Single rotations are only performed on leaf nodes.
- D) Double rotations are used to remove nodes from the tree.
10. **What is the AVL tree’s property related to its balance factor?**
- A) The balance factor of any node must be 0.
- B) The balance factor of any node must be between -1 and 1.
- C) The balance factor of any node must be greater than 1.
- D) The balance factor of any node must be less than -1.
Chapter # 23
Nothig important in this chapter for mcq.