Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Name: ____________________________ Mr.

Ritter
Advanced Topics in College Computer Science 2023-2024 School Year

Unit 5 Practice Questions

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());

II. Queue myQueue = new LinkedList();


III. Integer yourInt = new Comparable();
Choose one.

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

8. Which of the following causes an error?


I. List<Map> myList = new LinkedList<>();
myList.add(new TreeMap());

II. ListIterator myList = new Iterator();


III. Set mySet = new HashSet();
Choose one.

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.

ListNode current = firstNode;


while (current != null)
{
if( current.getValue().equals(val))
{
ListNode q = current. getNext();
current.setNext(q.getNext());
}
else
current = current.getNext();
}

Which is true about this code segment?


a. It works for all the nodes of the linked list.
b. It fails for only the first node of the list.
c. It fails for only the last node of the list.
d. It fails for the first and last nodes of the list but works for all others.
e. It fails for all nodes of the list.
12. Consider an insert method in a class that is a singly-linked list.

/** Precondition: current refers to a node in a nonempty linked list


* sorted in increasing order
* Postcondition: element inserted directly following node to which
* current points
*/
public void insert(ListNode current, Object element) {
/* code */
}

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:

/** Precondition: p points to a node in a nonempty linear linked list


* Postcondition: The value that p points to has been removed from
* the list
*/
public void remove(ListNode p) {
ListNode q = p.getNext();
p.setValue(q.getValue());
p.setNext(q.getNext());
}

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

II. p points to the last node in the list


III. p points to the first node, and there is more than one node in the list.
Choose one.

a. I Only

b. II Only

c. I and II

d. I and III

e. I, II, 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();

II. while(list1 != q.getNext()) {


p = list1;
list1 = list1.getNext();
p.setNext(list2);
list2 = p;
}
list1 = p;
III. p = q.getNext();
q.setNext(list2);
list2 = list1;
list1 = p;
Choose one of the following.
a. None
b. III only
c. I and III only
d. II and III only
e. I, II, and III

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.

public class Flight {


private String flightNumber;
private String departure;
private String destination;
private Flight connection;

public Flight(String flightNumber, String departure, String


destination) {
this.flightNumber = flightNumber;
this.departure = departure;
this.destination = destination;
}

public String getFlightNumber() { return flightNumber; }


public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}

public String getDeparture() { return departure; }


public void setDeparture(String departure) {
this.departure = departure;
}

public String getDestination() { return destination; }


public void setDestination(String destination) {
this.destination = destination;
}

public Flight getConnection() { return connection; }


public void setConnection(Flight connection) {
this.connection = connection;
}

/* toString implementation not shown */


}

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 class Trip {


private Flight firstFlight;

public Trip() {
this.firstFlight = null;
}

public Flight getFirstFlight() {


return firstFlight;
}

/* Assigns a value to this.firstFlight


* Precondition: the parameter firstFlight is non-null and has a
* null value in its connection.
*/
public void setFirstFlight(Flight firstFlight) {
/* to be implemented in part (a) */
}

/* Adds a flight to the end of the trip


* Precondition: setFirstFlight has already been called ensuring
* that the firstFlight field is non-null
*/
public void addFlight(String flightNumber, String departure,
String destination) {
/* to be implemented in part (b) */
}

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

/* Assigns a value to this.firstFlight


* Precondition: the parameter firstFlight is non-null and has a
* null value in its connection.
*/
public void setFirstFlight(Flight firstFlight) {

b) Implement the addFlight method.

/* Adds a flight to the end of the trip


* Precondition: setFirstFlight has already been called ensuring
* that the firstFlight field is non-null
*/
public void addFlight(String flightNumber, String departure,
String destination) {

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

You might also like