Expressions and Output()
Help Questions
AP Computer Science A › Expressions and Output()
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
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.
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?
11
12
15
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.
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?
7 G
8 G
11 g
8 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'.
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
5
6
7
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.
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));
0 53
32 25
32 53
32 212
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).
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
2
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.
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());
4
5
8
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.