Chapter 1: Java Building Blocks Flashcards

1
Q

T or F: A file can contain multiple classes.

A

True. But at most one public class.

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

T or F: A public class within the file ClassA.java can be named ClassB

A

False. A public class must be in a file of the same name

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

What is the file extension is used for bytecode files?

A

.class

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

T or F: Every java file requires a package declaration

A

False. Package declaration is optional

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

T or F: Constructors can have a return type

A

False

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

T or F: It is optional to include a constructor in a class

A

True. There is a default do-nothing constructor

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

Where do you put code initializer blocks?

A

Within a class, outside of methods.

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

Which runs first, code initializer block or field initialization?

A

Depends on the order they appear in the class

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

When does a constructor’s code block run?

A

After field and initializer blocks are done

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

T or F: You can refer to a field before it is initialized

A

False

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

What type of data does the following primitive hold?

Boolean

A

True or false

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

What type of data does the following primitive hold?

byte

A

8-bit integral value

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

What type of data does the following primitive hold?

short

A

16-bit integral value

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

What type of data does the following primitive hold?

int

A

32-bit integral value

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

What type of data does the following primitive hold?

long

A

64-bit integral value

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

What type of data does the following primitive hold?

float

A

32-bit floating-point value

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

What type of data does the following primitive hold?

double

A

64-bit floating-point value

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

What type of data does the following primitive hold?

char

A

16-bit Unicode value

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

T or F: A float literal doesn’t need the ‘f’ after it

A

False. Floating point literals are assumed to be doubles unless the ‘f’ is present

20
Q

What is the max value of a byte?

21
Q

What is the min value of a byte?

22
Q

What does it mean to have a numeric literal followed by an ‘L’?

A

It’s data type is long

23
Q

How do you write an octal literal?

A

Prefix with ‘0’

24
Q

How do you write a hex literal?

A

Prefix with ‘0x’

25
How do you write a binary literal?
Prefix with '0b'
26
A numeric literal can contain '_'. Where can't the underscore go?
- At the beginning or end of the literal. | - Immediately before or after a decimal.
27
Can a primitive be null?
No, but a reference variable can be null
28
Can a primitive be used to call methods?
No, but reference variables can
29
Is String s1, s2 = "s2; valid?
Yes. Multiple variables of the same type can be declared on the same line. Not all need to be initialized
30
Is String s1, long l1, l2 = 12L; valid?
No. You can only declare variables in the same line if they are the same type.
31
What characters can identifiers start with?
Identifiers must start with a letter, $, or _. Subsequent characters can be numbers
32
Is 'public' a valid identifier?
No. 'public' is a reserved word in Java
33
Is 'Public' a valid identifier?
Yes. Although 'public' is a reserved word, Java is case sensitive, so 'Public' is valid
34
What are the two unused reserved words?
const and goto
35
Do local variables have default initialization?
No
36
What is the default value of a boolean instance variable?
false
37
What is the default value of a integral instance variable?
0
38
What is the default value of a floating point instance variable?
0.0
39
What is the default value of a char instance variable?
'\u0000' (NUL)
40
What is the default value of a reference instance variable?
null
41
What is the scope of a local variable?
The method/block in which it is defined
42
What is the correct order of the following elements in a file? ``` Import statements package declaration class declaration ```
Package declaration Import statements class declaration - Package declaration and import statements are optional
43
Is System.gc() guaranteed to run?
No
44
What makes an object eligible for garbage collection?
It has no references pointing to it. | All references to it are out of scope
45
Is finalize() guaranteed to run?
No
46
T or F: If an object's finalize method is called and it's not successfully destroyed, the finalize method could run again later.
False. The finalize method can run at most one time.
47
What are the 6 main benefits of java?
``` Object oriented encapsulation platform independent robust simple secure ```