Oracle Java Certification Exam Sample Questions - Part 1

If you are preparing for Java SE 8 Oracle Certified Associate (OCA) certification and you want to check the sample questions that appear in the exam then you are in right place.

In this part 1 of the Oracle Java Certification Exam Sample Questions series, you can check out a few sample questions for reference for ava SE 8 Oracle Certified Associate (OCA) certification.

The answer to each question has given at end of this post.
Check out Part 2 - Oracle Java Certification Exam Sample Questions - Part 2.
Check out Part 3 - Oracle Java Certification Exam Sample Questions - Part 3 

1. Given the code fragment:

public class App {
    void calcBill() {
        // Line n1
        new Invoice().print();
    }
}
Which code fragment can be inserted at Line n1 to enable the class to compile?
A)
 
private class Invoice {
    void print() {System.out.println("Invoice Printed");}
}
B)
public class Invoice {
     void print() {System.out.println("Invoice Printed");}
}
C)
class Invoice {
    void print() {System.out.println("Invoice Printed");}
}
D)
protected class Invoice {
    void print() {System.out.println("Invoice Printed");}
}

2. Given

public interface Test {
    public void method1() {
        System.out.println("method1");
    }

    public
    default void method2() {
        System.out.println("method2");
    }

    public static void method3() {
        System.out.println("method3");
    }

    public abstract void method4();
}
Which statement is true?
A) Only method4() compiles
B) Only method2() and method4() compiles.
C) Only method2(), method3(), and method4() compiles.
D) Test.java compiles.

3. Given the code fragment:

import java.util.Arrays;

public class App {
    public static void main(String[] args) {
        String[] fruits = {
            "banana",
            "apple",
            "pears",
            "grapes"
        };
        Arrays.sort(fruits, (a, b) - > a.compareTo(b));
        for (String s: fruits) {
            System.out.print(" " + s);
        }
    }
}
What is the result?
A) apple banana grapes pears
B) pears grapes banana apple
C) banana apple pears grapes
D) Compilation fails

4. Given the code fragment:

import java.util.stream.Stream;

public class App {
    public static void main(String[] args) {
        Stream < Integer > nums = Stream.of(1, 2, 3, 4, 5);
        nums.filter(n - > n % 2 == 1);
        nums.forEach(p - > System.out.print(p));
    }
}
What is the result?
A) 135
B) 12345
C) Compilation fails
D) An exception is thrown at runtime

5. Given the code fragment:

import java.io.Closeable;
import java.io.IOException;

class MyResource1 implements Closeable {
    public void close() {
        System.out.print("r1 ");
    }
}

class MyResource2 implements AutoCloseable {
    public void close() throws IOException {
        System.out.print("r2 ");
        throw new IOException();
    }
}

public class App {
    public static void main(String[] args) {
        try (MyResource1 r1 = new MyResource1(); MyResource2 r2 = new MyResource2();) {
            System.out.print("try ");
        } catch (Exception e) {
            System.out.print("catch ");
            for (Throwable t: e.getSuppressed()) {
                System.out.println(t.getClass().getName());
            }
        }
    }
}
What is the result?
A) try r2 r1 catch java.io.IOException
B) try r2 r1 catch
C) try r1 r2 catch
D) Compilation fails.

Answers

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

Check out Part 2 - Oracle Java Certification Exam Sample Questions - Part 2.
Check out Part 3 - Oracle Java Certification Exam Sample Questions - Part 3 

Comments