comp sci test 2 review

Ace your homework & exams now with Quizwiz!

I. game.numPlayers++; II. game.addPlayer(); III. game.gameOver(); IV. game.endGame(); which are valid?

II and IV only

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.

Every class definition has each of the following EXCEPT

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 }

Every Pineapple object will have the same attributes

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

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?

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

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 35 minutes

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

Which instance variables are initialized

The value that a method outputs is called

a return value.

Which of the following lines of code, if located in a method in the same class as myMethod, will compile without error?

double result = myMethod(0, false);

Which of the following statements, if located in a method in the same class, will initialize the variable x to 11?

int x = function1(4, 5) + function2(1, 3);

Which of the following correctly uses this method to print out the start time of a Timer object called laundry?

laundry.startTime();

double d1 = 10.0; Double d2 = 20.0; Double d3 = new Double(30.0); double d4 = new Double(40.0); System.out.println(d1 + d2 + d3.doubleValue() + d4); What, if anything, is printed when the code segment is executed?

100.0

The purpose of a wrapper class is to

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

double rn = Math.random(); int num = /* missing code */; Which of the following could be used to replace /* missing code */ so that the code segment works as intended?

(int) (rn * (max - min + 1)) + min

What range of numbers would be generated by usingint num = (int) (Math.random() * 501);

0 - 500

public double puzzle(int x) { Double y = x / 2.0; y /= 2; return y.doubleValue(); } Assume that the method call puzzle(3) appears in a method in the same class as puzzle. What value is returned as a result of the method call?

0.75

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

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

Integer val = 10; int result1 = timesTwo(val); Integer result2 = result1; System.out.print(result2); What, if anything, is printed as a result of executing the code segment?

20

What happens when a int is autoboxed?

A int is autoboxed when it is converted to a Integer

BCEF

All of these statements will compile

What is an object in Java?

An object is something that contains both state and behavior.

String oldStr = "ABCDEF"; String newStr = oldStr.substring(1, 3) + oldStr.substring(4); System.out.println(newStr); What is printed as a result of executing the code segment?

BCEF

What would be printed by this code snippet? String language = "Java"; String opinion = " is fun!"; System.out.println(language + opinion);

Java is fun!

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

Math.abs(x)

Which of the following statements correctly stores the return value of goingUp when it is called on the Elevator object called hotel?

boolean up = hotel.goingUp();

What is printed when the code segment is executed?

comp omp mp p

Consider the following code segment. int[] indexes = {2, 1, 1}; System.out.println(recScramble("epic", indexes, 0)); What is printed as a result of executing the code segment?

ipce

Using the Student object called karel, which of the following is the correct way to set karel's honor status to true?

karel.setHonorStatus(true);

Which of the following statements, if located in a method in the Student class, will determine the average of all of the student's grades except for the lowest grade and store the result in the double variable newAverage ?

newAverage = (sumOfGrades() - lowestGrade()) / (numberOfGrades() - 1);

Which of the following methods is implemented correctly with respect to the method's return type?

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

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

sharkAge

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

suit = cardSuit; value = cardValue;

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

Consider the following code segment. int one = 1; int two = 2; String zee = "Z"; System.out.println(one + two + zee); What is printed as a result of executing the code segment?

3Z

Assume that the method call slope(1, 2, 5, 10) appears in a method in the same class. What is printed as a result of the method call?

8/4

ExamScore es = new ExamScore("12345", 80.0); es.bonus(5); System.out.println(es.getScore()); What is printed as a result of executing the code segment?

84.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

What is a constructor in Java?

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

What would be printed by the following code snippet? String lastName = "Vu"; String otherLastName = "Lopez"; int comparison = lastName.compareTo(otherLastName); System.out.println(comparison);

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

Which of these is not true about primitives and objects?

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

A String variable called color to represent the color of the car An int variable called year to represent the year the car was made A String variable called make to represent the manufacturer of the car A String variable called model to represent the model of the car The object vehicle will be declared as type Car. Which of the following descriptions is accurate?

An attribute of the vehicle object is color.

What is an instance method?

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

Each of the following statements appears in a method in the same class as doSomething. Which of the following statements are valid uses of the method doSomething ? doSomething(); String output = doSomething(); System.out.println(doSomething());

I only

Where would you find the 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

int a = 1988; int b = 1990; String claim = " that the world's athletes " + "competed in Olympic Games in "; String s = "It is " + true + claim + a + " but " + false + claim + b + "."; System.out.println(s); What, if anything, is printed when the code segment is executed?

It is true that the world's athletes competed in Olympic Games in 1988 but false that the world's athletes competed in Olympic Games in 1990.

Which of the following can replace /* missing code */ so that the method works as intended?

Math.sqrt(Math.pow(x + y, 2) / Math.abs(a - b))

Consider the following code segment, which appears in a method in the same class as printSum and printProduct. int num1 = 5; double num2 = 10.0; printSum(num1, num2); printProduct(num1, num2); What, if anything, is printed as a result of executing the code segment?

Nothing is printed because the code does not compile.

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 code segments, appearing in a class other than Point2D, will correctly create an instance of a Point2D object?

Point2D p = new Point2D(3.0, 4.0);

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

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

Which of the following code segments, if located in a method in a class other than Thing, will cause the message "Hello my friend" to be printed?

Thing a = new Thing();a.greet();

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.

A String variable called artist to represent the artist name A String variable called title to represent the song title A String variable called album to represent the album title The object happyBirthday will be declared as type Song. Which of the following statements is true?

happyBirthday is an instance of the Song class.

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

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 = shape.getArea();

Which of the following statements assigns a random integer between 1 and 10, inclusive, to rn ?

int rn = (int) (Math.random() * 10) + 1;

What is the importance of the null value?

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

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

toString


Related study sets

Exam 2- (10/21) Chapter 6-10 Sapling Questions

View Set

Astronomy Exam 3 Crash Questions!!!!

View Set

Unit 1 Basic Economic Concepts Review

View Set