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.
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?
AP Computer Science a Quiz
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.
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.
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.
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?
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?
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?
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?
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.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));
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));
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());
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.