Oracle Professional Java Exam Questions (OCJA:1z0-809)
Oracle Professional Java Exam Questions (OCJA:1z0-809)
by Rocky Jagtiani
Oracle Certified Professional in Java
(cleared OCPJP6 : 1Z0-851 in 2012 with 94%)
I train on Oracle Certifications from last 7 years
My training's are listed on https://training.suven.net
This article is split into 2 blog post. This is the second post. First post has questions on OCJA:1z0-808.
This post is 45 mins read only.
To re-assess my skills, I decided to give the OCJA:1z0-808 exam this month. Gave some International certification exams after a long time. Happy to clear OCJA:1z0-808 with 89% and OCJP:1z0-809 with 83%. The purpose of this blog-post is to highlight different types of Java problems, I mean programs one to would have to solve for clearing the Java Certification exams. You can find the 1z0-808 and 1z0-809 exam specs under "Course highlights" section here.
To run any of the programs on this blog-post , you may click here
Before you run any of the programs , try solving them without running on a online Java compiler.
You can even copy-paste these programs on your local machine and run them, but first try solving using only your own brains.
Tips :
- Time yourself well. Solve each problem with in 1.5 mins.
- Don't use paper and pen. Solve in your mind.
- Avoid all distractions like whatsapp, fb or ... . Lock your self in a study room and solve.
All the best. Let's start !!
import
java.math.BigDecimal;
class NumberTest {
public static void
main(String []args) {
Number [] numbers = new
Number[4];
numbers[0] = new
Number(0); // NUM
numbers[1] = new
Integer(1);
numbers[2] = new
Float(2.0f);
numbers[3] = new
BigDecimal(3.0); // BIG
for(Number num : numbers)
{
System.out.print(num +
" ");
}}}
Which one
of the following options correctly describes the behavior of this program?
a) Compiler error in line
marked with comment NUM because Number cannot be
instantiated.
b) Compiler error in line
marked with comment BIG because BigDecimal does not
inherit from Number.
c) When executed, this
program prints the following: 0 1 2.0 3.
d) When executed, this
program prints the following: 0.0 1.0 2.0 3.0.
------------------------------------------
class Base {
public Base() {
System.out.println("Base");
}
}
class Derived extends Base
{
public Derived() {
System.out.println("Derived");
}
}
class DeriDerived extends
Derived {
public DeriDerived() {
System.out.println("DeriDerived");
}
}
class Test {
public static void
main(String []args) {
Derived b = new
DeriDerived();
}
}
Options
a) Base
Derived
DeriDerived
b) Derived
DeriDerived
c) DeriDerived
Derived
Base
d) DeriDerived
Derived
------------------------------------------
3) Consider
the following program:
class Base {
public Base() {
System.out.print("Base
");
}
public Base(String s) {
System.out.print("Base:
" + s);
}
}
class Derived extends Base
{
public Derived(String s) {
super(); // Stmt-1
super(s); // Stmt-2
System.out.print("Derived
");
}
}
class Test {
public static void main(String
[]args) {
Base a = new
Derived("Hello ");
}
}
Select
three correct options from the following list:
a) Removing Stmt-1 will
make the program compilable and it will print the following:
Base Derived.
b) Removing Stmt-1 will
make the program compilable and it will print the following:
Base: Hello Derived.
c) Removing Stmt-2 will
make the program compilable and it will print the following:
Base Derived.
d) Removing both Stmt-1
and Stmt-2 will make the program compilable and it will print
the following: Base
Derived.
e) Removing both Stmt-1
and Stmt-2 will make the program compilable and it will print
the following: Base: Hello
Derived.
------------------------------------------
4) Consider
the following program and choose the correct option from the list of options:
class Base {
public void test() {}
}
class Base1 extends Base {
public void test() {
System.out.println("Base1");
}
}
class Base2 extends Base {
public void test() {
System.out.println("Base2");
}
}
class Test {
public static void
main(String[] args) {
Base obj = new Base1();
((Base2)obj).test(); //
CAST
}
}
a) The program will print
the following: Base1.
b) The program will print
the following: Base2.
c) The compiler will
report an error in the line marked with comment CAST.
d) The program will result
in an exception (ClassCastException).
------------------------------------------
5) Consider
the following program:
interface EnumBase { }
enum AnEnum implements
EnumBase { // IMPLEMENTS_INTERFACE ONLY_MEM;
}
class EnumCheck {
public static void
main(String []args) {
if(AnEnum.ONLY_MEM
instanceof AnEnum) {
System.out.println("yes,
instance of AnEnum");
}
if(AnEnum.ONLY_MEM
instanceof EnumBase) {
System.out.println("yes,
instance of EnumBase");
}
if(AnEnum.ONLY_MEM
instanceof Enum) { // THIRD_CHECK
System.out.println("yes,
instance of Enum");
}
}
}
Which one
of the following options is correct?
a) This program results in
a compiler error in the line marked with comment IMPLEMENTS_INTERFACE.
b) This program results in
a compiler error in the line marked with comment THIRD_CHECK.
c) When executed, this
program prints the following:
yes, instance of AnEnum
d) When executed, this
program prints the following:
yes, instance of AnEnum
yes, instance of EnumBase
e) When executed, this
program prints the following:
yes, instance of AnEnum
yes, instance of EnumBase
yes, instance of Enum
------------------------------------------
6) Consider
the following program:
class WildCard {
interface BI {}
interface DI extends BI {}
interface DDI extends DI
{}
static class C {}
static void foo(C arg) {}
public static void
main(String []args) {
foo(new C()); //
ONE
foo(new C()); //
TWO
foo(new C());
// THREE
foo(new C()); // FOUR
}
}
Which of
the following options are correct?
a) Line marked with
comment ONE will result in a compiler error.
b) Line marked with
comment TWO will result in a compiler error.
c) Line marked with
comment THREE will result in a compiler error.
d) Line marked with
comment FOUR will result in a compiler error.
------------------------------------------
7) In the context of Singleton pattern, which one of the following statements is true?
a) class P {
15) Consider
the following program and predict the output:
7) In the context of Singleton pattern, which one of the following statements is true?
a) A Singleton class must
not have any static members.
b) A Singleton class has a
public constructor.
c) A Factory class may use
Singleton pattern.
d) All methods of the
Singleton class must be private.
In the
context of Singleton pattern, which one of the following statements is true?
a) A Singleton class must
not have any static members.
b) A Singleton class has a
public constructor.
c) A Factory class may use
Singleton pattern.
d) All methods of the
Singleton class must be private.
------------------------------------------
8) Which one
of the following class definitions will compile without any errors?
a) class P
static T s_mem;
}
b) class Q {
T mem;
public Q(T arg) {
mem = arg;
}
}
c) class R {
T mem;
public R() {
mem = new T();
}
}
d) class S {
T []arr;
public S() {
arr = new T[10];
}
}
------------------------------------------
9) Which one
of the following interfaces declares a single method named iterator()?
(Note:
Implementing this interface allows an object to be the target of the for-each statement.)
a) Iterable
b) Iterator
c) Enumeration
d) ForEach
------------------------------------------
10) Consider
the following program:
import java.util.*;
class ListFromVarargs {
public static
List asList1(T... elements) {
ArrayList temp =
new ArrayList<>();
for(T element : elements)
{
temp.add(element);
}
return temp;
}
public static
List asList2(T... elements) {
ArrayList temp =
new ArrayList<>();
for(T element : elements)
{
temp.add(element);
}
return temp;
}
public static
List asList3(T... elements) {
ArrayList temp =
new ArrayList<>();
for(T element : elements)
{
temp.add(element);
}
return temp;
}
public static
List asList4(T... elements) {
List temp = new
ArrayList();
for(T element : elements)
{
temp.add(element);
}
return temp;
}
}
Which of
the asList definitions in this program will result in a compiler error?
a) The definition of
asList1 will result in a compiler error.
b) The definition of
asList2 will result in a compiler error.
c) The definition of
asList3 will result in a compiler error.
d) The definition of
asList4 will result in a compiler error.
e) None of the definitions
(asList1, asList2, asList3, asList4) will result in a compiler error.
------------------------------------------
11) Consider
the following program:
import java.util.*;
class TemplateType {
public static void
main(String []args) {
List
new ArrayList<>();
// ADD_MAP
Map
- ,
List
list.add(null); //
ADD_NULL
list.add(map);
list.add(new
HashMap
- ,
List>());
// ADD_HASHMAP
for(Map element : list) {
// ITERATE
System.out.print(element +
" ");
}}}
Which one
of the following options is correct?
a) This program will
result in a compiler error in line marked with comment ADD_MAP.
b) This program will
result in a compiler error in line marked with comment ADD_HASHMAP.
c) This program will
result in a compiler error in line marked with comment ITERATE.
d) When run, this program
will crash, throwing a NullPointerException in line marked with comment
ADD_NULL.
e) When run, this program
will print the following: null {} {}
------------------------------------------
12) Which of
the following statements are true about java.sql.Savepoint? (select all that
apply)
a) Savepoint is a point
within the current transaction that can be referenced from the Connection.rollback() method
b) When a transaction is
rolled back to a savepoint all changes made after that savepoint are undone.
c) Savepoints must be
named. It is not possible to have "unnamed savepoints".
d) java.sql.Savepoint is
an abstract class; it is implemented by the classes such as JDBCSavepoint,
ODBCSavepoint, and TransactionSavepoint in the java.sql package.
------------------------------------------
13) Consider
the following program and choose the appropriate option:
import java.util.*;
class Test {
public static void
main(String []args) {
Map map
=
new HashMap(); //#1
Map
map2 =
new HashMap(); //#2
Map
map3 = new HashMap<>(); //#3
Map<> map4 = new
HashMap(); //#4
}
}
a) Statement #1 and #2
will compile successfully.
b) Statement #2 and #3
will compile successfully.
c) Statement #3 and #4
will compile successfully.
d) Statement #4 and #1
will compile successfully.
------------------------------------------
14) Which of
the following statements are true regarding the classes or interfaces defined in the
java.util.concurrent package?
a) The Executor interface
declares a single method execute(Runnable command) that executes the given
command at sometime in the future.
b) The Callable interface
declares a single method call() that computes a result.
c) The Exchanger class
provides a “synchronization point at which threads can pair and swap elements
within pairs”.
d) The TimeUnit
enumeration represents time duration and is useful for specifying timing
parameters in concurrent programs.
------------------------------------------
import java.util.regex.*;
class Test {
public static void
main(String[] args) {
String str1 =
"xxzz";
String str2 =
"xyz";
String str3 =
"yzz";
Pattern pattern = Pattern.compile("(xx)*y?z{1,}");
Matcher matcher =
pattern.matcher(str1);
System.out.println(matcher.matches());
System.out.println(pattern.matcher(str2).matches());
System.out.println(
Pattern.compile("(xx)*y?z{1,}").
matcher(str3).matches());
}
}
a) true
false
true
b) true
false
false
c) false
false
false
d) false
false
true
e) true
true
true
------------------------------------------
16) Consider
the following program and predict the output:
import java.util.regex.*;
class Test {
public static void
main(String[] args) {
String str =
"Suneetha
N.=9876543210, Pratish Patil=9898989898";
Pattern pattern =
Pattern.compile("(\\w+)(\\s\\w+)(=)(\\d{10})");
Matcher matcher =
pattern.matcher(str);
String newStr =
matcher.replaceAll("$4:$2,$1");
System.out.println(newStr);
}
}
a) 9876543210:
N.,Suneetha, 9898989898: Patil,Pratish
b) Suneetha N.=9876543210,
Pratish Patil=9898989898
c) Suneetha N.=9876543210,
9898989898: Patil,Pratish
d) This program throws a
runtime exception.
------------------------------------------
17) Consider
the following program:
import java.util.*;
class Format {
public static void
main(String []args) {
Formatter formatter = new
Formatter();
Calendar calendar =
Calendar.getInstance(Locale.US);
calendar.set(/* year =*/
2012, /* month = */ Calendar.FEBRUARY, /* date = */ 1);
formatter.format("%tY/%
System.out.println(formatter);
}
}
Which one
of the following options correctly describes the behavior of this program?
a) The program throws a
MissingFormatArgumentException.
b) The program throws an
UnknownFormatConversionException.
c) The program throws an
IllegalFormatConversionException.
d) The program prints the
following: 12/February/01.
------------------------------------------
18) Consider
the following program:
import java.util.*;
public class
ResourceBundle_it_IT extends ListResourceBundle {
public Object[][]
getContents() {
return contents;
}
static final Object[][]
contents = {
{ "1",
"Uno" },
{ "2",
"Duo" },
{ "3",
"Trie" },
};
public static void
main(String args[]) {
ResourceBundle resBundle =
ResourceBundle.getBundle("ResourceBundle",
new Locale("it", "IT", ""));
System.out.println(resBundle.getObject(new
Integer(1).toString()));
}
}
Which one
of the following options correctly describes the behavior of this program?
a) This program prints the
following: Uno.
b) This program prints the
following: 1.
c) This program will throw
a MissingResourceException.
d) This program will throw
a ClassCastException.
------------------------------------------
19)
import java.util.*;
import
java.util.concurrent.*;
class SortedOrder {
public static void
main(String []args) {
Set set =
new CopyOnWriteArraySet(); // #1
set.add("2");
set.add("1");
Iterator
iter = set.iterator();
set.add("3");
set.add("-1");
while(iter.hasNext()) {
System.out.print(iter.next()
+ " ");
}}}
Which one
of the following options correctly describes the behavior of this program?
a) The program prints the
following: 2 1.
b) The program prints the
following: 1 2.
c) The program prints the
following: -1 1 2 3.
d) The program prints the
following: 2 1 3 -1.
e) The program throws a
ConcurrentModificationException
f ) This program results
in a compiler error in statement #1
------------------------------------------
20) Consider
the following program:
import java.io.*;
class CloseableImpl
implements Closeable {
public void close() throws
IOException {
System.out.println("In
CloseableImpl.close()");
}
}
class AutoCloseableImpl
implements AutoCloseable {
public void close() throws
Exception {
System.out.println("In
AutoCloseableImpl.close()");
}
}
class AutoCloseCheck {
public static void
main(String []args) {
try (Closeable
closeableImpl = new CloseableImpl();
AutoCloseable
autoCloseableImpl
= new AutoCloseableImpl())
{
} catch (Exception ignore)
{
// do nothing
}
finally {
// do nothing
}
}
}
Which one
of the following options correctly shows the output of this program when the
program is executed?
a) This program does not
print any output in console.
b) This program prints the
following output:
In
AutoCloseableImpl.close()
c) This program prints the
following output:
In
AutoCloseableImpl.close()
In CloseableImpl.close()
d) This program prints the
following output:
In CloseableImpl.close()
In
AutoCloseableImpl.close()
------------------------------------------
21) Consider the following program and predict the output:
------------------------------------------
21) Consider the following program and predict the output:
import java.nio.file.*;
class PathInfo {
public static void
main(String[] args) {
// assume that the current
directory is "D:\workspace\ch14-test"
Path testFilePath =
Paths.get(".\\Test");
System.out.println("file
name:" + testFilePath.getFileName());
System.out.println("absolute
path:" + testFilePath.toAbsolutePath());
System.out.println("Normalized
path:" + testFilePath.normalize());
}
}
a) file name:Test
absolute
path:D:\workspace\ch14-test\.\Test
Normalized path:Test
b) file name:Test
absolute
path:D:\workspace\ch14-test\Test
Normalized path:Test
c) file name:Test
absolute
path:D:\workspace\ch14-test\.\Test
Normalized path:D:\workspace\ch14-test\.\Test
d) file name:Test
absolute
path:D:\workspace\ch14-test\.\Test
Normalized
path:D:\workspace\ch14-test\Test
------------------------------------------
22) Which one of the following statement is NOT correct?
23) Consider the following program and choose the right option:
import java.util.Locale;
22) Which one of the following statement is NOT correct?
a) You need to use a
Statement when you need to send a SQL statement to the database
without any parameter.
b) PreparedStatement
represents a precompiled SQL statement.
c) PreparedStatement can
handle IN and OUT parameters.
d) CallableStatement is
used to execute stored procedures.
------------------------------------------23) Consider the following program and choose the right option:
import java.util.Locale;
class Test {
public static void
main(String []args) {
Locale locale1 = new
Locale("en"); //#1
Locale locale2 = new
Locale("en", "in"); //#2
Locale locale3 = new
Locale("th", "TH", "TH"); //#3
Locale locale4 = new
Locale(locale3); //#4
System.out.println(locale1
+ " " + locale2 + " " + locale3 + " " + locale4);
}
}
a) This program will print
the following: en en_IN th_TH_TH_#u-nu-thaith_TH_TH_#u-nu-thai.
b) This program will print
the following: en en_IN th_TH_TH_#u-nu-thai
(followed by a runtime
exception).
c) This program results in
a compiler error at statement #1.
d) This program results in
a compiler error at statement #2.
e) This program results in
a compiler error at statement #3.
f ) This program results
in a compiler error at statement #4.
------------------------------------------
Explanation to just few problems :
------------------------------------------
yes, instance of AnEnum
yes, instance of EnumBase
yes, instance of Enum
An enumeration can
implement an interface (but cannot extend a class, or cannot be a base class). Each enumeration constant
is an object of its enumeration type. An enumeration automatically extends the
abstract class java.util.Enum.
Hence, all the three
instanceof checks succeed.
Ans 8) b) class Q {
T mem;
public Q(T arg) {
mem = arg;
}
}
Option a): You cannot make
a static reference of type T.
Option c) and d): You
cannot instantiate the type T or T[] using new operator.
Ans 10) b) The definition of
asList2 will result in a compiler error.
In the asList2 method
definition, temp is declared as ArrayList. Since the template type is
a wild-card, you cannot put any element (or modify the container). Hence, the method
call temp.add(element); will result in a
compiler error.
Ans 16) c) Suneetha N.=9876543210,
9898989898: Patil,Pratish
The
first contact does not match with the specified regex (since "." is not covered
by "\w+"); hence, the first part of the string is unchanged. The second part of
string matches with the specified regex, so the replace rearranges the substring.
Ans 23) f ) This program results
in a compiler error at #4.
The Locale class supports
three constructors that are used in statements #1, #2, and #3; however, there is no
constructor in the Locale class that takes another Locale object as
argument, so the compiler
gives an error for statement #4.
----------------------
more sample questions are given here
----------------------
----------------------
more sample questions are given here
----------------------
Comments