Chapter 12: Java fundamentals Flashcards

1
Q

Is the following code valid?

private void doSomething() {
final int x;
}

A

Yes.

we do not need to assign a value when a final variable is declared. The rule is that it must be assigned a value before it can be used.

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

Is the following code valid?

final StringBuilder s = new StringBuilder();
s.append("test");
A

Yes.

The object reference is constant and never changes. The state of the object, however, can still change.

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

Is the following code valid?

public class Bear {
    static final int age;
    { age = 8; }
}
A

The code does not compile because the age variable is static, while the initializer is not.

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

What does final mean when applied to a method?

A

The method cannot be overridden.

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

What does final mean when applied to a reference variable?

A

The variable cannot be assigned more than once. The object associated with it can still be modified.

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

What does final mean when applied to a class?

A

The class cannot be extended.

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

Given the following enum

public enum Season {
WINTER, SPRING, SUMMER, FALL
}

Does the following code throw an exception?

Season s = Season.valueOf(“summer”);

A

Yes, it will throw an IllegalArgumentException.

It is case sensitive.

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

Given the following enum

public enum Season {
WINTER, SPRING, SUMMER, FALL
}

How can we loop over the set of enums and print the names?

A

Using Season.values() method, which returns an array of all the values, and the name() method.

for (Season season : Season.values()) {
System.out.println(season.name());
}

In addition we can print each order number using ordinal().

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

Is it possible to extend an enum?

A

No, an enum cannot be extended.

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

Does the following code compile?

Season summer = Season.SUMMER;
switch(summer) {
    case Season.SUMMER:
        // Do something
        break;
    default:
        // Do something
        break;
}
A

It doesn’t because the enum type (Season) is implicitely added by Java in the case statement. For that reason we only have to write the value (SUMMER) instead of Season.SUMMER.

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

Which of the following are allowed in an Enum object?

  • constructor
  • field
  • method
A

All of them.

page 503

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

Does the following code compile?

public enum ConstructorEnum {
    public ConstructorEnum() { }
}
A

No.

The constructor of an enum is implicitely private. This is reasonable since an enum can’t be extended and the constructors can be called only within the enum itself

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

Given the following enum: Season, the value: SUMMER, and the method: print().

How do we call the print method from outside the enum?

A

Season.SUMMER.print();

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

When does Java call the constructor of an enum?

A

When the enum is first called.

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

A nested class can come in what flavours?

A
  • Inner class
  • Static nested class
  • Local class
  • Anonymous class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an inner class?

A

An inner class is a non-static stype defined at the member level of a class.

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

What is a static nested class?

A

A static type defined at the member level of a class.

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

What is a local class?

A

A class defined within a method body.

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

What is an anonymous class?

A

A special case of a local class that does not have a name.

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

Wat are the properties of an inner class?

A
  • Can be declared public, protected, package-private or private
  • Can extend any class and implement interfaces
  • Can be marked abstract or final
  • Cannot declare static fields or methods, except for static final fields
  • Can access members of the outer class including private members
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Given top level class Outer and nested class Inner, how do you instantiate a nested class from outside the Outer class?

A

Outer.Inner inner = new Outer().new Inner();

or:

Outer outer = new Outer();
Inner inner = outer.new Inner();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

If Inner and Outer both define a variable x, how does inner acces both variables?

A
  • For x in Inner: x or this.x

- For x in Outer: Outer.this.x

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

What is the difference between a nested class and a static nested class?

A

For a static nested class, you do not need an instance of the enclosing class to access it (it acts like a static member field).

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

Can local classes access all local variables?

A

No, they can only access final or effectively final variables.

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

Which access modifiers are allowed for a local class?

A

None.

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

What is this type of class called?

SaleTodayOnly sale = new SaleTodayOnly() {
    int dollarsOff() { return 3; }
};
A

An anonymous class

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

What were anonymous classes frequently used for?

A

asynchronous tasks and event handlers.

28
Q

Which access modifiers are allowed for an anonymous class?

A

None.

29
Q

Which access modifiers are allowed for an Inner class?

A

All.

30
Q

Which access modifiers are allowed for a static nested class?

A

All.

31
Q

Are inner classes allowed to be abstract?

A

Yes.

32
Q

Are static nested classes allowed to be abstract?

A

Yes.

33
Q

Are local classes allowed to be abstract?

A

Yes.

34
Q

Are anonymous classes allowed to be abstract?

A

No.

35
Q

Are inner classes allowed to be final?

A

Yes.

36
Q

Are static nested classes allowed to be final?

A

Yes.

37
Q

Are local classes allowed to be final?

A

Yes.

38
Q

Are anonymous classes allowed to be final?

A

No.

39
Q

Which inner class type is allowed to have static methods?

A

Static nested class

40
Q

Which inner class type is allowed to have static variables that are not final?

A

Static nested class

41
Q

Which inner class type can extend or implement exactly one class/interface?

A

Anonymous class

42
Q

What is a default method?

A

A default method is a method defined in an interface with the default keyword and includes a method body.

43
Q

What are the Default interface definition rules?

