Data Types Flashcards

1
Q

What happens at compile time if a local variable remains uninitialized?

A

Compile time error.

Local variables must be explicitly initialized.

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

What is the default value for a boolean instance variable if it is not explicitly initialized?

A

False.

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

What are the primitive data types in Java

A

boolean
char
byte, short, int, long,
float and double.

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

The ~ operator

A

Bitwise negation.

Also called the unary operator, it operates only on integral types.

Maybe think of it as the proofreading symbol for swapping two words.

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

integral data type

A

a type that can contain integers.

They correspond to the primitive types:
byte, short, int and long

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

What data integrity risk is present when converting an int to a float?

A

Loss of precision.

A value of float type is only precise out to nine digits.

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

Integer.MIN_VALUE

A

A constant of the Integer class in the java.lang package representing the smallest value an integer variable can store.

-2^31 = -2147483648

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

Integer.MAX_VALUE

A

A constant of the Integer class in the java.lang package representing the largest value an integer variable can store.

2^31 - 1 = 2147483647

(That number is a Mersenne prime)

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

What are the two Boolean class constructors?

A

Boolean(String) and Boolean(boolean)

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

What does the Boolean(String) constructor do?

A

It allocates a Boolean object representing the value true if the string argument is not null and is equal, ignoring case, to the string “true”.

Otherwise, it allocates a Boolean object representing the value false.

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

What are the Boolean class static helper methods?

A

parseBoolean and valueOf

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

T/F: Conversion from char to long does not need a cast

A

True.

Conversion from a smaller primitive to a larger does not need a cast.

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

T/F: Conversion from int to float needs a cast.

A

False.

However, conversion from float to int does need a cast.

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

T/F: A final variable that fits into a smaller type still needs a cast for it to be assigned.

A

False. The compiler already knows its value. This is called implicit narrowing.

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

Does implicit narrowing apply to all primitive types?

A

No. It only applies to byte, short, int and char.

It does not apply to long, float, or double.

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

Can a long, float, or double be implicitly narrowed using the final keyword?

A

No.

17
Q

Do wrapper classes have a no-args constructor?

A

No.

This means the statement new Integer() is invalid.

18
Q

What does this code do:

String foo = “145”;
Long.valueOf(foo).longValue();

A

Long.valueOf(foo) returns a Long object containing “145”

longValue() returns 145.

19
Q

Can chaining be used to assign a value to a variable at its time of declaration?

e.g. int a = b = c = 42;

A

No.

In this example, if c was not previously declared, this will not compile:

int a = b = c = 42;

20
Q

Is this code valid?

String s = 'a';
A

No.

‘a’ is a char.

“a” is a string.

21
Q

What class does every class directly or indirectly extend?

A

Object

22
Q

What happens to a number that falls exactly between two numbers when rounding?

A

It always rounds up.

23
Q

Is this a valid assignment:

Runnable r = new Thread( );
A

Yes.

Thread implements the interface Runnable.

24
Q

What is the legal range for a byte?

A

-128 to 127

Byte is an 8-bit signed integer.

25
Q

Is this a valid integer variable declaration:

Int x = 10;

A

No.

Int with capital ‘I’ is not valid.

The wrapper class that can hold an int is Integer.

26
Q

What does the equals method of a wrapper class first check?

A

It first checks if the two objects are of the same class or not. If not, it returns false.

27
Q

How do you make an object eligible for garbage collection?

A

By making sure there are no references pointing to it.

28
Q

What conditions must be met for primitive conversion be used?

A

When all of the following are met:

  1. Expression is compile time constant of type byte, short, int or char.
  2. Variable type is byte, short or char.
  3. Value of expression can be represented in the type of variable.
29
Q

T/F: All local variables are initialized automatically and given default values if not explicitly initialized.

A

False.

Note: this is the case for all instance and static variables.

30
Q

T/F: It’s okay to leave a local variable not initialized if it is not used in the code.

A

True.

But why do it?

31
Q

What does equals( ) return?

A

It can be overridden, so you can’t tell unless you look at the code.

32
Q

What does hashCode( ) return?

A

It can be overridden, so you can’t tell unless you look at the code.

33
Q

Does the following code compile?

System.out.println(null + true);

A

No. The operator + cannot be applied to or boolean.