Deck 4: Writing Classes

Full screen (f)
exit full mode
Question
Because an Image cannot directly be added to a container, it must be displayed using an ImageView object.
Use Space or
up arrow
down arrow
to flip the card.
Question
Java methods can return more than one item if they are modified with the reserved word continue, as in public continue int foo() {...}
Question
Defining formal parameters requires including each parameter's type.
Question
Every class definition must include a constructor.
Question
The interface of a class is based on those data instances and methods that are declared public.
Question
Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header.
Question
An object should be encapsulated in order to guard its data and methods from inappropriate access.
Question
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 these preserve encapsulation
Question
Regarding the software failure described at the Denver International Airport in the text, it is critical to factor in errors and inefficiencies that alway occur in a complex system.
Question
While multiple objects of the same class can exist, in a given program there can only be one version of each class.
Question
To define a class that will represent a car, which of the following definition is most appropriate?

A) private class car
B) public class car
C) public class Car
D) public class CAR.
E) private class Car
Question
The behavior of an object is defined by the object's

A) instance data
B) constructor
C) visibility modifiers
D) methods
E) All of these
Question
All Java classes must contain a main method which is the first method executed when the Java class is called on.
Question
Accessors and mutators provide mechanisms for controlled access to a well-encapsulated class.
Question
A constructor may contain a return statement so long as no value (or expression) is returned.
Question
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
Question
A GUI control sets up an event but it is the programmer who writes the code for the event handler which executes when an event occurs.
Question
Java methods can only return primitive types.
Question
A method defined in a class can access the class's instance data without needing to pass them as parameters or declare them as local variables.
Question
The following method header definition will result in a syntax error:
public void aMethod();
Question
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
Question
The software failure at the Denver International Airport's baggage handling system is a good example of

A) how a large system can be obsolete by the time it is developed
B) how designers sometimes have too much faith in the technology they are using
C) how failures are often a result of multiple variables caused by a system as a whole in actual operation
D) how the use of a centralized computer is unrealistic in today's distributed processing
E) All of these
Question
Refer to Class Definition Ch 04-1: 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(sq);
Question
In a UML diagram for a class

A) classes are represented as rectangles
B) there may be a section containing the name of the class
C) there may be a section containing the attributes of the class
D) there may be a section containing the methods of the class
E) All of these
Question
Consider a Rational class designed to represent rational numbers as a pair of ints, along with methods reduce (to reduce the rational to simplest form), gcd (to find the greatest common divisor of two ints), as well as methods for addition, subtraction, multiplication, and division. Why should the reduce and gcd methods be declared to be private?

A) because they will never be used
B) because they will only be called from methods inside Rational
C) because they will only be called from the constructor of Rational
D) because they do not use any of the Rational instance data
E) This is incorrect; they should be declared as public
Question
Refer to Class Definition Ch 04-1: Which of the following patterns should be used in place of "xxxx" when instantiating df so that the gpa to be output is in typical form (like 3.810)?

A) "#.###"
B) "#.0"
C) "0.#"
D) "0.000"
E) "0.0##"
Question
Instance data for a Java class

A) are limited to primitive types
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
Question
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 cannot 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, float, or String method
Question
A class's 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
Question
Visibility modifiers include

A) public, private
B) public, private, protected
C) public, private, protected, final
D) public, protected, final, static
E) public, private, protected, static
Question
Given the method defined here, which of the following method calls is legal? public void doublefoo(double x)

A) doublefoo(0);
B) doublefoo(0.555);
C) doublefoo(0.1 + 0.2);
D) foo(0.1, 0.2);
E) all except D are legal
Question
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
Question
Refer to Class Definition Ch 04-1: 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);
Question
Given the method defined here, which of the following method calls is legal? public void foo(int a, int b)

A) foo(0, 0.1);
B) foo(0/1, 2*3);
C) foo(0);
D) foo();
E) foo(1+2, 3*0.1);
Question
An example of passing a message to a String where the message has a String parameter would occur in which of the following messages?

A) length
B) substring
C) equals
D) toUpperCase
E) None of these; it is not possible to pass a String as a parameter to a String
Question
The expressions that are passed to a method in an invocation are called