A
  1. A default method may be declared only within an interface
  2. A default method must be marked with the default keyword and include a method body.
  3. A default method is assumed to be public.
  4. A default method cannot be marked abstract, final, or static.
  5. A default method may be overridden by a class that implements the interface.
  6. If a class inherits two or more default methods with the same method signature, then the class must override the method.
44
Q

Where can default methods be defined?

A

In interfaces only.

45
Q

Can default methods be marked static?

A

No. They are associated with the instance of the class implementing the interface.

46
Q

Given the following code, what will be the output?

public interface Walk {
default int getSpeed() { return 1; }
}

public interface Run {
default int getSpeed() { return 3; }
}

public class Cat implements Walk, Run {
    public static void main(String[] args) {
        System.out.println(new Cat().getSpeed());
    }
}
A

The code will not compile because Java doesn’t know which default method to call.

47
Q

Given the following code, what will be the output?

public interface Walk {
default int getSpeed() { return 1; }
}

public interface Run {
default int getSpeed() { return 3; }
}

public class Cat implements Walk, Run {
    public int getSpeed() { return 2; }
    public static void main(String[] args) {
        System.out.println(new Cat().getSpeed());
    }
}
A

It will print 2 as the method in Cat overrides both default methods.

48
Q

Given the following code, how do we print the default getSpeed() in the Walk interface, from the Cat class?

public interface Walk {
default int getSpeed() { return 1; }
}

public interface Run {
default int getSpeed() { return 3; }
}

public class Cat implements Walk, Run {
    public int getSpeed() { return 2; }
    // ...
}
A

using Walk.super.getSpeed();

49
Q

Are interface methods allowed to be static?

A

Yes.

50
Q

What are the Static interface Method Definition rules?

A
  1. A static method must be marked with the static keyword and include a method body.
  2. A static method without an access modifier is assumed to be public
  3. A static method cannot be marked abstract or final
  4. A static method is not inherited and cannot be accessed in a class implementing the interface without a reference to the interface name.
51
Q

Are interface methods allowed to be private?

A

Since Java 9, yes.

52
Q

What are the Private Interface Method Definition rules?

A
  1. A private interface method must be marked with the private modifier and include a method body.
  2. A private interface method may be called only by default and private (non-static) methods within the interface definition.
53
Q

What are the Private Static Interface Method Definition rules?

A
  1. A private static method must be marked with the private and static modifiers and include a body.
  2. A private static interface method may be called only by other methods within the interface definition.
54
Q

What is the one exception to the single abstract method rule (functional interface)?

A

If a functional interface includes an abstract method with the same signature as a public method found in Object, then those methods do not count toward the single abstract method test.

This is because any class that implements the interface will inherit from Object.

55
Q

Is the following code a functional interface?

public interface Soar {
abstract String toString();
}

A

It is not because toString is a public method in Object and therefore doesn’t count towards the single abstract method test;

56
Q

Is the following code a functional interface?

public interface Soar {
abstract String toString();
}

A

It is not because toString is a public method in Object and therefore doesn’t count towards the single abstract method test;

57
Q

Is the following code a functional interface?

public interface Soar {
    String toString();
    void dive();
}
A

Yes, because void is the only abstract method which is not defined in Object.

58
Q

What is the type of the lambda parameter?

Predicate< Integer> p = x -> true;

A

Integer

59
Q

Is the following lambda valid?

(var num) -> 1

A

Yes

60
Q

Is the following lambda valid?

var num -> 1

A

No. When the type is added (like var in this case) you must use parenthesis.

61
Q

Is the following lambda valid?

(String x, var y, Integer z) -> true

A

No. In lambda expressions, when one of the parameters is of type var, all parameters must be var too.

62
Q

Is the following lambda valid?

(var x, var y, var z) -> { int c = 0; return 5; }

A

Yes, we can use local variables in a code block.

63
Q

Is the following lambda valid?

(a, b) -> { int a = 0; return 5; }

A

No. Variable a was already defined and is redeclared in the code block.

64
Q

Does the following anonymous class compile?

new Runnable() {
    static final int x = 0;
    static int y = 0;
@Override
public void run() {...} };
A

Anonymous classes cannot have any static members except for those that are constant.

This code won’t compile

65
Q

Does the following code compile?

class Polygon {
   public void display() {
      System.out.println("Inside the Polygon class");
   }
}
class AnonymousDemo {
   public void createClass() {
      Polygon p1 = new Polygon() {
         public void display() {
            super.display();
         }
      };
      p1.display();
   }
}
class Main {
   public static void main(String[] args) {
       AnonymousDemo an = new AnonymousDemo();
       an.createClass();
   }
}
A
66
Q

Write an anonymous class at the dots (…) and call the display method to print something.

interface Polygon {
public void display();
}

class AnonymousDemo {
   public void createClass() {
      ...
   }
}
class Main {
   public static void main(String[] args) {
      AnonymousDemo an = new AnonymousDemo();
      an.createClass();
   }
}
A
Polygon p1 = new Polygon() {
         public void display() {
            System.out.println("Inside an anonymous class.");
         }
      };
      p1.display();