Verityprep
Question Bank · Unit 4

AP® Computer Science A Data Collections Practice Questions

Work through AP Computer Science A practice questions on Data Collections, then check each answer against a worked solution.

Free · no sign-up Open questions need no account. Sign up or log in to open the rest.

Subtopic: Arraylists Clear filter

Questions 221–225 of 225
  1. 221
  2. 222
  3. 223
  4. 224
  5. 225

Question 221

Multiple choice Arraylists
Consider the following code segment. ArrayList<String> words = new ArrayList<String>(); words.add("ant");…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 222

Free response Arraylists
Information about players in a school league is stored in Player objects. public class Player { private String name; private int points; private boolean active;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 223

Multiple choice Arraylists
Consider the following code segment. ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(2); nums.add(4); nums.add(3); nums.add(5);…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 224

Free response Arraylists

A school system models course requests using the CourseRequest class. A request has a course name, a priority value, and a flag indicating whether the request has already been fulfilled.

public class CourseRequest
{
    private String courseName;
    private int priority;
    private boolean fulfilled;

    public CourseRequest(String name, int p, boolean done)
    {
        courseName = name;
        priority = p;
        fulfilled = done;
    }

    public String getCourseName()
    {
        return courseName;
    }

    public int getPriority()
    {
        return priority;
    }

    public boolean isFulfilled()
    {
        return fulfilled;
    }
}

public class CourseQueue
{
    private ArrayList<CourseRequest> requests;

    public CourseQueue(ArrayList<CourseRequest> data)
    {
        requests = data;
    }

    public ArrayList<String> topOpenRequests(int minPriority)
    { /* to be written */ }
}

Write the method topOpenRequests, which returns a new ArrayList<String> containing the names of all unfulfilled requests whose priority is at least minPriority and is equal to the highest priority among all such requests. Names must appear in the same order as the corresponding requests appear in requests. If no request qualifies, the method returns an empty list.

The answer and the full solution open in a window.

Question 225

Free response Arraylists
This question involves the analysis of messages exchanged in a chat room. The following Message class represents a single message in the chat room. public class…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 221 of 225
Page 23 of 23