Ch 4 - Methods and Encapsulation Flashcards

1
Q

Which of the following methods compile?

public void walk1() {}

public void walk2() { return; }

public String walk3() {}

A

public void walk1() {}

public void walk2() { return; }

public String walk3() {} – does not compile

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

What optional specifier is used when not providing a method body?

A

abstract

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

When a method is defined as public, where can it be called from?

A

Any class

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

Which of the following lines below do not compile:

public void walk1() {}

default void walk2() {}

void public walk3() {}

void walk4() {}

A

public void walk1() {}

default void walk2() {} – does not compile

void public walk3() {} – does not compile

void walk4() {}

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

What optional specifier is used for defining class methods?

A

static

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

Which of the following method signatures do not compile?

public void walk1() {}

public final void walk2() {}

public static final void walk3() {}

public final static void walk4() {}

public modifier void walk5() {}

public void final walk6() {}

final public void walk7() {}

A

public void walk1() {}

public final void walk2() {}

public static final void walk3() {}

public final static void walk4() {}

public modifier void walk5() {} – does not compile

public void final walk6() {} – does not compile

final public void walk7() {}

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

When a method is defined as protected, where can it be called from?

A

Can only be called from classes in the same package or subclasses.

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

Describe the different parts of the method signature and identify which are required and which aren’t.

A

Access modifier (public/private) - not required

optional specifier (final) - not required

return type (void) - required

method name (nap) - required

parameter list (int minutes) - required but can be empty parenthesis

optional exception list (throws Exception) - not required

method body ({}) - required, but can be empty

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

When a method is defined as private, where can it be called from?

A

Only from within the same class

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

When a method is defined as Default package protected, where can it be called from?

A

Can only be called from classes in the same package.

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

Which of the following methods compile?

public String walk1 { return “”; }

public walk2() {}

String walk3(int a) { if (a == 4) return “”; }

A

public String walk1 { return “”; }

public walk2() {} – does not compile

String walk3(int a) { if (a == 4) return “”; } – does not compile

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

true/false, a method signature does not have to provide a return type.

A

false. If there is not return type provided by the method, you must use the ‘void’ key word in the signature.

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

When a method returns a value, it must be assignable to…

A

…the return type.

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

Name the four access modifiers for methods.

A

public

private

protected

Default (package protected)

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

What optional specifier is used when a method is not allowed to be overridden by a subclass?

A

final

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

true/false, the following code compiles:

package pond.duck;

public class ParentDuck {

String noise = “quack”;

void quack() { System.out.println(noise); }

private void makeNoise() { quack();

}}

package pond.duck;

public class Duckling {

public void makeNoise() {

ParentDuck duck = new ParentDuck();

duck.quack();

System.out.println(duck.noise);

}}

A

true

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

What is the output of the following code?

public static void run(int… nums) {

System.out.println(nums[1]);

}

public static void main(String[] args) {

run(11, 22);

}

A

22

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

Which of the following methods will compile?

public void walk1(int… nums) {}

public void walk2(int start, int… nums) {}

public void walk3(int… nums, int start) {}

public void walk4(int… start, int… nums) {}

A

public void walk1(int… nums) {}

public void walk2(int start, int… nums) {}

public void walk3(int… nums, int start) {} // does not compile

public void walk4(int… start, int… nums) {} // does not compile

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

What would the output of the following program be?

public static void walk(int start, int… nums) {

System.out.println(nums.length);

}

public static void main(String[] args) {

walk(1);

walk(1, 2);

walk(1, 2, 3);

walk(1, new int[] {4,5});

walk(1, null);

}

A

0

1

2

2

NullPointerException

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

When using a vararg parameter in a method’s input parameter list, where must it be located?

A

It must be the last element.

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

Which of the following methods compile?

public void walk1() {}

public void 2walk() {}

public walk3 void() {}

public void Walk_$() {}

public void() {}

A

public void walk1() {}

public void 2walk() {} – does not compile

public walk3 void() {} – does not compile

public void Walk_$() {}

public void() {} – does not compile

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

Which of the following methods compile?

