Professional Documents
Culture Documents
Midterm Fall23
Midterm Fall23
Subject:
Cohort: Classroom:
INSTRUCTIONS
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
2. Question 2
Propose two solutions that allow the creation of non-mutable objects.
……………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
3. Question 3
Write a code snippet to iterate over LinkedList nodes to return a comma-separated, bracketed version of the list.
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
………………………………………………………………………………………………………………………………………
……………………………………………………………………………………………………………………………
2) Question 2
What is the Big O notation for inserting a new node at the beginning of a singly linked list?
(A) O(1)
(B) O(log n)
(C) O(n)
(D) O(n^2)
……………………………………………………………………………………………………………………………
3) Question 3
Which of the following is the right way to create an ArrayList?
(A) ArrayList <String> Sentences= new ArrayList <String> ()
(B) ArrayList <boolean> tests= new ArrayList <boolean> ()
(C) ArrayList <String> Sentences= new ArrayList <String> (100)
……………………………………………………………………………………………………………………………
4) Question 4
What is the complexity of the following code snippets
static void towerOfHanoi(int numDisks, char source, char destination, char auxiliary)
{
if (numDisks == 1) {
System.out.println("Move disk 1 from " + source + " to " + destination);
return;}
towerOfHanoi(numDisks - 1, source, auxiliary, destination);
System.out.println("Move disk " + numDisks + " from " + source + " to " + destination);
5) Question 5
What about recursion is true in comparison with iteration?
(A) Very expensive in terms of memory.
(B) Low performance.
(C) Every recursive program can be written with iteration too.
(D) All the above are true!
…………………………………………………………………………………………………………………………………………
6) Question 6
Suppose I have int b = new int[42]. What are the highest and lowest legal array indexes for b?
(A) 0 and 41
(B) 0 and 42
(C) 1 and 41
(D) 1 and 42
…………………………………………………………………………………………………………………………………………
7) Question 7
Suppose cursor refers to a node in a linked list (using the IntNode class with instance variables called data
and link). What statement changes cursor so that it refers to the next node?
(A) cursor++;
(B) cursor = link;
(C) cursor += link;
(D) cursor = cursor.link;
………………………………………………………………………………………………………………………………………….
8) Question 8
Which boolean expression indicates whether the numbers in two nodes (p and q) are the same. Assume that
neither p nor q is null.
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
…………………………………………………………………………………………………………………………………………
Class Store {
END
Good Luck