A) actual parameters
B) formal parameters
C) formal arguments
D) formals
E) Any of these
Question
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
Question
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 (can use either)
D) import
E) new
Question
Which of the following is not a kind of object that is used to create a graphical user interface in JavaFX?

A) control
B) event
C) image
D) event handler
E) All of these are objects used to create a GUI in JavaFX
Question
What happens if you declare a class constructor to have a void return type?

A) You will most likely receive a syntax error.
B) The program will compile with a warning but you'll get a run-time error.
C) There is nothing wrong with declaring a constructor with a void return type.
D) The class's default constructor will be used instead of the one you're declaring.
E) None of these
Question
Refer to Class Definition Ch 04-3: Write the constructor for this class.
Question
Refer to Class Definition Ch 04-2: Write a toString method that returns the player's name, position, and batting average. formatted to have one digit to the left of the decimal and three digits to the right of the decimal. Assume DecimalFormat has been imported.
Question
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.
Question
Refer to Class Definition Ch 04-4: Write the statement to instantiate an Box object, blueBox, with a length of 6, width of 4, and height of 2.
Question
Refer to Class Definition Ch 04-4: Add a constructor to class Box for a cube which has only 1 parameter, side.
Question
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.
Question
Refer to Class Definition Ch 04-1: 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) getClassRank(sq);public int updateHours(int moreHours)
{ return hours + moreHours; }
Question
Refer to Class Definition Ch 04-3: What visibility modifiers would you use for the methods that move the elevator up one floor, move the elevator down one floor, that request the elevator, and that start the elevator moving?
Question
Refer to Class Definition Ch 04-4: Write a statement to instantiate a cube with a length of 3, width of 3, and height of 3.
Question
Refer to Class Definition Ch 04-2: Write the constructor which is passed the player's name and position.
Question
Refer to Class Definition Ch 04-4: Write a statement to output the volume of the blue box.
Question
Assume method0 calls method1 and method2, method1 calls method3 which calls method4 and method5. Explain the chain of method calls (what order methods are called and from which method).
Question
Write a portion of a class named Employee. An employee will have a name, ID number, position, and hourly wage. Define the instance data for this class.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/53
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 4: Writing Classes
1
Because an Image cannot directly be added to a container, it must be displayed using an ImageView object.
True
2
Java methods can return more than one item if they are modified with the reserved word continue, as in public continue int foo() {...}
False
3
Defining formal parameters requires including each parameter's type.
True
4
Every class definition must include a constructor.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
5
The interface of a class is based on those data instances and methods that are declared public.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
6
Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
7
An object should be encapsulated in order to guard its data and methods from inappropriate access.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
8
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 these preserve encapsulation
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
9
Regarding the software failure described at the Denver International Airport in the text, it is critical to factor in errors and inefficiencies that alway occur in a complex system.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
10
While multiple objects of the same class can exist, in a given program there can only be one version of each class.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
11
To define a class that will represent a car, which of the following definition is most appropriate?

A) private class car
B) public class car
C) public class Car
D) public class CAR.
E) private class Car
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
12
The behavior of an object is defined by the object's

A) instance data
B) constructor
C) visibility modifiers
D) methods
E) All of these
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
13
All Java classes must contain a main method which is the first method executed when the Java class is called on.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
14
Accessors and mutators provide mechanisms for controlled access to a well-encapsulated class.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
15
A constructor may contain a return statement so long as no value (or expression) is returned.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
16
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
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
17
A GUI control sets up an event but it is the programmer who writes the code for the event handler which executes when an event occurs.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
18
Java methods can only return primitive types.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
19
A method defined in a class can access the class's instance data without needing to pass them as parameters or declare them as local variables.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
20
The following method header definition will result in a syntax error:
public void aMethod();
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
21
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
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
22
The software failure at the Denver International Airport's baggage handling system is a good example of

A) how a large system can be obsolete by the time it is developed
B) how designers sometimes have too much faith in the technology they are using
C) how failures are often a result of multiple variables caused by a system as a whole in actual operation
D) how the use of a centralized computer is unrealistic in today's distributed processing
E) All of these
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
23
Refer to Class Definition Ch 04-1: 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(sq);
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
24
In a UML diagram for a class

