AP Computer Science a Quiz: Expressions And Output
7 questions · exam conditions
0:00
Expressions And OutputQuestion 1 of 7

public class Rectangle { private double length; private double width;

public Rectangle(double l, double w) {
    length = l;
    width = w;
}

public double getArea() {
    return length * width;
}

public double getPerimeter() {
    return 2 * (length + width);
}

public void scale(double factor) {
    length *= factor;
    width *= factor;
}

}

Consider the following code segment that uses the Rectangle class shown above:

Rectangle rect = new Rectangle(4.0, 6.0); double original = rect.getArea(); rect.scale(1.5); double scaled = rect.getArea(); System.out.println(scaled / original);

What is printed as a result of executing this code segment?

1.5
2.25
3.0
9.0
← Back to quizzes

AP Computer Science a Quiz

AP Computer Science a Quiz: Expressions And Output

Practice Expressions And Output in AP Computer Science a with focused quiz questions that help you check what you know, review explanations, and build confidence with test-style prompts.

What this quiz covers

This quiz focuses on Expressions And Output, giving you a quick way to practice the rules, question types, and explanations that matter most for AP Computer Science a.

How to use this quiz

Try each quiz question before looking at the correct answer. Use the explanations to review missed ideas, then come back to similar questions until the pattern feels familiar.

All questions

Question 1

public class Rectangle { private double length; private double width;

public Rectangle(double l, double w) {
    length = l;
    width = w;
}

public double getArea() {
    return length * width;
}

public double getPerimeter() {
    return 2 * (length + width);
}

public void scale(double factor) {
    length *= factor;
    width *= factor;
}

}

Consider the following code segment that uses the Rectangle class shown above:

Rectangle rect = new Rectangle(4.0, 6.0); double original = rect.getArea(); rect.scale(1.5); double scaled = rect.getArea(); System.out.println(scaled / original);

What is printed as a result of executing this code segment?

  1. 1.5
  2. 2.25 (correct answer)
  3. 3.0
  4. 9.0

Explanation: The original rectangle has dimensions 4.0 × 6.0, so original area = 24.0. After scaling by 1.5, dimensions become 6.0 × 9.0, so scaled area = 54.0. The ratio 54.0/24.0 = 2.25. When both dimensions are scaled by factor f, the area is scaled by f². Choice A (1.5) represents the linear scale factor. Choice C (3.0) might result from adding instead of multiplying scale factors. Choice D (9.0) represents scaling each dimension twice by the factor.

Question 2

Consider the following code segment:

Math.max(Math.min(8, 12), Math.max(3, 7)) + Math.abs(-4);

What value does this expression evaluate to?

  1. 11
  2. 12 (correct answer)
  3. 15
  4. 16

Explanation: Working from inner to outer: Math.min(8, 12) = 8, Math.max(3, 7) = 7, Math.abs(-4) = 4. Then Math.max(8, 7) = 8. Finally, 8 + 4 = 12. Choice A (11) might result from incorrectly calculating one of the inner functions. Choice C (15) might result from using Math.max(8, 12) = 12 instead of Math.min. Choice D (16) might result from adding all the original values without proper function evaluation.

Question 3

Consider the following code segment:

String word = "programming"; String result = word.substring(0, 4) + word.substring(7).toUpperCase(); System.out.println(result.length() + " " + result.charAt(result.length() - 1));

What is the output?

  1. 8 G (correct answer)
  2. 8 g
  3. 7 G
  4. 11 g

Explanation: word.substring(0, 4) gives "prog". word.substring(7) gives "ming" (starting at index 7). "ming".toUpperCase() gives "MING". result = "prog" + "MING" = "progMING", which has length 8. result.charAt(result.length() - 1) = result.charAt(7) = 'G'. Output is "8 G". Choice B has lowercase 'g' (forgot toUpperCase). Choice C has length 7 (miscounted). Choice D has length 11 (used original word length) and lowercase 'g'.

Question 4

Consider the following code segment:

String text = "Java"; String modified = text.toLowerCase().replace('a', 'e'); System.out.println(modified.indexOf('e') + text.length());

What is printed as a result of executing this code?

  1. 7
  2. 6
  3. 5 (correct answer)
  4. 1

