IS5103 WEEK 4 QUIZ

1. What is the result of the following code? 
1. interface Climb { 
2. boolean isTooHigh(int height, int limit); 
3. } 
4. 
5. public class Climber { 
6. public static void main(String[] args) { 
7. check((h, m) -> h.append(m).isEmpty(), 5); 
8. } 
9. private static void check(Climb climb, int height) { 
10. if (climb.isTooHigh(height, 10)) 
11. System.out.println(“too high”); 
12. else 
13. System.out.println(“ok”); 
14. } 
15. } 

A) Compiler error on line 7 

2. Which lambda can replace the MySecret class to return the same value? 
interface Secret { 
String magic(double d); 

class MySecret implements Secret { 
public String magic(double d) { 
 return “Poof”; 
} }
Each correct answer represents a complete solution. Choose all that  apply. 

A) 1. (e) -> “Poof” 
2. (e) -> { String f = “”; return “Poof”; }  

3. Which of the following are valid lambda expressions? 
Each correct answer represents a complete solution. Choose all that apply.

A) 1. (final Camel c) -> {} 
2. (x,y) -> new RuntimeException()

4. What is the result of running the following class? 
1. import java.util.function.*; 
2. 
3. public class Panda { 
4. int age; 
5. public static void main(String[] args) { 
6. Panda p1 = new Panda(); 
7. p1.age = 1; 
8. check(p1, p -> {p.age < 5}); 
9. } 
10. private static void check(Panda panda, 
11. Predicate<Panda> pred) { 
12. String result = pred.test(panda) 
13. ? “match” : “not match”; 
14. System.out.print(result); 
15. } } 

A) Compiler error on line 8 

5. Which lambdas can replace the new Sloth() call in the main() method and  produce the same output at runtime? 
import java.util.List; 
interface Yawn { 
String yawn(double d, List<Integer> time); 

class Sloth implements Yawn { 
public String yawn(double zzz, List<Integer> time) { 
 return “Sleep: ” + zzz; 
} } 
public class Vet { 
public static String takeNap(Yawn y) { 
 return y.yawn(10, null); 

public static void main(String… unused) { 
 System.out.print(takeNap(new Sloth())); 
} }

A) (a,b) -> “Sleep: ” + (double)(b==null ? a : a) 

6. Which of the following are valid functional interfaces? 
public interface Transport { 
public int go(); 
public boolean equals(Object o); 

public abstract class Car { 
public abstract Object swim(double speed, int duration); } 
public interface Locomotive extends Train { 
public int getSpeed(); 

public interface Train extends Transport {} 
abstract interface Spaceship extends Transport {
default int blastOff(); 

public interface Boat { 
int hashCode(); 
int hashCode(String input); 

Each correct answer represents a complete solution. Choose all that apply. 

A) 1. Boat 
2. Transport 
3. Train 

7. Which of these can fill in the blank and have the code run without error? 
Set<?> set = Set.of(“lion”, “tiger”, “bear”); 
var s = Set.copyOf(set); 
Consumer<Object> consumer = ____________________;
s.forEach(consumer); 

A) System.out::println

8. What is the result of the following class? 
1. import java.util.function.*; 
2. 
3. public class Panda { 
4. int age; 
5. public static void main(String[] args) { 
6. Panda p1 = new Panda(); 
7. p1.age = 1; 
8. check(p1, p -> p.age < 5); 
9. } 
10. private static void check(Panda panda, 
11. Predicate<Panda> pred) { 
12. String result = 
13. pred.test(panda) ? “match” : “not match”; 
14. System.out.print(result); 
15. } }

A) Match 

9. Which statements about functional interfaces are true? 
Each correct answer represents a complete solution. Choose all that apply.

A) 1. A functional interface can contain default and private methods.
2.Abstract methods with signatures that are contained in public  methods of java.lang.Object do not count toward the abstract method  count for a functional interface. 

10. Which of the following functional interfaces contain an abstract method  that returns a primitive value?  
Each correct answer represents a complete solution. Choose all that apply.

A) 1. BooleanSupplier 
2. DoubleSupplier 
3. IntSupplier 

