Java Identifiers, Primitivies, & Reference Types Flashcards

1
Q

Legal Java identifies can only being with the following characters:

A

A letter
The $ character
The _ character

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

Can numbers be used in Java identifiers?

A

Yes but it can’t be the first character in the variable name.

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

If a Java identifier is composed only of letters, is it always a legal variable name?

A

No. Java reserved words can not be used as variable names.

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

With the exception of the first character, legal Java identifies can only contain the following characters:

A

Letters
Numbers
The $ character
The _ character

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

What structures in Java are named using identifiers?

A

Variables
Methods
Classes
Fields

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

What is a variable?

A

A variable is a name for a piece of memory that stores

data.

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

What are the minimum requirements when declaring a variable in Java?

A

At a minimum, a variable must have a variable type and an identifier (or name).

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

Java has two types of data variables, what are they?

A

Primitive Types

Reference Types

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

How many primitive types (or primitives) does Java have?

A

8

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

What are the Java primitive types?

A
boolean
byte
short
int
long
float
double
char
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How much memory is required for a byte? What values can it hold? What is its default value?

A

8-bits or 1 byte
-128 to 127
0

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

How much memory is required for a short?What values can it hold?What is its default value?

A

16-bits or 2 bytes
-32,768 to 32,767
0

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

How much memory is required for an int? What values can it hold? What is its default value?

A

32-bits or 4 bytes
-2^31 to (2^31)-1
0

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

How much memory is required for a long? What values can it hold? What is its default value?

A

64-bits or 8 bytes
-2^63 and a maximum value of (2^63)-1
0L

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

How much memory is required for a float?

A

32-bits or 4 bytes

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

How much memory is required for a double?

A

64-bits or 8 bytes

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

How much memory is required for a char in Java 8? What values can it hold? What is its default value?

A

16-bits or 2 bytes
A Unicode character
‘\u0000’

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

What is the default value of a boolean?

A

false

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

Does the following line of code compile?
long max = 3123456789;
Why or why not?

A

No. When using literal values, Java treats numbers like ints. This value is greater than the maximum int value. To fix the problem append ‘L’ to make Java treat this is a long.

long max = 3123456789L;

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

How do you specify that a literal is an octal value?

A

Prefix the number with a 0 and only use characters 1 - 7.

Example: 017

21
Q

How do you specify that a literal is a hexadecimal value?

A

Prefix the number with a 0x or 0X followed by the values 0 - 9 and/or A - F.
Example: 0xA4

22
Q

How do you specific that a literal is a binary value?

A

Prefix the number with a 0b or 0B followed by the values 0 - 1.
Example: 0b10110101

23
Q

Does the following code compile in Java 8?

int million = 1_000_000;

Why or why not?

A

Yes. As of Java 7, there was a feature added to numeric literals allowing you to add underscores to the number to make them easier to read.

You can add underscores anywhere except at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point.

24
Q

Does the following code compile in Java 8?int million = _1000000;Why or why not?

A

No. As of Java 7, there was a feature added to numeric literals allowing you to add underscores to the number to make them easier to read.

You can add underscores anywhere except at the beginning of a literal, the end of a literal, right before a decimal point, or right after a decimal point. This has an underscore at the beginning.

25
Q

Does the following code compile in Java 8?int million = 1_00_0.0_0;Why or why not?

A

Yes. As of Java 7, there was a feature added to numeric literals allowing you to add underscores to the number to make them easier to read.This doesn’t actually make the number easier to read but it is legal.

26
Q

What is a literal?

A

A Java literal is a syntactic representation of Boolean, character, numeric, or string data.

27
Q

How do you represent boolean literals?

A

With the keywords true or false.

28
Q

When using a numeric literal if there are no other qualifiers, what type does it default to?

A

An int.

29
Q

Can you assign a long literal to an int if the long value is less than the maximum size of an int?

A

No.

30
Q

Can you assign a binary literal value to an int if the binary value is less than the maximum size of an int?

A

Yes. This is also true of hexadecimal and octal values.

31
Q

What is appended to a numerical literal to denote that it is a long?

A

An L. You can use either the upper or lowercase L, however the uppercase L is recommended as the lowercase L looks like the number 1 in some fonts.

32
Q

What does a reference type refer to?

A

An object.

33
Q

Do reference types refer to primitives?

A

Not directly. A reference type always points to an object. However, the object could be nothing but a wrapper for a primitive.

34
Q

What are the 2 ways are values assigned to a reference?

A

A reference can be made to point to an existing object of the same type.

A reference can be made to point to a new object using the new keyword.

35
Q

Can a reference be assigned a value of null?

A

Yes.

36
Q

Can a primitive be assigned a value of null?

A

No.

37
Q

Can a reference be used to call a method?

A

Yes.

38
Q

Can a primitive be used to call a method?

A

No.

39
Q

How do you declare multiple variables on a single line?

A

There are two ways. Example:
int a; int b; int c;
OR
int a, b, c;

40
Q

How do you initialize multiple variables on a single line?

A

There are two ways. Example:
int a = 1; int b = 2; int c = 3;
OR
int a = 1, b = 2, c = 3;

41
Q

Can you declare variables of different types on the same line without a semicolon?

A
No. The following is an error:
int a, String b;
However:
int a; String b;
is acceptable.
42
Q

When using only a comma to declare multiple variables, how many variable type keywords can you use (between semicolons)?

A

Only 1. Example:
OK: double d1, d2, d3;
BAD: double d1, double d2;

43
Q

What is a local variable?

A

A local variable is a variable defined within a method.

44
Q

Do local variables have a default value?

A

No. They have garbage data in them. Code with local variables will not compile if the variable is used before being initialized.

45
Q

What is an instance variable?

A

An instance variable is any variable declared outside a method that is not declared static. They are so named because they are unique to their instance of the object.

46
Q

What is a class variable?

A

A class variable is any variable declared static and outside a method. They are so named because their is only one for all the objects made from the class.

47
Q

Which variables do not need to be initialized?

A

instance and class variables.

48
Q

What is the scope of a variable?

A

Scope is where in the code a variable can be accessed.

49
Q

How do you determine the scope of a variable?

A

A variable is not directly accessible outside the code block in which it was declared. However, as in the case of the class variable, it may be accessible across multiple objects.

Stated another way:
Local Variables: in scope from declaration to end of block

Instance Variables: in scope from declaration until object garbage collected

Class Variables: in scope from declaration until program ends.