AP CSA Unit 2

studied byStudied by 12 people
0.0(0)
Get a hint
Hint

What is an object in Java?

1 / 42

43 Terms

1

What is an object in Java?

  1. An object is a template for how to make new programs in Java.

  2. An object is something that contains both state and behavior.

  3. An object is a single part of a computer program.

  4. An object is a list of variables.

New cards
2

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.

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

A class specifies the attributes and behaviors for exactly one object.

Every object can choose which attributes and behavior it wants to keep from the class definition.

New cards
3

Every class definition has each of the following EXCEPT

  1. A name

  2. Defined attributes

  3. Defined behaviors to manipulate the state of the objects

  4. Defined objects as copies of the class

New cards
4

Consider this class definition of a Pineapple.

public class Pineapple
{
private boolean isRipe;
private String color;
private double weight;

// Rest of class goes here
}

Java

When we use this class to create Pineapple objects, which of the following is guaranteed to be true?

  1. Every Pineapple object will be yellow

  2. Every Pineapple object must choose which attributes it wants.

  3. Every Pineapple object will have the same attributes.

  4. It is impossible to know what attributes the Pineapple objects will have since the attributes are not listed here.

Answered


New cards
5

What is a constructor in Java?

  1. A constructor is something that holds the private state of an instance.

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

  3. A constructor is a method with instructions on how to use a class.

  4. A constructor is a syntax rule in Java for placing curly brackets.

New cards
6

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
}

Java

Which of the following is the correct /* implementation */ code for the constructor in the Card class?

cardSuit = suit;
cardValue = value;

Card = new Card (cardSuit, cardValue);

Card = new Card (suit, value);

suit = cardSuit;
value = cardValue;
//Right Answer Above

suit = "Hearts";
value = 3;
New cards
7
/** 
* The Shark class describes a shark.
*
* Every shark has a region where it lives and an age.
* z
*/

public class Shark
{
// Attributes
private String habitat;
private int age;

public Shark(String region, int sharkAge)
{
habitat = region;
age = sharkAge;
}
}

Java

Which of the following choices is a formal parameter of the constructor?

habitat

String

Shark

sharkAge

New cards
8

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

  1. Which instance variables are initialized

  2. The parameter types

  3. The order of the parameters

  4. The name of the constructor

New cards
9

Which of the following is NOT a valid way to overload this constructor? For brevity, only the signature is given.

Pineapple(String color)
  1. Pineapple() Java

  2. Pineapple(String color, int age)

    Java

  3. Pineapple(int age, String species)

    Java

  4. FancyPineapple(String color, int age)

New cards
10

What is the importance of the null value?

null allows a reference variable to hold every object’s address simultaneously.

null prevents the reference variable from referring to another object again.

null restricts the class to only making one object.

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

New cards
11

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

The memory address of an object

The memory address of the reference variable

An object

The name of an object

New cards
12

Consider this code snippet that uses a class called Rectangle.

int roomHeight = 40;
int roomWidth = roomHeight * 3;

Rectangle room = new Rectangle(roomHeight, roomWidth);

Java

Which of the following is a reference variable?

room

roomHeight

roomWidth

Rectangle

New cards
13

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

Being a client of a class means that there a single method that we can use.

Being a client of a class means that we are the author of the class implementation.

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

Being a client of a class means that the class has documentation.

New cards
14

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 how the class you are a client of was implemented.

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

You need to know what other programs are using the class as a client.

You need to know the programmer who wrote the class.

New cards
15

What is the purpose of overloading a class’ constructor?

It allows the user to create more than one object from the class.

It allows the user to make different types of objects from a single class type.

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

It allows the user to call different constructors for the same object to initialize different instance variables.

New cards
16

What is an instance method?

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

An instance method is a piece of code that does not depend on any specific instances (objects), just on the general class.

An instance method adds functionality to a class by creating private fields.

An instance method adds functionality to the class by printing out a result.

New cards
17

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;
}
}
  1. (The Correct Answer)

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

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;

New cards
19

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;

New cards
20

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;
}
}

Java

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.

New cards
21

What are parameters?

The value that a method returns.

The values that a method prints to the screen.

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

The type that is given to a variable.

New cards
22

Consider the Circle class below.