public void walk1() {}

public void walk2();

public void walk3(int a) { int name = 5; }

A

public void walk1() {}

public void walk2(); – does not compile

public void walk3(int a) { int name = 5; }

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

true/false, the following code compiles:

package pond.duck;

public class ParentDuck {

private String noise = “quack”;

private void quack() { System.out.println(noise); }

private void makeNoise() { quack();

}}

package pond.duck;

public class Duckling {

public void makeNoise() {

ParentDuck duck = new ParentDuck();

duck.quack(); System.out.println(duck.noise);

}}

A

false

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

How many exceptions can you put in the optional exception list?

A

As many as you want.

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

Which of the following methods compile?

public void walk1() {}

public void walk2 {}

public void walk3(int a) {}

public void walk4(int a; int b) {}

public void walk5(int a, int b) {}

A

public void walk1() {}

public void walk2 {} – does not compile

public void walk3(int a) {}

public void walk4(int a; int b) {} – does not compile

public void walk5(int a, int b) {}

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

How many vararg parameters can you have in a method’s input parameters list?

A

only one

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

true/false, the following code compiles:

package pond.duck;

public class ParentDuck {

String noise = “quack”;

void quack() {

System.out.println(noise);

}

private void makeNoise() {

quack();

}}

package pond.swan;

import pond.duck.ParentDuck;

public class Cygnet {

public void makeNoise() {

ParentDuck duck = new ParentDuck();

duck.quack();

System.out.println(duck.noise);

}}

A

false

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

The protected access modifier adds the ability to what?

A

Access members of a parent class.

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

true/false, the following code compiles:

package pond.shore;

public class Bird {

protected String text = “floating”;

protected void floatInWater() {

System.out.println(text);

}}

package pond.inland;

import pond.shore.Bird;

public class Bird2 {

public void watchBird() {

Bird bird = new Bird();

bird.floatInWater();

System.out.println(bird.text);

}}

A

false, protected members are only visible to sub classes and classes in the same package.

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

true/false, the following code compiles:

package pond.shore;

public class Bird {

protected String text = “floating”;

protected void floatInWater() {

System.out.println(text);

}}

package pond.swan;

import pond.shore.Bird;

public class Bird2 extends Bird {

public void swim() {

floatInWater();

System.out.println(text);

}}

A

true

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

true/false, the following code compiles:

package pond.shore;

public class Bird {

protected String text = “floating”;

protected void floatInWater() {

System.out.println(text);

}}

package pond.goose;

import pond.shore.Bird;

public class Bird2 extends Bird {

public void swim() {

Bird2 other = new Bird2();

other.floatInWater();

System.out.println(other.text);

}}

package pond.duck;

import pond.goose.Bird2;

public class Bird3 {

public void watch() {

Bird2 bird2 = new Bird2();

bird2.floatInWater();

}}

A

false

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

true/false, the following code compiles:

package pond.shore;

public class Bird {

protected String text = “floating”;

protected void floatInWater() {

System.out.println(text);

}}

package pond.swan;

import pond.shore.Bird;

public class Bird2 extends Bird {

public void swim() {

Bird2 other = new Bird2();

other.floatInWater();

System.out.println(other.text);

}}

A

true

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

true/false, the following code compiles:

package pond.shore;

public class Bird {

protected String text = “floating”;

protected void floatInWater() {

System.out.println(text);

}}

package pond.goose;

import pond.shore.Bird;

public class Bird2 extends Bird {

public void watchBird() {

floatInWater();

System.out.println(text);

}}

A

true, Bird2 is a subclass of Bird.

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

true/false, the following code compiles:

package pond.shore;

public class Bird {

protected String text = “floating”;

protected void floatInWater() {

System.out.println(text);

}}

package pond.goose;

import pond.shore.Bird;

public class Bird2 extends Bird {

public void swim() {

Bird2 other = new Bird2();

other.floatInWater();

System.out.println(other.text);

}

pubic void swim2() {

Bird other = new Bird2();

other.floatInWater();

System.out.println(other.text);

}}

A

false.

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

true/false, the following code compiles:

