IS5103 Lesson-9 Quiz

1.Suppose you need to display a collection of products for sale, which
may contain duplicates. Additionally, you have a collection of sales that
you need to track, sorted by the natural order of the sale ID, and you
need to retrieve the text of each. Which of the following from the java.util
package best suit your needs for this scenario?
Each correct answer represents a complete solution. Choose all that apply.
A) ArrayList, TreeMap

2.What is the result of the following statements?
1. var greetings = new ArrayDeque();
2.greetings.offerLast(“hello”);
3.greetings.offerLast(“hi”);
4.greetings.offerFirst(“ola”);
5.greetings.pop();
6.greetings.peek();
7.while (greetings.peek() != null)
8.System.out.print(greetings.pop());
A) Hellohi

3.Which options are true of the following code?
1.__ q = new LinkedList<>();
2.q.add(10);
3.q.add(12);
4.q.remove(1);
5.System.out.print(q);
Each correct answer represents a complete solution. Choose all that apply.
A) If we fill in the blank with List, the output is [10]. ,
If we fill in the blank with var, the output is [10].

4.Which of the following will compile when filling in the blank?
1. var list = List.of(1, 2, 3);
2. var set = Set.of(1, 2, 3);
3. var map = Map.of(1, 2, 3, 4);
4. _.forEach(System.out::println);
Each correct answer represents a complete solution. Choose all that
apply.
A) List
Set
map.keySet()
map.values()

5.What is the result of the following code?
1.Map m = new HashMap();
2.m.put(123, “456”);
3.m.put(“abc”, “def”);
4.System.out.println(m.contains(“123”));
A) Compiler error on line 7

6.What is the result of the following code?
1.var map = Map.of(1,2, 3, 6);
2.var list = List.copyOf(map.entrySet());
3.
4. List one = List.of(8, 16, 2);
5.var copy = List.copyOf(one);
6.var copyOfCopy = List.copyOf(copy);
7.var thirdCopy = new ArrayList<>(copyOfCopy);
8.
9.list.replaceAll(x -> x * 2);
10.one.replaceAll(x -> x * 2);
11.thirdCopy.replaceAll(x -> x * 2);
12.
13.System.out.println(thirdCopy);
Each correct answer represents a complete solution. Choose all that
A) One line fails to compile. ,
If any lines with compiler errors are removed, the code throws an
exception at runtime.

7.What is the result of the following code?
var map = new HashMap();
map.put(1, 10);
map.put(2, 20);
map.put(3, null);
map.merge(1, 3, (a,b) -> a + b);
map.merge(3, 3, (a,b) -> a + b);
System.out.println(map);
A) {1=13, 2=20, 3=3}

8.What is the result of the following program?
1. public class MyComparator implements Comparator {
2.public int compare(String a, String b) {
3.return b.toLowerCase().compareTo(a.toLowerCase());
4. }
5.public static void main(String[] args) {
6.String[] values = { “123”, “Abb”, “aab” };
7.Arrays.sort(values, new MyComparator());
8.for (var s: values)
9.System.out.print(s + ” “);
10. }
11. }
A) Abb aab 123

9.Which of the following can fill in the blank to print [7, 5, 3]?
1.public record Platypus(String name, int beakLength) {
2.@Override public String toString() {return “” + beakLength;}
3.
4.public static void main(String[] args) {
5.Platypus p1 = new Platypus(“Paula”, 3);
6.Platypus p2 = new Platypus(“Peter”, 5);
7.Platypus p3 = new Platypus(“Peter”, 7);
8.
9. List list = Arrays.asList(p1, p2, p3);
10.
11.Collections.sort(list, Comparator.comparing______);
12.
13.System.out.println(list);
14.}
15.}
Each correct answer represents a complete solution. Choose all that apply.
A) (Platypus::beakLength).reversed()
(Platypus::name).thenComparingInt(Platypus::beakLength).reversed()

10.What is the result of the following program?
public record Sorted(int num, String text)
implements Comparable, Comparator {

public String toString() { return “” + num; }
public int compareTo(Sorted s) {
return text.compareTo(s.text);
}
public int compare(Sorted s1, Sorted s2) {
return s1.num – s2.num;
}
public static void main(String[] args) {
var s1 = new Sorted(88, “a”);
var s2 = new Sorted(55, “b”);
14. var t1 = new TreeSet();
t1.add(s1); t1.add(s2);
16. var t2 = new TreeSet(s1);
t2.add(s1); t2.add(s2);
System.out.println(t1 + ” ” + t2);
} }
A) [88, 55] [55, 88]

11.What is the result of the following code?
Comparator c1 = (o1, o2) -> o2 – o1;
Comparator c2 = Comparator.naturalOrder();
Comparator c3 = Comparator.reverseOrder();

var list = Arrays.asList(5, 4, 7, 2);
Collections.sort(list,_);
Collections.reverse(list);
Collections.reverse(list);
System.out.println(Collections.binarySearch(list, 2));
A) One or more of the comparators can fill in the blank so that the
code prints 0.

12.Which of the following statements are true?
Each correct answer represents a complete solution. Choose all that apply.
A) Comparator is in the java.util package.
compare() is in the Comparator interface.
compare() takes two method parameters.

13.Which of the following are true?
1. List q = List.of(“mouse”, “parrot”);
2.var v = List.of(“mouse”, “parrot”);
3.
4.q.removeIf(String::isEmpty);
5.q.removeIf(s -> s.length() == 4);
6.v.removeIf(String::isEmpty);
7.v.removeIf(s -> s.length() == 4);
Each correct answer represents a complete solution. Choose all that apply.
A) Exactly two of these lines contain a compiler error.,
If any lines with compiler errors are removed, this code throws an
exception.

14.Which of these statements compile?
Each correct answer represents a complete solution. Choose all that apply.
A) HashSet<? Super ClassCastException> set = new
HashSet<Expection>();
Map<String, ? extends Number> hm = new HashMap<String, Integer>();

15.What is the result of the following code?
public record Hello<T>(T t) {
public Hello(T t) { this.t = t; }
private<T> void println(T message) {
System.out.print(t + “-” + message);
}
public static void main(String[] args) {
new Hello<String>(“hi”).println(1);
new Hello(“hola”).println(true);


} }
A) hi-1hola-true

16.Which of the following method signatures are valid overrides of
the hairy() method in the Alpaca class?
import java.util.*;
public class Alpaca {
public List hairy(List list) { return null; }
}
Each correct answer represents a complete solution. Choose all that apply.
A) public List hairy(ArrayList list) { return null; }
public ArrayList hairy(List list) { return null;

17.Which of these statements can fill in the blank so that the Wildcard
class compiles successfully?
1.public class Wildcard {
2. public void showSize(List list) {
3.System.out.println(list.size());
4.}
5.public static void main(String[] args) {
6.Wildcard card = new Wildcard();
7________________________;
8.card.showSize(list);
9.} }
Each correct answer represents a complete solution. Choose all that apply.
A) ArrayList<? super Date> list = new ArrayList<Date>()
ArrayList <? extends Number>list = new ArrayList <integer>()

18.Which of the following lines can be inserted to make the code
compile?
class W {}
class X extends W {}
class Y extends X {}
class Z {
// INSERT CODE HERE
}
Each correct answer represents a complete solution. Choose all that apply.
A) W w1 = new W();
W w2 = new X();

19.What code change is needed to make the method compile, assuming
there is no class named T?
public static T identity(T t) {
return t;
}
A) Add after the static keyword.

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 *