Using Objects Review

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Consider the following method. public int timesTwo (int n) { return n * 2; } The following code segment appears in a method in the same class as the timesTwo method. 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

Consider the following code segment. double x = 4.5; int y = (int) x * 2; System.out.print(y); What is printed as a result of executing the code segment?

8

What is an instance method?

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

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.

Consider the code segment below. 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.

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

Consider the following code segment. int w = 1; int x = w / 2; double y = 3; int z = (int) (x + y); Which of the following best describes the results of compiling the code segment?

The code segment compiles without error.

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

The memory address of an object

Consider the following code snippet. public static void main(String args[]) { final int z; z = 20; z = 30; System.out.println(z); } What value of z is actually printed?

This code gives an error.

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.

The code segment below is intended to calculate the circumference c of a circle with the diameter d of 1.5. The circumference of a circle is equal to its diameter times pi. /* missing declarations */ c = pi * d; Which of the following variable declarations are most appropriate to replace /* missing declarations */ in this code segment?

final double pi = 3.14159; double d = 1.5; double c;

Which of the following statements stores the value 3 in x?

int x = 8 % 5;

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

Consider the following code segment, which is intended to assign to num a random integer value between min and max, inclusive. Assume that min and max are integer variables and that the value of max is greater than the value of min. 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 using int num = (int) (Math.random() * 501);

0-500

Consider the following code segment. String str = "0"; str += str + 0 + 8; System.out.println(str); What is printed as a result of executing the code segment?

0008

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

Consider the following code segment. int x = 5; x += 6 * 2; x -= 3 / 2; What value is stored in x after the code segment executes?

16

Consider the following class definition. public class ExamScore { private String studentId; private double score; public ExamScore(String sid, double s) { studentId = sid; score = s; } public double getScore() { return score; } public void bonus(int b) { score += score * b/100.0; } } Assume that the following code segment appears in a class other than ExamScore. 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

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.

Consider the following class declaration. public class GameClass { private int numPlayers; private boolean gameOver; public GameClass() { numPlayers = 1; gameOver = false; } public void addPlayer() { numPlayers++; } public void endGame() { gameOver = true; } } Assume that the GameClass object game has been properly declared and initialized in a method in a class other than GameClass. Which of the following statements are valid? game.numPlayers++; game.addPlayer(); game.gameOver(); game.endGame();

II and IV only

Consider the following methods, which appear in the same class. public void printSum(int x, double y) { System.out.println(x + y); } public void printProduct(double x, int y) { System.out.println(x * y); } 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.

The Student class has been defined to store and manipulate grades for an individual student. The following methods have been defined for the class. /* Returns the sum of all of the student's grades */ public double sumOfGrades() { /* implementation not shown */ } /* Returns the total number of grades the student has received */ public int numberOfGrades() { /* implementation not shown */ } /* Returns the lowest grade the student has received */ public double lowestGrade() { /* implementation not shown */ } 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);


Ensembles d'études connexes

Economics - Mankiw Ch18 Production Factors Market

View Set

Python повторение материалов 1-6

View Set

8th Grade Science - Earth and Space Science

View Set

AH1 MOD 6 CARDIO Shock Iggy NCLEX, Iggy Chp 35 - Care of Patients with Cardiac Problems, CH 33, 35 Cardiac, Nursing Management: Coronary Artery Disease and Acute Coronary Syndrome, Nursing Assessment: Cardiovascular System

View Set

Chapter 10: Interest Groups - Vocabulary + Quiz

View Set