Oracle Java Certification Exam Sample Questions - Part 3

In Oracle Java Certification Exam Sample Questions - Part 2, we have seen a few Oracle Java Certification exam sample questions. In part 3, 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.
Check out Part 2 -  Oracle Java Certification Exam Sample Questions - Part 2.

1. Given:

public class App {
    static void doCalc(byte...a) {
        System.out.print("byte...");
    }

    static void doCalc(long a, long b) {
        System.out.print("long, long");
    }

    static void doCalc(Byte s1, Byte s2) {
        System.out.print("Byte, Byte");
    }

    public static void main(String[] args) {
        byte b = 5;
        doCalc(b, b);
    }
}
A. byte…
B. long, long
C. Byte, Byte
D. compilation error

Answer

B. long, long

2. Which code fragment correctly assigns a numeric literal?

A)
byte b1 = b1011;
B)
byte b2 = 1011b;
C)
byte b3 = 0b1001;
D)
byte b4 = 0xb001;

Answer

C)
byte b3 = 0b1001;

3. Given the fragment:

public class MathFun {
    public static void main(String[] args) {
        int number1 = 0b0111;
        int number2 = 0111_000;
        System.out.println("Number1: " + number1);
        System.out.println("Number2: " + number1);
    }
}
What is the result?
A)
Number1: 7
Number2: 7
B)
Number1: 7
Number2: 111_000
C)
Number1: 0b0111
Number2: 0111000
D) Compilation fails


Answer

A)
Number1: 7
Number2: 7

4. What is the output of the following program?

public class Test {

    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = new String("hello");

        s2 = s2.intern();
        System.out.println(s1 == s2);
    }
}
a) false
b) true
c) None

Answer

b) true
Explanation: We know that the intern() method will return the String object reference from the string pool since we assign it back to s2 and now both s1 and s2 are having the same reference. It means that s1 and s2 references pointing to the same object.

5. 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).

Answer

d) The program will result in an exception (ClassCastException).
Explanation: The dynamic type of variable obj is Base1 that you were trying to cast into Base2. This is not supported and so results in an exception.
Check out Part 1 - Oracle Java Certification Exam Sample Questions - Part 1.
Check out Part 2 -  Oracle Java Certification Exam Sample Questions - Part 2.


Comments