Chapter 2 Flashcards

1
Q

Which of the following are valid java identifiers?

A. _
B. _helloWorld$
C. true
D. java.lang
E. Public
F. 1980_s
G. _Q2_
A

B, E, G

  • Single underscore is not allowed.
  • It cannot be a java keyword (case sensitive)
  • Dot is not allowed
  • It cannot start with a number
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an instance initializer?

A

A code block that is inside a class but outside a method.

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

What is the output?

public class Egg {
    public Egg() {
        number = 5;
    }
    public static void main(String[] args) {
        Egg egg = new Egg();
        System.out.println(egg.number);
    }
    private int number = 3;
    { number = 4; }
}
A

The output is 5.

Fields and instance initializers are run in the order in which they appear in the file.
The constructor runs after all fields and instance initializer blocks have run.

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

Java has 8 built in data types. Which are they?

A

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

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

What is the size of the primitive type ‘boolean’?

A

1 bit

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

What is the size of the primitive type ‘byte’?

A

8 bits / 1 byte

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

What is the size of the primitive type ‘short’?

A

16 bits / 2 bytes

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

What is the size of the primitive type ‘int’?

A

32 bits / 4 bytes

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

What is the size of the primitive type ‘long’?

A

64 bits / 8 bytes

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

What is the size of the primitive type ‘float’?

A

32 bit / 4 bytes

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

What is the size of the primitive type ‘double’?

A

64 bits / 8 bytes

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

What is the size of the primitive type ‘char’?

A

16 bits / 2 bytes

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

True or false

A float does not require the letter ‘f’ following the number in Java.

A

False.

The letter ‘f’ is required so Java knows its a float.

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

A byte can hold a value from … to …

A

-128 to 127

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

What is the difference between a short and a char?

A

Short and char are closely related, as both as stored as integral types with 16 bit length. The difference is that short is signed (which means it can hold positive and negative numbers) and char is unsigned (which means it can only hold positive numbers, including 0).

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

When writing a numeric literal, what data type does java assume you are writing?

A

An int. This is why we need to specify an ‘L’ after the number if it should be a long.

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

Java allows other formats other than the decimal system to write numbers. Name the following formats:

  1. 017
  2. 0xFF
  3. 0b10
A
  1. Octal
  2. Hexadecimal
  3. Binary
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Explain why the following does or does not compile:

  1. 1000_.00
  2. _1000.00
  3. 1_00_0.0_0
  4. 1000.00_
  5. 1_____2
A
  1. Does not compile. Underscore cannot be next to decimal point.
  2. Does not compile. Underscore cannot be at the start.
  3. Compiles fine.
  4. Does not compile. Underscore cannot be at the end.
  5. Compiles fine.
19
Q

Which if these can compile?

  • int value = null;
  • String value = null;
A

String value = null compiles fine.

A primitive data type cannot be assigned null.

20
Q

Does this code compile or not? Why?

String reference = “Hello”;
int len = reference.length();
int len2 = len.length();

A

Reference types can be used to call methods but primitives to not have methods declared on them. Thus len.length() is not valid.

21
Q

An identifier is the name of a variable, method, class, interface, or package. There are 4 rules for legal identifiers. What are they?

A
  • Identifiers must begin with a letter, a $ symbol, or a _ symbol.
  • Identifiers can include numbers but not start with them.
  • A single underscore _ is not allowed
  • A java reserved word is not allowed (case sensitive)
22
Q

Whcih of these identifiers are legal?

  1. someidentifier
  2. hollywood@vine
  3. __SomeIdentifier$
  4. 3DPointClass
A

1 and 3.

@ is not allowed and an identifier cannot start with a number.

23
Q

Which of the following are legal/illegal declarations? why?

  1. boolean b1, b2;
  2. String s1 = “1”, s2;
  3. double d1, double d2;
  4. int i1; int i2;
  5. int i3; i4;
