AP CSA CodeHS section 2

Ace your homework & exams now with Quizwiz!

The purpose of a wrapper class is to

"Wrap" a primitive value to convert it to an object

Java automatically converts between objects and primitives in the process of autoboxing and unboxing. What happens when a int is autoboxed?

A int is autoboxed when it is converted to a Integer

What would be printed by the following code snippet? String lastName = "Vu"; String otherLastName = "Lopez"; int comparison = lastName.compareTo(otherLastName); System.out.println(comparison); Zero because both strings start with capital letters A positive number because "Vu" comes before "Lopez" in lexicographical order. A negative number because "Vu" comes before "Lopez" in lexicographical order. A positive number because "Vu" comes after "Lopez" in lexicographical order. A negative number because "Vu" comes after "Lopez" in lexicographical order.

A positive number because "Vu" comes after "Lopez" in lexicographical order.

Which of these is not a difference between primitives and objects? An object stores an address as a value while a primitive stores a literal value. An object has data and methods associated with it while a primitive only stores data. When passed to a method, objects are passed by reference while primitives are copied and passed by value. A primitive has data and methods associated with it while an object only stores data.

A primitive has data and methods associated with it while an object only stores data.

Strings are immutable. This means that

Once a String variable has been assigned a value, the value cannot be modified but the variable can be assigned to a different value.

Which of the following is NOT a valid way to overload this constructor? For brevity, only the signature is given. Pineapple Pineapple(String color) a. Pineapple Pineapple() b. Pineapple Pineapple(String color, int age) c. Pineapple Pineapple(int age, String species) d. Pineapple FancyPineapple(String color, int age)

Pineapple FancyPineapple(String color, int age)

Which of the following would properly print this quote by Edsger W. Dijkstra (an early pioneer of Computer Science) as shown below? "Testing shows the presence, not the absence of bugs" --- Edsger W. Dijkstra System.out.println('"Testing shows the presence, not the absence of bugs"'); System.out.println("--- Edsger W. Dijkstra"); System.out.println("Testing shows the presence, not the absence of bugs"); System.out.println("--- Edsger W. Dijkstra"); System.out.println("\"Testing shows the presence, not the absence of bugs\""); System.out.println("--- Edsger W. Dijkstra"); System.out.println(""Testing shows the presence, not the absence of bugs""); System.out.println("--- Edsger W. Dijkstra");

System.out.println("\"Testing shows the presence, not the absence of bugs\""); System.out.println("--- Edsger W. Dijkstra");

What are parameters?

The formal names given to the data that gets passed into a method.

A reference variable holds a special value. What is this special value?

The memory address of an object

A Timer class is a class that represents a minute timer. A partial definition of the class is given below. public class Timer { private int length; public Timer(int duration) { length = duration; } public void endTime() { System.out.print("The timer will end in " ); System.out.print(length); System.out.println(" minutes"); } public void addFiveMinutes() { length = length + 5; } } What is the output of the following main method? public static void main(String[] args){ Timer muffins = new Timer(30); muffins.endTime(); muffins.addFiveMinutes(); muffins.endTime(); } The timer will end in 30 minutes The timer will end in 30 minutes The timer will end in 35 minutes The timer will end in 30 minutes The timer will end in 30 minutes This method won't print anything because it doesn't have any print statements.

The timer will end in 30 minutes The timer will end in 35 minutes

The value that a method outputs is called

a return value.

Consider the follow class: public class Rectangle { private int width; private int height; public Rectangle(int rectWidth, int rectHeight) { width = rectWidth; height = rectHeight; } public int getArea() { return width * height; } public int getHeight() { return height; } public int getWidth() { return width; } public String toString() { return "Rectangle with width: " + width + " and height: " + height; } } If a new variable Rectangle shape = new Rectangle(10, 20); was initialized, what is the correct syntax for retrieving the area of shape? int area = Rectangle.getArea(); int area = shape.getArea(); shape.getArea(10,20); Rectangle.getHeight() * Rectangle.getWidth(); shape.getArea();

int area = shape.getArea();

Suppose there is a class called Student. One of the methods is given below. It sets the instance variable isHonors to the parameter value. public void setHonorStatus(boolean status) { isHonors = status; } Using the Student object called karel, which of the following is the correct way to set karel's honor status to true? karel.setHonorStatus(isHonors = true); karel.isHonors = true; karel.setHonorStatus(status=true); karel.setHonorStatus(true);

karel.setHonorStatus(true);

Which of the following methods is implemented correctly with respect to the method's return type? public String getColor() { return "Red"; } public int getColor() { return "Red"; } public void getColor() { return "Red"; } public Color() { return "Red"; }

public String getColor() { return "Red"; }

Which of the following is a correctly written method for the class below? public class Timer { private int startMin; private int length; public Timer(int minute, int duration) { startMin = minute; length = duration; } public Timer(int duration) { startMin = 0; length = duration; } } public void addFiveMinutes() { length = length + 5; } public addFiveMinutes() { length = length + 5; } addFiveMinutes() { length = length + 5; } public void addFiveMinutes { length = length + 5; }

public void addFiveMinutes() { length = length + 5; }

Consider this code snippet that uses a class called Rectangle. int roomHeight = 40; int roomWidth = roomHeight * 3; Rectangle room = new Rectangle(roomHeight, roomWidth); Which of the following is a reference variable? room roomHeight roomWidth Rectangle

room

Consider the following methods involving strings and integers. public void update(String x, int y) { x = x + "Retriever"; y = y * 3; } public void myTest() { String s = "Golden"; int num = 7; update(s, num); /*end of method*/ } When a call to myTest() is invoked, what are the values of s and num at the point indicated by the end of method? s is the string "Golden"; num = 7 s is the string "GoldenRetriever"; num= 7 s is the string "Golden"; num = 21 s is the string "GoldenRetriever"; num = 21 s is the string "Retriever"; num = 21

