Unit 2 Comp Sci

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

Consider the following method, which takes as input a temperature in degrees Fahrenheit and returns the corresponding temperature in degrees Celsius. public double fahrenheitToCelsius(double f) { double c = (f - 32) * 5 / 9; return c; } Assume that each of the following code segments appears in a method in the same class as fahrenheitToCelsius. Which of the following code segments prints the temperature in degrees Celsius that corresponds to 32 degrees Fahrenheit?

double f = 32.0; double c = fahrenheitToCelsius(f); System.out.println(c); The non-void method fahrenheitToCelsius takes a double value as a parameter and returns a double value. The returned value is stored in the double variable c, which is then printed.

Consider the following class definition. public class AnimalPrinter { public void printDog() { System.out.println("dog"); } public void printCat() { System.out.println("cat"); } /* constructors not shown */ } The method myMethod appears in a class other than AnimalPrinter. The method is intended to produce the following output. dog cat Assume that an AnimalPrinter object myPrinter has been properly declared and initialized inside myMethod. Which of the following code segments, if located in myMethod, will produce the intended output?

myPrinter.printDog(); myPrinter.printCat(); To work correctly, the dot operator must be used to invoke a non-static method of a class on an object of that class. This code segment correctly invokes the myPrinter object's methods printDog() and printCat() and will produce the desired output.

Consider the following methods, which appear in the same class. public void methodA(int arg) { int num = arg * 10; methodB(num); } public void methodB(int arg) { System.out.print(arg + 10); } Consider the call methodA(4), which appears in a method in the same class. What, if anything, is printed as a result of the call methodA(4) ?

50 The value 4 is passed to methodA and stored in methodA's parameter arg. This value is multiplied by 10. The result 40 is then passed to methodB and stored in methodB's parameter arg. The value 10 is added to arg, and the result 50 is printed.

A student has created a Book class. The class contains variables to represent the following. An int variable called pages to represent the number of pages A boolean variable called isHardcover to indicate whether or not the book is hardcover The object story will be declared as type Book. Which of the following descriptions is accurate?

An attribute of the story object is isHardcover. The story object is an instance of the Book class. The story object has two attributes: pages and isHardcover.

Consider the following method. public void adjust(double max, double min, double total, double n) { total = total - max - min; n = n - 2.0; System.out.println(total / n); } Consider the call adjust(25.0, 5.0, 60.0, 5.0), which appears in a method in the same class. What is printed as a result of the method call?

The value 20.0 would have been printed if the value of total was never updated after being passed to the method.

Consider the following class declaration. public class Thing { private String color; public Thing() { color = "Blue"; } public Thing(String setColor) { color = setColor; } } Which of the following code segments, when appearing in a class other than Thing, would create a reference of type Thing with a value of null ?

Thing someThing; This code segment declares a variable of type Thing named someThing, but never initializes it. Therefore, its value will be equal to null.

Consider the following method. public double secret(int x, double y) { return x / 2.0; } Which of the following lines of code, if located in a method in the same class as secret, will compile without error?

double result = secret(4, 4.0); In order for the code to compile without error, result must be the same data type as the return type of secret. In addition, the values passed to secret must be compatible with the types of the parameters x and y. The statement double result = secret(4, 4.0); compiles without error.

A school administrator has created a Student class. The class contains variables to represent the following. An int variable called studentID to represent the student's ID number A String variable called studentName to represent the student's name The school administrator has also created a Parent class. The class contains variables to represent the following. A String variable called parentName to represent the parent's name A String variable called email to represent the parent's e-mail address The object penelope will be declared as type Student. The object mrsPatel will be declared as type Parent. Which of the following descriptions is accurate?

An attribute of the mrsPatel object is email. The mrsPatel object is an instance of the Parent class. The mrsPatel object has two attributes: parentName and email.

Consider the following method, which returns the lesser of its two parameters. public int min(int first, int second) { /* implementation not shown */ } Assume that each of the following expressions appears in a method in the same class as the method min. Assume also that the int variables p, q, and r have been properly declared and initialized. Which of the following expressions evaluates to the minimum value among p, q, and r? 1. min(min(p, q), r) 2. min(p, min(q, r)) 3. min(min(p, q), p)

