Exam 4: Writing Classes

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

If a method does not have a return statement, then

Free
(Multiple Choice)
5.0/5
(36)
Correct Answer:
Verified

B

Consider a Rational class designed to represent rational numbers as a pair of int's, along with methods reduce (to reduce the rational to simplest form), gcd (to find the greatest common divisor of two int's), as well as methods for addition, subtraction, multiplication, and division. Why should the reduce and gcd methods be declared to be private.

Free
(Multiple Choice)
4.9/5
(37)
Correct Answer:
Verified

B

Write a toString method that returns the player's name, position and batting average formatted to have 1 digit to the left of the decimal and 3 digits to the right of the decimal. Assume DecimalFormat has been imported.

Free
(Essay)
4.9/5
(29)
Correct Answer:
Verified

public String toString( )
{
DecimalFormat df = new DecimalFormat("0.000");
return name + "\t" + position + "\t" + df.format(battingAverage);
}

A constructor may contain a return statement so long as no value (or expression) is returned.

(True/False)
4.8/5
(36)

Accessors and mutators provide mechanisms for controlled access to a well-encapsulated class.

(True/False)
4.9/5
(33)

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

(Multiple Choice)
4.8/5
(40)

For the questions below, 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 } } -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?

(Multiple Choice)
4.7/5
(39)

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

(True/False)
4.9/5
(43)

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

(Multiple Choice)
4.7/5
(36)

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?

(Multiple Choice)
4.8/5
(28)

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

(Multiple Choice)
4.7/5
(36)

In order to preserve encapsulation of an object, we would do all of the following except for which one?

(Multiple Choice)
4.7/5
(44)

For the questions below, 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 patterns should be used in place of "xxxx" when instantiating df so that the gpa to be output in typical form (like 3.810)?

(Multiple Choice)
4.8/5
(31)

When reasoning about a car, we use such information as its make and model, year, color, cost (or worth), number of miles, gas mileage, insurance cost, and so forth. Consider a program that will attempt to determine how practical a car might be on a road trip. Which of these types of information might you use in your program as instance data? Provide a justification for your selection.

(Essay)
4.9/5
(34)

While multiple objects of the same class can exist, in a given program there can be only one version of each class.

(True/False)
4.8/5
(39)

Explain why it would be a poor decision to make instance data public instead of private. Give an example of why this could result in a problem.

(Essay)
4.8/5
(31)

A GUI container is an object that defines a screen element.

(True/False)
4.8/5
(31)

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

(True/False)
4.8/5
(35)

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

(Multiple Choice)
4.9/5
(38)

Defining formal parameters requires including each parameters type.

(True/False)
4.9/5
(36)
Showing 1 - 20 of 56
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)