Chapter 4 - Using Classes and Objects Flashcards
An object’s data items are also known as .
a) fields
b) functions
c) themes
d) instances
A fields
You send messages or information to an object through its .
a) fields
b) methods
c) classes
d) type
methods
When you use an object and its methods, think of being able to send a message to the object to direct it to accomplish some task—you can tell the Party object named myGraduationParty to set the date and time you request. Even though yourAnniversaryParty is also a member of the Party class, and even though it also has access to setDate() and setTime() methods, the arguments you send to yourAnniversaryParty methods will be different from those you send to myGraduationParty methods. Within any object-oriented program, you are continuously making requests to objects’ methods and often including arguments to send information as part of those requests.
A program or class that instantiates objects of another prewritten class is a(n) .
a) class client
b) superclass
c) object
d) patron
A class client
You can call an application or class that instantiates objects of another class a class client or a class user . Understanding classes and how objects are instantiated from them is the heart of object-oriented thinking.
The body of a class is always written .
a) in a single line, as the first statement in a class
b) within parentheses
c) between curly braces
d) as a method call
C between curly braces
Most class data fields are .
a) private
b) public
c) static
d) final
A private
You already have learned that the access specifier for most Java methods is public. However, most fields, such as empNum in the Employee class, are private, which provides the highest level of security. Assigning private access to a field means that no other classes can access the field’s values, and only methods of the same class are allowed to set, get, or otherwise use private variables. The principle used in creating private access is sometimes called information hiding and is an important component of object-oriented programs. A class’s private data can be changed or manipulated only by a class’s own methods and not by methods that belong to other classes.
The concept of allowing a class’s private data to be changed only by a class’s own methods is known as .
a) structured logic
b) object orientation
c) information hiding
d) data masking
C information hiding
the object-oriented programming principle used when creating private access for data fields that describes that a class’s private data can be changed or manipulated only by a class’s own methods.
Suppose you declare an object as Book myJournal;. Before you store data in myJournal, you .
a) also must explicitly allocate memory for it
b) need not explicitly allocate memory for it
c) must explicitly allocate memory for it only if it has a constructor
d) cannot explicitly declare memory for it
A also must explicitly allocate memory for it
When you declare an integer as int someValue;, you notify the compiler that an integer named someValue will exist, and you reserve computer memory for it at the same time. When you declare the someEmployee instance of the Employee class, you are notifying the compiler that you will use the identifier someEmployee. However, you are not yet setting aside computer memory in which the Employee named someEmployee might be stored—that is done automatically only for primitive type variables. To allocate the needed memory for an object, you must use the new operator .
If a class is named Student, the class constructor name is .
a) any legal Java identifier
b) any legal Java identifier that begins with S
c) StudentConstructor()
d) Student()
D Student()
Whether you write your own constructor or use the one automatically created by Java, the name of the constructor is always the same as the name of the class whose objects it constructs.
If you use the automatically supplied default constructor when you create an object, .
a) int fields are set to 0 (zero)
b) char fields are set to blank
c) boolean fields are set to true
d) String fields are set to blanks
A int fields are set to 0 (zero)
When the prewritten, default constructor for the Employee class is called, it establishes one Employee object with the identifier provided. The automatically supplied default constructor provides the following specific initial values to an object’s data fields:
Numeric fields are set to 0 (zero). Character fields are set to Unicode '\u0000'. Boolean fields are set to false. Fields that are object references (for example, String fields) are set to null (or empty).
If you declare a variable as an instance variable within a class, and you declare and use the same variable name within a method of the class, then within the method, .
a) the variable used inside the method takes precedence
b) the class instance variable takes precedence
c) the two variables refer to a single memory address
d) an error will occur
A the variable used inside the method takes precedence
this refers to the ‘this’ reference; Usually, you neither want nor need to refer to the ‘this’ reference within the instance methods that you write, but the this reference is always there, working behind the scenes so that the data field for the correct object can be accessed
A constructor parameters.
a) can receive
b) cannot receive
c) must receive
d) can receive a maximum of 10
A can receive parameters
a class contains constructors that are invoked to create objects from the class blueprint; constructor declarations look like method declarations – except that they use the name of the class and have no return type; You don’t have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass.
A constructor overloaded.
a) must be
b) cannot be
c) can be
d) is always automatically
C can be
as long as the parameter lists and data types are different then you can overload a constructor; it is similar to overloading a method
Usually, you want each instantiation of a class to have its own copy of .
a) the data fields but not the methods
b) the methods but not the data fields
c) both the data fields and the methods
d) neither the data fields nor the methods
A the data fields but not the methods
If you create a class that contains one method, and you instantiate two objects, you usually store for use with the objects.
a) one copy of the method
b) two copies of the method
c) two different methods containing two different this references
d) data only (the methods are not stored)
A one copy of the method
The this reference .
a) can be used implicitly
b) must be used implicitly
c) must not be used implicitly
d) must not be used
A can be used implicitly