Arrays and Collections – 28

本篇收集 Arrays and Collections 考古題

Q001

NO.9 Given the code fragment:
int[ ] secA = { 2, 4, 6, 8, 10 };
int[ ] secB = { 2, 4, 8, 6, 10 };
int res1 = Arrays.mismatch(secA, secB) ;
int res2 = Arrays.compare(secA, secB) ;
System.out.print (res1 + " : " + res2) ;
What is the result?
A. -1 : 2
B. 2 : -1
C. 2 : 3
D. 3 : 0


Answer: B

Arrays.mismatch
查詢從那個元素始不一樣,索引編號從 0 開始。

Arrays.compare
若 a, b 二者相等,傳回 0
第一個不相等的元素,如果 a[i] < a[j],傳回 -1
第一個不相等的元素,如果 a[i] > a[j],傳回  1

Q002

NO.12 Given:
public class Employee {
    private String name;
    private String neighborhood;
    private LocalDate birthday;
    private int salary;
}
and
List<Employee> roster = new ArrayList<>(...); Map<String, Optional<Employee>> m = roster.stream() // Line 1 Which code fragment on line 1 makes the m map contain the employee with the highest salary for each neighborhood? A. .collect(Collectors.maxBy(Employee: getSalary, Collectors.groupingBy (Comparator.comparing(e -> e.getNeighborhood())))); B. .collect(Collectors.groupingBy (Employee::getNeighborhood, Collectors.maxBy (Comparator.comparing (Employee:: getSalary)))); C. .collect(Collectors.groupingBy(e -> e.getNeighborhood(), Collectors.maxBy((x, y) -> y.getSalary() - x.getSalary()))); D. .collect(Collectors.maxBy((x, y) -> y.getSalary() - x.getSalary(), Collectors.groupingBy (Employee::getNeighborhood))); A. Option A B. Option B C. Option C D. Option D Answer: BC

注意 B 如果是 Employee::getSalary())); 那就是錯的,因為 getSalary 不能有 ()。

C也是正確的。

上述的 Class Employee 完整代碼如下

class Employee {
    private String name;
    private String neighborhood;
    private LocalDate birthday;
    private int salary;
    public String getNeighborhood(){
        return neighborhood;
    }
    public int getSalary(){
        return salary;
    }
}

🔒 更多內容,請登入會員繼續閱讀。

立即登入

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *