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
How do you implement Cloneable for a class with mutable class type attributes?
Invoke clone() of the base class, then reset the values of mutable instance variables by invoking their respective clone methods.
26
Cloneable can be implemented for all classes with mutable attributes.
False - Cloneable can only be properly implemented if the interface is already implemented for the mutable attributes.
27
Inner classes must be defined at the beginning of the outer class's definition.
False - its location is irrelevant.
28
The name of an inner class may be reused for something outside the outer class definition.
True - it is local to the outer class.
29
The outer class has access to the inner class's private methods/attributes.
True.
30
The inner class has access to the outer class's private methods/attributes.
True.
31
The inner class should always be marked private.
False - not if the inner class is used outside the outer class.
32
It is legal to invoke a (non-static) method of the inner class from the outer class, on an object of the outer class.
False, an object of the inner class must be used as a a calling object.
33
Within the definition of the inner/outer classes, the modifiers public and private are equivalent.
True.
34
When a class with an inner class is compiled, 2 separate .class files are produced.
True.
35
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?
C1.class and C1$C2.class.
36
When must an inner class be static? (2)
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
Can instance variables of the outer class be referenced by a static inner class?
No.
38
Can methods of the outer class be referenced by a static inner class?
Only static methods of the outer class can be invoked.
39
Can an outer class reference static methods/variables of its static inner class?
Yes, if their invocation is prefaced with the name of the inner class and a dot.
40
An inner class marked public can be used outside the outer class.
True.
41
An instance of a non static inner class can be created before creating an instance of the outer class.
False - It must be created using an object of the outer class.
42
Create an instance of an inner class C2, if the outer class is C1 (use default constructors).
C1 c1 = new C1(); C1.C2 c2 = c1.new C2();
43
An instance of the inner class can invoke methods of the outer class.
False - it can only invoke methods from the inner class.
44
You can create instances of static inner classes.
True.
45
Write code that would create an instance of a static inner class (C2), if the outer class is C1.
C1.C2 c2object = new C1.C2();
46
The following is valid: innerObject.InnerNonstaticMethod();
True.
47
The following is valid: innerStaticObject.InnerStaticMethod();
True.
48
The following is valid: OuterClass.InnerStaticClass.InnerNonstaticMethod();
False.
49
The following is valid: OuterClass.InnerStaticClass.InnerStaticMethod();
True.
50
The following is valid: InnerStaticObject.OuterMethod();
False.
51
Outer non-static methods can be called from a static inner class's methods.
False.
52
Outer static methods can be called from a static inner class's methods.
True.
53
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?
The inner class method.
54
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?
OuterClassName.this.methodName();
55
Can you nest classes within already nested classes?
Yes.
56
Create an instance of class C, if class C is nested in class B, and class B is nested in class A.
A a1 = new A(); A.B b1 = a1.new B(); A.B.C c1 = b1.new C();
57
An outer class can be a derived class.
True.
58
An inner class can be a derived class.
True.
59
If class A is nested with class B, and class C extends class A, will class C automatically have B as an inner class?
Yes.
60
If class A is nested with class B, and class C extends class A, can class C override the nested class B?
No.
61
Instantiate an instance of an anonymous class that extends the Car class, whose only (additional) attribute is (int) numberOfWheels.
Car c1 = new Car() { private int numberOfWheels; };
62
The type assigned to an anonymous class must already exist.
True.
63
The type assigned to an anonymous class can be an interface.
True - in fact it most often is.