AP CSA Unit 1 Assessment
Multiple Choice
1.
Given the method header double convert(int whole, double fraction), which method call will compile?
A. convert(4.5, 2.0)
B. convert(4, 2.5)
C. convert(4)
D. convert(4, 2.5, 1)
2.
Assume that x is a double variable with a positive value. Which of the following code segments can be used to round x to the nearest integer and store the rounded value in the variable result?
A.
int result = (int) x;
B.
int result = (int) x + 0.5;
C.
int result = (int)(x + 0.5);
D.
int result = (int) x + (int) 0.5;
3.
What is the value of x after the following code executes?
int x = 5;
x += 3;
x *= 2;
A. 10
B. 13
C. 16
D. 18
4.
What is the value of the following expression?
Math.abs(Math.pow(-2, 3)) + Math.sqrt(16)
A. 4.0
B. 8.0
C. 12.0
D. 20.0
5.
Three test scores have equal weight. A final exam is worth twice as much as each test. Which expression correctly calculates the weighted average?
A. (test1 + test2 + test3 + exam) * 2 / 5.0
B. (test1 + test2 + test3 + 2 * exam) / 5.0
C. (test1 + test2 + test3) / 3.0 + 2 * exam
D. (test1 + test2 + test3) / 2.0 + exam / 2.0
6.
What is printed when integer addition precedes string concatenation?
System.out.println(4 + 2 + " points");
A. 6 points
B. 42 points
C. 4 + 2 points
D. Compilation error
7.
What is the value of result after the following code executes?
int result = (int) 9.9 * 2;
A. 19
B. 19.8
C. 18
D. Compilation error
8.
What is printed when concatenation starts with a string?
System.out.println("Score: " + 4 + 2);
A. Score: 42
B. Score: 6
C. Score: 4 + 2
D. Score: 4 2
9.
What is printed by a string containing a newline and escaped quotes?
System.out.println("Hi\n\"AP\"");
A. Hi\n"AP"
B. Hi "AP"
C.
Hi
"AP"
D.
Hi
AP
10.
What is printed by consecutive print and println calls?
System.out.print("A");
System.out.print("B");
System.out.println("C");
System.out.print("D");
A. AB C D
B. ABC followed by a newline then D
C. ABCD
D. AB followed by a newline then CD
11.
Consider the following information about the calculateBill method.
- The method is a
staticmethod in theStoreclass. - The method has one
intparameter. - The method has return type
double. - The method can be accessed from another class.
Which of the following code segments, when appearing in a method in a class other than Store, will compile without error?
A.
double tot = calculateBill(5);
B.
double tot = Store.calculateBill(5);
C.
double tot;
tot.calculateBill(5);
D.
double tot = 5.0;
tot.calculateBill();
12.
I.
String str = "Good " + "morning," + " sunshine!";
II.
String str = "Good";
str += " morning, sunshine!";
III.
String str = " morning, ";
str = "Good" + str + "sunshine!";
Which of the following code segments can be used to set the value of the string str to "Good morning, sunshine!"?
A. I only
B. II only
C. II and III only
D. I, II, and III
13.
The AnimalPrinter class includes the following two methods.
| Method Signature | Explanation |
|---|---|
public void printDog() | Prints the word "dog" and then moves the cursor to a new line. |
public void printCat() | Prints the word "cat" and then moves the cursor to a new line. |
The method myMethod appears in a class other than AnimalPrinter. The method is intended to produce the following output.
Intended output
dog
cat
Assume that an AnimalPrinter object myPrinter has been properly declared and initialized inside myMethod. Which of the following code segments, if located in myMethod, will produce the intended output?
A.
printDog();
printCat();
B.
printDog(AnimalPrinter);
printCat(AnimalPrinter);
C.
AnimalPrinter.printDog();
AnimalPrinter.printCat();
D.
myPrinter.printDog();
myPrinter.printCat();
14.
Consider the following variable declaration.
int x;
Which of the following values can be stored in the variable x?
A. 0.5
B. 1
C. true
D. false
15.
Which pair of method headers forms a valid overload?
A. int adjust(int x) and double adjust(int x)
B. int adjust(int x) and int adjust(double x)
C. int adjust(int x) and int adjust(int value)
D. int adjust(int x) and int adjust(int x)
16.
Assume that the following variable declarations have been made.
double d = Math.random();
double r;
Which of the following assigns a value to r from the uniform distribution over the range 0.5 ≤ r < 5.5?
A.
r = d + 0.5;
B.
r = d + 0.5 * 5.0;
C.
r = d * 5.0;
D.
r = d * 5.0 + 0.5;
17.
What is the value of x after the following code executes?
double x = (double) 10 / 4;
A. 2
B. 2.0
C. 2.5
D. 4
18.
A program stores a sensor temperature, the number of readings, and whether the sensor is active. Which declarations are most appropriate?
A.
int temperature;
double readings;
String active;
B.
double temperature;
double readings;
boolean active;
C.
double temperature;
int readings;
boolean active;
D.
String temperature;
int readings;
boolean active;
19.
The following is an excerpt of a class specification that appears in an API library.
public class Shirt
The Shirt class is used to create objects that represent shirts.
The Shirt class has color and size variables that hold information about the characteristics of a shirt.
The Shirt constructor initializes a Shirt object with the given color and size.
The getColor and getSize methods return information about a shirt.
A poloShirt object will be declared as type Shirt.
Based on the class specification, which of the following descriptions is accurate?
A. The color and size variables are attributes of the poloShirt object.
B. The color and size variables are behaviors of the poloShirt object.
C. The poloShirt object is an attribute of the getColor and getSize methods.
D. The poloShirt object is a behavior of the getColor and getSize methods.
20.
A student has created a Song class. The class contains the following three variables.
- A
Stringvariable namedartistto represent the artist name - A
Stringvariable namedtitleto represent the song title - A
Stringvariable namedalbumto represent the album title
The object happyBirthday will be declared as type Song.
Which of the following statements is true?
A. artist, title, and album are instances of the Song class.
B. happyBirthday is an instance of three String objects.
C. happyBirthday is an instance of the Song class.
D. Song is an instance of the happyBirthday object.
21.
Given the method header double average(int total, int count), which statement correctly calls the method and stores its returned value?
A. int result = average(18, 4);
B. double result = average(18.0, 4);
C. double result = average(18, 4);
D. double result = average(18);
22.
The Sofa class has only two constructors. Partial declarations of the two constructors are shown.
public class Sofa(int numOfSeats, double length, String color)
{ /* implementation not shown */ }
public class Sofa(int numOfSeats, double length)
{ /* implementation not shown */ }
Which of the following declarations, appearing in a class other than Sofa, will correctly create a Sofa object?
A.
Sofa s1 = new Sofa(3, 7.5);
B.
Sofa s2 = new Sofa(3.0, 8.0);
C.
Sofa s3 = new Sofa("3", "7.5", "brown");
D.
Sofa s4 = new Sofa(3.0, 8.0, "blue");
23.
What is printed when a substring result is not assigned?
String word = "coding";
word.substring(0, 3);
System.out.println(word);
A. cod
B. coding
C. ding
D. Compilation error
24.
The following code segment appears in a method. In the code segment, num1 and num2 are int variables. The method is intended to print the sum of num1 and num2.
int result = num1 + num2;
System.out.println(result);
Which of the following preconditions for the method is most appropriate to avoid an overflow error?
A.
/** Precondition: num1 and num2 are both positive. */
B.
/** Precondition: num1 is not equal to num2 */
C.
/** Preconditions: num1 is between Integer.MIN_VALUE
* and Integer.MAX_VALUE, inclusive.
* num2 is between Integer.MIN_VALUE
* and Integer.MAX_VALUE, inclusive.
*/
D.
/** Precondition: (num1 + num2) is between Integer.MIN_VALUE
* and Integer.MAX_VALUE, inclusive.
*/
25.
Which expression can replace /* missing code */ so the program prints BCEF?
String oldStr = "ABCDEF";
String newStr = /* missing code */;
System.out.println(newStr);
A. oldStr.substring(1, 2) + oldStr.substring(4)
B. oldStr.substring(1, 2) + oldStr.substring(5)
C. oldStr.substring(1, 3) + oldStr.substring(4)
D. oldStr.substring(1, 3) + oldStr.substring(5)
26.
Consider the following description of the MagicNumber class which includes one constructor and two methods.
public MagicNumber() - Constructs a MagicNumber object that represents a number
public void displayNumber() - Displays the number to the screen
public void add_2() - Increases the number by 2
I.
MagicNumber.add_2();
MagicNumber.displayNumber();
II.
MagicNumber n1 = new MagicNumber();
n1.add_2();
n1.displayNumber();
III.
n2.add_2();
n2.displayNumber();
When located in a method in a class other than MagicNumber, which of the following code segments will compile without error?
A. I only
B. II only
C. II and III only
D. None of the code segments will compile.
27.
Which code segment correctly calls the method and stores its returned value?
Assume the following method appears in the same class as the code segment.
public double average(int total, int count) {
return (double) total / count;
}
A. int result = average(18, 4);
B. double result = average(18, 4);
C. double result = average(18.0, 4.0);
D. average = double result(18, 4);
28.
Consider the following class definition.
public class Beverage
{
private int temperature;
public Beverage(int t)
{
temperature = t;
}
public int getTemperature()
{
return temperature;
}
public boolean equals(Object other)
{
if (other == null)
{
return false;
}
Beverage b = (Beverage) other;
return (b.getTemperature() == temperature);
}
}
The following code segment appears in a class other than Beverage. Assume that x and y are properly declared and initialized int variables.
Beverage hotChocolate = new Beverage(x);
Beverage coffee = new Beverage(y);
boolean same = /* missing code */;
Which of the following can be used as a replacement for /* missing code */ so that the boolean variable same is set to true if and only if the hotChocolate and coffee objects have the same temperature values?
A. (hotChocolate == coffee)
B. hotChocolate.equals(coffee)
C. hotChocolate.equals(coffee.getTemperature())
D. hotChocolate.getTemperature().equals(coffee.getTemperature())
29.
Consider the following code segment.
System.out.print("*");
System.out.println("**");
System.out.println("***");
System.out.print("****");
What is printed as a result of executing the code segment?
A.
*
**
***
****
B.
*
**
*******
C.
***
***
****
D.
***
*******
30.
Consider the following descriptions of two methods that appear in the same class.
| Method Signature | Explanation |
|---|---|
public void methodA(int arg) | Calls methodB with the value of arg * 10 |
public void methodB(int arg) | Displays the value of arg + 10 |
Consider the call methodA(4), which appears in a method in the same class. What, if anything, is printed as a result of the call methodA(4)?
A. 40
B. 50
C. 140
D. Nothing is printed.
Fill & Explain
31.
Initially int a and int b hold arbitrary readings for which a+b and a-b fit in int. The required final values are oldA+oldB and oldA-oldB respectively. Repair the fragment by adding at most one temporary int variable. Explain why simply reordering the two assignments does not work for all inputs.
a = a + b;
b = a - b;
32.
Assume Book has a public constructor Book(String title). Consider the code below. State how many Book objects are constructed, which variables refer to the same object at the end, and whether missing refers to an empty Book object.
Book first = new Book("A");
Book second = first;
Book third = new Book("A");
Book missing = null;
33.
What is the output of the following code?
int a = 7;
int b = 3;
b = a + b;
a = b - a;
System.out.println(a + " " + b);
Output: ______
34.
What is the output of the following code?
double a = 7;
int b = (int) (a / 2);
double c = (double) b / 2;
System.out.println(b + " " + c);
Output: ______
35.
A parcel record stores code "0042", a whole-number item count 3, a measured mass 2.75, and a paid status of false. Declare four appropriately typed Java variables initialized to those values. Explain why changing code to int and mass to int can each lose required information, and identify whether int mass = 2.75 would compile.
36.
What is the value stored in part?
String word = "banana";
int pos = word.indexOf("na");
String part = word.substring(pos, pos + 3);
Value: ______
38.
Class Pass has exactly these public constructors:
Pass(String name, int visits)
Pass(int visits, double price)
Which calls compile? For each valid call, identify the constructor by its ordered parameter types.
new Pass("Lee", 4); // A
new Pass(4, 12); // B
new Pass(4.0, 12); // C
new Pass(4, "Lee"); // D
39.
Assume min and max are int variables with -1000 <= min < max <= 1000. Write a Java expression that generates a random integer from min to max, inclusive.
Expression: ______
40.
Complete the substring call to remove exactly the first and last characters.
The expression must work for every non-null word of length at least 2.
String word = "PLANET";
String middle = ____;
System.out.println(middle);
Code Tracing
41.
Write the exact output.
int a = 7;
int b = 3;
String label = "N";
System.out.println(label + a + b);
System.out.println(a + b + label);
System.out.println(label + (a / b) + ":" + (a % b));
42.
Write the exact output.
double amount = 13;
int whole = (int) (amount / 4);
double half = (double) whole / 2;
System.out.println(whole + " " + half);
43.
Write the output of string length and space-index arithmetic.
String text = "AP";
text = text + " CSA";
int len = text.length();
int index = text.indexOf(" ");
System.out.println(len - index);
Final output: ______
44.
Write the output after narrowing conversion and integer division.
int num = (int) (9.9 * 2);
double value = num / 4;
System.out.println(value);
Final output: ______
Programming
A complete class or main method is not required unless the task asks for one.
45.
Declare an int totalMinutes initialized to 137. Write Java statements that print the number of whole hours and remaining minutes in the form "2 h 17 min". The calculations must also work if totalMinutes is changed to any nonnegative int. Do not hard-code the quotient or remainder.
46.
Extract and print a suffix using String methods.
You do not need to write the complete main method signature.
Starting with a String containing "programming", write a Java code segment that uses String methods to create and print End: mming. Do not hard-code "mming" or its starting index.
47.
Write Java statements that use exactly one call to Math.random() to assign an equally likely random integer from 2 through 8 inclusive to int ticket, then print ticket. Do not use another random API.
48.
Read an integer and a double with Scanner and print their sum.
You do not need to write the complete main method signature.
Write a Java code segment that creates a Scanner, reads an int score followed by a double bonus, and prints their sum as a double. Include any required import, but do not write a complete class.