chapter 2 Flashcards

1
Q

How do you create an instance of a class?

A
write "new" before the class name and parentheses after the class name.
e.g. Foo f = new Foo();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do constructor methods do?

A

Create new objects.

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

What are two characteristics of constructor methods?

A

Their name matches the name of the class, and they do not have return types.

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

What is the purpose of a constructor method?

A

To initialize fields in an instance of a class.

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

What keyword is required anytime a constructor is used?

A

new

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

What is a code block?

A

The lines of code between {}.

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

What are instance initializers?

A

Blocks of code that run outside of a method.

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

What order are fields and initializer blocks run?

A

The order in which they appear in the file.

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

When does the constructor run in relation to fields and initializer blocks?

A

After all fields and instance initializer blocks have run.

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

What are the two data types Java applications contain?

A

primitive and reference types.

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

How many and what are the built-in primitive data types Java has?

A

8 types.

boolean: true or false
byte: 8-bit integral value
short: 16-bit integral value
int: 32-bit integral value
long: 64-bit integral value
float: 32-bit floating-point value
double: 64-bit floating-point value
char: 16-bit Unicode value

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

What is and what is not a primitive data type in Java?

A

A single value in memory, not an object.

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

What does a float value require following the number in Java?

A

f

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

Are all numeric types signed in Java?

A

Yes.

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

What does it mean when a numeric type is signed?

A

The type reserves one bit to cover a negative range.

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

What are the ranges of values possible for each of the 6 primitive numeric types in Java?

A

byte: -128 to 127
short: -32,768 to 32,767
int: -2,147,483,648 to 2,147,483,647
long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,80
float: 7 decimal digits
double: 16 decimal digits

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

What are the similarities and differences between short and char?

A

Both are stored as integral types with the same 16-bit length. Short is signed, char is not.

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

How are floating point numbers stored in most computer systems?

A

Scientific notation.

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

What is a number called when it is present in code?

A

A literal.

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

What does Java assume as the default type for a literal?

A

an int.

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

How can a numeric literal be made a long type?

A

Append an L (or an l) to the literal.

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

What can be added to numeric literals to allow easier reading?

A

underscores _

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

Where are underscores NOT allowed to be placed in a numeric literal?

A

The beginning or end of a literal.

Right before or after the decimal point.

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

What are the two ways a reference type can be assigned to a value?

A

To another object of the same or compatible type.

To a new object using the new keyword.

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

What are some differences between primitive and reference types?

A
Reference types:
1. Can be assigned null
2. Can be used to call methods
Primitive types:
1.  Have lowercase type names
26
Q

What is a variable?

A

A name for a piece of memory that stores data.

27
Q

What is required in variable declaration?

A

A type and a name.

28
Q

What is an identifier?

A

The name for a variable, method, class, interface, or package.

29
Q

What are the four rules for legal identifiers?

A
  1. must begin with a letter, $, or _
  2. can include numbers but not start with them
  3. single underscore is not a valid identifier
  4. cannot use a Java reserved word
30
Q

What is camelCase and snake_case?

A

camelCase:
convention where the first letter of each word is capitalized.
snake_case:
convention where an underscore separates each word.

31
Q

What is the rule for declaring multiple variables in the same declaration (inline)?

A

All the variables must be the same type.

32
Q

What is a local variable?

A

A variable defined within a constructor, method, or initializer block.

33
Q

What is true about initializing local variables?

A

They do not have a default value and must be initialized before use.

34
Q

What is the name of variables that are passed to constructors or methods?

A

parameters.

35
Q

What is an instance variable?

A

A value defined within a specific instance of an object.

36
Q

What is a class variable?

A

A variable that is defined at the class level and shared among all instances of the class.

37
Q

How can you tell a variable is a class variable?

A

It has the keyword static in front of it.

38
Q

What types of variables do not require initialization before using?

A

instance and class variables. They are assigned default values at declaration.

39
Q

Define the default values for each type.

A
boolean: false
byte, short, int, long: 0
float, double: 0.0
char: '\u0000' (NULL)
all object refs: null
40
Q

When was the var keyword introduced?

A

Java 10.

41
Q

What types of variables can you use keyword var with?

A

local variables.

42
Q

What is the formal name of the var feature?

A

local variable type reference.

43
Q

How is the type determined when keyword var is used to initialize a local variable?

A

The compile infers it from the value.

44
Q

When is the type associated with a variable declared with var determined?

A

At compile time.

45
Q

When using var, does the variable need to be assigned a value on the same line as the declaration in order to compile?

A

yes

46
Q

Can null be assigned to a variable that has been declared using the var keyword?

A

no

47
Q

What does it mean when a word is a reserved type name?

A

It cannot be used to define a type, such as a class, interface, or enum.

48
Q

Is var a reserved type name?

A

Yes

49
Q

Is var a reserved word?

A

No

50
Q

Where can var not be used to declare a variable?

A

Parameters for constructors and methods, instance variables, and class variables.

51
Q

Can the type of a var change?

A

no

52
Q

Is var permitted in a multiple-variable declaration?

A

no

53
Q

What is the largest possible scope for a local variable?

A

The method they are defined in.

54
Q

What is the scope of an instance variable?

A

From declaration until object is available for garbage collection.

55
Q

What is the scope of a class aka static variable?

A

From declaration until the program ends.

56
Q

Where are all Java objects stored?

A

In your programs memory heap (free store).

57
Q

What is garbage collection?

A

The process of freeing memory on the heap by deleting objects that are no longer reachable in your program.

58
Q

What is the built-in method to help support garbage collection that can be called at any time?

A

System.gc();

59
Q

When is System.gc(); guaranteed to run?

A

never.

60
Q

When is an object available for garbage collection?

A
  1. The object no longer has any references pointing to it.

2. All references to the object have gone out of scope.

61
Q

What gets garbage collected, the object or the reference?

A

The object.