Original Oracle Core Java dumps (OCJA:1z0-808)
Oracle Core Java Exam Questions (OCJA:1z0-808)
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 first post. Other post has questions on OCJP:1z0-809.
This post is 22 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 a 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 !!
1) Consider the following program:
class
NullInstanceof {
public
static void main(String []args) {
String
str = null;
if(str
instanceof Object) // NULLCHK
System.out.println("str
is Object");
else
System.out.println("str
is not Object");
} }
Which one of the following options
correctly describes the behavior of this program?
a)
This program will result in a compiler error in line marked with comment
NULLCHK.
b)
This program will result in a NullPointerException in line marked with comment
NULLCHK.
c)
When executed, this program will print the following: str is Object.
d)
When executed, this program will print the following: str is not Object.
------------------------------------------
2) Consider the following program:
class
ArrayCompare {
public
static void main(String []args) {
int
[]arr1 = {1, 2, 3, 4, 5};
int
[]arr2 = {1, 2, 3, 4, 5};
System.out.println("arr1
== arr2 is " + (arr1 == arr2));
System.out.println("arr1.equals(arr2)
is " + arr1.equals(arr2));
System.out.println("Arrays.equals(arr1,
arr2) is " +
java.util.Arrays.equals(arr1,
arr2));
} }
Which one of the following options
provides the output of this program when executed?
a)
arr1 == arr2 is false
arr1.equals(arr2)
is false
Arrays.equals(arr1,
arr2) is true
b)
arr1 == arr2 is true
arr1.equals(arr2)
is false
Arrays.equals(arr1,
arr2) is true
c)
arr1 == arr2 is false
arr1.equals(arr2)
is true
Arrays.equals(arr1,
arr2) is true
d)
arr1 == arr2 is true
arr1.equals(arr2)
is true
Arrays.equals(arr1,
arr2) is false
e)
arr1 == arr2 is true
arr1.equals(arr2)
is true
Arrays.equals(arr1,
arr2) is true
------------------------------------------
3) Consider the
following program:
interface Side { String getSide(); }
class Head implements Side {
public String getSide() { return "Head "; }
}
class Tail implements Side {
public String getSide() { return "Tail "; }
}
class Coin {
public static void overload(Head side) { System.out.print(side.getSide()); }
public static void overload(Tail side) { System.out.print(side.getSide()); }
public static void overload(Side side) { System.out.print("Side "); }
public static void overload(Object side) { System.out.print("Object "); }
public static void main(String []args) {
Side firstAttempt = new Head();
Tail secondAttempt = new Tail();
overload(firstAttempt);
overload((Object)firstAttempt);
overload(secondAttempt);
overload((Side)secondAttempt);
}}
What is the output of this program when
executed?
a)
Head Head Tail Tail
b)
Side Object Tail Side
c)
Head Object Tail Side
d)
Side Head Tail Side
------------------------------------------
4) Consider the following program:
class
Base {
public
static void foo(Base bObj) { System.out.println("In
Base.foo()"); bObj.bar();
}
public
void bar() { System.out.println("In
Base.bar()");
} }
class
Derived extends Base {
public
static void foo(Base bObj) {
System.out.println("In
Derived.foo()");
bObj.bar();
}
public
void bar() {
System.out.println("In
Derived.bar()");
} }
class
OverrideTest {
public
static void main(String []args) {
Base
bObj = new Derived();
bObj.foo(bObj);
} }
What is the output of this program when
executed?
a)
In
Base.foo()
In
Base.bar()
b)
In
Base.foo()
In
Derived.bar()
c)
In
Derived.foo()
In
Base.bar()
d)
In
Derived.foo()
In
Derived.bar()
------------------------------------------
5) Consider the following program:
class CannotFlyException extends Exception {}
interface Birdie {
public abstract void fly() throws CannotFlyException;
}
interface Biped {
public void walk();
}
abstract class NonFlyer {
public void fly() { System.out.print("cannot fly "); } // LINE A
}
class Penguin extends NonFlyer implements Birdie, Biped { // LINE B
public void walk() { System.out.print("walk "); }
}
class PenguinTest {
public static void main(String []args) {
Penguin pingu = new Penguin();
pingu.walk();
pingu.fly();
}
}
Which one of the following options
correctly describes the behavior of this program?
a)
Compiler error in line with comment LINE A because fly() does not declare to
throw CannotFlyException.
b)
Compiler error in line with comment LINE B because fly() is not defined and
hence need to declare it abstract.
c)
It crashes after throwing the exception CannotFlyException.
d)
When executed, the program prints “walk cannot fly”.
------------------------------------------
6) Consider the following program:
abstract class AbstractBook {
public String name;
}
interface Sleepy {
public String name = "undefined";
}
class Book extends AbstractBook implements Sleepy {
public Book(String name) {
this.name = name; // LINE A
}
public static void main(String []args) {
AbstractBook philosophyBook = new Book("Principia Mathematica");
System.out.println("The name of the book is " + philosophyBook.name); // LINE B
}
}
Which one of the following options
correctly describes the behavior of this program?
a)
The program will print the output "The name of the book is Principia
Mathematica".
b)
The program will print the output "The name of the book is
undefined".
c)
The program will not compile and result in a compiler error "ambiguous
reference to name" in line marked with comment LINE A.
d)
The program will not compile and result in a compiler error "ambiguous
reference to name" in line marked with comment LINE B.
------------------------------------------
7) Consider the following program:
class InvalidValueException extends IllegalArgumentException {}
class InvalidKeyException extends IllegalArgumentException {}
class BaseClass {
void foo() throws IllegalArgumentException {
throw new IllegalArgumentException();
}
}
class DeriClass extends BaseClass {
public void foo() throws IllegalArgumentException {
throw new InvalidValueException();
}
}
class DeriDeriClass extends DeriClass {
public void foo() { // LINE A
throw new InvalidKeyException();
}
}
public class Sctpl {
public static void main(String []args) {
try {
BaseClass base = new DeriDeriClass();
base.foo();
} catch(RuntimeException e) {
System.out.println(e);
} } }
Which one of the following options
correctly describes the behavior of this program?
a)
The program prints the following: InvalidKeyException.
b)
The program prints the following: RuntimeException.
c)
The program prints the following: IllegalArgumentException.
d)
The program prints the following: InvalidValueException.
e)
When compiled, the program will result in a compiler error in line marked with
comment Line A due to missing throws clause.
------------------------------------------
8) Consider the following program:
class AssertionFailure {
public static void main(String []args) {
try {
assert false;
} catch(RuntimeException re) {
System.out.println("RuntimeException");
} catch(Exception e) {
System.out.println("Exception");
} catch(Error e) { // LINE A
System.out.println("Error" + e);
} catch(Throwable t) {
System.out.println("Throwable");
}}}
This
program is invoked in command line as follows:
java
AssertionFailure
Choose one of the following options
describes the behavior of this program:
a)
Compiler error at line marked with comment LINE A
b)
Prints "RuntimeException" in console
c)
Prints "Exception"
d)
Prints "Error"
e)
Prints "Throwable"
f)
Does not print any output on console.
------------------------------------------
9) Consider the following program:
class ForTest {
public static void main(String... ars){
int i = 0;
for ( ; i<7 font="" i="" nbsp="">7>
for (i=0; ; i++) break;//(2)
for (i=0; i<6 font="" i="">6>
for ( ; ; ); //(4)
}}
Select the one correct answer. Options are :
a)
The code will fail to compile because the initialization part(1) is missing.
b)
The code will fail to compile because the condition checking part(2) is
missing.
c)
The code will fail to compile because
the expression in the last section of
the for statement (3) is empty.
d)
The code will fail to compile because
the for statement (4) is invalid.
e)
The code will compile without error, but will never terminate when run.
------------------------------------------
10) Given:
10:
public class From {
11:
String title;
12:
int value;
13:
public From() { // constructor 1
14:
title += " World";
15:
}
16:
public From(int value) { // overloading constructor
17:
this.value = value;
18:
title = "Hi";
19:
From();
20:
}
21:
}
and:
30:
From c = new From(5);
31:
System.out.println(c.title);
What
is the result ? Options are:
A.
Hi
B.
Hi World
C.
Compilation fails
D.
Hi World 5
E.
The code runs with no output.
------------------------------------------
11) Given
5. class Payload {
6. private int weight;
7. public Payload (int w) { weight = w; }
8. public void setWeight(int w) { weight = w; }
9. public int getWeight() { return weight;
10.
} }
11.
public class TestPayload {
12.
static void changePayload(Payload p) {/* insert code */}
13.
public static void main(String... args) {
14.
Payload p = new Payload(406);
15.
p.setWeight(106);
16.
changePayload(p);
17.
System.out.println("p is " + p.getWeight());
18.
} }
Which
code fragment,inserted at the end of line 12, produces the output p is 145 ?
Options are :
A.
p.setWeight(145);
B.
p.changePayload(145);
C.
p=new Payload(145);
D.
Payload.setWeight(145);
E.
p=payload.setWeight(145);
------------------------------------------
12) Which statements cause a compiler error? (Select
one correct answer)
1. float[]
a = new float(3);
2. float a2[ ] = new float[ ];
3. float[ ] a1 = new float[3];
4. float a3[ ]
= new float[3];
5. float a5[ ] = {1.0f, 2.0f, 2.0f};
Options:
A.
2,4
B.
1,5
C.
4,5
D.
1,2
------------------------------------------
13) Given
class
MultiDimTest {
public
static void main(String args[ ]) {
int
x = 4;
int
b[][][] = new int[x][x=2][x];
System.out.println(b.length
+ "," + b[0].length + "," + b[0][0].length);
}}
What
is the output after executing the above program ? Options:
A.
4,2,2
B.
2,2,2
C.
4,4,4
D.
4,2,4
------------------------------------------
14) Given:
4.
class Cola { Pepsi p = new Pepsi(); }
5.
class SoftDrink { void test(){
System.out.print("s");} }
6.
class Pepsi extends SoftDrink { void
test() { System.out.print("p");}}
7.
8. class CocaCola extends Cola {
9. public static void main(String [] args) {
10.
CocaCola cc = new CocaCola();
11.
cc.p.test();
12.
Cola t = new CocaCola();
13.
t.p.test();
14.
}
15.
}
Which
two are true? (Choose two.)
Options:
A.
The output is sp.
B.
The output is pp.
C.
SoftDrink is-a Pepsi.
D.
SoftDrink has-a Pepsi.
E.
CocaCola is-a SoftDrink.
F.
CocaCola has-a SoftDrink.
------------------------------------------
15) Given two files, BlackBear.java and Fish.java:
1.
package animals.mammals;
2.
3.
public class BlackBear extends Bear {
4.
void hunt() {
5.
Fish f = findFish();
6.
f.consume();
7.
}
8.
}
1.
package animals.fish;
2.
3.
public class Fish extends WaterSpecies {
4.
public void consume() { /* do stuff */ }
5.
}
If
both classes are in the correct directories for their packages, and
the Mammal class correctly defines the findFish() method, which
change allows this code to Compile and Run ?
Options:
A.
add import animals.mammals.*; at line 2 in Fish.java
B.
add import animals.fish.*; at line 2 in
BlackBear.java
C.
add import animals.fish.Fish.*; at line 2 in BlackBear.java
D.
add import animals.mammals.BlackBear.*; at line 2 in Fish.java
------------------------------------------
Explanation to just few problems :
------------------------------------------
Ans 3) Side
Object Tail Side
Overloading
is based on the static type of the objects (while overriding and runtime
resolution resolves to the dynamic type of the objects). Here is how the calls
to the overload() method are resolved:
•
overload(firstAttempt); --> firstAttempt is of type Side, hence it resolves
to overload(Side).
•
overload((Object)firstAttempt); -> firstAttempt is casted to Object, hence
it resolves to overload(Object).
•
overload(secondAttempt); -> secondAttempt is of type Tail, hence it resolves
to overload(Tail).
•
overload((Side)secondAttempt); -> secondAttempt is casted to Side, hence it
resolves to overload(Side).
Ans 5) When
executed, the program prints "walk cannot fly".
In
order to override a method, it is not necessary for the overridden method to
specify an exception. However, if the exception is specified, then the
specified exception must be the same or a subclass of the specified exception
in the method defined in the super class (or interface).
Ans 14) B , F
Analysis :
- Cola has-a Pepsi , Pepsi is-a Softdrink , CocaCola is-a Cola.
- On combining we get : Cola has-a Pepsi is-a Softdrink.
- Hence Cola has-a Softdrink.
- Hence CocaCola also has-a Softdrink.
Ans 15) B
Draw tree structure
for package.subpackage.classes. Then place Mammal class under animals.fish
package. So that Fish class can be used by findfish() method of Mammal class.
As animals.mammals.BlackBear wants to use findfish() , hence place the import
statement at line 2 of BlackBear.java.
Comments