Ch 4 - Methods and Encapsulation Flashcards
Which of the following methods compile?
public void walk1() {}
public void walk2() { return; }
public String walk3() {}
public void walk1() {}
public void walk2() { return; }
public String walk3() {} – does not compile
What optional specifier is used when not providing a method body?
abstract
When a method is defined as public, where can it be called from?
Any class
Which of the following lines below do not compile:
public void walk1() {}
default void walk2() {}
void public walk3() {}
void walk4() {}
public void walk1() {}
default void walk2() {} – does not compile
void public walk3() {} – does not compile
void walk4() {}
What optional specifier is used for defining class methods?
static
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() {}
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() {}
When a method is defined as protected, where can it be called from?
Can only be called from classes in the same package or subclasses.
Describe the different parts of the method signature and identify which are required and which aren’t.
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
When a method is defined as private, where can it be called from?
Only from within the same class
When a method is defined as Default package protected, where can it be called from?
Can only be called from classes in the same package.
Which of the following methods compile?
public String walk1 { return “”; }
public walk2() {}
String walk3(int a) { if (a == 4) return “”; }
public String walk1 { return “”; }
public walk2() {} – does not compile
String walk3(int a) { if (a == 4) return “”; } – does not compile
true/false, a method signature does not have to provide a return type.
false. If there is not return type provided by the method, you must use the ‘void’ key word in the signature.
When a method returns a value, it must be assignable to…
…the return type.
Name the four access modifiers for methods.
public
private
protected
Default (package protected)
What optional specifier is used when a method is not allowed to be overridden by a subclass?
final
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);
}}
true
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);
}
22
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) {}
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
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);
}
0
1
2
2
NullPointerException
When using a vararg parameter in a method’s input parameter list, where must it be located?
It must be the last element.
Which of the following methods compile?
public void walk1() {}
public void 2walk() {}
public walk3 void() {}
public void Walk_$() {}
public void() {}
public void walk1() {}
public void 2walk() {} – does not compile
public walk3 void() {} – does not compile
public void Walk_$() {}
public void() {} – does not compile
Which of the following methods compile?
public void walk1() {}
public void walk2();
public void walk3(int a) { int name = 5; }
public void walk1() {}
public void walk2(); – does not compile
public void walk3(int a) { int name = 5; }
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);
}}
false
How many exceptions can you put in the optional exception list?
As many as you want.
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) {}
How many vararg parameters can you have in a method’s input parameters list?
only one
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
The protected access modifier adds the ability to what?
Access members of a parent class.
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.
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
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
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
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.
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.
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.
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.
true/false, a static method can call an instance method or member.
false Well, it can if the method instantiates the object.
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
true/false, a static member can call an instance member.
false
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
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
true/false, an instance method can call a static method or member.
true
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.
Having a static method eliminates what?
…the need for the caller to instantiate the object just to call the method.
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.
true/false, a static method can call another static method or member.
true
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
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.
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.
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.
What are static imports used for?
Importing static members of classes.
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
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.
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.
} }
true/false, you cannot initialize a static variable on the same line in which it’s declared.
false
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.
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
}
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
Assigning a new primitive or reference to a parameter does or does not change the caller?
Does not
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.
true/false, when using static imports, you can import multiple static methods or variables of the same name.
false.
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.
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
true/false, you can use a static import to import a class.
false. A static import is only for importing static class members.
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
Calling methods on a reference to an object does or does not affect the caller?
Does
Does the following code compile?
public void fly(Integer numMiles) {}
fly(3);
Yes, through autoboxing
What is method overloading?
When there are different method signatures with the same name but different type parameters.
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);
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.
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
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.
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) {}
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.
Inside a constructor, the “this” keyword tells Java what?
That you want to reference an instance variable.
What happens if you do not create a constructor in your java classes?
Java will create a do-nothing one for you.
When would you use only a private constructor?
When your class only has static methods.
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);
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);
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
When is the do-nothing constructor created for you?
At compile time.
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.
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
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
When Java automatically creates you a do-nothing constructor, what is that constructor known as?
The default constructor.
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;
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
When the do-nothing constructor is created, in which file will you find it?
The .class file.
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
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.
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”);
Which keyword do you use to call one constructor from within another constructor?
this
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
What is the order of initialization?
- initialize superclass first
- Static variables and initializers in the order they appear.
- Instance variables and initializers in the order they appear.
- Constructors
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.
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.
You can have multiple constructors in the same class as long as what?
As long as they have different method signatures.
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
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.
How can you make an encapsulated class immutable?
Do not include any setter methods. Do any assigning of variables in the constructor.
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
What is a lambda expression?
It is a block of code that gets passed around.
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
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.
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
Is this a valid call to a Lambda expression?
a,b -> a.startsWith(“test”);
no. needs parenthesis around the parameter list.
Is this a valid call to a Lambda expression? () -> true;
yes
In what package is the Predicate interface?
java.util.function
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]
What is deferred execution?
It means that the code is specified now but will run later.
Is this a valid call to a Lambda expression?
a -> a.startsWith(“test”);
yes
Is this a valid call to a Lambda expression?
(a,b) -> a.startsWith(“test”);
yes
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.
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
Is this a valid call to a Lambda expression?
(String a, String b) -> a.startsWith(“test”);
yes
What is an interface with just one method called?
A functional interface.
“Encapsulation” refers to what?
Preventing callers from changing the object’s instance variables directly.
Is this a valid call to a Lambda expression?
(String a) -> a.startsWith(“test”);
yes
Is this a valid call to a Lambda expression?
a -> { return a.startsWith(“test”) }
no. missing a semicolon.
A functional interface can be used with what type of programming?
functional programming
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)
Is this a valid call to a Lambda expression?
a -> { a.startsWith(“test”); }
no. missing the “return” keyword.
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.
What happens to your lambda expression if your functional interface has more than one method?
It doesn’t compile.
What is the name of the special predicate built into the ArrayList class?
removeIf()