Explanation: When you encounter questions involving String methods in Java, remember that most String operations create new String objects rather than modifying the original. You need to trace through each method call step by step. Let's work through this code systematically. Starting with String text = "Java", we have our initial string. The expression text.toLowerCase().replace('a', 'e') first converts "Java" to "java" (all lowercase), then replaces every 'a' with 'e', giving us "jeve". This result is stored in the modified variable. Now for the print statement: modified.indexOf('e') + text.length(). The indexOf('e') method finds the first occurrence of 'e' in "jeve", which is at index 1 (remember, String indices start at 0). The text.length() gives us 4, since "Java" has 4 characters. Therefore, we calculate 1 + 4 = 5. Looking at the wrong answers: A) 7 likely comes from incorrectly finding the last 'e' at index 3, then adding 4 (3 + 4 = 7). B) 6 might result from mistakenly thinking the first 'e' is at index 2, then adding 4. D) 1 represents just the index of the first 'e' without adding the length of the original string. Study tip: When working with String method chains, write out each intermediate result. Also remember that indexOf() returns the first occurrence of a character, starting from index 0. Practice tracing through these step-by-step transformations—they're common on the AP exam.

Question 5

public class Temperature { private double celsius;

public Temperature(double c) {
    celsius = c;
}

public double getFahrenheit() {
    return celsius * 9.0 / 5.0 + 32.0;
}

public double getCelsius() {
    return celsius;
}

public void setCelsius(double c) {
    celsius = c;
}

}

What is printed when the following code segment is executed?

Temperature temp = new Temperature(0.0); System.out.println((int)(temp.getFahrenheit())); temp.setCelsius(100.0); System.out.println((int)(temp.getFahrenheit() / 4.0));

  1. 32 53 (correct answer)
  2. 32 212
  3. 0 53
  4. 32 25

Explanation: For 0°C: getFahrenheit() = 0 * 9/5 + 32 = 32.0, cast to int gives 32. For 100°C: getFahrenheit() = 100 * 9/5 + 32 = 180 + 32 = 212.0. Then 212.0/4.0 = 53.0, cast to int gives 53. Choice B shows the second line as 212 (forgot to divide by 4). Choice C shows 0 for the first line (forgot the +32 in conversion). Choice D shows 25 for the second line (might be from 100/4 instead of using the full conversion).

Question 6

public class Circle { private double radius;

public Circle(double r) {
    radius = r;
}

public double getArea() {
    return Math.PI * radius * radius;
}

public double getCircumference() {
    return 2 * Math.PI * radius;
}

public void setRadius(double r) {
    radius = r;
}

public double getRadius() {
    return radius;
}

}

What is printed when the following code segment is executed?

Circle c = new Circle(2.0); double ratio1 = c.getArea() / c.getCircumference(); c.setRadius(4.0); double ratio2 = c.getArea() / c.getCircumference(); System.out.println((int)(ratio2 / ratio1));

  1. 1
  2. 2 (correct answer)
  3. 4
  4. 8

Explanation: For radius r, area/circumference = πr²/(2πr) = r/2. When radius = 2.0, ratio1 = 2.0/2 = 1.0. When radius = 4.0, ratio2 = 4.0/2 = 2.0. Therefore ratio2/ratio1 = 2.0/1.0 = 2.0, cast to int gives 2. Choice A (1) might result from thinking the ratios are equal. Choice C (4) represents the ratio of the radii squared. Choice D (8) might result from incorrect calculation of area ratios.

Question 7

public class Counter { private int value;

public Counter() {
    value = 0;
}

public Counter(int initial) {
    value = initial;
}

public void increment() {
    value++;
}

public void decrement() {
    value--;
}

public int getValue() {
    return value;
}

public void reset() {
    value = 0;
}

}

What is the output of the following code segment?

Counter c1 = new Counter(5); Counter c2 = new Counter(); c1.decrement(); c2.increment(); int result = c1.getValue() * c2.getValue(); System.out.println(result + c1.getValue());

  1. 4
  2. 5
  3. 8 (correct answer)
  4. 9

Explanation: This question tests your ability to trace through object instantiation, method calls, and variable operations step by step. When working with custom classes, you need to carefully track how each object's state changes through method calls. Let's trace through the code execution. First, Counter c1 = new Counter(5) creates a Counter object with an initial value of 5. Then Counter c2 = new Counter() uses the default constructor, setting c2's value to 0. Next, c1.decrement() reduces c1's value from 5 to 4, and c2.increment() increases c2's value from 0 to 1. When we calculate int result = c1.getValue() * c2.getValue(), we get 4 × 1 = 4. Finally, System.out.println(result + c1.getValue()) prints 4 + 4 = 8. Looking at the wrong answers: Choice (A) gives 4, which would be correct if you forgot the final addition of c1.getValue() and only printed the result of the multiplication. Choice (B) gives 5, which might occur if you confused the initial values or forgot about the decrement operation on c1. Choice (D) gives 9, which could happen if you mistakenly thought c1's final value was 5 instead of 4, leading to 4 + 5 = 9. When tracing through object-oriented code, always track each object's state separately and execute operations in the exact order they appear. Write down intermediate values to avoid losing track of changes to instance variables.