A) classes are represented as rectangles
B) there may be a section containing the name of the class
C) there may be a section containing the attributes of the class
D) there may be a section containing the methods of the class
E) All of these
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
25
Consider a Rational class designed to represent rational numbers as a pair of ints, along with methods reduce (to reduce the rational to simplest form), gcd (to find the greatest common divisor of two ints), as well as methods for addition, subtraction, multiplication, and division. Why should the reduce and gcd methods be declared to be private?

A) because they will never be used
B) because they will only be called from methods inside Rational
C) because they will only be called from the constructor of Rational
D) because they do not use any of the Rational instance data
E) This is incorrect; they should be declared as public
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
26
Refer to Class Definition Ch 04-1: Which of the following patterns should be used in place of "xxxx" when instantiating df so that the gpa to be output is in typical form (like 3.810)?

A) "#.###"
B) "#.0"
C) "0.#"
D) "0.000"
E) "0.0##"
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
27
Instance data for a Java class

A) are limited to primitive types
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
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
28
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 cannot 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, float, or String method
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
29
A class's 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
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
30
Visibility modifiers include

A) public, private
B) public, private, protected
C) public, private, protected, final
D) public, protected, final, static
E) public, private, protected, static
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
31
Given the method defined here, which of the following method calls is legal? public void doublefoo(double x)

A) doublefoo(0);
B) doublefoo(0.555);
C) doublefoo(0.1 + 0.2);
D) foo(0.1, 0.2);
E) all except D are legal
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
32
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
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
33
Refer to Class Definition Ch 04-1: 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);
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
34
Given the method defined here, which of the following method calls is legal? public void foo(int a, int b)

A) foo(0, 0.1);
B) foo(0/1, 2*3);
C) foo(0);
D) foo();
E) foo(1+2, 3*0.1);
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
35
An example of passing a message to a String where the message has a String parameter would occur in which of the following messages?

A) length
B) substring
C) equals
D) toUpperCase
E) None of these; it is not possible to pass a String as a parameter to a String
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
36
The expressions that are passed to a method in an invocation are called

A) actual parameters
B) formal parameters
C) formal arguments
D) formals
E) Any of these
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
37
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
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
38
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 (can use either)
D) import
E) new
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
39
Which of the following is not a kind of object that is used to create a graphical user interface in JavaFX?

A) control
B) event
C) image
D) event handler
E) All of these are objects used to create a GUI in JavaFX
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
40
What happens if you declare a class constructor to have a void return type?

A) You will most likely receive a syntax error.
B) The program will compile with a warning but you'll get a run-time error.
C) There is nothing wrong with declaring a constructor with a void return type.
D) The class's default constructor will be used instead of the one you're declaring.
E) None of these
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
41
Refer to Class Definition Ch 04-3: Write the constructor for this class.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
42
Refer to Class Definition Ch 04-2: Write a toString method that returns the player's name, position, and batting average. formatted to have one digit to the left of the decimal and three digits to the right of the decimal. Assume DecimalFormat has been imported.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
43
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.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
44
Refer to Class Definition Ch 04-4: Write the statement to instantiate an Box object, blueBox, with a length of 6, width of 4, and height of 2.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
45
Refer to Class Definition Ch 04-4: Add a constructor to class Box for a cube which has only 1 parameter, side.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
46
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.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
47
Refer to Class Definition Ch 04-1: 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) getClassRank(sq);public int updateHours(int moreHours)
{ return hours + moreHours; }
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
48
Refer to Class Definition Ch 04-3: What visibility modifiers would you use for the methods that move the elevator up one floor, move the elevator down one floor, that request the elevator, and that start the elevator moving?
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
49
Refer to Class Definition Ch 04-4: Write a statement to instantiate a cube with a length of 3, width of 3, and height of 3.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
50
Refer to Class Definition Ch 04-2: Write the constructor which is passed the player's name and position.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
51
Refer to Class Definition Ch 04-4: Write a statement to output the volume of the blue box.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
52
Assume method0 calls method1 and method2, method1 calls method3 which calls method4 and method5. Explain the chain of method calls (what order methods are called and from which method).
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
53
Write a portion of a class named Employee. An employee will have a name, ID number, position, and hourly wage. Define the instance data for this class.
Unlock Deck
Unlock for access to all 53 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 53 flashcards in this deck.