Friday, 22 April 2016

Oracle Java SE 7 Programmer II - Sample Questions

Well, it's that time of the year again. My previous boss wanted me to sit this exam (which is the second part of the exam I sat last year), and my new boss had no objection to me doing it. Today is the big day. As the last part of my study, I'm going through the sample questions, which don't seem to have reasoning for their answers, so here's my attempt at explaining it to get a better understanding myself.

1.Given: 

import java.util.*;
public class Primes2 {
public static void main(String[] args) {
Integer[] primes = {2, 7, 5, 3};
MySort ms = new MySort();
Arrays.sort(primes, ms);
for(Integer p2: primes)
System.out.print(p2 + " ");
}
static class MySort implements Comparator {
public int compare(Integer x, Integer y) {
return y.compareTo(x);
}
}
}

What is the result?
A) 2 3 5 7
B) 2 7 5 3
C) 7 5 3 2
D) Compilation fails.

The answer is: D
It looks like it's OK, but MySort says it implements Comparator, but doesn't override compare(Object o1, Object o2), so it won't compile.


2. Given: 

class Class1 {
String v1;
}
class Class2 {
Class1 c1;
String v2;
}
public class Class3 {
Class2 c1;
String i3;
}

Which three options correctly describe the relationship between the classes?
A) Class2 has-a i3
B) Class1 has-a v2
C) Class2 has-a v2
D) Class3 has-a v1
E) Class2 has-a Class3
F) Class2 has-a Class1

The answer is: C, D, F
I think this is a stupid question, but oh well. Class2 has-a v2, which you can see in the class declaration (it contains String v2). Class3 has-a v1, which it gets through Class2's Class1's v1. Class2 has-a Class1, which is also in the class declaration.


3. Given this code snippet: 

Map m = new HashMap();
MyKeys m1 = new MyKeys(1);
MyKeys m2 = new MyKeys(2);
MyKeys m3 = new MyKeys(1);
MyKeys m4 = new MyKeys(new Integer(2));
m.put(m1, "car");
m.put(m2, "boat");
m.put(m3, "plane");
m.put(m4, "hovercraft");
System.out.print(m.size());

And this class:

class MyKeys {
Integer key;
MyKeys(Integer k) { key = k; }
public boolean equals(Object o) {
return ((MyKeys)o).key == this.key;
}
}

What is the result?
A) 2
B) 3
C) 4
D) Compilation fails.
E) An exception is thrown at run time.

The answer is : C

The reason why it's 4 is because each MyKeys being put in the map is a new instance of key (see the fact that it's being created with the new keyword). Because MyKeys' equals() method does an == comparison, which checks for object instance equality, and each instance is new, m1 != m2 != m3 != m4, so each will be its own entry in the HashMap. It might look like it won't compile, because m4 is being created with new Integer(2), and the rest are ints, but with autoboxing, that'll be converted to an int, too, and I'm pretty sure with old pre-1.5 Java, it doesn't care.

4. Given: 

import java.util.*; 
public class MyScan { 
public static void main(String[] args) { 
String in = "1 a 10 . 100 1000"; 
Scanner s = new Scanner(in); 
int accum = 0; 
for(int x = 0; x < 4; x++) { 
accum += s.nextInt(); 
System.out.println(accum); 


What is the result? 
A) 4
B) 10
C) 111
D) 1111
E) Compilation fails.
F) An exception is thrown at run time.

The answer is: F
It will scan the 1, but when you call nextInt() again, it hits "a", which is not an int, nor can it be made into an int. So it throws an InputMismatchException.

5. Given: 

public class Truthy {
public static void main(String[] args) {
int x = 7;
assert(x == 6) ? "x == 6" : "x != 6";
}
}


What is the result if you try to compile Truthy.java and then run it with assertions enabled? 

A) Truthy.java does NOT compile. 
B) Truthy.java compiles and the output is "x != 6".
C) Truthy.java compiles and an AssertionError is thrown with no additional output.
D) Truthy.java compiles and an AssertionError is thrown with "x != 6" as additional output.

The answer is: A
Truthy does not compile because you can't assert Strings. Kinda unexpected, as I picked A because I thought it would fail due to the ternary operator, but assert(x==6)? true: true; compiles and runs successfully. So it seems to fail to compile, because you are returning Strings from the ternary operator, instead of the boolean that assert expects. So perhaps it's doing the equivalent of assert(x==6 ? "x==6" : "x!=6");, which I do not expect to compile - and it doesn't.

6.Given the code fragment: 

try {
// assume "conn" is a valid Connection
// assume a valid Statement object is created
// assume rollback invocations will be valid

// use SQL to add 10 to a checking account
Savepoint s1 = conn.setSavePoint();
// use SQL to add 100 to the same checking account

Savepoint s2 = conn.setSavePoint();
// use SQL to add 1000 to the same checking account

// insert valid rollback method invocation here

} catch (Exception e) { }



Which two statements are true? 
A) If conn.rollback(s1) is inserted, account will be incremented by 10.
B) If conn.rollback(s1) is inserted, account will be incremented by 1010.
C) If conn.rollback(s2) is inserted, account will be incremented by 100. 
D) If conn.rollback(s2) is inserted, account will be incremented by 110.
E) If conn.rollback(s2) is inserted, account will be incremented by 1000.

The answer is: A, D

Assuming this particular database supports multiple save points, if you rollback to s1, then you've only added 10 at that point. If you roll back to s2, then you've added 10 and 100, to give a total of 110. 

7. Given: 

public class Bees {
public static void main(String[] args) {
try {
new Bees().go();
} catch (Exception e ) {
System.out.println("thrown to main");
}
}
synchronized void go() throws InterruptedException {
Thread t1 = new Thread();
t1.start();
System.out.print("1 ");
t1.wait(5000);
System.out.print("2 ");
}
}


What is the result? 
A) 1 then 2 with little delay 
B) 1 then 2 after 5 seconds
C) 1 thrown to main 
D) 1 2 thrown to main
E) Compilation fails.


The answer is: C
So the first trick into working this one out is to work out what type of Exception is being thrown, because it's not an InterruptedException! Turns out it's an IllegalMonitorStateException, which is thrown when the current thread does not own the object's monitor. But what does that mean, since we're in a synchronized block, so shouldn't we own the current monitor? We own the monitor on Bees, and not the thread t1.

8. Given: 

import java.text.*;
public class Align {
public static void main(String[] args) {
String[] sa = {"111.234", "222.5678"};
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(3);
for(String s: sa) 
System.out.println(nf.parse(s));
}
}


What is the result? 
A) 111.234
222.567 
B) 111.234
222.568 
C) 111.234
222.5678
D) Compilation fails. 
E) An exception is thrown at run time.

The answer is: D
nf.parse() has a checked Exception: ParseException, so that call needs to be in a try block.

No comments: