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.

Questions 461–470 of 776
  1. 461
  2. 462
  3. 463
  4. 464
  5. 465
  6. 466
  7. 467
  8. 468
  9. 469
  10. 470

Question 461

Multiple choice Using Objects

What is printed?

String w1 = "banana";
String w2 = "band";
String w3 = "ban";
int r1 = w1.compareTo(w2);
int r2 = w2.compareTo(w3);
System.out.println((r1 > 0) + " " + (r2 < 0) + " " + (r1 + r2 > 0));
Pick an answer, then check it. The solution opens in a window.

Question 462

Multiple choice Using Objects

What is printed?

String p = "Java";
String q = p.substring(1) + p.length() + p.substring(0, 1);
System.out.println(q + " " + q.indexOf("4"));
Pick an answer, then check it. The solution opens in a window.

Question 463

Multiple choice Primitive Types

What is printed?

int a = 100, b = 3;
int q = a / b;
int r = a % b;
String s = "" + q + r + (q + r);
System.out.println(s);
Pick an answer, then check it. The solution opens in a window.

Question 464

Multiple choice Using Objects

What is printed by the following code segment?

String s = "programming";
String a = s.substring(0, s.indexOf("gram"));
String b = s.substring(s.indexOf("m") + 1, s.indexOf("ing"));
System.out.println(a.length() + b.length() + " " + a + b);
Pick an answer, then check it. The solution opens in a window.

Question 465

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 466

Multiple choice Primitive Types

What is printed?

int x = 47;
int a = x / 10;
int b = x % 10;
int y = b * 10 + a;
int z = y / 10 + y % 10;
System.out.println(z);
Pick an answer, then check it. The solution opens in a window.

Question 467

Multiple choice Primitive Types

What is printed?

int n = 1234;
int d1 = n / 1000;
int d2 = n / 100 % 10;
int d3 = n / 10 % 10;
int d4 = n % 10;
System.out.println(d4 * 1000 + d3 * 100 + d2 * 10 + d1);
Pick an answer, then check it. The solution opens in a window.

Question 468

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 469

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 470

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 461 of 776
Page 47 of 78