I and II only One way to determine the minimum of three values is to compare two values and then compare the lesser of the two with the third value. The order of comparisons does not matter, so expressions I and II will both return the correct result. Expression III will not always work correctly. Because it involves comparisons between only two of the three values (p and q), expression III will return a wrong result when r is the minimum value.

Consider the following class declaration. public class VetRecord { private String name; private int age; private int weight; private boolean needsVaccine; public VetRecord(String nameP, int ageP, int weightP, boolean needsVaccineP) { name = nameP; age = ageP; weight = weightP; needsVaccine = needsVaccineP; } public VetRecord(String nameP, int ageP, int weightP) { name = nameP; age = ageP; weight = weightP; needsVaccine = true; } } A new constructor is to be added to the VetRecord class. Which of the following is NOT a possible header for the new constructor?

VetRecord(String nameP, int weightP, int ageP) The VetRecord class already contains a constructor with a matching signature: String, int, int. Adding a second constructor with the same signature will cause a compile-time error.

Consider the following Vbox class. public class Vbox { private int width; private int height; private int depth; public Vbox(int w, int h, int d) { width = w; height = h; depth = d; } public Vbox(int len) { width = len; height = len; depth = len; } } Which of the following declarations, appearing in a method in a class other than Vbox, will correctly instantiate a Vbox object? 1. Vbox b1 = new Vbox(4); 2. Vbox b2 = new Vbox(2, 8, 4); 3. Vbox b3 = new Vbox(4.0, 4.0, 4.0);

I and II only Option I creates a Vbox object using the constructor with the signature Vbox(int len), which has a single int parameter. Option II is also correct. It creates a Vbox object using the constructor with the signature Vbox(int w, int h, int d), which has three int parameters. Option III is not correct. There is no constructor with a signature of three double parameters.

Consider the following class. public class MagicNumber { private int num; public MagicNumber() { num = 10; } public void displayNumber() { System.out.println(num); } public void add_2() { num = num + 2; } } When located in a method in a class other than MagicNumber, which of the following code segments will compile without error? MagicNumber.add_2();MagicNumber.displayNumber(); MagicNumber n1 = new MagicNumber();n1.add_2();n1.displayNumber(); n2.add_2();n2.displayNumber();

II only Code segment I will not compile. To call a nonstatic method outside the MagicNumber class, a MagicNumber object must be created and the nonstatic method must be called using the dot operator on the object name. Code segment II will compile successfully, as it first creates an object of the class MagicNumber and then uses the dot operator to call nonstatic methods that manipulate an instance variable of that object. Code segment III will not compile because the variable n2 has not been properly declared and initialized.

An int variable called grade to represent the student's grade level A String variable called name to represent the student's name A double variable called average to represent the student's grade point average A method called updateAverage that updates the student's average. The object greg will be declared as type Student. Which of the following descriptions is accurate?

greg is an instance of the Student class. The object greg has been declared as type Student, which creates it as an instance of the Student class.

Consider the following class. public class Purchase { private double purchase; private double tax; public Purchase(double purchaseAmt, double taxAmt) { purchase = purchaseAmt; tax = taxAmt; } public void totalAmount() { System.out.print(purchase + tax); } } Assume that a Purchase object p has been properly declared and initialized. Which of the following code segments will successfully print the total purchase amount associated with p?

p.totalAmount(); The dot operator is needed to invoke a non-static method of a class on an object of that class. This code segment will correctly print the purchase amount associated with p.

Consider the following method. public void printSomething (int num, boolean val) { num--; System.out.print(val); System.out.print(num); } Consider the following code segment, which appears in a method in the same class as printSomething. printSomething(1, true); printSomething(2, true); What is printed as a result of executing the code segment?

true0true1 The first call to printSomething passes 1 to the parameter num and true to the parameter val. The value of num is decreased by 1, then val and num are each printed, resulting in the output true0. The second call to printSomething passes 2 to the parameter num and true to the parameter val. The value of num is decreased by 1, then val and num are each printed, resulting in the output true1.


Ensembles d'études connexes

EMR Chapter 11 Behavioral Emergencies

View Set

Biology 1 (Biology 1610) CH.7 - "Membrane Structures and Functions."

View Set

Chapter 5 Rights/Liberties and Bill of Rights

View Set