s is the string "Golden"; num = 7

public class Shark { // Attributes private String habitat; private int age; public Shark(String region, int sharkAge) { habitat = region; age = sharkAge; } } Which of the following choices is a formal parameter of the constructor?

sharkAge

What method must a class implement in order to concatenate an object of the class with a String object?

toString

What is the output of the following code snippet? String forest = "Amazon Rainforest"; System.out.println(forest.indexOf('a')); System.out.println(forest.indexOf('g')); System.out.println(forest.indexOf('n'));

2 -1 5

Consider the Circle class below. public class Circle { private double radius; public Circle(double circleRadius) { radius = circleRadius; } public void setRadius(int newRadius) { radius = newRadius; } public void printDiameter() { double diameter = 2 * radius; System.out.println(diameter); } } What is the output of the main method below? public class MyProgram { public static void main(String[] args) { Circle pizza = new Circle(12); pizza.printDiameter(); pizza.setRadius(10); pizza.printDiameter(); } } 24.0 20.0 24.0 24.0 Nothing will print because the code contains an error. 24.0

24.0 20.0

Java automatically converts between objects and primitives in the process of autoboxing and unboxing. What happens when a Double is unboxed?

A Double is unboxed when it is converted to a primitive value.

Which of the following best describes the relationship between a class and an object?

A class definition specifies the attributes and behavior of every object that will be made.

What is a constructor in Java?

A constructor allows us to create a new instance of a class, usually initializing instance variables.

Which of the following statements will not compile? You may assume any text in this font is an initialized variable. I. "Tilly is " + age + " years old" II. "My favorite letter is " + 'k' III. greeting + name IV. "Our team, " + teamName + " has " + numPlayers III because you can't combine two variables I, II, III, IV because you can only concatenate two things at a time. I, IV because you can't combine Strings and numbers. All of these statements will compile

All of these statements will compile

What is an instance method?

An instance method is a piece of code called on a specific instance (an object) of the class. It is called with a receiver object.

What is an object in Java?

An object is something that contains both state and behavior.

What does it mean to be a client of a class?

Being a client of a class means that we can use its methods and functionality without necessarily understanding how it works.

Every class definition has each of the following EXCEPT A name Defined attributes Defined behaviors to manipulate the state of the objects Defined objects as copies of the class

Defined objects as copies of the class

Consider this class definition of a Pineapple. public class Pineapple { private boolean isRipe; private String color; private double weight; // Rest of class goes here } When we use this class to create Pineapple objects, which of the following is guaranteed to be true?

Every Pineapple object will have the same attributes.

Suppose a program is a client of the Player class. Here is a snippet of code contained in the program Player firstPlayer = new Player("Karel", "Warrior", "Mote Prime", 90); Looking at the documentation of the class, you find the signature for the constructor, shown below. Player Player(String name, String role, String location, int health); Where would you find the formal parameters? In the program. In the documentation. In the library. Both the program and the documentation contain formal parameters.

In the documentation.

What is the purpose of overloading a class' constructor?

It allows the user to set the values of different combinations of the instance variables when the object is created.

String language = "Java"; String opinion = " is fun!"; System.out.println(language + opinion);

Java is fun!

Which of the following is NOT part of the constructor signature?

Which instance variables are initialized

You are using a class as a client. What would you need to know in order to create an object of the class you intend to use?

You need to know the formal parameters in order to pass in actual parameters.

Question: 3 Suppose you have a class called Elevator. The Elevator class has a method called goingUp, partially defined below public boolean goingUp() { // code omitted } Which of the following statements correctly stores the return value of goingUp when it is called on the Elevator object called hotel? int up = hotel.goingUp(); double up = hotel.goingUp(); boolean up = hotel.goingUp(); String up = hotel.goingUp();

boolean up = hotel.goingUp();

Which of the following correctly calls the method addFiveMinutes on an object of the Timer class called kitchenTimer? kitchenTimer(addFiveMinutes) Timer.addFiveMinutes() kitchenTimer.addFiveMinutes() kitchenTimer.addFiveMinutes

kitchenTimer.addFiveMinutes()

Suppose the class Timer has a method called startTime that prints out the starting time of the Timer. Which of the following correctly uses this method to print out the start time of a Timer object called laundry? System.out.println(laundry.startTime()); System.out.println(laundry.startTime); int start = laundry.startTime(); laundry.startTime(); laundry.startTime;

laundry.startTime();

What is the importance of the null value?

null allows a reference variable to be empty and not hold any memory address.

Refer to the Card class shown below. public class Card { private String suit; private int value; //13 values for each suit in deck (0 to 12) public Card (String cardSuit, int cardValue) { /* implementation */} // Rest of the class goes here } Which of the following is the correct /* implementation */ code for the constructor in the Card class?

suit = cardSuit; value = cardValue;


Related study sets

RH124 Chapter 7: Controlling Access to Files

View Set

NFA 201- SmartBook Unit 4: How Food Becomes You

View Set

Biology Fundamentals Book Vocabulary

View Set

Chapter 7: Legal dimensions of nursing practice

View Set

Laws and Regulation- General and Life Insurance Sheet

View Set

pt 125 negative reinforcement( escape behavior) and discriminative stimuli

View Set

Week 3 - Decentralisation, Responsibility Centres & Performance Evaluation Systems; ROI, RI and EVA

View Set

PN Fundamentals Online Practice 2020 B with NGN

View Set

Topic 5 Quick Check: Funding with Debt

View Set

LEED Project Surroundings & Public Outreach

View Set

Layers of the Integument and Subcutaneous layer

View Set