Deck 7: Object-Oriented Programming, Part 2: User-Defined Classes
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/78
Play
Full screen (f)
Deck 7: Object-Oriented Programming, Part 2: User-Defined Classes
1
Which access modifier can be referenced by methods of the same class only?
A) Public
B) Private
C) Protected
D) None of these is correct.
A) Public
B) Private
C) Protected
D) None of these is correct.
B
2
What is the purpose of class constructors?
A) To initiate private access modifiers
B) To return a void
C) To initialize the instance variables of the new object
D) All of these are correct.
A) To initiate private access modifiers
B) To return a void
C) To initialize the instance variables of the new object
D) All of these are correct.
C
3
Inside the curly braces we define the data of the class, called its ___________, and the methods.
A) fields
B) client programs
C) access modifier
D) loop updater
A) fields
B) client programs
C) access modifier
D) loop updater
A
4
Which of the following is a correct class name?
A) Execute
B) String
C) math
D) add
A) Execute
B) String
C) math
D) add
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
5
The instance variables of a class hold the data for each object of that class.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
6
What will happen if you do not put curly braces around a method body?
A) You will get the message illegal start of expression.
B) You will get the message ';' expected.
C) You will get one or more compiler errors.
D) All of these are correct.
A) You will get the message illegal start of expression.
B) You will get the message ';' expected.
C) You will get one or more compiler errors.
D) All of these are correct.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
7
If the return type of the method is void, as in main, there is no need for a return statement. Given that control automatically returns to the caller when the end of the method is reached, most programmers omit the return statement in void methods.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
8
Which of the following statements regarding the default initial value of instance variables byte and boolean is correct?
A) The instance variable byte is treated like the variable float, whereas boolean is given the same value as double.
B) Like byte, boolean is given the value of null.
C) Numeric variables such as boolean are given the value of 0 and byte variables are given the value false.
D) Numeric variables such as byte are given the value of 0 and boolean variables are given the value false.
A) The instance variable byte is treated like the variable float, whereas boolean is given the same value as double.
B) Like byte, boolean is given the value of null.
C) Numeric variables such as boolean are given the value of 0 and byte variables are given the value false.
D) Numeric variables such as byte are given the value of 0 and boolean variables are given the value false.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
9
Which of the following is an advantage of providing multiple constructors?
A) It allows the method to skip the default constructor.
B) It gives the client the ability to avoid overloading the method.
C) It gives the client a choice of ways to create an object.
D) None of these is correct.
A) It allows the method to skip the default constructor.
B) It gives the client the ability to avoid overloading the method.
C) It gives the client a choice of ways to create an object.
D) None of these is correct.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
10
If you do not write a constructor for a class, the compiler will provide a default constructor.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
11
The following is the general form of which method?
Public void setInstanceVariable( dataType newValue )
{
// validate newValue, then assign to the instance variable
}
A) Mutator
B) Uneven
C) Public
D) Autoclass
Public void setInstanceVariable( dataType newValue )
{
// validate newValue, then assign to the instance variable
}
A) Mutator
B) Uneven
C) Public
D) Autoclass
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
12
Declaring a variable with the same name as the parameter of a method inside the method body will not have any effect.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
13
How does a method know for which object the method was called?
A) Any instance variable referred to by a method is considered to be declared to the object.
B) JVM sets the object reference, setName, to refer to the object for which the method has been called.
C) JVM sets the object reference, this, to refer to the object for which the method has been called.
D) None of these is correct.
A) Any instance variable referred to by a method is considered to be declared to the object.
B) JVM sets the object reference, setName, to refer to the object for which the method has been called.
C) JVM sets the object reference, this, to refer to the object for which the method has been called.
D) None of these is correct.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
14
A parameter to a mutator method with the same name as the instance variable being changed hides the instance variable.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
15
Why should you provide new versions of the toString and equals methods from the Object class?
A) To get better functionality
B) To get a true return
C) To be able to change headers
D) All of these are correct.
A) To get better functionality
B) To get a true return
C) To be able to change headers
D) All of these are correct.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
16
The __________ method is called automatically when an object reference is used as a String.
A) enum
B) toString
C) overriding
D) getName
A) enum
B) toString
C) overriding
D) getName
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
17
An instance method can reference which of the following?
A) Class variables
B) Instance variables
C) Class methods
D) All of these are correct.
A) Class variables
B) Instance variables
C) Class methods
D) All of these are correct.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
18
Methods that are defined to be non-static are subject to which of the following important restrictions?
A) Can reference only static variables
B) Cannot use the object reference this
C) Both reference only static variables and not use the object reference this
D) None of these is correct.
A) Can reference only static variables
B) Cannot use the object reference this
C) Both reference only static variables and not use the object reference this
D) None of these is correct.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
19
When you define a static variable for your class, it is better to define its accessor and mutator methods as class methods.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
20
static variables that are constants are usually declared to be public because they typically are provided to allow the client to set preferences for the operations of a class. If you define a static variable for your class that is not a constant, it is best to define it as private and provide accessor and mutator methods, as appropriate, for client access to the static variable.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
21
Static data belong to the class, rather than to a particular object, or instance, of the class.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
22
Which of the following is a similarity between ordinal( ) and compareTo( Enum eObj )?
A) Both return enum
B) Both return 0 if two objects are the same
C) Both return int
D) Both set the default value as 0
A) Both return enum
B) Both return 0 if two objects are the same
C) Both return int
D) Both set the default value as 0
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
23
The enum objects are instantiated as constant objects, meaning that their values can be changed.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
24
enum Colors { RED, YELLOW, BLUE }; can be used to write a statement that represents colors in various order.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
25
The enum functionality is built into java.lang, so we must use an import statement to define enum types.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
26
A _____________ is a collection of related classes that can be imported into programs.
A) package
B) name program
C) valueOf method
D) main method
A) package
B) name program
C) valueOf method
D) main method
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
27
Name duplication is when a file with the same name as a file you are trying to use already exists.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
28
Which of the following is not one of the criteria for a well-defined class?
A) The class should be easy to understand and use.
B) The class documentation should be clear, easy to understand, and complete.
C) The class documentation should represent the class well.
D) All of these are criteria for a well-defined class.
A) The class should be easy to understand and use.
B) The class documentation should be clear, easy to understand, and complete.
C) The class documentation should represent the class well.
D) All of these are criteria for a well-defined class.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
29
How should slashes and asterisks be used in a Javadoc comment?
A) Start with /* and end it with /**.
B) Start with /* and end with */.
C) Start with /** and end with */.
D) None of these is correct.
A) Start with /* and end it with /**.
B) Start with /* and end with */.
C) Start with /** and end with */.
D) None of these is correct.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
30
Javadoc is an executable program used to produce HTML-based documentation similar to the documentation available on Oracle's Java website.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
31
Javadoc recognizes two types of tags: block tags and comment tags.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
32
Block tags start with the character @.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
33
Javadoc discards all whitespace characters and the * at the beginning of each line until a non-whitespace character and non * character is encountered, so starting each comment with an * makes the Javadoc stand out.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
34
The data type of the return value of the toString method is:
A) Object.
B) boolean.
C) String.
D) class.
A) Object.
B) boolean.
C) String.
D) class.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
35
The data type of the return value of the equals method is:
A) Object.
B) boolean.
C) equals.
D) class.
A) Object.
B) boolean.
C) equals.
D) class.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
36
The enum functionality is built into which package?
A) java.enum
B) java.lang
C) java.io
D) java.util
E) java.text
A) java.enum
B) java.lang
C) java.io
D) java.util
E) java.text
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
37
A javadoc comment starts with:
A) //.
B) /*.
C) /**.
D) */.
A) //.
B) /*.
C) /**.
D) */.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
38
A javadoc comment ends with:
A) //.
B) /*.
C) /**.
D) */.
A) //.
B) /*.
C) /**.
D) */.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
39
Mutator methods can be void methods or can return the reference this.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
40
Mutator methods take no parameters.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
41
The access modifier of accessor and mutator methods is usually __________.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
42
A method parameter or local variable that has the same name as an instance variable will __________ that instance variable.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
43
The enum objects are instantiated as __________ objects.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
44
A package statement uses the keyword __________.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
45
The __________ method typically returns a String representation of the object data.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
46
A(n) _________ is a collection of classes that can be imported into a program.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
47
In the instance variable declaration private int value:
A) private is the access modifier, int is the name of the instance variable, and value is the data type.
B) private is the name of the instance variable, int is the data type, and value is the access modifier.
C) private is the access modifier, int is the data type, and value is the name of the instance variable.
D) private is the data type, int is the access modifier, and value is the name of the instance variable.
A) private is the access modifier, int is the name of the instance variable, and value is the data type.
B) private is the name of the instance variable, int is the data type, and value is the access modifier.
C) private is the access modifier, int is the data type, and value is the name of the instance variable.
D) private is the data type, int is the access modifier, and value is the name of the instance variable.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
48
A method of a class can access:
A) only the instance variables of its class.
B) only its parameters.
C) only the local variables the method defines.
D) the instance variables of its class, its parameters, and its local variables.
A) only the instance variables of its class.
B) only its parameters.
C) only the local variables the method defines.
D) the instance variables of its class, its parameters, and its local variables.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
49
It is possible to create user-defined classes in Java.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
50
The body of a method must be surrounded by curly braces.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
51
The default constructor takes no parameters.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
52
A method of a class can call other methods of that same class.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
53
Accessor methods are void methods.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
54
Accessor methods take no parameters.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
55
static methods can use the object reference this.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
56
static methods can call non-static methods.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
57
static methods can reference non-static variables.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
58
When we initialize an enum type, we use String literals.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
59
In the class header public class TV, public is the access modifier and TV is the name of the class.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
60
The access modifier of constructors should be __________.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
61
When we provide several constructors, we are __________ that method.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
62
The name of a method, along with its parameter list, is called the __________ of that method.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
63
The reference __________ refers to the object reference with which this method has been called.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
64
When we code the toString method of a class, we are __________ the toString method of the Object class.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
65
We can define class variables using the keyword __________.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
66
price is an instance variable of a class. Inside the method public void change( int price ), how do we refer to the instance variable price and not the parameter price?
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
67
What are the three access modifiers?
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
68
In addition to modifying the value of an instance variable, what is the other function of a mutator method?
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
69
What syntax is used to define a class?
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
70
Analyze why a programmer would want to make known the public method names and their APIs, but not the code of the class.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
71
Compare and contrast methods with instance variables.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
72
Construct a method in which you could omit the return expression.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
73
Suppose Angelica ran a program with multiple constructors and got a compiler error. How could she improve her program to avoid the error?
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
74
What is an advantage of returning the reference this for several methods-for example, mutators?
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
75
How would you defend taking the time to design your own classes?
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
76
In terms of accessing instance variables, are static or non-static variables more restricted?
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
77
What is Javadoc?
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck
78
Give an example of a Javadoc tag.
Unlock Deck
Unlock for access to all 78 flashcards in this deck.
Unlock Deck
k this deck