Chapter 6: Class Design Flashcards

1
Q

which code can be inserted to have the code print 2?

public class BirdSeed {
private int numBags;
boolean call;

public BirdSeed() {
// Line 1
call = false;
// Line 2
}

public BirdSeed(int numBags) { 
this.numBags = numBags 
}

main() { 
var seed = new BirdSeed();
print( seed.numBags);
} }
A

Replace Line 1 with this(2)

Wrong options for both lines:
BirdSeed(2); because you can’t call a constructor like that without using the new keyword.
new BirdSeed(2): still wrong because that makes a new object rather than setting the fields in this one. The result is the code prints 0 not 2

(because in main you make an object and accesses that object’s variable. we don’t want that object making a new object with the desired fields, that doesn’t help us).

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

Which modifier pairs can be used together in a method declaration?

static and final
private and final

A

both of these

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

Which statements about methods are true?

overridden methods must have the same signature
hidden methods must have the same signature

A

both of these

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

class X {
public X ( int i) { } }
public class Y extends X {
public Y() { }
main method()
}

A

won’t compile - Y is using a no-args constructor which it’s parent class does not have

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

What completes the constructor so the code prints out 50?

class X { int i }
public class Y extends X {
int i;
public Y (int i) { // code here }
main method() { 
X x = new Y(50);
print x.i; } 
}
A

super.i = i;

an instance variable with the same name as an inherited instance variable is hidden, not overridden. This means both variables exist, and the one that is used depends on the location and reference type.

because the main method uses a reference type of X to access the ` i ` variable, the variable in X, not Y, has to be set to 50.

So update the variable in the parent class using super

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

Which of the following declare immutable classes?

public final class A { private final int x; }
public class B { private int x = 1;}
public class C { private final int x = 1;}
public final class D { }
public final class E {
private final Object x = new Object(); }

A

D and E

A doesn’t compile, x is final without being initialised when declared (either in-line or { } or in constructor)

B and C aren’t marked final, which means a subclass could extend them and add mutable fields

D and E are marked final and have only private final members

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

class A {
protected void m1(long input) { print “A-long” }
void m1(int input) { print “A-void-int” } }

public class B extends A {
protected void m1(int input) { print “B-protected-void-int” }

main() {
A a = new B();
a.m1( (short)4);
a.m1(4);
a.m1(5L)
} }

A

B-protected-void-int
B-protected-void-int
A-long

2 overloaded versions of m1 method in A.
The one that takes an int is correctly overridden by B.
remember the overriding method can have broader access than the overridden, and protected is broader than package / default access

due to polymorphism, the overriding method replaces the overridden one on all calls, even if the other class is used as a reference type.

note short is automatically cast to the larger int type

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

What is the result of the following code?

abstract class X { 
private final void a() { print("a"); }
protected X() { print("x"); } }

public class Y extends X {
public Y() { print("y");}
protected void a() { print("Y-a") }
main { var y = new Y();   y.a();  }}
A

compiles without issue, output is:
x y Y-a
basically superclass constructors are called in the same way for both abstract and non-abstract classes

new Y calls the constructor -> public Y()
this has an implicit super to X
so it goes X’s constructor, Y’s constructor, Y method call

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

Which statements are true?

An overridden method may declare a new exception, provided it is not checked

if an inhertied method returns void, then the overridden version of the method must return void

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

What allows the following to compile?

public class X { 
public X (long a) { \_\_\_\_ ; }
private X (int b) { super() ; } }

class Y extends X {
protected Y (String s) { super (2L); }
public Y () {  \_\_\_\_ ; } }
A

this(3), this(“”)
the int calls the private X constructor
the empty string calls the protected Y constructor

this((short)1), this(null)
shorts can be implicitly cast to ints
and the null argument will again activate Y’s string constructor

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

public class X {
StringBuilder value = new StringBuilder(“x”);
{ value.append(“a”); }
private X() { value.append(“b”); }
public X(String s) { this(); value.append(s); }

main {
Object x = new X();
x = new X (“c”);
print ( ( (X)x).value);
}}

A

variable declarations and instance initializors are ran first setting value to “xa”
the private constructor is then called, -> “xab”

a new X object is created, replacing our old x reference variable
first value gets set to “xa” like before, then we call our string constructor, that calls our private one with this(), -> “xab”, then control comes back to our string constructor and we get “xabc”

N.B. static main() method can access private constructors in the same class

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

How many lines have compilation errors in the following code?

public class X {
public X (Integer i) {}
protected static Integer a() throws Exception() {
print “a”; return 1; }

class Y extends X {
public Number a() throws RuntimeException {
print “Y-a”; return 2; } }

A

2 - 1 in one line, 2 in another. tricky.

because X declares a constructor and it’s not a no-args constructor, Y has to have an explicit call to a super() constructor - presumably the one taking an Integer

also, there’s 2 errors with Y’s a() method.
First, it isn’t covariant as Number is a supertype, not subtype of Integer. Second, the inherited method is static but the overridden one isn’t = invalid override.

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

Which classes will compile and include a default constructor created by the compiler?

public class A {}
public class B { public B() {} }
public class C { public C c() {return null;} }

A

A and C

B includes a constructor so the compiler won’t supply one.
For C, the c() is a method declaration not a constructor

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

Which of the following statements about inheritance are correct?

  1. A class can implement any number of interfaces
  2. If class X implements interface Y, then X is a subtype of Y
  3. Multiple inheritance is the property of a class to have multiple direct superclasses
A

all 3

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

abstract class X {
boolean y();
}

A

won’t compile
y() isn’t marked abstract and also does not contain a method body.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void moveTo(double deltaX, double deltaY);

Abstract classes can have non-abstract methods

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