Q1. Given the code fragment:
LocalDate valentinesDay =LocalDate.of(2015, Month.FEBRUARY, 14);
LocalDate nextYear = valentinesDay.plusYears(1);
nextYear.plusDays(15); //line n1
System.out.println(nextYear);
What is the result?
A. 2021-02-14
B. A DateTimeException is thrown.
C. 2021-02-29
D. A compilation error occurs at line n1.
Answer: B
Q2. Given:
What is the result?
A. 6 7 8
B. 7 8 9
C. 0 1 2
D. 6 8 10
E. Compilation fails
Answer: A
Q3. Given the code fragment:
9.
Connection conn = DriveManager.getConnection(dbURL, userName, passWord);
10.
String query = “SELECT id FROM Employee”;
11.
try (Statement stmt = conn.createStatement()) {
12.
ResultSet rs = stmt.executeQuery(query); 13.stmt.executeQuery(“SELECT id FROM Customer”);
14.
while (rs.next()) {
15.
//process the results 16.System.out.println(“Employee ID: “+ rs.getInt(“id”)); 17.}
18.
} catch (Exception e) {
19.
System.out.println (“Error”);
20.
}
Assume that:
The required database driver is configured in the classpath.
The appropriate database is accessible with the dbURL, userName, and passWord exists.
The Employee and Customer tables are available and each table has id column with a few
records and the SQL queries are valid.
What is the result of compiling and executing this code fragment?
A. The program prints employee IDs.
B. The program prints customer IDs.
C. The program prints Error.
D. compilation fails on line 13.
Answer: D
Q4. 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
Q5. Which statement is true about java.time.Duration?
A. It tracks time zones.
B. It preserves daylight saving time.
C. It defines time-based values.
D. It defines date-based values.
Answer: C
Reference: http://tutorials.jenkov.com/java-date-time/duration.html#accessing-the-time-of-a-duration
Q6. Given the code fragment:
List<String> listVal = Arrays.asList(“Joe”, “Paul”, “Alice”, “Tom”);
System.out.println (
// line n1
);
Which code fragment, when inserted at line n1, enables the code to print the count of string elements whose length is greater than three?
A. listVal.stream().filter(x -> x.length()>3).count()
B. listVal.stream().map(x -> x.length()>3).count()
C. listVal.stream().peek(x -> x.length()>3).count().get()
D. listVal.stream().filter(x -> x.length()>3).mapToInt(x -> x).count()
Answer: C
Q7. Given: What is the result?
A. Marrown String out of limits JesOran
B. Marrown String out of limits Array out of limits
C. Marrown String out of limits
D. Marrown NanRed JesOran
Answer: A
Q8. Given the code fragment:
UnaryOperator<Integer> uo1 = s -> s*2;line n1
List<Double> loanValues = Arrays.asList(1000.0, 2000.0);
loanValues.stream()
.filter(lv -> lv >= 1500)
.map(lv -> uo1.apply(lv))
.forEach(s -> System.out.print(s + “ “));
What is the result?
A. 4000.0
B. 4000
C. A compilation error occurs at line n1.
D. A compilation error occurs at line n2.
Answer: B
Q9. Given the code fragments:
4.
void doStuff() throws ArithmeticException, NumberFormatException, Exception {
5.
if (Math.random() >-1 throw new Exception (“Try again”);
6.
} and
24.
try {
25.
doStuff ( ):
26.
} catch (ArithmeticException | NumberFormatException | Exception e) {
27.
System.out.println (e.getMessage()); }
28.
catch (Exception e) {
29.
System.out.println (e.getMessage()); }
30.
}
Which modification enables the code to print Try again?
A. Comment the lines 28, 29 and 30.
B. Replace line 26 with:
} catch (Exception | ArithmeticException | NumberFormatException e) {
C. Replace line 26 with:
} catch (ArithmeticException | NumberFormatException e) {
D. Replace line 27 with:
throw e;
Answer: C
Q10. Given:
final class Folder {//line n1 //line n2 public void open () { System.out.print(“Open”); } } public class Test { public static void main (String [] args) throws Exception { try (Folder f = new Folder()) { f.open(); } } } Which two modifications enable the code to print Open Close?
A. Replace line n1 with:
class Folder implements AutoCloseable {
B. Replace line n1 with:
class Folder extends Closeable {
C. Replace line n1 with:
class Folder extends Exception {
D. At line n2, insert:
final void close () {
System.out.print(“Close”);
}
E. At line n2, insert:
public void close () throws IOException {
System.out.print(“Close”);
}
Answer: A,C