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
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) {}
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) {}
26
How many vararg parameters can you have in a method's input parameters list?
only one
27
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); }}
false
28
The protected access modifier adds the ability to what?
Access members of a parent class.
29
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); }}
false, protected members are only visible to sub classes and classes in the same package.
30
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); }}
true
31
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(); }}
false
32
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); }}
true
33
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); }}
true, Bird2 is a subclass of Bird.
34
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); }}
false.
35
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); }}
true, Bird2 is in the same package as Bird.
36
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); }}
false. The code does not compile because the members of Bird are not being accessed in Bird2 via inheritance.
37
true/false, a static method can call an instance method or member.
false Well, it can if the method instantiates the object.
38
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); }}
3
39
true/false, a static member can call an instance member.
false
40
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); }}
true
41
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); }}
0 0
42
true/false, an instance method can call a static method or member.
true
43
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; }
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.
44
Having a static method eliminates what?
...the need for the caller to instantiate the object just to call the method.
45
When a member has default (package private) permissions, what does it mean?
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.
46
true/false, a static method can call another static method or member.
true
47
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); }}
5
48
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(); }}
compiler error on this line: public static void third { System.out.println(name); } the variable name is not static.
49
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(); }}
compiler error on this line: third(); You cannot call a non static method from a static method, such as main.
50
Does this code compile? private static final ArrayList values = new ArrayList\<\>(); public static void main(String[] args) { values.add("changed"); }
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.
51
What are static imports used for?
Importing static members of classes.
52
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; }}
1abcd
53
Does the following code compile? import static statics.A.TYPE; import static statics.B.TYPE;
No, you cannot import multiple static members with the same name.
54
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"); } }
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
true/false, you cannot initialize a static variable on the same line in which it's declared.
false
56
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; }
Yes, although NUM\_SECONDS\_PER\_HOUR is final, the static initializer is the first assignment. Since it occurs upfront, it is ok.
57
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; }
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
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"; }
Webby
59
Assigning a new primitive or reference to a parameter does or does not change the caller?
Does not
60
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; }
No. The first assignment of NUM\_SECONDS\_PER\_HOUR is ok in the static initializer, but the second one is not.
61
true/false, when using static imports, you can import multiple static methods or variables of the same name.
false.
62
Does this code compile? public class Initializers() { private static final int NUM\_BUCKETS = 45; public static void main(String[] args) { NUM\_BUCKETS = 5; } }
No, compiler error on this line: NUM\_BUCKETS = 5; the error happens because the variable is labeled as final.
63
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"); }
Webby
64
true/false, you can use a static import to import a class.
false. A static import is only for importing static class members.
65
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; }
4
66
Calling methods on a reference to an object does or does not affect the caller?
Does
67
Does the following code compile? public void fly(Integer numMiles) {} fly(3);
Yes, through autoboxing
68
What is method overloading?
When there are different method signatures with the same name but different type parameters.
69
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);
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
Does the following code compile? public void fly(int numMiles) {} public int fly(int numMiles) {}
No. The return type does not make a difference on overloading methods.
71
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); }}
string object
72
Does the following code compile? public void fly(int[] lengths) {} public void fly(int... lengths) {}
No. Because java treats varargs as an array, it considers these two methods to be the same.
73
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);
This method: public void fly(int numMiles) {}
74
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(); }}
No, this line produces a compiler error: Rabbit4 r4 = new Rabbit4(); This is because the Rabbit4 class only has a private constructor.
75
Inside a constructor, the "this" keyword tells Java what?
That you want to reference an instance variable.
76
What happens if you do not create a constructor in your java classes?
Java will create a do-nothing one for you.
77
When would you use only a private constructor?
When your class only has static methods.
78
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); }}
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
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); }}
Compiler error on this line: p.fly(123L);
80
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); }}
int long
81
When is the do-nothing constructor created for you?
At compile time.
82
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() { } }
Rabbit1 only.
83
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); }}
long long
84
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")); }}
142
85
When Java automatically creates you a do-nothing constructor, what is that constructor known as?
The default constructor.
86
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; } }
This line should have the variables reversed: length = this.length;
87
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); } }
int
88
When the do-nothing constructor is created, in which file will you find it?
The .class file.
89
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) {} }
2 4 6 8 5
90
true/false, the following code compiles: public Hamster(int weight) { System.out.println("in the constructor"); // ready to call this this(weight, "brown"); }
false. The "this" keyword must be the first non commented line in the constructor.
91
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"); }}
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
Which keyword do you use to call one constructor from within another constructor?
this
93
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(); }}
0 10 Torchie constructor
94
What is the order of initialization?
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
What is the special rule about using the "this" keyword to call one constructor from another constructor?
It must be the first noncommented statement in the constructor.
96
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"); } }
Compiler error on this line: Hamster(weight, "Brown"); A constructor can be called only by writing 'new' before the name of the constructor.
97
You can have multiple constructors in the same class as long as what?
As long as they have different method signatures.
98
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(); } }
ready to construct 0 Torchie 10 constructor
99
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; } }
true. Instance variables marked final can have their value assigned in a constructor.
100
How can you make an encapsulated class immutable?
Do not include any setter methods. Do any assigning of variables in the constructor.
101
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()); } }
initial
102
What is a lambda expression?
It is a block of code that gets passed around.
103
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; }
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
What is meant by encapsulation?
We set up the class so only methods in the class with the variables can refer to the instance variables.
105
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()); } }
initial added more
106
Is this a valid call to a Lambda expression? a,b -\> a.startsWith("test");
no. needs parenthesis around the parameter list.
107
Is this a valid call to a Lambda expression? () -\> true;
yes
108
In what package is the Predicate interface?
java.util.function
109
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);
[long ear, floppy, hoppy] [hoppy]
110
What is deferred execution?
It means that the code is specified now but will run later.
111
Is this a valid call to a Lambda expression? a -\> a.startsWith("test");
yes
112
Is this a valid call to a Lambda expression? (a,b) -\> a.startsWith("test");
yes
113
When constructing a lambda expression, when should you put parenthesis around the parameter list?
Always, except when you just have one parameter and it doesn't specify the data type.
114
When java tries to find the best overloaded method, in what order does it look through the overloaded methods?
Exact match wider primitives autoboxing varargs
115
Is this a valid call to a Lambda expression? (String a, String b) -\> a.startsWith("test");
yes
116
What is an interface with just one method called?
A functional interface.
117
"Encapsulation" refers to what?
Preventing callers from changing the object's instance variables directly.
118
Is this a valid call to a Lambda expression? (String a) -\> a.startsWith("test");
yes
119
Is this a valid call to a Lambda expression? a -\> { return a.startsWith("test") }
no. missing a semicolon.
120
A functional interface can be used with what type of programming?
functional programming
121
When working with a Lambda expression, when can braces and the return statement be omitted?
When there's a single statement. a -\> a.equals(b)
122
Is this a valid call to a Lambda expression? a -\> { a.startsWith("test"); }
no. missing the "return" keyword.
123
Is this a valid call to a Lambda expression? (a,b) -\> { int a = 0; return 5; }
no. you can't redeclare a parameter inside the body.
124
What happens to your lambda expression if your functional interface has more than one method?
It doesn't compile.
125
What is the name of the special predicate built into the ArrayList class?
removeIf()