Q1. Given:
public class product { int id; int price;
public Product (int id, int price) {
this.id = id;
this.price = price;
}
public String toString() { return id + “:” + price; }
}
and the code fragment:
List<Product> products = Arrays.asList(new Product(1, 10),
new Product (2, 30),
new Product (2, 30));
Product p = products.stream().reduce(new Product (4, 0), (p1, p2) -> {
p1.price+=p2.price;
return new Product (p1.id, p1.price);});
products.add(p);
products.stream().parallel()
.reduce((p1, p2) - > p1.price > p2.price ? p1 : p2)
.ifPresent(System.out: :println);
What is the result?
A. 2 : 30
B. 4 : 0
C. 4 : 60
D. 4 : 60
2 : 30
3 : 20
1 : 10
E.
The program prints nothing.
Answer: D
Q2. Given the code fragment:
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: C
Q3. Given:
1.
abstract class Shape {
2.
Shape ( ) { System.out.println (“Shape”); }
3.
protected void area ( ) { System.out.println (“Shape”); }
4.
}
5.
6.
class Square extends Shape {
7.
int side;
8.
Square int side { 9./* insert code here */
10.
this.side = side;
11.
}
12.
public void area ( ) { System.out.println (“Square”); }
13.
}
14.
class Rectangle extends Square {
15.
int len, br;
16.
Rectangle (int x, int y) {
17.
/* insert code here */
18.
len = x, br = y;
19.
}
20.
void area ( ) { System.out.println (“Rectangle”); }
21.
}
Which two modifications enable the code to compile?
A. At line 1, remove abstract
B. At line 9, insert super ( );
C. At line 12, remove public
D. At line 17, insert super (x);
E. At line 17, insert super (); super.side = x;
F. At line 20, use public void area ( ) {
Answer: C,D
Q4. Given:
class CheckClass {
public static int checkValue (String s1, String s2) {
return s1 length() – s2.length();
}
}
and the code fragment:
String[] strArray = new String [] {“Tiger”, “Rat”, “Cat”, “Lion”}
//line n1
for (String s : strArray) {
System.out.print (s + “ “);
}
Which code fragment should be inserted at line n1 to enable the code to print Rat Cat Lion Tiger?
A. Arrays.sort(strArray, CheckClass : : checkValue);
B. Arrays.sort(strArray, (CheckClass : : new) : : checkValue);
C. Arrays.sort(strArray, (CheckClass : : new).checkValue);
D. Arrays.sort(strArray, CheckClass : : new : : checkValue);
Answer: D
Q5. Given the code fragment:
List<String> empDetails = Arrays.asList(“100, Robin, HR”,
“200, Mary, AdminServices”,
“101, Peter, HR”);
empDetails.stream()
.filter(s-> s.contains(“1”))
.sorted()
.forEach(System.out::println); //line n1
What is the result?
A. 100, Robin, HR 101, Peter, HR
B. E. A compilation error occurs at line n1.
C. 100, Robin, HR 101, Peter, HR 200, Mary, AdminServices
D. 100, Robin, HR 200, Mary, AdminServices 101, Peter, HR
Answer: C
Q6. Given that course.txt is accessible and contains:
Course : : Java
and given the code fragment:
public static void main (String[ ] args) {
int i;
char c;
try (FileInputStream fis = new FileInputStream (“course.txt”);
InputStreamReader isr = new InputStreamReader(fis);) {
while (isr.ready()) { //line n1
isr.skip(2);
i = isr.read ();
c = (char) i;
System.out.print(c);
}
} catch (Exception e) {
e.printStackTrace();
}
}
What is the result?
A. ur :: va
B. ueJa
C. The program prints nothing.
D. A compilation error occurs at line n1.
Answer: A
Q7. Given the code fragment:
ZonedDateTime depart = ZonedDateTime.of(2015, 1, 15, 3, 0, 0, 0, ZoneID.of(“UTC-7”)); ZonedDateTime arrive = ZonedDateTime.of(2015, 1, 15, 9, 0, 0, 0, ZoneID.of(“UTC-5”)); long hrs = ChronoUnit.HOURS.between(depart, arrive); //line n1 System.out.println(“Travel time is” + hrs + “hours”);
What is the result?
A. Travel time is 4 hours
B. Travel time is 6 hours
C. Travel time is 8 hours
D. An exception is thrown at line n1.
Answer: D
Q8. Given:
What is the result?
A. The sum is 2
B. The sum is 14
C. The sum is 15
D. The loop executes infinite times
E. Compilation fails
Answer: E
Q9. Which statement is true about the single abstract method of the java.util.function.Function interface?
A. It accepts one argument and returns void.
B. It accepts one argument and returns boolean.
C. It accepts one argument and always produces a result of the same type as the argument.
D. It accepts an argument and produces a result of any data type.
Answer: C
Reference: http://winterbe.com/posts/2014/03/16/java-8-tutorial/ (functions)
Q10. Given:
IntStream stream = IntStream.of (1,2,3); IntFunction<Integer> inFu= x -> y -> x*y;//line n1 IntStream newStream = stream.map(inFu.apply(10));//line n2
newStream.forEach(System.output::print);
Which modification enables the code fragment to compile?
A. Replace line n1 with:
IntFunction<UnaryOperator> inFu = x -> y -> x*y;
B. Replace line n1 with:
IntFunction<IntUnaryOperator> inFu = x -> y -> x*y;
C. Replace line n1 with:
BiFunction<IntUnaryOperator> inFu = x -> y -> x*y;
D. Replace line n2 with:
IntStream newStream = stream.map(inFu.applyAsInt (10));
Answer: D