Question 41

Given:
class Student {
String course, name, city;
public Student (String name, String course, String city) {
this.course = course; this.name = name; this.city = city;
}
public String toString() {
return course + ":" + name + ":" + city;
}
and the code fragment:
List<Student> stds = Arrays.asList(
new Student ("Jessy", "Java ME", "Chicago"),
new Student ("Helen", "Java EE", "Houston"),
new Student ("Mark", "Java ME", "Chicago"));
stds.stream()
. collect(Collectors.groupingBy(Student::getCourse))
. forEach(src, res) -> System.out.println(scr));
What is the result?
  • Question 42

    Given the code fragment:
    class CallerThread implements Callable<String> {
    String str;
    public CallerThread(String s) {this.str=s;}
    public String call() throws Exception {
    return str.concat("Call");
    }
    }
    and
    public static void main (String[] args) throws InterruptedException, ExecutionException
    {
    ExecutorService es = Executors.newFixedThreadPool(4); //line n1
    Future f1 = es.submit (newCallerThread("Call"));
    String str = f1.get().toString();
    System.out.println(str);
    }
    Which statement is true?
  • Question 43

    Given:
    public interface Moveable<Integer> {
    public default void walk (Integer distance) {System.out.println("Walking");) public void run(Integer distance);
    }
    Which statement is true?
  • Question 44

    Given:
    public class Emp {
    String fName;
    String lName;
    public Emp (String fn, String ln) {
    fName = fn;
    lName = ln;
    }
    public String getfName() { return fName; }
    public String getlName() { return lName; }
    }
    and the code fragment:
    List<Emp> emp = Arrays.asList (
    new Emp (“John”, “Smith”),
    new Emp (“Peter”, “Sam”),
    new Emp (“Thomas”, “Wale”));
    emp.stream()
    //line n1
    .collect(Collectors.toList());
    Which code fragment, when inserted at line n1, sorts the employees list in descending order of fNameand then ascending order of lName?
    .sorted (Comparator.comparing(Emp::getfName).reserved().thenComparing
  • Question 45

    Given the definition of the Country class:
    public class country {
    public enum Continent {ASIA, EUROPE}
    String name;
    Continent region;
    public Country (String na, Continent reg) {
    name = na, region = reg;
    }
    public String getName () {return name;}
    public Continent getRegion () {return region;}
    }
    and the code fragment:
    List<Country> couList = Arrays.asList (
    new Country ("Japan", Country.Continent.ASIA),
    new Country ("Italy", Country.Continent.EUROPE),
    new Country ("Germany", Country.Continent.EUROPE));
    Map<Country.Continent, List<String>> regionNames = couList.stream ()
    .collect(Collectors.groupingBy (Country ::getRegion,
    Collectors.mapping(Country::getName, Collectors.toList()))));
    System.out.println(regionNames);
    What is the output?