package pond.shore;

public class Bird {

protected String text = “floating”;

protected void floatInWater() {

System.out.println(text);

}}

package pond.shore;

public class Bird2 {

public void watchBird() {

Bird bird = new Bird();

bird.floatInWater();

System.out.println(bird.text);

}}

A

true, Bird2 is in the same package as Bird.

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

true/false, the following code compiles:

package pond.shore;

public class Bird {

protected String text = “floating”;

protected void floatInWater() {

System.out.println(text);

}}

package pond.swan;

import pond.shore.Bird;

public class Bird2 extends Bird {

public void swim() {

Bird other = new Bird();

other.floatInWater();

System.out.println(other.text);

}}

A

false. The code does not compile because the members of Bird are not being accessed in Bird2 via inheritance.

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

true/false, a static method can call an instance method or member.

A

false Well, it can if the method instantiates the object.

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

What is the output of the following lines of code?

public class Counter {

private static int count;

public Counter() { count++; }

public static void main(String[] args) {

Counter c1 = new Counter();

Counter c2 = new Counter();

Counter c3 = new Counter();

System.out.println(count);

}}

A

3

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

true/false, a static member can call an instance member.

A

false

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

true/false, the following code will compile:

package pond.duck;

public class Bird {

public String name = “helpful”;

public void swim() {

System.out.println(“swim”);

}}

package pond.goose;

import pond.duck.Bird;

public class Goose {

public void swim() {

Bird bird = new Bird();

bird.swim();

System.out.println(bird.name);

}}

A

true

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

What is the output of the following code?

public class Koala {

public static int count = 0;

}

public class KoalaTester {

public static void main(String[] args) {

Koala k = new Koala();

System.out.println(k.count);

k = null;

System.out.println(k.count);

}}

A

0

0

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

true/false, an instance method can call a static method or member.

A

true

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

Does the following code compile, and if not, why?

public class Gorilla {

public static int count;

public static void addGorilla() { count++; }

public void babyGorilla() { count++; }

public void announceBabies() {

addGorilla();

babyGorilla();

}

public static void announceBabiesToEveryone() {

addGorilla();

babyGorilla();

}

public int total;

public static double average = total / count;

}

A

The code does not compile here:

public static void announceBabiesToEveryone() {

addGorilla();

babyGorilla(); // does not compile

}

It also does not compile on this line:

public static double average = total / count;

You cannot make a static reference to a non static member.

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

Having a static method eliminates what?

A

…the need for the caller to instantiate the object just to call the method.

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

When a member has default (package private) permissions, what does it mean?

A

It means that a member is private to classes in the same package. In other words, only classes in the same package can access it.

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

true/false, a static method can call another static method or member.

A

true

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

What is the output of the following code?

public class Koala {

public static int count = 0;

}

public class KoalaTester {

public static void main(String[] args) {

Koala.count = 4;

Koala k1 = new Koala();

Koala k2 = new Koala();

k1. count = 6;
k2. count = 5;

System.out.println(Koala.count);

}}

A

5

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

What is the output of the following code?

public class Static {

private String name = “Static class”;

public static void first() {}

public static void second() {}

public static void third() {

System.out.println(name);

}

public static void main(String[] args) {

first();

second();

third();

}}

A

compiler error on this line:

public static void third { System.out.println(name); }

the variable name is not static.

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

What is the output of the following code?

public class Static {

private String name = “Static class”;

public static void first() {}

public static void second() {}

public void third() {

System.out.println(name);

}

public static void main(String[] args) {

first();

second();

third();

}}

A

compiler error on this line:

third();

You cannot call a non static method from a static method, such as main.

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

Does this code compile?

private static final ArrayList values = new ArrayList<>();

public static void main(String[] args) { values.add(“changed”); }

A

yes, “values” is a reference variable. All the compiler can do is make sure that we don’t try to reassign “values” to a different object.

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

What are static imports used for?

A

Importing static members of classes.

52
Q

What is the output of the following code?