public class Circle
{

private double radius;

public Circle(double circleRadius)
{
radius = circleRadius;
}

public void setRadius(double 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

New cards
23

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);

New cards
24

The value that a method outputs is called

a print statement.

an argument.

a parameter.

a return value.

New cards
25

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();

New cards
26

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();

New cards
27

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";

}

New cards
28

Strings are immutable. This means that

Once a String variable has been assigned a value, the value of the variable must always have the same value.

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

The value of a String variable can be modified, but the variable cannot be reassigned.

The value of a String variable cannot be modified and the variable cannot be reassigned.

New cards
29

What would be printed by this code snippet?

String language = "Java";
String opinion = " is fun!";

System.out.println(language + opinion);

Java

Javais fun!

Java is fun!

This code would not compile. You can’t add Strings.

New cards
30

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
  1. System.out.println('"Testing shows the presence, not the absence of bugs"');

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

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

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

New cards
31

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

New cards
32

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

A constructor

length

toString

print

New cards
33

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'));
  1. This will throw an error because ‘g’ is not in the string.

  2. 0
    -1
    5

  3. 1
    0
    6

  4. 2
    -1
    5

New cards
34

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.

New cards
35

Consider the following code snippet. What would the output be?

String school = "Rydell High School";

System.out.println(school.substring(8));
System.out.println(school);

igh School

Rydell High School

gh School

Rydell High School

igh School

igh School

gh School

gh School

i

i

New cards
36

The purpose of a wrapper class is to

Allow methods to be called on a primitive value

“Wrap” an object to convert it to a primitive value

Allow primitive to be passed to methods

“Wrap” a primitive value to convert it to an object

New cards
37

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.

A Double is unboxed when it is converted to an Integer

A Double is unboxed when it is passed to a method.

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

New cards
38

Java automatically converts between objects and primitives in the process of autoboxing and unboxing.

What happens when an int is autoboxed?

A int is autoboxed when it is converted to a primitive value.

A int is autoboxed when it is converted to a Double

A int is autoboxed when it is passed to a method.

A int is autoboxed when it is converted to a Integer

New cards
39

Which of these is not true about 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, changes made to the object will show in the calling method, but changes to the primitive will not.

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

New cards
40

Which of these is an example of calling a static method?

point.setX(x)

Math.abs(x)

student.getName()

square(x)

New cards
41

Which of the following describes the difference between static methods and instance methods?

Static methods cannot return a value while instance methods may or may not return a value

Static methods can be called without using an object while instance methods need to be called on an object

A class can only have one static method.

Static methods must be called using an object while instance methods can be called without using an object

New cards
42

What range of numbers would be generated by using

int num  = (int) (Math.random() * 501);

1 - 500

1 - 501

0 - 501

0 - 500

New cards
43

What would this program print?

double sideLength = Math.sqrt(64);
double height = Math.pow(3, 2);
double difference = Math.abs(sideLength - height);

System.out.println(difference);

1.0

-1.0

0.0

8.0

New cards

Explore top notes

note Note
studied byStudied by 4 people
... ago
5.0(1)
note Note
studied byStudied by 21 people
... ago
5.0(1)
note Note
studied byStudied by 21 people
... ago
5.0(1)
note Note
studied byStudied by 1 person
... ago
5.0(1)
note Note
studied byStudied by 6 people
... ago
5.0(1)
note Note
studied byStudied by 31 people
... ago
5.0(1)
note Note
studied byStudied by 6 people
... ago
5.0(1)
note Note
studied byStudied by 674 people
... ago
5.0(4)

Explore top flashcards

flashcards Flashcard (63)
studied byStudied by 22 people
... ago
5.0(1)
flashcards Flashcard (85)
studied byStudied by 14 people
... ago
5.0(1)
flashcards Flashcard (183)
studied byStudied by 7 people
... ago
5.0(1)
flashcards Flashcard (20)
studied byStudied by 1 person
... ago
5.0(1)
flashcards Flashcard (34)
studied byStudied by 21 people
... ago
5.0(1)
flashcards Flashcard (58)
studied byStudied by 17 people
... ago
5.0(1)
flashcards Flashcard (58)
studied byStudied by 12 people
... ago
5.0(2)
flashcards Flashcard (76)
studied byStudied by 452 people
... ago
5.0(7)
robot