Verityprep
Question Bank · Unit 1

AP® Computer Science A Using Objects and Methods Practice Questions

Work through AP Computer Science A practice questions on Using Objects and Methods, 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: Using Objects Clear filter

Questions 351–360 of 546
  1. 351
  2. 352
  3. 353
  4. 354
  5. 355
  6. 356
  7. 357
  8. 358
  9. 359
  10. 360

Question 351

Free response Using Objects

The Riverside Bridge Authority operates an automated toll collection system. Vehicles crossing the bridge are charged based on the number of axles and the time of day. A standard 2-axle vehicle pays a base toll of $2.50. For each axle beyond two, an additional $1.25 is added to the base. During peak hours — defined as hours 7 through 9 and hours 16 through 18, inclusive, using a 24-hour clock — the total toll is multiplied by 1.5. During all other hours, no multiplier is applied.

PART A

Write the method public double calculateToll(int axles, int hour) that returns the toll as described above.

For example, calculateToll(2, 8) returns 3.75 (base 2.50 \(\times\) peak 1.5). calculateToll(5, 12) returns 6.25 (\(2.50 + 3 \times 1.25 = 6.25\), off-peak). calculateToll(3, 17) returns 5.625 (\((2.50 + 1.25) \times 1.5\)).

public double calculateToll(int axles, int hour) {

PART B

Write the method public int peakVehicleCount(ArrayList<Integer> hours) that counts how many entries fall within peak hours (7–9 or 16–18 inclusive).

For example, if hours is [6, 8, 12, 17, 18, 22, 7], the method returns 4 (entries 8, 17, 18, 7 are peak).

public int peakVehicleCount(ArrayList<Integer> hours) {
The answer and the full solution open in a window.

Question 352

Multiple choice Using Objects

What is printed?

String s = "PROGRAMMING";
int k = s.indexOf("RAM");
String t = s.substring(k, k + 5);
System.out.println(t + " " + t.indexOf("M"));
Pick an answer, then check it. The solution opens in a window.

Question 353

Multiple choice Using Objects

What is printed?

String s = "JAVA";
String t = "";
t = t + s.substring(3);
t = t + s.substring(1, 3);
t = t + s.substring(0, 1);
System.out.println(t + " " + t.indexOf("A"));
Pick an answer, then check it. The solution opens in a window.

Question 354

Multiple choice Using Objects

What is printed?

String s = "COMPUTATION";
String a = s.substring(0, 3);
String b = s.substring(s.length() - 3);
String c = a + b;
System.out.println(c.indexOf("IO") + c.length());
Pick an answer, then check it. The solution opens in a window.

Question 355

Multiple choice Using Objects

What is printed?

String s1 = "APPLE";
String s2 = "APPLICATION";
boolean a = s1.compareTo(s2) < 0;
boolean b = s1.equals(s2.substring(0, s1.length()));
boolean c = s1.length() < s2.length();
System.out.println(a + " " + b + " " + c);
Pick an answer, then check it. The solution opens in a window.

Question 356

Multiple choice Using Objects

What is printed?

String word = "ABRACADABRA";
String first = word.substring(0, 4);
String last = word.substring(word.length() - 4);
System.out.println(first.equals(last) + " " + first.compareTo(last));
Pick an answer, then check it. The solution opens in a window.

Question 357

Multiple choice Using Objects
public static String getResult() { String text = "banana"; int len = text.length(); String part =…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 358

Multiple choice Using Objects
The Book class is defined as follows: public class Book { private String title; private int pages; public Book(String t, int p) { title = t; pages = p; } public…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 359

Multiple choice Using Objects
What is printed?
String s = "TRIGONOMETRY";
int k = s.indexOf("NOM");
String t = s.substring(k, k + 4);
System.out.println(t + " " + t.indexOf("O"));
Pick an answer, then check it. The solution opens in a window.

Question 360

Multiple choice Using Objects
What is printed?
String s = "PARAMETRIC";
int n = s.indexOf("MET");
String out = s.substring(n - 2, n + 5);
System.out.println(out);
Pick an answer, then check it. The solution opens in a window.
Question 351 of 546
Page 36 of 55