public class ReturningValues {

public static void main(String[] args) {

int number = 1;

String letters = “abc”;

number(number);

letters = letters(letters);

System.out.println(number + letters);

}

public static int number(int number) {

number++;

return number;

}

public static String letters(String letters) {

letters += “d”;

return letters;

}}

A

1abcd

53
Q

Does the following code compile?

import static statics.A.TYPE;

import static statics.B.TYPE;

A

No, you cannot import multiple static members with the same name.

54
Q

Which of the following lines of code do not compile?

import static java.util.Arrays;

import static java.util.Arrays.asList;

static import java.util.Arrays.*;

public class StaticImports {

public static void main(String[] args) {

Arrays.asList(“one”);

} }

A

import static java.util.Arrays; // does not compile. static imports are only for static class members, not classes

import static java.util.Arrays.asList;

static import java.util.Arrays.*; // does not compile. the terms static and import are in the wrong order

public class StaticImports {

public static void main(String[] args) {

Arrays.asList(“one”); // does not compile. Although we’ve statically imported asList, we haven’t imported Arrays.

} }

55
Q

true/false, you cannot initialize a static variable on the same line in which it’s declared.

A

false

56
Q

Does the following code compile?

private static final int NUM_SECONDS_PER_HOUR;

static {

int numSecondsPerMinute = 60;

int numMinutesPerHour = 60;

NUM_SECONDS_PER_HOUR = numSecondsPerMinute * numMinutesPerHour;

}

A

Yes, although NUM_SECONDS_PER_HOUR is final, the static initializer is the first assignment. Since it occurs upfront, it is ok.

57
Q

Does the following code compile?

private static int one;

private static final int two;

private static final int three = 3;

private static final int four;

static {

one = 1;

two = 2;

three = 3;

two = 4;

}

A

No, the following lines do not compile:

private static int one;

private static final int two;

private static final int three = 3;

private static final int four; // does not compile

static {

one = 1;

two = 2;

three = 3; // does not compile

two = 4; // does not compile

}

58
Q

What is the output of the following code?

public static void main(String[] args) {

String name = “Webby”;

speak(name);

System.out.println(name);

}

public static void speak(String name) { name = “Sparky”; }

A

Webby

59
Q

Assigning a new primitive or reference to a parameter does or does not change the caller?

A

Does not

60
Q

Does the following code compile?

private static final int NUM_SECONDS_PER_HOUR;

static { int numSecondsPerMinute = 60;

int numMinutesPerHour = 60;

NUM_SECONDS_PER_HOUR = numSecondsPerMinute * numMinutesPerHour;

NUM_SECONDS_PER_HOUR = 100;

}

A

No. The first assignment of NUM_SECONDS_PER_HOUR is ok in the static initializer, but the second one is not.

61
Q

true/false, when using static imports, you can import multiple static methods or variables of the same name.

A

false.

62
Q

Does this code compile?

public class Initializers() {

private static final int NUM_BUCKETS = 45;

public static void main(String[] args) {

NUM_BUCKETS = 5;

} }

A

No, compiler error on this line:

NUM_BUCKETS = 5;

the error happens because the variable is labeled as final.

63
Q

What is the output of the following code?

public static void main(String[] args) {

StringBuilder name = new StringBuilder();

speak(name);

System.out.println(name);

}

public static void speak(StringBuilder s) {

s.append(“Webby”);

}

A

Webby

64
Q

true/false, you can use a static import to import a class.

A

false. A static import is only for importing static class members.

65
Q

What is the output of the following code?

public static void main(String[] args) {

int num = 4;

newNumber(num);

System.out.println(num);

}

public static void newNumber(int num) { num = 8; }

A

4

66
Q

Calling methods on a reference to an object does or does not affect the caller?

A

Does

67
Q

Does the following code compile?

public void fly(Integer numMiles) {}

fly(3);

A

Yes, through autoboxing

68
Q

What is method overloading?

A

When there are different method signatures with the same name but different type parameters.

69
Q

Consider the following two methods:

public void fly(int[] lengths) {}

public void fly(int… lengths) {}

Can you call each of them with the following lines?

