Chapter 3: Assignments Flashcards

1
Q

Where do instance variables, local variables and objects live?

A

Heap: instance variables and objects
Stack: local variables

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

What are the three integer literals?

A

Decimal literal - base10 - (0-9)
int length = 3; //3

Octal literal - base8 - (0-7)
Prefixed with a 0
int length = 03; //3
int length = 010; //8
int length = 077; // 63
int length = 0100; // 64
Hexadecimal - base16 (0-F)
Prefixed with 0x or 0X, not case sensitive
int length = 0xF; // 15
int length = 0x01; //16
int length = 0xDEADCAFE; // Valid
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to assign the three integer literals to a long?

A

Postfix with a l or L.

long length = 1L;
long length = 0xFl;
long length = 0100L;

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

What is wrong?

float f = 10.50;

A

Floating-point literals are double by default. Prefix with F or f.

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

What is wrong?

double d = 2310293.231d;

A

Nothing, Floating-point literals are double by default, optionally they can be post-fixed with a d or D.

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

What assignments are inccorect?

  1. char d = -12;
  2. char d = (char) -98;
  3. char f = 70000;
  4. char c = ‘"’
A
  1. Char is a 16 bit unsigned integer, needs a cast because loss of percision; 2 is correct, with cast.
  2. Does not fit in a 16 bit integer.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What assignments are inccorect?

  1. byte a = 3;
  2. byte b = 8;
  3. byte c = a + b;
A
  1. expressions containing only variables that are ints or smaller are evaluated to an int. An int cannot be assigned to a byte without a cast.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the return type:

  1. byte a = 1; byte b = 2; a + b
  2. byte a = 1; long b = 2; a + b;
  3. short a = 1; byte b = 2; a + b;
  4. long a = 1; float f = 2F; a + b;
A
  1. int
  2. long
  3. int
  4. float

Anything involing only smaller or int variables will result in int. (1 AND 3) Otherwise the result type will be the largest used holder (2 AND 4).

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

When do you need to explicitly cast for primitives?

A

When narrowing, putting something into a smaller holder.

short a = 1;
byte b = (byte) a;
long l = a; // No cast needed, larger container.
double d = l; // No cast needed, larger container.

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

What will be the result?
long l = 130L;
byte b = (byte) l;

A

130 does not fit in a byte. So the bits will be choped of from the left side. If the first bit (the sign bit) is now a 1 it will turn it into a negative number: -126.

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

Name the 4 variables scopes based on the from shortest life span to the longest life span.

A
  1. Block variables, as long as the code-block executes.
  2. Local variables, as long as the method executes.
  3. Instance variables, as long as the object lives.
  4. Static variables, as long as the class is loaded in the JVM.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What will be printed?
int[] numbers = new int[10};
System.out.println(numbers[0]);

A

0

The array element get their default value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
What is wrong?
public class Bar {
public static void main(String[] args) {
int days;
int year;
System.out.println("The year is: " + year);
}
}
A

Local primitive is not initialized. Local variables do not get their default value if not assigned. Its fine to leave days unitialized since we do not use it.

Object also do not get a default value. So you need to explicitly set a null value if desired.
Object aObject = null;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
What will be printed?
class Bar {
   public int myIntValue = 4;
   public void doStuff(int myIntValue) {
       myIntValue++;
   }
}
Bar b = new Bar();
b.doStuff(b.myIntValue);
System.out.println(b.myIntValue);
A

The instance variable myIntValue is untouched since the method doStuff shadows the myIntValue variable reference.

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

What is wrong?
String [] arrayName []; // 1
String[5] strings; // 2

A
  1. Nothing, this is a valid multi-dimensional array declaration.
  2. Length of array cannot be set in the declaration.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is wrong?
int[] testScores;
testScores = {1,2,3}

A

Testscores is already initialized to a null value. You need to use the new keyword like:
testScores = new int[] {1,2,3};

17
Q

What primiteves can a primitive array hold?

A

Any primitive that can be implicitly promoted to the decalered array type. For example: short, byte, char can all be assigned to an int array type.

18
Q

What objects can a object array hold?

A

All objects that pass de IS-A test for the array type (inheritance: extends or implements).

