AP CSA Code.org Unit 2 TEST

0.0(0)
Studied by 2 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/14

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 4:17 AM on 11/3/25
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

15 Terms

1
New cards

Which one of the following declarations are NOT correct?

A. double size;

B. String style = "high-top";

C. boolean inStock = true;

D. int price = 40.99;

E. String material = "canvas";

D

2
New cards

What is the final value of temperature?

int temperature = 72; String dayOfWeek = "Sunday"; dayOfWeek = "Monday"; temperature = 68; dayOfWeek = "Tuesday"; dayOfWeek = "Wednesday";

A. "Wednesday"

B. 68

C. "Monday"

D. 72

E. "Sunday"

B. 68

3
New cards

Consider the following constructor, which is intended to assign the parameter x to an instance variable also named x. What changes can be made for the code to work as intended?

public Mystery(int x) { x = x; // line 2 }

I. Change line 2 to this.x = x;

II. Change line 2 to x = this.x;

III. Change the parameter name to newX and update line 2 to x = newX;

A. I only

B. II only

C. III only

D. I and III

E. II and III

D. I and III

4
New cards

The Store class will contain String attributes for its name and website. The class will also contain a constructor.

class Store { / missing code / }

Which of the following replacements for / missing code / is the most appropriate implementation of the Store class?

A. Private String name;

Private String website;

Public store (String nm, String web)

{Implementation not shown}

A (or whatever anwser has private instance variables and public constructor/method)

5
New cards

Consider the following class definition. Each object of the class Dog will store the dog's name as name, the dog's age as age, and the number of puppies the dog has as puppies.

public class Dog { private String name; private int age; private int puppies; public Dog(String n, int a, int p) { name = n; age = a; puppies = p; } public Dog(String n, int a) { name = n; age = a; puppies = 0; } }

I. Dog d = new Dog("Caesar", 3, 0);

II. Dog d = new Dog("Bailey", 5, 8);

III. Dog d = new Dog("Koda", 3);

Which of the following code segments, found in a class other than Dog, can be used to create a dog that is 3 years old with no puppies?

D. I and III

6
New cards

Which of these is an overloading constructor?

A. When a constructor takes two parameters.

B. When a constructor takes more than one parameter.

C. When one constructor is defined in a class.

D. When more than one constructor is defined in a class.

D

7
New cards

Consider the following code segment.

int x = 2; double y = 1.5; double z = 4.0; z = y * 4 - (15 % x + z);

What is the value of z after this code segment is executed?

A. 0.0

B. 1.0

C. 4.0

D. 5.0

E. Running the code results in an ArithmeticException

B. 1.0

8
New cards

Consider the following class definitions.

public class Plant { private String leafColor; public Plant(String leafColor) { this.leafColor = leafColor; } public void setLeafColor(String color) { leafColor = color; } } public class Tree extends Plant { private double treeDiameter; public Tree (String treeColor, double diameter) { super(treeColor); this.treeDiameter = diameter; } }

The following statement appears in a method in another class.

Plant maple = new Tree("red", 3.5);

Which of the following best describes the effect of executing the statement?

A. The Plant variable maple is instantiated as a Tree. The instance variable treeDiameter is initialized with the value from the parameter diameter. The Tree constructor cannot set the instance variable leafColor because a subclass does not have access to a private variable in its superclass.

B. The Plant variable maple is instantiated as a Tree. The call super(treeColor) returns a value from the instance variable leafColor in the superclass and makes it accessible in the subclass. The instance variable treeDiameter is then initialized with the value from the parameter diameter.

C. The Plant variable maple is instantiated as a Tree. The instance variable treeDiameter is initialized with the value from the parameter diameter. No other initializations are made to any instance variables.

D. The Plant variable maple is instantiated as a Tree. The call super(treeColor) invokes the Plant constructor and initializes the instance variable leafColor with the value from the parameter treeColor. The instance variable treeDiameter is then initialized with the value from the parameter diameter.

E. The code segment will not execute because the Plant variable maple cannot be instantiated as a Tree.

D.

9
New cards

Which of the following changes to GroceryItem will allow other classes to access but not modify the value of price?

public class GroceryItem { private double price; public GroceryItem(double newPrice) { price = newPrice; } }

A. Make price public.

B. Include the method private double getPrice() { return price; }

C. Include the method public void getPrice(double newPrice) { newPrice = price; }

D. Include the method private void getPrice(double newPrice) { newPrice = price; }

E. Include the method public double getPrice() { return price; }

E.

10
New cards

Consider the following class definition. The class does not compile.

public class FoodTruck { private String owner; // Constructor not shown public getOwner() { return owner; } }

The accessor method getOwner is intended to return the name of the owner of the FoodTruck object. Which of the following best explains why the class does not compile?

A. The return type of the getOwner method needs to be defined as void.

B. The getOwner method should be declared as private.

C. The return type of the getOwner method needs to be defined as String.

D. The getOwner method requires a parameter.

E. The return type of the getOwner method needs to be defined as boolean.

C. Return type needs to be defined as string

11
New cards

Consider the following class definition.

public class Student { private double gpa; public Student(double newGpa) { gpa = newGpa; } public void setGpa (double newGpa) { gpa = newGpa; } }

Consider the following code segment, which appears in a method in a class other than Student. The code segment does not compile.

Student s = new Student(3.0); System.out.println("The new GPA is " + s.setGpa(3.5));

Which of the following best explains the reason why the code segment will not compile?

A. The code segment attempts to access the private variable gpa from outside the Student class.

B. The gpa cannot change.

C. The Student class constructor is invoked incorrectly.

D. The setGpa method cannot be called from outside the Student class.

E. The setGpa method does not return a value that can be printed.

E

12
New cards

Consider the following class definition. The class does not compile.

public class FoodTruck { private String owner; // Constructor not shown public void setOwner(String owner) { this.owner = owner; return owner; } }

Which of the following best explains why the class does not compile?

A. There should be no return type in the setOwner method header.

B. The setOwner method should be declared as private.

C. The setOwner method should not return.

D. The setOwner method should not have a parameter.

E. The return type in the setOwner method header needs to be defined as Owner.

C

13
New cards

Consider the following class definition.

public class Mystery { private int x; // Constructor not shown. }

Which of the following is a correct header for a method of the Mystery class that would change the value of the private instance variable x given an int argument so that it can be called from a class other than Mystery?

A. private setX(int x)

B. private void setX(int x)

C. public setX(int x)

D. public void setX(int x)

E. public setX()

D

14
New cards

Consider the following class definition.

public class Average { private int sum; public int inputs; // Constructor not shown. }

Which best describes the visibility of the instance variables of this class?

A. sum is visible outside of the class, inputs is visible only within the class.

B. sum is visible only within the class, inputs is visible outside of the class.

C. Both sum and inputs are visible only within the class.

D. Both sum and inputs are visible outside of the class.

E. There is not enough information in the class definition provided to know the visibility of the instance variables.

B

15
New cards

Which of the following correctly prints the output:

You say "goodbye", I say "hello".

C. System.out.println("You say \"goodbye\",\nI say \"hello\".");