Interfaces & Inner Classes Flashcards

1
Q

An interface is not a class.

A

True.

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

Interfaces contain method definitions.

A

False - they usually only contain method headings.

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

Interfaces can contain constant definitions.

A

True.

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

Interface can never give a definition to its own method.

A

False - default methods (as of Java 8) have definitions. But generally, no.

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

Interfaces solve which Java restriction?

A

Java’s restriction on multiple inheritance.

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

An interface and all of its method headings should be declared public.

A

True.

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

An interface can give private/protected/package access to methods in specific circumstances.

A

False.

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

When a class implements an interface, that class can decide to make specific methods private.

A

False - they must all remain public.

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

A method may be written with a parameter of an interface type.

A

True - this parameter accepts any class that implements the interface.

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

Method headings in interfaces must end in a semicolon.

A

True.

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

Write a class header for class “C” that implements 3 interfaces (I1, I2, and I3).

A

public class C implements I1, I2, I3 {…}

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

How many of the methods listed in the interface(s) definition(s) must be defined by the class implementing them?

A

All methods listed must be implemented.

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

Abstract classes cannot implement interfaces.

A

False - they may implement 1+ interfaces.

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

Abstract classes must define all methods listed in the interface(s) they implement.

A

False - abstract classes do not need to provide implementation - this is left for the concrete class to do.

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

Interfaces can be derived from other interfaces.

A

True.

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

Write the header for an Interface “I1” that extends another interface, “I2”.

A

public interface I1 extends I2 {…}

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

Variables defined in an interface must be public, static, and final.

A

True.

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

The public, static, and final modifiers must precede all variables defined in an interface.

A

False, they may be omitted since the compiler understands that they must already be public, static, and final.

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

What are the 2 possible cases of inconsistent interfaces?

A

Constants with the same name, but different values, and methods with the same name, but with different return types.

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

The Serializable interface is completely empty.

A

True.

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

The Cloneable interface is completely empty.

A

True.

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

Object.clone() does a bit-by-bit copy of the object’s data in storage. This is adequate behaviour for what types of Objects?

A

For immutable class types.

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

What Exception is thrown when the Object’s clone() method is called on an instance that does not implement cloneable?

A

CloneNotSupportedException

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

CloneNotSupportedException is a checked exception.

A

True.

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

How do you implement Cloneable for a class with mutable class type attributes?

A

Invoke clone() of the base class, then reset the values of mutable instance variables by invoking their respective clone methods.

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

Cloneable can be implemented for all classes with mutable attributes.

A

False - Cloneable can only be properly implemented if the interface is already implemented for the mutable attributes.

27
Q

Inner classes must be defined at the beginning of the outer class’s definition.

A

False - its location is irrelevant.

28
Q

The name of an inner class may be reused for something outside the outer class definition.

A

True - it is local to the outer class.

29
Q

The outer class has access to the inner class’s private methods/attributes.

A

True.

30
Q

The inner class has access to the outer class’s private methods/attributes.

A

True.

31
Q

The inner class should always be marked private.

A

False - not if the inner class is used outside the outer class.

32
Q

It is legal to invoke a (non-static) method of the inner class from the outer class, on an object of the outer class.

A

False, an object of the inner class must be used as a a calling object.

33
Q

Within the definition of the inner/outer classes, the modifiers public and private are equivalent.

A

True.

34
Q

When a class with an inner class is compiled, 2 separate .class files are produced.

A

True.

35
Q

If the outer class name is C1, and the inner class name is C2, then what would be the names of the .class files following compilation?

A

C1.class and C1$C2.class.

36
Q

When must an inner class be static? (2)

A

If an object of the inner class is created within a static method of the outer class, and if the inner class must have static members.

37
Q

Can instance variables of the outer class be referenced by a static inner class?

A

No.

38
Q

Can methods of the outer class be referenced by a static inner class?

A

Only static methods of the outer class can be invoked.

39
Q

Can an outer class reference static methods/variables of its static inner class?

A

Yes, if their invocation is prefaced with the name of the inner class and a dot.

40
Q

An inner class marked public can be used outside the outer class.

A

True.

41
Q

An instance of a non static inner class can be created before creating an instance of the outer class.

A

False - It must be created using an object of the outer class.

42
Q

Create an instance of an inner class C2, if the outer class is C1 (use default constructors).

A

C1 c1 = new C1();
C1.C2 c2 = c1.new C2();

43
Q

An instance of the inner class can invoke methods of the outer class.

A

False - it can only invoke methods from the inner class.

44
Q

You can create instances of static inner classes.

A

True.

45
Q

Write code that would create an instance of a static inner class (C2), if the outer class is C1.

A

C1.C2 c2object = new C1.C2();

46
Q

The following is valid:
innerObject.InnerNonstaticMethod();

A

True.

47
Q

The following is valid:
innerStaticObject.InnerStaticMethod();

A

True.

48
Q

The following is valid:
OuterClass.InnerStaticClass.InnerNonstaticMethod();

A

False.

49
Q

The following is valid:
OuterClass.InnerStaticClass.InnerStaticMethod();

A

True.

50
Q

The following is valid:
InnerStaticObject.OuterMethod();

A

False.

51
Q

Outer non-static methods can be called from a static inner class’s methods.

A

False.

52
Q

Outer static methods can be called from a static inner class’s methods.

A

True.

53
Q

If a method is invoked in the inner class, and there exists a method by that name in both the outer and inner classes, which is invoked?

A

The inner class method.

54
Q

If there exists a method in both the outer and inner classes with the same name, how would you invoke the outer class’s method?

A

OuterClassName.this.methodName();

55
Q

Can you nest classes within already nested classes?

A

Yes.

56
Q

Create an instance of class C, if class C is nested in class B, and class B is nested in class A.

A

A a1 = new A();
A.B b1 = a1.new B();
A.B.C c1 = b1.new C();

57
Q

An outer class can be a derived class.

A

True.

58
Q

An inner class can be a derived class.

A

True.

59
Q

If class A is nested with class B, and class C extends class A, will class C automatically have B as an inner class?

A

Yes.

60
Q

If class A is nested with class B, and class C extends class A, can class C override the nested class B?

A

No.

61
Q

Instantiate an instance of an anonymous class that extends the Car class, whose only (additional) attribute is (int) numberOfWheels.

A

Car c1 = new Car() {
private int numberOfWheels;
};

62
Q

The type assigned to an anonymous class must already exist.

A

True.

63
Q

The type assigned to an anonymous class can be an interface.

A

True - in fact it most often is.