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?

A

127

21
Q

What is the min value of a byte?

A

-128

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
Q

How do you write a binary literal?

A

Prefix with ‘0b’

26
Q

A numeric literal can contain ‘_’. Where can’t the underscore go?

A
  • At the beginning or end of the literal.

- Immediately before or after a decimal.

27
Q

Can a primitive be null?

A

No, but a reference variable can be null

28
Q

Can a primitive be used to call methods?

A

No, but reference variables can

29
Q

Is
String s1, s2 = “s2;
valid?

A

Yes. Multiple variables of the same type can be declared on the same line. Not all need to be initialized

30
Q

Is
String s1, long l1, l2 = 12L;
valid?

A

No. You can only declare variables in the same line if they are the same type.

31
Q

What characters can identifiers start with?

A

Identifiers must start with a letter, $, or _. Subsequent characters can be numbers

32
Q

Is ‘public’ a valid identifier?

A

No. ‘public’ is a reserved word in Java

33
Q

Is ‘Public’ a valid identifier?

A

Yes. Although ‘public’ is a reserved word, Java is case sensitive, so ‘Public’ is valid

34
Q

What are the two unused reserved words?

A

const and goto

35
Q

Do local variables have default initialization?

A

No

36
Q

What is the default value of a boolean instance variable?

A

false

37
Q

What is the default value of a integral instance variable?

A

0

38
Q

What is the default value of a floating point instance variable?

A

0.0

39
Q

What is the default value of a char instance variable?

A

‘\u0000’ (NUL)

40
Q

What is the default value of a reference instance variable?

A

null

41
Q

What is the scope of a local variable?

A

The method/block in which it is defined

42
Q

What is the correct order of the following elements in a file?

Import statements
package declaration
class declaration
A

Package declaration
Import statements
class declaration

  • Package declaration and import statements are optional
43
Q

Is System.gc() guaranteed to run?

A

No

44
Q

What makes an object eligible for garbage collection?

A

It has no references pointing to it.

All references to it are out of scope

45
Q

Is finalize() guaranteed to run?

A

No

46
Q

T or F: If an object’s finalize method is called and it’s not successfully destroyed, the finalize method could run again later.

A

False. The finalize method can run at most one time.

47
Q

What are the 6 main benefits of java?

A
Object oriented
encapsulation
platform independent
robust
simple
secure