QUESTION 121
Given:
class MyClass implements AutoCloseable {
int test;
public void close() {}
public MyClass copyObject() {return this;}
}
and the code fragment:
MyClass obj=null;
try(MyClass obj1 =new MyClass())
{
obj1.test=100;
obj=obj1.copyObject(); //line n1
}
System.out.println(obj.test); //line n2
What is the result?
A. An exception is thrown at
line n2
.
B.
100
C. A compilation error occurs because the
try
block is declared without a
catch
or
finally
block.
D. A compilation error occurs at
line n1
.
Correct Answer:
B
Section: (none)
Explanation

Explanation/Reference:
QUESTION 122
Which two methods from the
java.util.stream.Stream
interface perform a reduction operation?
(Choose two.)
A.
count ()
B.
collect ()
C.
distinct ()
D.
peek ()
E.
filter ()
Correct Answer:
AB
Section: (none)
Explanation
Explanation/Reference:
Reference:
https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
QUESTION 123
Which code fragment is required to load a JDBC 3.0 driver?
A.
Connection con = Connection.getDriver
(“jdbc:xyzdata://localhost:3306/EmployeeDB”);
B.
Class.forName(“org.xyzdata.jdbc.NetworkDriver”);
C.
Connection con = DriverManager.getConnection
(“jdbc:xyzdata://localhost:3306/EmployeeDB”);
D.
DriverManager.loadDriver (“org.xyzdata.jdbc.NetworkDriver”);
Correct Answer:
B
Section: (none)
Explanation
Explanation/Reference:
QUESTION 124
Given:
public class Foo<K,V>{
private K key;
private V value;
public Foo(K key,V value) {this.key=key;this.value=value;}
public static<T> Foo<T,T> twice(T value){
return new Foo<T,T>(value,value);
}
public K getKey() {return key;}
public V getValue() {return value;}
}
Which option fails?
A.
Foo<String, Integer> mark = new Foo<String, Integer> (“Steve”, 100);
B.
Foo<String, String> pair = Foo.<String>twice (“Hello World!”);
C.
Foo<Object, Object> percentage = new Foo<String, Integer>(“Steve”, 100);
D.
Foo<String, String> grade = new Foo <> (“John”, “A”);
Correct Answer:
A
Section: (none)
Explanation
Explanation/Reference:
QUESTION 125
Given the code fragment:
List<Integer>prices =Arrays.asList(3,4,5);
prices.stream()
.filter(e->e>4)
.peek(e->System.out.print("Price"+e)) //line n1
.map(n->n-1) //line n2
.peek(n->System.out.println("New Price"+n)); //line n3
Which modification enables the code to print
Price 5 New Price 4
?
A. Replace
line n2
with
.map (n -> System.out.println (“New Price” + n –1))
and
remove
line n3
B. Replace
line n2
with
.mapToInt (n -> n – 1);
C. Replace
line n1
with
.forEach (e -> System.out.print (“Price” + e))
D. Replace
line n3
with
.forEach (n -> System.out.println (“New Price” + n));
Correct Answer:
D
Section: (none)
Explanation

Explanation/Reference:
QUESTION 126
Given the definition of the
Book
class:
public class Book {
private int id;
private String name;
public Book(int id,String name) {
this.id=id;
this.name=name;
}
public int getId() {return id;}
public String getName() {return name;}
public void setId(int id) {this.id=id;}
public void setName(String name) {this.name=name;}
}
Which statement is true about the
Book
class?
A. It demonstrates encapsulation.
B. It is defined using the factory design pattern.
C. It is defined using the singleton design pattern.
D. It demonstrates polymorphism.
E. It is an immutable class.
Correct Answer:
A
Section: (none)
Explanation
Explanation/Reference:
QUESTION 127
Given the code fragment:
ProductCode<Number,Integer> c1=new ProductCode<Number, Integer>();//c1 instantiation
ProductCode<Number,String> c2= new ProductCode<Number,String>();//c2 instantiation
You have been asked to define the
ProductCode
class. The definition of the
ProductCode
class must
allow
c1
instantiation to succeed and cause a compilation error on
c2
instantiation.
Which definition of
ProductCode
meets the requirement?

Correct Answer:
B
Section: (none)
Explanation
Explanation/Reference:
QUESTION 128
Given the code fragment:
Map<Integer,Integer> mVal=new HashMap<>();
mVal.put(1,10);
mVal.put(2,20);
//line n1
c.accept(1,2);
mVal.forEach(c);
Which statement can be inserted into
line n1
to print
1,2; 1,10; 2,20;
?
A.
BiConsumer<Integer,Integer> c = (i, j) -> {System.out.print (i + “,” + j+
“; “);};
B.
BiFunction<Integer, Integer, String> c = (i, j)
–>
{System.out.print (i + “,”
+ j+ “; “)};
C.
BiConsumer<Integer, Integer, String> c = (i, j)
–>
{System.out.print (i + “,”
+ j+ “; “)};
D.
BiConsumer<Integer, Integer, Integer> c = (i, j)
–>
{System.out.print (i +
“,” + j+ “; “);};
Correct Answer: A
Section: (none)
Explanation

Explanation/Reference:
Reference:
https://www.concretepage.com/java/jdk-8/java-8-biconsumer-bifunction-bipredicate-example
BiConsumer接口说明:
https://blog.youkuaiyun.com/qq_28410283/article/details/80704487
QUESTION 129
Given the code fragment:
List<String>nums=Arrays.asList("EE","SE");
String ans =nums
.parallelStream()
.reduce("Java ", (a,b)->a.concat(b));
System.out.print(ans);
What is the result?
A.
Java EEJava EESE
B.
Java EESE
C. The program prints either:
Java EEJava SE
or
Java SEJava EE
D.
Java EEJava SE
Correct Answer:
D
Section: (none)
Explanation

Explanation/Reference:
QUESTION 130
Given the code fragments :
public class Product {
String name;
Integer price;
Product(String name,Integer price){
this.name=name;
this.price=price;
}
public void printVal() {
System.out.print(name+"Price:"+price+"");
}
public void setPrice(int price) {this.price=price;}
public Integer getPrice() {return price;}
}
and
List<Product> li= Arrays.asList(new Product("TV",1000),new Product("Refrigerato",2000));
Consumer<Product> raise=e->e.setPrice(e.getPrice()+100);
li.forEach(raise);
li.stream().forEach(Product::printVal);
What is the result?
A.
TV Price :1100 Refrigerator Price :2100
B. A compilation error occurs.
C.
TV Price :1000 Refrigerator Price :2000
D. The program prints nothing.
Correct Answer:
A
Section: (none)
Explanation

Explanation/Reference: