Chapter 4 Test

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

If the instruction Swapper s = new Swapper(0, "hello", 0); is executed followed by s.toString( ); what value is returned from s.toString( )?

"00"

If we have Swapper r = new Swapper (5, "no", 10); then r.swap( ); returns which of the following?

"no"

The instruction Die d = new Die(10, 0); results in

A syntax error

In the Rational class, defined in chapter 4, the methods reduce and gcd are declared to be private. Why?

Because they will only be called from methods inside of Rational

In order to preserve encapsulation of an object, we would do all of the following except for which one? a) Make the instance data private b) Define the methods in the class to access and manipulate the instance data c) Make the methods of the class public d) Make the class final e) All of the above preserve encapsulation

Make the class final

public class Student { private String name; private String major; private double gpa; private int hours; public Student(String newName, String newMajor, double newGPA, int newHours) { name = newName; major = newMajor; gpa = newGPA; hours = newHours; } public String toString( ) { DecimalFormat df = new DecimalFormat("xxxx"); // xxxx needs to be replaced return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours } } *Which of the following could be used to instantiate a new Student s1? a) Student s1 = new Student( ); b) s1 = new Student( ); c) Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33); d) new Student s1 = ("Jane Doe", "Computer Science", 3.333, 33); e) new Student(s1);*

Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33);

public Die( ) public Die(int faces) { { numFaces = 6; if(faces < MIN_FACES) numFaces = 6; faceValue = 1; else numFaces = faces; } faceValue = 1; } *The instruction Die d = new Die(10); results in*

The Die d having numFaces = 10 and faceValue = 1

Which of the following criticisms is valid about the Swapper class?

The instance data z is visible outside of Swapper

A set of code has already instantiated c to be a Coin and has input a String guess, from the user asking whether the user guesses that a coin flip will result in "Heads" or "Tails". Which of the following sets of code will perform the coin flip and see if the user's guess was right or wrong? a) c.flip( ); if(c.isHeads( ).equals(guess)) System.out.println("User is correct"); b) if(c.flip( ).equals(guess)) System.out.println("User is correct"); c) if(c.isHeads( ).equals(guess)) System.out.println("User is correct"); Lewis/Loftus/Cocking: Chapter 4 Test Bank TB 39 d) c.flip( ); if(c.toString( ).equals(guess)) System.out.println("User is correct"); e) c.flip( ).toString( ); if(c.equals(guess)) System.out.println("User is correct");

c.flip( ); if(c.toString( ).equals(guess)) System.out.println("User is correct");

Consider a method defined with the header: public void doublefoo(double x). Which of the following method calls is legal? a) doublefoo(0); b) doublefoo(0.555); c) doublefoo(0.1 + 0.2); d) doublefoo(0.1, 0.2); e) all of the above are legal except for d

e) all of the above are legal except for d

An example of passing a message to a String where the message has a String parameter occurs in which of the following messages?

equals

A constructor must always return an int.

false

A method defined without a return statement will cause a compile error.

false

All Java classes must contain a main method which is the first method executed when the Java class is called upon.

false

Every class definition must include a constructor.

false

Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header.

false

Java methods can return more than one item if they are modified with the reserved word continue, as in public continue int foo( ) { ... }

false

Java methods can return only primitive types (int, double, boolean, etc)

false

Method decomposition is the process of creating overloaded versions of a method that does the same thing but operate on different data types.

false

The methods in a class should always be made public so that those outside the class can use them

false

The return statement must be followed a single variable that contains the value to be returned

false

A class may contain methods but not variable declarations.

false, can do both

A class's instance data are the variables declared in the main method.

false, declared in class

Consider a method defined with the header: public void foo(int a, int b). Which of the following method calls is legal? a) foo(0, 0.1); b) foo(0 / 1, 2 * 3); c) foo(0); d) foo( ); e) foo(1 + 2, 3 * 0.1);

foo(0 / 1, 2 * 3);

A class' constructor usually defines

how an object is initialized

If a method does not have a return statement, then

it must be a void method

A variable whose scope is restricted to the method where it was declared is known as a(n)

local variable

Consider a sequence of method invocations as follows: main calls m1, m1 calls m2, m2 calls m3 and then m2 calls m4, m3 calls m5. If m4 has just terminated, what method will resume execution?

m2

Having multiple class methods of the same name where each method has a different number of or type of parameters is known as

method overloading

The behavior of an object is defined by the object's

methods

Which of the following reserved words in Java is used to create an instance of a class? a) class b) public c) public or private, either could be used d) import e) new

new

The relationship between a class and an object is best described as

objects are instances of classes

Instance data for a Java class may be

primitive types or object

To define a class that will represent a car, which of the following definitions is most appropriate? a) private class car b) public class car c) public class Car d) public class CAR e) private class Car

public class Car

Another method that might be desired is one that updates the Student's number of credit hours. This method will receive a number of credit hours and add these to the Student's current hours. Which of the following methods would accomplish this? a) public int updateHours( ) { return hours; } b) public void updateHours( ) { hours++; } c) public updateHours(int moreHours) { hours += moreHours; } d) public void updateHours(int moreHours) { hours += moreHours; } e) public int updateHours(int moreHours) { return hours + moreHours; }

public void updateHours(int moreHours) { hours += moreHours; }

Assume that another method has been defined that will compute and return the student's class rank (Freshman, Sophomore, etc). It is defined as: public String getClassRank( ) TB 38 Lewis/Loftus/Cocking: Chapter 4 Test Bank *Given that s1 is a student, which of the following would properly be used to get s1's class rank? a) s1 = getClassRank( ); b) s1.toString( ); c) s1.getHours( ); d) s1.getClassRank( ); e) getClassRank(s1);*

s1.getClassRank( );

What does the following code compute? int num = 0; for(int j = 0; j < 1000; j++) { c.flip( ); if(c.isHeads()) num++; } double value = (double) num / 1000;

the percentage of heads flipped out of 1000 flips

A constructor is a method that gets called automatically whenever an object is created, for example with the new operator.

true

A constructor must have the same name as its class.

true

A method defined in a class can access the class' instance data without needing to pass them as parameters or declare them as local variables.

true

An object may be made up of other objects.

true

Defining formal parameters requires including each parameters type.

true

If a method takes a double as a parameter, you could pass it an int as the actual parameter.

true

The body of a method may be empty.

true

The different versions of an overloaded method are differentiated by their signatures.

true

The following method header definition will result in a syntax error: public void aMethod( );

true

The interface of a class is based on those data instances and methods that are declared public.

true

The number and types of the actual parameters must match the number and types of the formal parameters.

true

The println method on System.out is overloaded.

true

While multiple objects of the same class can exist, there is only one version of the class.

true


Ensembles d'études connexes

Pregnancy, Labor, Childbirth, Postpartum - Uncomplicated (Level 1)

View Set

Biology Chapter 3.3: Membrane Transport

View Set