11. Which of the following lambda expressions can be passed to a function of  Predicate<String> type? 
Each correct answer represents a complete solution. Choose all that apply.

A) 1. s -> s.isEmpty() 
2.(String s) -> s.isEmpty()

12. Which of these statements is true about the following code? 
public void method() { 
x((var x) -> {}, (var x, var y) -> false); 
} public void x(Consumer<String> x, BinaryOperator<Boolean> y) {}

A) The code compiles, and the x in each lambda refers to a different type. 

13. Which of the following is equivalent to this code? 
UnaryOperator<Integer> u = x -> x * x; 

A) Function<Integer, Integer> f = x -> x*x; 

14. Which statements are true? 
Each correct answer represents a complete solution. Choose all that apply.

A) 1. The Consumer interface is good for printing out an existing value.              
2. The Predicate interface has a method named test().

15. What does the following code output? 
Function<Integer, Integer> s = a -> a + 4; 
Function<Integer, Integer> t = a -> a * 3; 
Function<Integer, Integer> c = s.compose(t); 
System.out.print(c.apply(1)); 

A) 7 

16. Which functional interfaces complete the following code? For line  2, assume m and n are instances of functional interfaces that exist and  have the same type as y. 
1. _________ x = String::new; 
2. _________ y = m.andThen(n); 
3. _________ z = a -> a + a; 
Each correct answer represents a part of the solution. Choose all that apply. 

A) 1. BiConsumer<String, String> 
2. Supplier<String> 
3. UnaryOperator<String> 

17. Which of the following can be inserted without causing a  compilation error? 
public void remove(List<Character> chars) { 
char end = ‘z’;
Predicate<Character> predicate = c -> { 
 char start = ‘a’; return start <= c && c <= end; }; 
// INSERT LINE HERE 

Each correct answer represents a complete solution. Choose all that apply.

A) 1. char start = ‘a’; 
2. char c = ‘x’; 
3. chars = null; 

18. How many times is true printed out by this code? 
import java.util.function.Predicate; 
public class Fantasy { 
public static void scary(String animal) { 
 var dino = s -> “dino”.equals(animal); 
 var dragon = s -> “dragon”.equals(animal); 
 var combined = dino.or(dragon); 
 System.out.println(combined.test(animal)); 

public static void main(String[] args) { 
 scary(“dino”); 
 scary(“dragon”); 
scary(“unicorn”); 

}

A) The code does not compile. 

19. Which is true of the following code? 
int length = 3; 
for (int i = 0; i<3; i++) { 
if (i%2 == 0) { 
Supplier<Integer> supplier = () -> length; // A 
System.out.println(supplier.get()); // B 
} else { 
 int j = i; 
 Supplier<Integer> supplier = () -> j; // C 
 System.out.println(supplier.get()); // D 

}

A) The code compiles successfully. 

20. Which lambda expression, when entered into the blank line in the  following code, causes the program to print hahaha? 
import java.util.function.Predicate; 
public class Hyena { 
private int age = 1; 
public static void main(String[] args) { 
 var p = new Hyena(); 
 double height = 10; 
 int age = 1; 
 testLaugh(p, _________________________); 
 age = 2; 

static void testLaugh(Hyena panda, Predicate<Hyena> joke) { 
 var r = joke.test(panda) ? “hahaha” : “silence”; 
 System.out.print(r); 


Each correct answer represents a complete solution. Choose all that  apply.

A) 1. var -> p.age <= 10 
2. h -> h.age < 5 

21. Which of the following can be inserted without causing a compilation  error? 
public void remove(List<Character> chars) { 
char end = ‘z’; 
// INSERT LINE HERE 
Predicate<Character> predicate = c -> { 
 char start = ‘a’; return start <= c && c <= end; }; 
}

A) chars = null;

Other Links:

Statistics Quiz

Networking Quiz

See other websites for quiz:

Check on QUIZLET

Check on CHEGG


 

 




Leave a Reply

Your email address will not be published. Required fields are marked *