Ch12 Java Fundmentals 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

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
5
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
6
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
7
Q

Which of the following are allowed in an Enum object?

constructor
field
method

A

All of them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
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
9
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
10
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
11
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
12
Q

What is an inner class?

A

An inner class is a non-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
13
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
14
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
15
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
16
Q

What 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
17
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
18
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
19
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
20
Q

Which access modifiers are allowed for a local class?

A

None.

21
Q

What is this type of class called?

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

A

An anonymous class

22
Q

What were anonymous classes frequently used for?

A

asynchronous tasks and event handlers.

23
Q

Which access modifiers are allowed for an anonymous class?

A

None.

24
Q

Which access modifiers are allowed for a static nested class?

A

All.

25
Q

Are static nested classes allowed to be abstract?

A

Yes.

26
Q

Are local classes allowed to be abstract?

A

Yes.

27
Q

Are inner classes allowed to be final?

A

Yes.

28
Q

Are static nested classes allowed to be final?

A

Yes.

29
Q

Are local classes allowed to be final?

A

Yes.

30
Q

Are anonymous classes allowed to be final?

A

No.

31
Q

Which inner class type is allowed to have static methods?

A

Static nested class

32
Q

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

A

Static nested class

33
Q

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

A

Anonymous class

34
Q

What are the Default interface definition rules?

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

Can default methods be marked static?

A

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

36
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();

37
Q

Are interface methods allowed to be static?

A

Yes.

38
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.
39
Q

Are interface methods allowed to be private?

A

Since Java 9, yes.

40
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.
41
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.
42
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.

43
Q

Is the following lambda valid?

(var num) -> 1

A

Yes

44
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.

45
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.

46
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

47
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();

48
Q

Which statements concerning the relation between a non-static inner class and its outer class instances are true?

A. Member variables of the outer instance are always accessible to inner instances, regardless of their accessibility modifiers.
B. Member variables of the outer instance can always be referred to using only the variable name within the inner instance.
C. More than one inner instance can be associated with the same outer instance.
D. An inner class can extend its outer class.
E. A final outer class cannot have any inner classes.

A

A, C, D

B: This is possible only if that variable is not shadowed by another variable in inner class.

E: There is no such rule.

49
Q

Which of the following options can be a part of a correct inner class declaration or a combined declaration and instance initialization ?

A. private class C { }
B. new SimpleInterface() { }
C. new ComplexInterface(x) { }
D. private final abstract class C { }
E. new ComplexClass() implements SimpleInterface { }

A

A, B

C: You cannot pass parameters when you implement an interface by an anonymous class.
D: A final class can never be abstract.
E: ‘implements’ part comes only in class definition not in instantiation.