Unit 5 Practice Questions 1
Unit 5 Practice Questions 1
Ritter
Advanced Topics in College Computer Science 2023-2024 School Year
Unambiguously circle your answers. There are more questions here than will be on the test.
There will be 3 questions on the OOP concepts covered in Unit 4.
1. An implementation of a linked list contains private fields and private inner classes. Which of
the following object-oriented programming concepts explains this implementation choice?
a. Abstraction
b. Encapsulation
c. Inheritance
d. Polymorphism
2. A programmer initializes a data structure with the following line of code.
List<String> myList = new ArrayList<>();
This means that myList can be treated as a List and the programmer must change only this
line of code to switch it to a LinkedList or some other implementation. Which object-
oriented programming concept does this take advantage of?
a. Abstraction
b. Encapsulation
c. Inheritance
d. Polymorphism
3. When designing an implementation of a linked list, the programmer can choose from many
options of how to represent the list and which fields to use. One programmer might choose
head and size fields, while another might choose head and tail fields. Choosing which fields
to use to represent a concept is best described with which object-oriented programming
concept?
a. Abstraction
b. Encapsulation
c. Inheritance
d. Polymorphism
1
4. Simon is creating a new app for visitors to the local Zoo. This app will help tourists learn
about the animals in the park. The programmer decides to use an abstract class called
Animal and have a hierarchy of subclasses storing data about the animals in the zoo. This
will reduce the amount of code the programmer needs to write because the derived classes
will already have the methods provided in the superclasses. Re-using code from superclasses
is part of which object-oriented programming concept?
a. Abstraction
b. Encapsulation
c. Inheritance
d. Polymorphism
5. Abigail is writing a program which identifies seashells using images of the shells. The
project will have three major parts, one which gets the image from the phone’s camera and
stores the image in a folder, one which handles the analysis of the images, and one which
displays the information on the phone screen. Abigail’s first step is to decide, for each part of
the project, which methods will need to be public and therefore callable from the other parts
of the program. Which should Abigail use for this part of the system?
a. Interfaces
b. Abstract Classes
c. (Concrete) Classes
6. Rick is a teacher, sick of grading. He is writing a program which automates the grading
process for his geometry class. His class hierarchy currently has several classes representing
question types such as MultipleChoice, TrueFalse, and Proof. Rick would like to add
a pointsPossible instance variable to every single one of these classes. So, Rick writes a
type called Question. A Question will never be instantiated because all Rick’s questions
fall into one of the previously mentioned types. Which should Rick choose for a java type
entitled Question, from which all subclasses should inherit the pointsPossible field?
a. Interfaces
b. Abstract Classes
c. (Concrete) Classes
2
7. Which of the following causes an error?
I. List<HashMap> myList = new LinkedList<>();
myList.add(new Map());
a. None of them
b. I Only
c. II Only
d. III Only
e. I and II
f. I and III
g. II and III
h. All three
a. None of them
b. I Only
c. II Only
d. III Only
e. I and II
f. I and III
g. II and III
h. All three
3
9. An investment firm has access to a very fast fiber optic cable that sends stock trade
information to the New York Stock Exchange. They hire Lidija to write a program that
collects internal requests from the firm’s traders (human and AI) and put them into a
queue to be sent along the cable. Request are placed in the back of the queue and sent
along the cable from the front. The queue will constantly vary in size and requests will
never be added in the middle. Lidija should implement the queue as a
a. An ArrayList
b. A LinkedList
10. A program keeps track of runners in a marathon. The list of runners is always in the order in
which the runners are in the race. ie) The runner in first place is at the 0 index in the list.
Over the course of the marathon, the runners switch places often. However, these switches
are rare in comparison to the most important operation which is binary search to find out at
which position a particular runner is. The server gets thousands of requests per second from
apps for people trying to keep track of their friends and family running the race. Binary
search requires fast random access. The list is better [INCOMPLETE]
a. An ArrayList
b. A LinkedList
4
11. The following segment is supposed to search for and remove from a singly-linked list all
nodes whose data fields are equal to val, a previously defined value. Assume that
firstNode is accessible and references the first node in the list, and that the list is
nonempty.
What is the runtime of the /* code */, assuming the most efficient algorithm?
a) 𝑂(1)
b) 𝑂(𝑛)
c) 𝑂(𝑛! )
d) 𝑂(log(𝑛))
e) 𝑂(𝑛 log (𝑛))
5
13. Consider the following method from removing a value from a singly linked list:
In which of the following cases will the remove method fail to work as intended?
I. p points to any node other than the first or last node
a. I Only
b. II Only
c. I and II
d. I and III
14. Suppose that the precondition of the remove method in the previous question is changed
so that the method always works as intended. What is the runtime of the algorithm?
a) 𝑂(𝑛)
b) 𝑂*√𝑛,
c) 𝑂(1)
d) 𝑂(𝑛! )
e) 𝑂(log (𝑛))
6
15. Suppose that list1 and list2 refer to the first nodes (heads) of two singly linked lists, and
that q points to some node in the first list. The first piece of the first list, namely all the nodes
up to and including the one pointed to by q, is to be removed and attached to the front of
list2, maintaining the order of the nodes of its original list, and list2 should point to the
augmented list. If neither q nor list1 is originally null, then this task is correctly performed
by which of the following program segments? Assume that p and q are both correctly
declared to be of type ListNode.
I. q.setNext(list2);
list2 = list1;
list1 = q.getNext();
7
16. To fly from one city to another, one must often take multiple flights. Each flight has a
unique flight number, a departure airport, and destination airport. The full class is shown
below.
Note that there is a connection field of type Flight. This means that a Flight behaves similarly
to a ListNode.
8
Another class called Trip stores represents multiple flights which can be chained together, by
implementing a singly-linked list.
public Trip() {
this.firstFlight = null;
}
/*
* Prints out all Flights in this trip
* Precondition: setFirstFlight has already been called ensuring
* that the firstFlight field is non-null
*/
public void displayTrip() {
/* to be implemented in part (c) */
}
}
9
a) Implement the setFirstFlight method.
10
c) Implement the displayTrip method. You are given that the print command in Java is
System.out.println(Object o).
/*
* Prints out all Flights in this trip
* Precondition: setFirstFlight has already been called ensuring
* that the firstFlight field is non-null
*/
public void displayTrip() {
11