Oracle Java Certification Exam Sample Questions - Part 2

In Oracle Java Certification Exam Sample Questions - Part 1, we have seen a few Oracle Java Certification exam sample questions. In part 2, we will see a few more sample questions which will help to prepare for the exam.
Check out Part 1 - Oracle Java Certification Exam Sample Questions - Part 1.
The answer to each question has given at end of this post.

1. Which is a valid functional interface?

A)
public interface Useful<E> {
    E getStuff();
    void putStuff(E e);
}
B)
public interface Useful {
    void doStuff();
    default void doOtherStuff() {}
}
C)
@FunctionalInterface
public interface Useful{ 
   default void doStuff(){} 
}
D)
public interface Useful {
    abstract void doStuff();
    abstract void doCalc();
}

2. Given:

public abstract class Customer {
    private String name;

    public Customer(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public abstract void buy();
}
Which two statements are true about Customer?
A) The Customer class cannot be extended.
B) The Customer class cannot be instantiated.
C) Subclasses of Customer cannot override getName() method.
D) Concrete subclasses of Customer must use a default constructor.
E) Concrete subclasses of Customer must implement the buy() method.
F) Subclasses of Customer must implement the buy() method.

3. Given the code fragment:

import java.util.ArrayDeque;
import java.util.Queue;

public class App {
    public static void main(String[] args) {
        Queue < String > products = new ArrayDeque < String > ();
        products.add("p1");
        products.add("p2");
        products.add("p3");
        System.out.print(products.peek());
        System.out.print(products.poll());
        System.out.println("");
        products.forEach(s - > System.out.print(s));
    }

}
What is the result?
A.
p1p1
p2p3
B.
p1p2
p1p2p3
C.
p1p2
p3
D.
p1p1
p1p2p3

4. Given the code fragment:

try (Connection con = DriverManager.getConnection(url, uname, pwd)) {
    Statement stmt = con.createStatement();
    System.out.print(stmt.executeUpdate("INSERT INTO User VALUES (500,'Ramesh')"));
}
Assuming the SQL query executes properly, what is the result?
A) true
B) false
C) 1

5. Given the code fragment:

public class TestFun {
    public static void main(String[] args) {
        List<Integer> nums = Arrays.asList(1,2,3,4,5);
        // Line n1
    }
}
Which code fragment can be inserted at Line n1 to enable the code to print 2 4?
A)
nums.peek(n -> n%2 == 0)
 .forEach( s -> System.out.print(" "+s));
B)
nums.filter(n -> n%2 == 0)
 .forEach( s -> System.out.print(" "+s));
C)
nums.map(n -> n%2 == 0)
 .forEach( s -> System.out.print(" "+s));
D)
nums.stream()
 .filter(n -> n%2 == 0)
 .forEach( s -> System.out.print(" "+s));

Answers

1. B
2. B and E
3. A
4. C
5. D

Comments