fly(new int[] {1, 2, 3};

fly(1, 2, 3);

A

No. You can call either method with this line:

fly(new int[] {1, 2, 3};

But you can only call the varargs method with this line:

fly(1, 2, 3);

70
Q

Does the following code compile?

public void fly(int numMiles) {}

public int fly(int numMiles) {}

A

No. The return type does not make a difference on overloading methods.

71
Q

What is the output of the following code:

public class ReferenceTypes {

public void fly(String s) { System.out.print(“string “);

}

public void fly(Object o) { System.out.print(“object “); }

public static void main(String[] args) {

ReferenceTypes r = new ReferenceTypes();

r. fly(“test”);
r. fly(56);

}}

A

string object

72
Q

Does the following code compile?

public void fly(int[] lengths) {}

public void fly(int… lengths) {}

A

No. Because java treats varargs as an array, it considers these two methods to be the same.

73
Q

Consider the following two methods:

public void fly(int numMiles) {}

public void fly(Integer numMiles) {}

Which method will be used by the following line?

fly(3);

A

This method:

public void fly(int numMiles) {}

74
Q

Does the following code compile?

class Rabbit1 { }

class Rabbit2 { public Rabbit2() {} }

class Rabbit3 { public Rabbit3(boolean b) { } }

class Rabbit4 { private Rabbit4() {} }

public class RabbitsMultiply {

public static void main(String[] args) {

Rabbit1 r1 = new Rabbit1();

Rabbit2 r2 = new Rabbit2();

Rabbit3 r3 = new Rabbit3(true);

Rabbit4 r4 = new Rabbit4();

}}

A

No, this line produces a compiler error:

Rabbit4 r4 = new Rabbit4();

This is because the Rabbit4 class only has a private constructor.

75
Q

Inside a constructor, the “this” keyword tells Java what?

A

That you want to reference an instance variable.

76
Q

What happens if you do not create a constructor in your java classes?

A

Java will create a do-nothing one for you.

77
Q

When would you use only a private constructor?

A

When your class only has static methods.

78
Q

What is the output of the following code?

public class Conversions {

public static void play(Long l) { }

public static void play(Long… l) { }

public static void main(String[] args) {

play(4);

play(4L);

}}

A

Compiler error on this line:

play(4);

We can’t convert 4 to int, then to a long, and then to a Long (you can only convert once). The second line works because it is passing a long which is then converted to a Long:

play(4L);

79
Q

What is the output of the following code?

public class Plane {

public void fly(int l) { System.out.print(“int “); }

public static void main(String[] args) {

Plane p = new Plane();

p.fly(123L);

}}

A

Compiler error on this line:

p.fly(123L);

80
Q

What is the output of the following code?

public class Plane {

public void fly(int i) {

System.out.print(“int “);

}

public void fly(long l) { System.out.print(“long “); }

public static void main(String[] args) {

Plane p = new Plane();

p. fly(123);
p. fly(123L);

}}

A

int long

81
Q

When is the do-nothing constructor created for you?

A

At compile time.

82
Q

Which of the following classes get a default no-argument constructor created?

class Rabbit1 {}

class Rabbit2 { public Rabbit2() {} }

class Rabbit3 {public Rabbit3(boolean b) { } }

class Rabbit4 { private Rabbit4() { } }

A

Rabbit1 only.

83
Q

What is the output of the following code?

public class Plane {

public void fly(long l) {

System.out.print(“long “);

}

public static void main(String[] args) {

Plane p = new Plane();

p. fly(123);
p. fly(123L);

}}

A

long long

84
Q

What is the output of the following code?

public class Glider2 {

public static String glide(String s) { return “1”; }

public static String glide(String… s) { return “2”; }

public static String glide(Object o) { return “3”; }

public static String glide(String s, String t) { return “4”; }

public static void main(String[] args) {

System.out.print(glide(“a”));

System.out.print(glide(“a”, “b”));

System.out.print(glide(“a”, “b”, “c”));

}}

A

142

85
Q

When Java automatically creates you a do-nothing constructor, what is that constructor known as?

A

The default constructor.

86
Q

What is the problem with the following code?

public class Bunny {

private int length;

private int height;

public Bunny(int length, int height) {

length = this.length;

} }

A

This line should have the variables reversed:

length = this.length;

87
Q

What is the output of the following code?

public class Plane {

public void fly(int l) {

System.out.print(“int “);

}

public static void main(String[] args) {

Plane p = new Plane();

p.fly((int) 123L);

} }

A

int

88
Q

When the do-nothing constructor is created, in which file will you find it?

A

The .class file.

89
Q

What is the output of the following code?

public class YetMoreInitializationOrder {

static { add(2); }

static void add(int num) { System.out.print(num + “ “); }

YetMoreInitializationOrder() { add(5); }

static { add(4); }

{ add(6); }

static { new YetMoreInitializationOrder(); }

{ add(8); }

public static void main(String[] args) {}

}

A

2 4 6 8 5

90
Q

true/false, the following code compiles:

public Hamster(int weight) {

System.out.println(“in the constructor”);

// ready to call this

this(weight, “brown”);

}

A

false. The “this” keyword must be the first non commented line in the constructor.

91
Q

What happens when the single parameter constructor is called?

public class Hamster {

private String color;

private int weight;

public Hamster(int weight, String color) {

this. weight = weight;
this. color = color;

}

public Hamster(int weight) {

new Hamster(weight, “Brown”);

}}

A

The code compiles, but because we are instantiating a new object with the “new” keyword yet not assigning it anywhere, the object we just created gets ignored. The correct way to handle this is to replace this:

new Hamster(weight, “Brown”);

with this:

this(weight, “Brown”);

92
Q

Which keyword do you use to call one constructor from within another constructor?

A

this

93
Q

What is the output of the following code?

public class InitializationOrderSimple {

private String name = “Torchie”;

{ System.out.println(name); }

private static int COUNT = 0;

static { System.out.println(COUNT); }

static { COUNT += 10; System.out.println(COUNT); }

public InitializationOrderSimple() {

System.out.println(“constructor”);

}}

public class CallInitializationOrderSimple {

public static void main(String[] args) {

InitializationOrderSimple init = new InitializationOrderSimple();

}}

A

0

10

Torchie

constructor

94
Q

What is the order of initialization?

A
  1. initialize superclass first
  2. Static variables and initializers in the order they appear.
  3. Instance variables and initializers in the order they appear.
  4. Constructors
95
Q

What is the special rule about using the “this” keyword to call one constructor from another constructor?

A

It must be the first noncommented statement in the constructor.

96
Q

What is the output of the following code?

public class Hamster {

private String color;

private int weight;

public Hamster(int weight, String color) {

this. weight = weight;
this. color = color;

}

public Hamster(int weight) {

Hamster(weight, “Brown”);

} }

A

Compiler error on this line:

Hamster(weight, “Brown”);

A constructor can be called only by writing ‘new’ before the name of the constructor.

97
Q

You can have multiple constructors in the same class as long as what?

A

As long as they have different method signatures.

98
Q

What is the output of the following code?

public class InitializationOrder {

private String name = “Torchie”;

{ System.out.println(name); }

private static int COUNT = 0;

static { System.out.println(COUNT); }

{ COUNT += 10; System.out.println(COUNT); }

public InitializationOrder() { System.out.println(“constructor”);

} }

public class CallInitializationOrderSimple {

public static void main(String[] args) {

System.out.println(“ready to construct”);

new InitializationOrder();

} }

A

ready to construct

0

Torchie

10

constructor

99
Q

true/false, the following code compiles:

public class MouseHouse {

private final int volume;

private final String name = “The Mouse House”;

public MouseHouse(int length, int width, int height) {

volume = length * width * height;

} }

A

true. Instance variables marked final can have their value assigned in a constructor.

100
Q

How can you make an encapsulated class immutable?

A

Do not include any setter methods. Do any assigning of variables in the constructor.

101
Q

What is the output of the following code?

public class Sb {

private StringBuilder builder;

public Sb(StringBuilder sb) {

builder = new StringBuilder(sb);

}

public StringBuilder getBuilder() {

return new StringBuilder(builder);

}

public static void main(String[] args) {

StringBuilder sb = new StringBuilder(“initial”);

Sb prob = new Sb(sb);

sb.append(“ added”);

StringBuilder gotBuilder = prob.getBuilder();

gotBuilder.append(“ more”);

System.out.println(prob.getBuilder());

} }

A

initial

102
Q

What is a lambda expression?

A

It is a block of code that gets passed around.

103
Q

Which of the following lines follow JavaBeans naming conventions for encapsulation?

private boolean playing;

private String name;

public boolean getPlaying() { return playing; }

public boolean isPlaying() { return playing; }

public String name() { return name; }

public void updateName(String n) { name = n; }

public void setname(String n) { name = n; }

A

private boolean playing; // yes

private String name; // yes

public boolean getPlaying() { return playing; } // yes

public boolean isPlaying() { return playing; } // yes

public String name() { return name; } // no

public void updateName(String n) { name = n; } // no

public void setname(String n) { name = n; } // no

104
Q

What is meant by encapsulation?

A

We set up the class so only methods in the class with the variables can refer to the instance variables.

105
Q

What is the output of the following code?

public class Sb {

private StringBuilder builder;

public Sb(StringBuilder sb) {

builder = sb;

}

public StringBuilder getBuilder() { return builder; }

public static void main(String[] args) {

StringBuilder sb = new StringBuilder(“initial”);

Sb prob = new Sb(sb);

sb.append(“ added”);

StringBuilder gotBuilder = prob.getBuilder();

gotBuilder.append(“ more”);

System.out.println(prob.getBuilder());

} }

A

initial added more

106
Q

Is this a valid call to a Lambda expression?

a,b -> a.startsWith(“test”);

A

no. needs parenthesis around the parameter list.

107
Q

Is this a valid call to a Lambda expression? () -> true;

A

yes

108
Q

In what package is the Predicate interface?

A

java.util.function

109
Q

What is the output of the following code?

List bunnies = new ArrayList<>();

bunnies. add(“long ear”);
bunnies. add(“floppy”);
bunnies. add(“hoppy”);

System.out.println(bunnies);

bunnies.removeIf(s -> s.charAt(0) != ‘h’);

System.out.println(bunnies);

A

[long ear, floppy, hoppy]

[hoppy]

110
Q

What is deferred execution?

A

It means that the code is specified now but will run later.

111
Q

Is this a valid call to a Lambda expression?

a -> a.startsWith(“test”);

A

yes

112
Q

Is this a valid call to a Lambda expression?

(a,b) -> a.startsWith(“test”);

A

yes

113
Q

When constructing a lambda expression, when should you put parenthesis around the parameter list?

A

Always, except when you just have one parameter and it doesn’t specify the data type.

114
Q

When java tries to find the best overloaded method, in what order does it look through the overloaded methods?

A

Exact match

wider primitives

autoboxing

varargs

115
Q

Is this a valid call to a Lambda expression?

(String a, String b) -> a.startsWith(“test”);

A

yes

116
Q

What is an interface with just one method called?

A

A functional interface.

117
Q

“Encapsulation” refers to what?

A

Preventing callers from changing the object’s instance variables directly.

118
Q

Is this a valid call to a Lambda expression?

(String a) -> a.startsWith(“test”);

A

yes

119
Q

Is this a valid call to a Lambda expression?

a -> { return a.startsWith(“test”) }

A

no. missing a semicolon.

120
Q

A functional interface can be used with what type of programming?

A

functional programming

121
Q

When working with a Lambda expression, when can braces and the return statement be omitted?

A

When there’s a single statement. a -> a.equals(b)

122
Q

Is this a valid call to a Lambda expression?

a -> { a.startsWith(“test”); }

A

no. missing the “return” keyword.

123
Q

Is this a valid call to a Lambda expression?

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

A

no. you can’t redeclare a parameter inside the body.

124
Q

What happens to your lambda expression if your functional interface has more than one method?

A

It doesn’t compile.

125
Q

What is the name of the special predicate built into the ArrayList class?

A

removeIf()