007 - Classes and Objects Flashcards

1
Q

In a class declaration. The __________ (the area between the braces) contains all the code that provides for the life cycle of the objects created from the class:

A

class body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
The class body contains: 
A. \_\_\_\_\_\_\_\_\_\_ for initializing new objects
B. \_\_\_\_\_\_\_\_\_\_ for the fields that provide the state of the class and its objects
C. \_\_\_\_\_\_\_\_\_\_ to implement the behavior of the class and its objects.
A

constructors

declarations

methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

class declarations can include these components, in order:

  1. _________ such as public, private.
  2. _________, with the initial letter capitalized.
  3. __________ preceded by the keyword extends.
  4. _________ preceded by the keyword implements.
  5. __________, surrounded by braces, {}.
A
  1. Modifiers
  2. The class name
  3. the class’s parent (superclass), if any. (A class can only extend (subclass) one parent.)
  4. A comma-separated list of interfaces implemented by the class, if any (A class can implement more than one interface.)
  5. The class body
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

There are several kinds of variables:

  1. Member variables in a class: __________.
  2. Variables in a method or block of code: __________.
  3. Variables in method declarations: __________.
A

fields

local variables

parameters

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Field declarations are composed of three components, in order:

  1. __________, such as public or private.
  2. The field’s __________.
  3. The field’s __________.
A
  1. Zero or more modifiers
  2. type
  3. name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

__________ used lets you control what other classes have access to a member field.

A

The first (left-most) modifier

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

__________—the field is accessible from all classes.

A

public modifier

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

__________ —the field is accessible only within its own class.

A

private modifier

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In the spirit of encapsulation, it is common to make fields __________.

A

private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

All variables must have a __________.
You can use __________ such as int, float, boolean, etc.

Or you can use __________, such as strings, arrays, or objects.

A

type

primitive types

reference types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

the same naming rules and conventions are used for method and class names, except that

the first letter of a class name \_\_\_\_\_\_\_\_\_\_, 
the first (or only) word in a method name \_\_\_\_\_\_\_\_\_\_.
A

should be capitalized

should be a verb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
The only required elements of a method declaration are:
the method's \_\_\_\_\_\_\_\_\_\_, 
the method's \_\_\_\_\_\_\_\_\_\_,
a pair of \_\_\_\_\_\_\_\_\_\_, 
and a \_\_\_\_\_\_\_\_\_\_.
A

return type
name
parentheses, ()
body between braces, {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

More generally, method declarations have six components, in order:

1__________such as public, private.

  1. __________ the data type of the value returned by the method, or void.
  2. __________ the rules for fields apply to methods as well, convention is a little different.
  3. __________ in parenthesis.
  4. __________ to be discussed later.
  5. __________, enclosed between braces.
A
  1. Modifiers
  2. The return type
  3. The method name
  4. The parameter list: a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.
  5. An exception list
  6. The method body: the method’s code, including the declaration of local variables, goes here.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Two of the components of a method declaration comprise the method signature: __________ and __________.

A

the method’s name

the parameter types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Definition: Two of the components of a method declaration comprise __________: the method’s name and the parameter types.

A

the method signature

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

By convention, method names should be __________ or __________, followed by adjectives, nouns, etc.
In multi-word names, the first letter of each of the second and following words __________.

A

a verb in lowercase or a
multi-word name that begins with a verb in lowercase

should be capitalized

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Typically, a method has a unique name within its class. However, a method might have the same name as other methods due to __________.

A

method overloading

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Java can distinguish between methods with different __________.

A

method signatures

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

__________ means that methods within a class can have the same name if they have different parameter lists

A

Method overloading

20
Q

Overloaded methods are differentiated by the __________ and the __________ of the __________ passed into the method.

A

The number and the type of the arguments

21
Q

You cannot declare more than one method with the same name and the same number and type of arguments, because __________.

A

the compiler cannot tell them apart

22
Q

__________, so you cannot declare two methods with the same signature even if they have a different return type.

A

The compiler does not consider return type when differentiating methods

23
Q

__________ should be used sparingly, as they can make code much less readable.

A

Overloaded methods

24
Q

Constructor declarations look like method declarations—except that __________ and __________.

A

they use the name of the class and have no return type

25
Q

To create a new Bicycle object called myBike, __________ is called by __________

A

a constructor

the new operator

Bicycle myBike = new Bicycle(30, 0, 8);

26
Q

new Bicycle(30, 0, 8)

creates __________ for the object and initializes __________.

A

space in memory

its fields

27
Q

As with methods, the Java platform differentiates __________ on the basis of the number of arguments in the list and their types.

A

constructors

28
Q

You cannot write two constructors that have the same number and type of arguments for the same class, because the platform would not be able to tell them apart. Doing so causes a __________.

A

compile-time error

29
Q

You don’t have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically __________ for any class without constructors.

A

provides a no-argument, default constructor

30
Q

If your class has no explicit superclass, then it has an __________, which does have a no-argument constructor.

A

implicit superclass of Object

31
Q

You can use __________in a constructor’s declaration to control which other classes can call the constructor.

A

access modifiers

32
Q

__________ refers to the ilist of variables in a method declaration. __________ are the actual values that are passed in when the method is _________.

A

Parameters

Arguments

invoked

33
Q

You can use a construct called __________ to pass an arbitrary number of values to a method. You use it when you don’t know how many of a particular type of argument will be passed to the method.

A

varargs

34
Q

__________ is a shortcut to creating an array manually.

A

varargs

35
Q

To use varargs, you follow the type of the last parameter by __________ then a space, and __________. The method can then be called with any number of that parameter, including __________.

A

an ellipsis (three dots, …),

the parameter name

none

36
Q

You will most commonly see varargs with __________.

A

the printing methods;
for example, this printf method:

public PrintStream printf(String format, Object… args)

allows you to print an arbitrary number of objects. It can be called like this:

System.out.printf(“%s: %d, %s%n”, name, idnum, address);
or like this

System.out.printf(“%s: %d, %s, %s, %s%n”, name, idnum, address, phone, email);

37
Q

The __________ of a parameter must be unique in its scope. It cannot be __________ for the same method or constructor, and it cannot be __________ of a local variable within the method or constructor.

A

name

the same as the name of another parameter

the name

38
Q

A parameter can have the same name as one of the class’s fields. If this is the case, the parameter is said to __________ the field.

A

shadow

39
Q

__________ can make your code difficult to read and is conventionally used only within constructors and methods that set a particular field.

A

Shadowing fields

40
Q

Primitive arguments, such as an int or a double, are passed into methods __________. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters __________ and any changes to them __________.

A

by value

are gone

are lost

41
Q

Reference data type parameters, such as objects, are also passed into methods __________. This means that when the method returns, the passed-in reference __________ However, the values of the object’s fields __________ in the method, if they have __________.

A

by value

still references the same object as before.

can be changed in the method, if they have the proper access level.

42
Q

An object creation statement can have three parts:

  1. __________: associate a variable name with an object type.
  2. __________: The new keyword is a Java operator that creates the object.
  3. _________: The new operator is followed by a call to a constructor, which initializes the new object.
A
  1. Declaration
  2. Instantiation
  3. Initialization
43
Q

Simply declaring a reference variable __________ For that, you need to use the __________.

A

does not create an object.

new operator

44
Q

The new operator __________ by allocating memory for a new object and returning a reference to that memory. The new operator also invokes __________.

A

instantiates a class

the object constructor

45
Q

The phrase “instantiating a class” means the same thing as “_________”

A

creating an object.

46
Q

The new operator requires a single, postfix argument: a call to a _________.

A

constructor