티스토리 뷰
[OCPJP]Java SE 8 Programmer II Certification Exam | 1Z0-809 덤프/DUMP 문제#81~93
혲이. 2021. 3. 1. 22:06NEW QUESTION 81
Which two reasons should you use interfaces instead of abstract classes? (Choose two.)
A. You expect that classes that implement your interfaces have many common methods or fields, or require access modifiers other than public.
B. You expect that unrelated classes would implement your interfaces.
C. You want to share code among several closely related classes.
D. You want to declare non-static on non-final fields.
E. You want to take advantage of multiple inheritance of type.
Answer: BE
NEW QUESTION 82
Given the code fragments:
1 2 3 4 5 6 7 8 |
class Person //line n1 { String name; Person(String name){ this.name = name; } //line n2 } |
and
1 2 3 |
List < Person > emps = new ArrayList < >(); /*code that adds objects of the person class to the emps list goes here*/ Collections.sort(emps); |
Which two modifications enable to sort the elements of the emps list? (Choose two.)
A. Replace line n1 with class Person extends Comparator<Person>
B. At line n2 insert public int compareTo (Person p) { return this.name.compareTo (p.name);}
C. Replace line n1 with class Person implements Comparable<Person>
D. At line n2 insert public int compare (Person p1, Person p2) { return p1.name.compareTo (p2.name);}
E. At line n2 insert:public int compareTo (Person p, Person p2) { return p1.name.compareTo (p2.name);}
F. Replace line n1 with class Person implements Comparator<Person>
Answer: BC
NEW QUESTION 83
Given the definition of the Employee class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class Employee { String dept, name; public Employee(String d, String n) { dept = d; name = n; } public String toString() { return getDept() + ":" + getName(); } public String getDept() { return dept; } public String getName() { return name; } } |
and this code fragment:
1 2 3 4 5 6 7 8 |
List < Employee > emps = Arrays.asList( new Employee("sales", "Ada"), new Employee("sales", "Bob"), new Employee("hr", "Bob"), new Employee("hr", "Eva")); Stream < Employee > s = emps.stream().sorted(Comparator.comparing((Employee e) ->e.getDept()) .thenComparing((Employee e) ->e.getName())); List < Employee > eSorted = s.collect(Collectors.toList()); System.out.println(eSorted); |
What is the result?
A. [sales:Ada, hr:Bob, sales:Bob, hr:Eva]
B. [Ada:sales, Bob:sales, Bob:hr, Eva:hr]
C. [hr:Eva, hr:Bob, sales:Bob, sales:Ada]
D. [hr:Bob, hr:Eva, sales:Ada, sales:Bob]
Answer: D
Explanation: sort(Comparator.comparing())는 오름차순 정렬을 하는데 thenComparing이 추가되면 다중정렬을 의미한다.
NEW QUESTION 84
Given:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Book.java: public class Book { private String read(String bname) { return "Read" + bname; } } EBook.java: public class EBook extends Book { public String read(String url) { return "View" + url; } } Test.java: public class Test { public static void main(String[] args) { Book b1 = new Book(); b1.read("Java Programing"); Book b2 = new EBook(); b2.read("http://ebook.com/ebook"); } } |
What is the result?
A. Read Java Programming View http:/ ebook.com/ebook
B. Read Java Programming Read http:/ ebook.com/ebook
C. The EBook.java file fails to compile.
D. The Test.java file fails to compile.
Answer: D
Explanation: Answer is D method read has private access modifier in class Book.java and both b1 and b2 are of reference type Book so compiler checks there first.
*부모 클래스의 메소드의 접근 제한자보다 좁아질 수 없다. 확장될 수는 있다.
NEW QUESTION 85
Given the code fragment:
1 2 3 4 5 6 |
Path path1 = Paths.get("/software/././sys/readme.txt"); Path path2 = path1.normalize(); Path path3 = path2.relativize(path1); System.out.print(path1.getNameCount()); System.out.print(" : " + path2.getNameCount()); System.out.print(" : " + path3.getNameCount()); |
What is the result?
A. 5 : 3 : 6
B. 6 : 5 : 6
C. 3 : 3 : 4
D. 4 : 4 : 4
Answer: A
Explanation: getNameCount()- 루트주소 다음부터 몇 개의 계층으로 이루어져 있는지 갯수 반환
relativize()는 두 경로사이의 상대경로를 생성한다
normalize()는 중복되는 경로를 삭제한다.
/software/././sys/readme.txt
/software/sys/readme.txt
../../././sys/readme.txt
NEW QUESTION 86
Given:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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:
1 2 3 4 5 |
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 fName and then ascending order of lName?
A. .sorted (Comparator.comparing(Emp::getfName).reserved().thenComparing(Emp::getlName))
B. .sorted (Comparator.comparing(Emp::getfName).thenComparing(Emp::getlName))
C. .map(Emp::getfName).sorted(Comparator.reserveOrder())
D. .map(Emp::getfName).sorted(Comparator.reserveOrder().map (Emp::getlName).reserved
Answer: A
NEW QUESTION 87
Given the code fragment:
1 2 3 4 |
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStremReader(System. in)); System.out.print("Enter GDP: "); //line 1 } |
Which code fragment, when inserted at line 1, enables the code to read the GDP from the user?
A. int GDP = Integer.parseInt (br.readline());
B. int GDP = br.read();
C. int GDP = br.nextInt();
D. int GDP = Integer.parseInt (br.next());
Answer: A
NEW QUESTION 88
Given the code fragments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Employee { Optional < Address > address; Employee(Optional < Address > address) { this.address = address; } public Optional < Address > getAddress() { return address; } } class Address { String city = "New York"; public String getCity { return city: } public String toString() { return city; } } |
and
1 2 3 4 |
Address address = null; Optional < Address > addrs1 = Optional.ofNullable(address); Employee e1 = new Employee(addrs1); String eAddress = (addrs1.isPresent()) ? addrs1.get().getCity() : "City Not available"; |
What is the result?
A. New York
B. City Not available
C. null
D. A NoSuchElementException is thrown at run time.
Answer: B
Explanation: Optional.ofNullable(value)
null인지 아닌지 확신할 수 없는 객체를 담고 있는 Optional 객체를 생성합니다. Optional.empty()와 Optional.ofNullable(value)를 합쳐놓은 메소드라고 생각하시면 됩니다. null이 넘어올 경우, NPE를 던지지 않고 Optional.empty()와 동일하게 비어 있는 Optional 객체를 얻어옵니다. 해당 객체가 null인지 아닌지 자신이 없는 상황에서는 이 메소드를 사용하셔야 합니다.
isPresent 메서드로 현재 Optional이 보유한 값이 null인지 아닌지를 확인할 수 있습니다.
NEW QUESTION 89
Which action can be used to load a database driver by using JDBC3.0?
A. Add the driver class to the META-INF/services folder of the JAR file.
B. Include the JDBC driver class in a jdbc.properties file.
C. Use the java.lang.Class.forName method to load the driver class.
D. Use the DriverManager.getDriver method to load the driver class.
Answer: C
Explanation:
예시
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
String databaseURL = “jdbc: mysql: //localhost:3306/test”; String user = “user”; String password = “password”; Connection conn = null; try { Class.forName(“com.mysql.jdbc.Driver”);//C답안 conn = DriverManager.getConnection(databaseURL, user, password); if (conn != null) { System.out.println(“Connected to the database”); } } catch(ClassNotFoundException ex) { System.out.println(“Could not find database driver class”); ex.printStackTrace(); } catch(SQLException ex) { System.out.println(“An error occurred.Maybe user / password is invalid”); ex.printStackTrace(); } |
NEW QUESTION 90
Given the code fragments:
1 2 3 4 5 6 7 8 9 10 |
class R implements Runnable { public void run() { System.out.println("Run..."); } } class C implements Callable < String > { public String call() throws Exception { return "Call..."; } } |
and
1 2 3 4 5 |
ExecutorService es = Executors.newSingleThreadExecutor(); es.execute(new R()); //line n1 Future < String > f1 = es.submit(new C()); //line n2 System.out.println(f1.get()); es.shutdown(); |
What is the result?
A. The program prints Run... and throws an exception.
B. A compilation error occurs at line n1.
C. Run...Call...
D. A compilation error occurs at line n2.
Answer: C
NEW QUESTION 91
Given the code fragments:
1 2 3 4 5 6 7 8 |
public static Optional<String> getCountry(String loc){ Optional<String> couName = Optional.empty(); if("Paris".equals(loc)) couName = Optional.of("France"); else if("Mumbai".equals(loc)) couName = Optional.of("India"); return couName; } |
and
1 2 3 4 5 6 7 |
Optional<String> city1 = getCountry("Paris"); Optional<String> city2 = getCountry("Las Vegas"); System.out.println(city1.orElse("Not Found")); if(city2.isPresent()) city2.ifPresent(x -> System.out.println(x)); else System.out.println(city2.orElse("Not Found")); |
What is the result?
A. France
Optional[NotFound]
B. Optional [France]
Optional [NotFound]
C. Optional[France]
Not Found
D. France
Not Found
Answer: D
Explanation: orElse는 null인 경우 내부코드를 실행한다.
NEW QUESTION 92
Given the code fragment:
1 2 3 4 |
List<Integer> li = Arrays.asList(10,20,30); Function<Integer, Integer> fn = f1 -> f1 + f1; Consumer<Integer> conVal = s->System.out.print("Val:" + s +" "); li.stream().map(fn).forEach(conVal); |
What is the result?
A. Val:20 Val:40 Val:60
B. Val:10 Val:20 Val:30
C. A compilation error occurs.
D. Val: Val: Val:
Answer: A
NEW QUESTION 93
Given:
1 2 3 4 5 6 7 8 9 10 |
class Person{ String name; int age; public Person(String name, int age){ this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } |
and the code fragment:
1 2 3 4 5 6 |
List < Person > sts = Arrays.asList(new Person("Jack", 30), new Person("Mike Hill", 21), new Person("Thomas Hill", 24)); Stream < Person > resList = sts.stream().filter(s ->s.getAge() >= 25); //line n1 long count = resList.filter(s ->s.getName().contains("Hill")).count(); System.out.print(count); |
What is the result?
A. 0
B. A compilation error occurs at line n1.
C. An Exception is thrown at run time.
D. 2
Answer: A
'Language > java' 카테고리의 다른 글
컬렉션(Collection) sorted() (0) | 2021.05.26 |
---|---|
자바 컬렉션/Java Collection (List, Set, Queue, Map) (0) | 2021.05.25 |
[OCPJP]Java SE 8 Programmer II Certification Exam | 1Z0-809 덤프/DUMP 문제#61~80 (0) | 2021.03.01 |
[OCPJP]Java SE 8 Programmer II Certification Exam | 1Z0-809 덤프/DUMP 문제#41~60 (0) | 2021.03.01 |
[OCPJP]Java SE 8 Programmer II Certification Exam | 1Z0-809 덤프/DUMP 문제#21~40 (0) | 2021.03.01 |
- Total
- Today
- Yesterday
- 백준퇴사
- c#
- 런타임에러
- 개발중캐시삭제
- 프론트엔드
- C# java 차이점
- 백준14501
- boj
- 퇴사
- 프론트엔드개발자
- 백준
- C++
- 캐시삭제
- html꿀팁
- script버전
- html
- 선언적트랜잭션 #noRollbackFor #@Transactional
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |