Oracle Certified Professional (OCP) : Java SE 8 Programmer II EXAM Questions 25
Oracle Certified Professional (OCP) : Java SE 8 Programmer II EXAM Questions 25
enum Flags {
TRUE, FALSE;
Flags() {
System.out.println("HELLO");
}
}
class Outer {
private String name = "James Gosling";
//Insert inner class definition here
}
Options
A. It accepts two int arguments, adds them and returns
the int value
B. It accepts two String arguments, concatenates them
and returns the String instance
C. It accepts a String and an int arguments, concatenates
them and returns the String instance
D. Not possible to define the purpose
3 Answer
class Printer<String> {
private String t;
Printer(String t){
this.t = t;
}
}
public class Test {
public static void main(String[] args) {
Printer<Integer> obj = new Printer<>(100);
System.out.println(obj);
}
}
4 Options
A. 100
B. Some text containing @ symbol
C. Compilation error in Printer class
D. Compilation error in Test class
4 Answer
A. 100
B. Some text containing @ symbol
C. Compilation error in Printer class
D. Compilation error in Test class
4 Answer
A. 100
B. Some text containing @ symbol
C. Compilation error in Printer class
D.should
Type parameter Compilation error
not be a Java in Test&class
keyword a valid Java identifier. Naming
convention for Type parameter is to use uppercase single character.
In class Printer<String>, 'String' is a valid Java identifier and hence a valid type
parameter even though it doesn't follow the naming convention of uppercase
single character.
Printer<Integer> obj = new Printer<>(100); => Type argument is Integer and it
correctly creates an instance of Printer class passing Integer object 100.
Printer class doesn't override toString() method and hence
'System.out.println(obj);' prints some text containing @ symbol.
What will be the result of compiling and executing Test class?
5
package com.udayan.ocp;
import java.util.ArrayList;
import java.util.List;
A. AB
B. Compilation error
C. Runtime exception
5 Answer
A. AB
B. Compilation error
C. Runtime exception
5 Answer
A. AB
B. Compilation error
C. Runtime exception
A. true:true:3
B. false:false:3
C. false:true:3
D. true:false:3
7 Answer
A. true:true:3
B. false:false:3
C. false:true:3
D. true:false:3
7 Answer
deque.pop() => removes and returns the HEAD element, true in this case.
deque => [*false, true, false].
deque.peek() => retrieves but doesn't remove the HEAD element, false in this
case. deque => [*false, true, false].
A. 55
B. 56
C. 66
D. Compilation error
8 Answer
A. 55
B. 56
C. 66
D. Compilation error
8 Answer
A. 55
B. 56
C. 66
andThen isD.
the Compilation error
default method defined in Consumer interface, so it is
invoked on consumer reference variable.
System.out.println(map);
}
}
9 Options
A. {1=null, 2=two}
B. {1=ONE, 2=TWO}
C. {1=ONE, 2=two}
D. {1=null, 2=two}
E. {1=null, 2=TWO}
9 Answer
A. {1=null, 2=two}
B. {1=ONE, 2=TWO}
C. {1=ONE, 2=two}
D. {1=null, 2=two}
E. {1=null, 2=TWO}
9 Answer
A. {1=null, 2=two}
B. {1=ONE,
Though reference variable2=TWO}
of NavigableMap is used but putIfAbsent method is from
C. {1=ONE,
Map interface. 2=two}
It is a default method added in JDK 8.0.
D. {1=null,
BiConsumer<T, 2=two}t, U u);
U> : void accept(T
Lambda expression corresponding to 'map::putIfAbsent;' is '(i, s) ->
E. {1=null, 2=TWO}
map.putIfAbsent(i, s)'
This is the case of "Reference to an Instance Method of a Particular Object".
TreeMap sorts the data on the basis of natural order of keys.
import java.util.Arrays;
import java.util.List;
A. 1st4th
B. 2nd3rd
C. Optional[1st4th]
D. Optional[2nd3rd]
10 Answer
A. 1st4th
B. 2nd3rd
C. Optional[1st4th]
D. Optional[2nd3rd]
10 Answer
A. 1st4th
B. 2nd3rd
C. Optional[1st4th]
D. Optional[2nd3rd]
filter method filters all the strings ending with "d".
import java.util.stream.IntStream;
A. 6
B. 14
C. 98
D. None of the above options
11 Answer
A. 6
B. 14
C. 98
D. None of the above options
11 Answer
A. 6
B. 14
IntStream.rangeClosed(int start, int end) => Returns a sequential
C. start
stream from 98 to end, both inclusive and with a step of 1.
D. None of the above options
IntStream.map(IntUnaryOperator) => Returns a stream consisting
of the results of applying the given function to the elements of this
stream.
package com.udayan.ocp;
import java.util.Scanner;
A. Compilation error
B. Runtime Exception
C. On execution program terminates successfully after
printing 'HELLO' on to the console
12 Answer
A. Compilation error
B. Runtime Exception
C. On execution program terminates successfully
after printing 'HELLO' on to the console
12 Answer
A. Compilation error
B. Runtime Exception
C. On execution program terminates successfully
after printing 'HELLO' on to the console
Even though Scanner is created in try-with-resources block,
calling close() method explicitly doesn't cause any problem.
Scanner class allows to invoke close() method multiple times.
In this case, it will be called 3 times: twice because of
scan.close() and once because of try-with-resources
statement.
import java.time.*;
A. 12:0:0
B. 13:0:0
C. 14:0:0
13 Answer
A. 12:0:0
B. 13:0:0
C. 14:0:0
13 Answer
You should be aware of Day light saving mechanism to answer this question.
Suppose daylight time starts at 2 AM on particular date.
A. 12:0:0
Current time: 1:59:59 [Normal time].
Next second: 3:00:00 [Time is not 2:00:00 rather it is 3:00:00. It is Daylight saving time].
B. 13:0:0
Clock just jumped from 1:59:59 to 3:00:00.
C. 14:0:0
Now Suppose daylight time ends at 2 AM on particular date.
Current time: 1:59:59 [Daylight saving time].
Next second: 1:00:00 [Time is not 2:00:00 rather it is 1:00:00. Clock switched back to normal
time].
Clock just went back from 1:59:59 to 1:00:00.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
A. Yes
B. No
14 Answer
A. Yes
B. No
14 Answer
A. Yes
B. No
DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL
); statement returns formatter to format date part.
date is of LocalDateTime type hence it has both date
part and time part.
'formatter.format(date)' simply formats the date part
and ignores time part.
NOTE: You should aware of other formatter related
methods for the OCP exam, such as: 'ofLocalizedTime'
and 'ofLocalizedDateTime'
Given code of Test.java file:
15
package com.udayan.ocp;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
A. Files.lines(Paths.get("F:\\Book. B. Files.lines(Paths.get("F:\\Book.java"
java")).forEach(System.out::pri )).stream().forEach(System.out::prin
ntln); tln);
C. Files.readAllLines(Paths.get("F: D. Files.readAllLines(Paths.get("F:\\Bo
\\Book.java")).forEach(System. ok.java")).stream().forEach(System.o
out::println); ut::println);
15 Answer
A. Files.lines(Paths.get("F:\\Book. B. Files.lines(Paths.get("F:\\Book.java"
java")).forEach(System.out::pri )).stream().forEach(System.out::prin
ntln); tln);
C. Files.readAllLines(Paths.get("F: D. Files.readAllLines(Paths.get("F:\\Bo
\\Book.java")).forEach(System. ok.java")).stream().forEach(System.o
out::println); ut::println);
15 Answer
A. Files.lines(Paths.get("F:\\Book. B. Files.lines(Paths.get("F:\\Book.java"
java")).forEach(System.out::pri )).stream().forEach(System.out::prin
Below are thentln);
declarations of lines and readAllLines
tln); methods from Files
class:
public static Stream<String> lines(Path path) throws IOException {...}
public static List<String> readAllLines(Path path) throws IOException {...}
C. Files.readAllLines(Paths.get("F: D. Files.readAllLines(Paths.get("F:\\Bo
\\Book.java")).forEach(System.
'Files.lines(Paths.get("F:\\Book.java"))' ok.java")).stream().forEach(System.o
returns Stream<String> object.
out::println); ut::println);
Hence forEach() can be invoked but stream() can't be invoked.
A. Compilation error.
B. An exception is thrown at runtime.
C. Program executes successfully and produces no output.
D. Program executes successfully and prints 'Welcome!' on to the console.
16 Answer
A. Compilation error.
B. An exception is thrown at runtime.
C. Program executes successfully and produces no output.
D. Program executes successfully and prints 'Welcome!' on to the console.
16 Answer
A. Windows
Note that Compilation
shortcuterror.
and symbolic links are different.
Shortcut is just a regular file and symbolic link is a File System object.
B. An exception is thrown at runtime.
C. symbolic
To create Programlink executes
used in this successfully
question, I usedand produces
below command: no output.
D. Program
C:\TEMP>mklink executes successfully and prints 'Welcome!' on to the console.
msg .\Parent\Child\Message.txt
And below message was displayed on to the console for the successful creation of
symbolic link 'symbolic link created for msg <<===>> .\Parent\Child\Message.txt'.
Given code successfully reads the file and prints 'Welcome!' on to the console.
Given structure of EMPLOYEE table:
17 EMPLOYEE (ID integer, FIRSTNAME varchar(100), LASTNAME
varchar(100), SALARY real, PRIMARY KEY (ID))
EMPLOYEE table contains below records:
101 John Smith 12000
102 Sean Smith 15000
103 Regina Williams 15500
104 Natasha George 14600
Also assume:
URL is correct and db credentials are: root/password.
SQL query is correct and valid.
The JDBC 4.2 driver jar is configured in the classpath.
What will be the result of compiling and executing Test class?
17 package com.udayan.ocp;
import java.sql.*;
A. An
Given query exception
returns is thrown
below records: at runtime.
101 B. John
101 Smith 12000
102 C. Sean
102 Smith 15000
103 Regina Williams 15500
104
D. 103
Natasha George 14600
E. 104
Initially cursor is just before the 1st record.
'rs.relative(-3);' doesn't throw any exception but keeps the cursor just before the 1st
record. According to javadoc of relative method, "Attempting to move beyond the
first/last row in the result set positions the cursor before/after the first/last row". Same
is true for absolute method as well.
import java.util.Locale;
import java.util.ResourceBundle;
A. French/Canada
B. Canada
C. Hindi
D. India
E. Runtime Exception
18 Answer
A. French/Canada
B. Canada
C. Hindi
D. India
E. Runtime Exception
18 Answer
import java.util.concurrent.*;
20 package com.udayan.ocp;
class A<T extends String> {
}
class B<T super String> {
}
Which of the following statement is correct?
20 package com.udayan.ocp;
class A<T extends String> {
}
class B<T super String> {
}
Which of the following statement is correct?
import java.util.ArrayList;
import java.util.List;
System.out.println(list2);
}
}
21 Options
A. ABC
B. BC
C. Runtime exception
D. Compilation error
21 Answer
A. ABC
B. BC
C. Runtime exception
D. Compilation error
21 Answer
A. ABC
list1 is of List<String> type and contains 2 elements "A" and "B".
B. BC
list2 is of List<? extends Object> exception
C. Runtime type, which means any List whose type extends
from Object. As String extends Object, hence 'List<? extends Object> list2 = list1;'
works. D. Compilation error
import java.util.ArrayList;
import java.util.List;
A. HELLO
B. Compilation error
C. Program executes successfully but nothing is printed
on to the console.
22 Answer
A. HELLO
B. Compilation error
C. Program executes successfully but nothing is printed
on to the console.
22 Answer
A. HELLO
B. Compilation error
C. Program executes successfully but nothing is printed
on to the console.
Method signature for anyMatch method:
boolean anyMatch(Predicate<? super T>) : Returns true if
any of the stream element matches the given Predicate. If
stream is empty, it returns false and predicate is not
evaluated.
import java.util.concurrent.*;
A. RecursiveTask<Long>
B. RecursiveTask
C. RecursiveAction
D. RecursiveAction<Long>
E. RecursiveTask<Object>
F. RecursiveAction<Object>
23 Answer
A. RecursiveTask<Long>
B. RecursiveTask
C. RecursiveAction
D. RecursiveAction<Long>
E. RecursiveTask<Object>
F. RecursiveAction<Object>
23 Answer
RecursiveTask is a All
Select generic
That class
Applywhich extends from ForkJoinTask<V> class.
RecursiveTask<V> declares compute() method as: 'protected abstract V
compute();'A. RecursiveTask<Long>
In the givenB.code overriding method returns Long, so classes from which class
RecursiveTask
Task can extend are: RecursiveTask<Long>, RecursiveTask<Object> [co-variant
C. RecursiveAction
return typeD.
in overriding method] and RecursiveTask [co-variant return type in
RecursiveAction<Long>
overriding method].
E. RecursiveTask<Object>
F. RecursiveAction<Object>
RecursiveAction is a non-generic class which extends from ForkJoinTask<Void>
class.
RecursiveAction declared compute() method as: 'protected abstract void
compute();'
In the given code overriding method returns Long, hence RecursiveAction can't
be used as super class of Task.
What will be the result of compiling and executing Test class?
24 package com.udayan.ocp;
import java.util.Scanner;
A. Normal Termination
B. Exception is thrown at runtime
C. Compilation error
24 Answer
A. Normal Termination
B. Exception is thrown at runtime
C. Compilation error
24 Answer
A. Normal Termination
B. Exception is thrown at runtime
C. Compilation error
E null
PRINT
10
25 Answer
A. Compilation error B. PRINT
null
10
C. null D. PRINT
10 10
PRINT Null
E null
PRINT
10
25 Answer
A. Compilation error B. PRINT
null
10
C. null D. PRINT
10 10
PRINT Null
Method reference 'Test::print' is for the run() method implementation of Runnable and
'Test::get'
E isnull
for the call() method implementation of Callable.
Future<?> PRINT
is valid return type for both the method calls. get() method throws 2 checked
exceptions: InterruptedException and ExecutionException, hence given code compiles fine.
10
get() method waits for task completion, hence 'PRINT' will be printed first.
future1.get() returns null and future2.get() returns 10.
OCA – 1Z0-809
Exam Practice Test