19
Q
What assignments will fail:
int[] a;
char[] c = new char[3];
a = c; // 1
Cars[] cars;
Lexus[] lexuss = new Lexus[3];
Dog[] dogs = new Dog[3];
cars = dogs; // 2
cars = lexuss; //3
int[][] chart = new int[5][];
a = chart; // 4
A

1, 2, 4

  1. Primitive arrays can only be assigned to the same type array reference.
  2. Object arrays can only be assignedn to the same type or a super type.
  3. You cannot assign an array with a different diemsnion.
20
Q
What will be printend and in what order?
public class Blocks extends SuperBlocks{
    static { System.out.println("Static Blocks"); }
    { System.out.println("Blocks1"); }
    { System.out.println("Blocks2"); }
    Blocks() { System.out.println("Construct Blocks"); }
    public static void main(String[] args) { new Blocks(); }
}
class SuperBlocks {
    static { System.out.println("Static SuperBlocks"); }
    { System.out.println("SuperBlocks1"); }
    SuperBlocks() { System.out.println("Construct SuperBlocks"); }
}
A
Static SuperBlocks
Static Blocks
SuperBlocks1
Construct SuperBlocks
Blocks1
Blocks2
Construct Blocks
  1. Super class static blocks
  2. Class static blocks
  3. Super class code blocks
  4. Constructor Super class
  5. Class code blocks.
  6. Constructor class.
21
Q

What constructors does a Wrapper class have (Integer, Character etc.)

A

The primitve type or a String representation.

Except for Character which only has a constructor for the primitive and float wich can also take a double,

22
Q
What is the result of the following:
Integer i = new Integer("GeenInteger"); // 1
Boolean b = new Boolean("GeenBoolean"); // 2
A
// 1 Throws NumberFormatException
// 2 Anything that does not equals to true will result in a false boolean.
23
Q

What do the parseXxx() methods and valueOf() methods do?

A

Wrapper.parseXxx(): parse a String to a primitive. Optionaly take a radix argument, to define the base.
valueOf(): parse a String to a wrapper object.
Optionaly take a radix argument, to define the base.

Long.parseLong(“101010”, 2); // Results in 42.

24
Q

What is the xxxValue method?

A

A method on the wrapper objects for converting the objects to a primitve value.,

25
Q

What is boxing and unboxing?

A
Boxing: auto wrapping a primitive in a wrapper object. 
Integer y = new Integer(10); y++;
Unboxing: auto unwrapping a wrapper object to a primitive.
Integer y = 10;
26
Q

How many objects are created?
Integer x = 22;
x++;

A
  1. Wrapper object are immutable, so a new object is created when incremented.
27
Q
What is the result?
public class Wrappers {
    static Integer x;
    public static void main(String[] args) {
        Wrappers wrappers = new Wrappers();
        wrappers.doStuff(x);
    }
private void doStuff(int x) {
    System.out.println("Value is " + x);
} }
A

NullPointerException. x cannot be unboxed to a primitve because the value is null.

28
Q

Overloaded methods: what happens if a exact match isn’t found for the arguments provided?

A

The JVM chooses the method with the smallest argument that is wider than the given parameters.

byte b = 2;
void doStuff(int x) {}
.doStuff(b);
29
Q

What is the order to determince which overloaded method to execute?

A
  1. Widening
  2. Boxing
  3. Var-args
30
Q
What is wrong?
public class FooBar {
    public static void main(String[] args) {
        new FooBar().test(new Integer(5));
    }
    void test(Long x) {}
}
A

Widening does not work with wrapper classes.

31
Q

What is widening?

A

Putting a primitive or object in to a larger holder.

int x = 1;
void doStuff(long l) {}
doStuff(x);
Dog d = new Dog();
void doStuff(Animal a);
doStuff(d);
32
Q

What are the rules for overloading regarding widening, boxing and var-args?

A
  1. Primitives use the smallest method argument possible
  2. Boxing and var-args can only be use individually
  3. You cannot widen wrapper types
  4. You cannot widen and then box (an int can’t become a Long).
  5. You can box and then widen (an int can become an Object, via Integer).
  6. You can combine var-args with either widening or boxing.
33
Q

Using == wrappers created throug boxing is tricky, why?

A

Those with small values (lower than 127) will equal to true if compared using ==. Larger values don’t.