chapter 4 practice test

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

The volume of a cylinder is equal to the height times the area of the circular base. The area of the circular base is equal to π (pi) times the square of the radius. The code segment below is intended to compute and print the volume of a cylinder with radius r and height h. Assume that the double variables r, h, and pi have been properly declared and initialized. / missing code / System.out.print(volume); Which of the following can be used to replace / missing code / so that the code segment works as intended? I. double baseArea = pi r r; double volume = baseArea * h; II. double volume = pi r r; volume = volume * h; III. double volume = pi r r * h;

I, II, and III

What is a mutator method? a. A method that modifies a value. b. A method that provides read-only access to a value. c. A method that has the same name, but different parameters, as another method. d. A method that is called when an object is first created. e. A method that does not return a value.

a. A method that modifies a value.

A class' constructor usually defines a. how an object is initialized b. how an object is interfaced c. the number of instance data in the class d. the number of methods in the class e. if the instance data are accessible outside of the object directly

a. how an object is initialized

Consider the following code: x = 9 sum = 0 while (x < 19) { x = x + 1 num = int (input("Enter a number: ")); sum = sum + num; print (str (x) + ": " + str (num) + " Total: " + str(sum)) Which of the following is the loop control variable? a. x b. int c. sum d. num

a. x

The Coin class, as defined in Chapter 4, consists of a constructor, and methods flip, isHeads and toString. The method isHeads returns true if the last flip was a Heads, and false if the last flip was a Tails. The toString method returns a String equal to "Heads" or "Tails" depending on the result of the last flip. Using this information, answer questions 16 - 17A 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"); c.flip( ); ___________________________________________________________________________ d. 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");

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

public class Swapper { private int x; private String y; public int z; public Swapper(int a, String b, int c) { x = a; y = b; z = c; } public String swap( ) { int temp = x; x = z; z = temp; return y; } public String toString( ) { if (x < z) { return y; } else { return "" + x + z; } } If we have Swapper r = new Swapper (5, "no", 10); then r.swap( ); returns which of the following? a. nothing b. "no" c. "no510" d. "510" e. "15"

b. "no"

In the Rational class, defined in chapter 4, the methods reduce and gcd are declared to be private. Why? a. Because they will never be used b. Because they will only be called from methods inside of Rational c. Because they will only be called from the constructor of Rational d. Because they do not use any of Rational's instance data e. Because it is a typo and they should be declared as public

b. Because they will only be called from methods inside of Rational

Use the following information to answer questions 19 - 20. The Die class from chapter 4 has two constructors defined as follows. Assume MIN_FACES is an int equal to 4. 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 _________ a. The Die d having numFaces = 6 and faceValue = 1 b. The Die d having numFaces = 10 and faceValue = 1 c. The Die d having numFaces = 10 and faceValue = 10 d. The Die d having numFaces = 6 and faceValue = 10 e. A syntax error

b. The Die d having numFaces = 10 and faceValue = 1

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

b. foo(0 / 1, 2 * 3);

Setting a starting value for a variable is called __________. a. testing b. initializing c. incrementing d. repeating

b. initializing

If a method does not have a return statement, then a. it will produce a syntax error when compiled b. it must be a void method c. it can not be called from outside the class that defined the method d. it must be defined to be a public method e. it must be an int, double, or String method

b. it must be a void method

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? a. m1 b. m2 c. m3 d. m5 e. main

b. m2

The relationship between a class and an object is best described as... a. classes are instances of objects b. objects are instances of classes c. objects and classes are the same thing d. classes are programs while objects are variables e. objects are the instance data of classes

b. objects are instances of classes

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

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

For questions 21 - 23, use the following class definition: public class Swapper { private int x; private String y; public int z; public Swapper(int a, String b, int c) { x = a; y = b; z = c; } public String swap( ) { int temp = x; x = z; z = temp; return y; } public String toString( ) { if (x < z) { return y; } else { return "" + x + z; } } If the instruction Swapper s = new Swapper(0, "hello", 0); is executed followed by s.toString( ); what value is returned from s.toString( )? a. "hello" b. "hello00" c. "00" d. "0" e. 0

c. "00"

For questions 13-15, use the following class definition import java.text.DecimalFormat; 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

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

Consider the following code: c = 10 while (c > 5) { print ("Next number: " + str(c)) c = c - 1 } It should print out: Next number: 10 Next number: 9 Next number: 8 Next number: 7 Next number: 6 What is wrong? a. c = 10 should be c = 1 b. It should be c = c + 1 c. The c = c - 1 is not inside the loop, this will cause the loop not to stop d. Nothing, the code runs as it should

c. The c = c - 1 is not inside the loop, this will cause the loop not to stop

A variable whose scope is restricted to the method where it was declared is known as a(n) a. parameter b. global variable c. local variable d. public instance data e. private instance data

c. local variable

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

c. public class Car

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; a. the number of Heads flipped out of 1000 flips b. the number of Heads flipped in a row out of 1000 flips c. the percentage of heads flipped out of 1000 flips d. the percentage of times neither Heads nor Tails were flipped out of 1000 flips e. nothing at all

c. the percentage of heads flipped out of 1000 flips

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

d) s1.getClassRank( );

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

d. Make the class final

The behavior of an object is defined by the object's ___________. a. instance data b. constructor c. visibility modifiers d. methods e. all of the above

d. methods

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

d. s1.getClassRank( );

Use the following information to answer questions 19 - 20. The Die class from chapter 4 has two constructors defined as follows. Assume MIN_FACES is an int equal to 4. 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, 0); results in _________ a. The Die d having numFaces = 6 and faceValue = 1 b. The Die d having numFaces = 10 and faceValue = 1 c. The Die d having numFaces = 10 and faceValue = 10 d. The Die d having numFaces = 6 and faceValue = 10 e. A syntax error

e. A syntax error

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

Instance data for a Java class ... a. are limited to primitive types (e.g., int, double, char) b. are limited to Strings c. are limited to objects (e.g., Strings, classes defined by other programmers) d. may be primitive types or objects, but objects must be defined to be private e. may be primitive types or objects

e. may be primitive types or objects

Having multiple class methods of the same name where each method has a different number of or type of parameters is known as a. encapsulation b. information hiding c. tokenizing d. importing e. method overloading

e. method overloading

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

e. new

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

false

True or false? Every class definition must include a constructor.

false

True or false? Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header.

false

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

True or false? Java methods can return only primitive types (int, double, boolean, etc)

false

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

true

True or false? 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

True or false? An object may be made up of other objects.

true

True or false? Defining formal parameters requires including each parameters type.

true

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

true

True or false? The println method on System.out is overloaded.

true

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

true


Ensembles d'études connexes

Accounting 206 McGraw-Hill Video Lecture & Assessment LO 2-5, 6, 7

View Set

Caring for Diverse & Vulnerable Populations Exam 3 Review

View Set

Russia before and after the 1917 relvolutions

View Set

Theatre Appreciation Final Exam Study Guide

View Set

Job 34 - Flashcard MC Questions - Ted Hildebrandt

View Set

Digital Marketing Associate exam

View Set