A
  1. legal. It declares two variables without initializing them.
  2. legal. It declares two variables and initailized only one of them.
  3. not legal. they must share the same type declaration and not repeat it: ‘double d1, d2’ is legal.
  4. legal. Although they are seperate statements (;).
  5. not legal. The second statement doesn’t have a type.
24
Q

What is a local variable?

A

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

25
Q

Why do local variables need to be initialized before use?

A

They don’t have a default value.

26
Q

True or false: the following code is invalid.

public void findAnswer( boolean check) { }

public void checkAnswer() {
boolean value;
findAnswer(value);
}

A

True.

It is invalid because it tries to use a variable that is not initialized. Variables passed to a constructor or method need to be initialized.

27
Q

Of the following variable types, which ones do or do not require to be initalized by the programmer before use?

  • local variables
  • instance variables
  • class variables (static variables)
A
  • Local variables need to be initialized by the programmer

- Instance and class variables are initialized implicitely.

28
Q

What is the default initialization value of a boolean?

A

false

29
Q

What is the default initialization value of a byte/short/int/long?

A

0 (zero)

30
Q

What is the default initialization value of a float/double?

A

0.0 (zero dot zero)

31
Q

What is the default initialization value of a char?

A

‘\u0000’ (NUL)

32
Q

What is the default initialization value of an object reference?

A

null

33
Q

Does the following code compile?

public class VarKeyword {
    var greeting = "hello";
}
A

No.

Var can only be used for local variables. The variable greeting is an instance variable.

34
Q

Why does the following code not compile?

public void test() {
    var number = 7;
    number = 4;
    number = "five";
}
A

On the line with the var keyword, the compiler determines that we want an int variable, because it is assigned a number. Assigning a string to an int variable causes a compiler error.

35
Q

Why does the following code not compile?

public void test() {
    var number = (short) 10;
    number = (byte) 5;
    number = 1_000_000;
}
A

The last line does not compile because the type of var is short. a byte fits inside a short so that line does compile.

36
Q

Does the following code compile?

public void compileTest(boolean check) {
    var question;
    question = 1;
    var answer;
    if (check) {
        answer = 2;
    else {
        answer = 3;
    }
    System.out.println(answer);
}
A

It does not compile because for a local variable type inference (var), the compiler looks only at the line with the decleration to determine the type of var. Since question and answer are not assigned values on the same line where they are defined, the compiler does not know what to make of them.

37
Q

Does Java allow var in multiple variable declarations?

example:

public void test() {
    var a = 2, b = 3;
}
A

Java does not allow var in multiple declarations.

38
Q

Does the following local variable compile?

var test = null;

A

No, because the compiler cannot determine the type in the assignment.

39
Q

Does the following code compile?

public void test() {
    var n = "hello";
    n = null;
    var m = 1;
    m = null;
}
A

It does not compile because the type for m is an int and a primitive int cannot be assigned null.
n = null compiles fine.

40
Q

Does the following local variable compile?

var test = (String) null;

A

Since the type is provided, this line compiles fine. The variable test will be of type String.

41
Q

Does the following code compile?

public int addition(var a, var b) {
return a + b;
}

A

It does not compile because a and b are method parameters. Var can only be used as local variables.

42
Q

Is var a reserved word within Java?

A

No. Var is not a reserved word and allowed to be used as an identifier.

It is considered to be a reserved type name, meaning it cannot be used to define a type, such as a class, interface or enum.

43
Q

What does the command System.gc() do?

A

It suggests that the JVM kick off garbage collection. The JVM may perform garbage collection at that moment, or it might be busy and decide not to. The command is not guaranteed to make the garbage collection do anything.

44
Q

Which of the following object(s) are eligible for garbage collection?

public static void main(String[] args) {
    String one, two;
    one = new String("a");
    two = new String("b");
    one = two;
    String three = one;
    one = null;
}
A

The String object “a” is eligible for garbage collection because no variable is pointing to that object.