COMPUTER SCIENCE CSCI 1112
Final Exam
"Justify why a while loop is recommended for iteration in a linked list instead of a for
loop? - CORRECT ANSWER It is recommended because a linked list does not have a
fixed length that can be easily expressed as an index range."
"What is an iterator? - CORRECT ANSWER An object that enables the traversal of a
data structure and access to its elements without exposing its underlying
implementation."
"How does using an iterator for iteration differ from indexed based iteration? -
CORRECT ANSWER Iterator-based iteration does not rely on index values to access
elements, but instead uses a next() method to move through the elements of the data
structure one by one. In contrast, indexed based iteration relies on accessing elements
by their index position."
"Why should iterator based iteration be used for linked lists over index based iteration? -
CORRECT ANSWER Linked lists are not indexed-based data structures, meaning that
accessing elements by their index position would require iterating through the list until
the desired element is reached. This process can be inefficient and time-consuming,
especially for large lists. In contrast, using an iterator allows for efficient traversal of a
linked list, since it moves from one element to the next without the need to iterate
through the entire list."
"What is a policy layer? - CORRECT ANSWER An abstraction layer that prescribes the
interface for a data type but also specifies one or more rules that the data type
behaviors must always ensure such as FIFO and LIFO."
"Explain First-in First-out (FIFO) - CORRECT ANSWER The oldest item must be the
next item removed"
"Explain Last-in First-out (LIFO) - CORRECT ANSWER The newest item must be the
next item removed"
"Define the interface for a stack - CORRECT ANSWER push(object) - adds an element
to the top of the stack
pop() - removes the element at the top of the stack
isEmpty() - determines if the stack has any elements"
"Define the interface for a queue - CORRECT ANSWER enqueue(object) - appends an
element to the end of the queue
dequeue() - removes the element at the front of the queue
isEmpty() - determines if the queue has any elements"
,2
"Define the insertion/removal policy for a stack - CORRECT ANSWER A stack is a
linear data structure in which the insertion and deletion operations are only performed at
the top of the stack (LIFO)"
"Define the insertion/removal policy for a queue - CORRECT ANSWER Elements are
removed in the same order they are inserted (FIFO)"
"Describe the implementation of a stack using an array - CORRECT ANSWER The
push operation is implemented by storing the new element at the first index and shifting
all subsequent elements down one index. The pop operation is implemented by
returning the element at the first index and shifting all subsequent elements up one
index."
"Analyze the efficiency of stack operations when implemented using an array -
CORRECT ANSWER For push and pop operations, all elements must be shifted;
therefore, it has a linear time complexity. Additionally, arrays require reallocation to
accommodate for multiple push calls; therefore, it is generally a linear time operation."
"Describe the implementation of a stack using a linked list - CORRECT ANSWER
Linked lists can implement a stack by creating a singly-linked-list with only a head to
achieve all stack behaviors in constant time as elements can be pushed an popped
from the top of the list without having to reallocate an array."
"Analyze the efficiency of stack operations when implemented using a linked list -
CORRECT ANSWER The efficiency of push and pop operations are O(1) or constant
time as the head can be used to add and remove elements by simply altering its
pointer."
"Describe the implementation of a queue using an array - CORRECT ANSWER The
enqueue operation is implemented by adding a new element after the last element and
the dequeue operation requires the removal and return of the element at the first index
and then shifts all subsequent elements one index to the left."
"Analyze the efficiency of queue operations when implemented using an array -
CORRECT ANSWER The efficiency of enqueue and dequeue operations using an
array is typically linear time as an array must be periodically reallocated to support
multiple enqueue operations and the dequeue operation requires the shift of each
successive element."
"Describe the implementation of a queue using a linked list - CORRECT ANSWER
With the use of a tail, enqueue simply alters the tail pointer to the new element and
dequeue alters the head pointer to point to the second element, resulting int he removal
of the first element."
"Give an example in Java of a deep copy. - CORRECT ANSWER int [] A = new int[5];
, 2
A[0] = 1;
A[1] = 10;
int[] B = new int[5];
for i=0 to 5-1
B[i]=A[i]"
"When a primitive value is copied, is that a deep copy or a shallow copy? - CORRECT
ANSWER Deep copy"
"When a reference or non-primitive value is copied, is that a deep copy or a shallow
copy? - CORRECT ANSWER Shallow copy"
"Define "state" in terms of computing. - CORRECT ANSWER The combination of a
variable or object's original values plus any modifications that occurred up to a certain
point in a program."
"What does it mean to say that a program evolves through its execution? - CORRECT
ANSWER As a program executes, the values of some variables - whether they'd be
primitive or reference - changes, therefore, influencing the outcome of a program."
"Why is timing a program a poor way to measure its performance? - CORRECT
ANSWER Some programs may be more complex than others which will cause them to
take longer regardless of whether it is the most efficient solution."
"What variable affect the execution time of a program? - CORRECT ANSWER
Hardware, software, and CPU"
"Why do we use algorithmic time complexity to measure a program's efficiency rather
than execution time? - CORRECT ANSWER Time complexity refers to the number of
times a statement iterates rather than determining the actual time a program takes to
execute because that can rely on many factors that may be out of the programmer's
control."
"What are the benefits of using an abstract measure like algorithmic time complexity
over a rigidly specific measure like execution time? - CORRECT ANSWER It allows
programmers to not bother with constants of proportionality (coefficients) as they are
irrelevant in comparison. Smaller terms also do not have much of an effect in a function;
therefore, they can be omitted (ex. 3n^3 + 12n^2 + 16n -> O(n^3) depicts the real
growth of the function)."
"Why can we ignore coefficients and smaller terms in an algorithmic time complexity
analysis? - CORRECT ANSWER Constants of proportionality are irrelevant when
determining time complexity analysis."