Java 11 Certification Dumps for OCAJP 1Z0-815 latest 2020 Set 3

    What is the output of the program?
    public class Test {
        void switchNumber(double number) {
           switch (number) { // Line 1
                case 1:
                    System.out.println("Integer");
              case 1.0:
                   System.out.println("Floating point");
           }
       }
       public static void main(String[] args) {
          Test test = new Test();
            test.switchNumber(1)
    // Line 2
        }
    }


    Options are :

    • Integer
    • Floating point
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :Compilation fails on line 1

    Which array initialization is invalid?
    int[][] matrix1 = {?{1, 2}, {3}};
    int[][] matrix2;
    matrix2 = {?{1, 2}, {3, 4}};
    int[][] matrix3 = new int[][]{?{1, 2}, {3, 4}};
    int[][] matrix4 = {new int[2], new int[2]};


    Options are :

    • matrix1
    • matrix2
    • matrix3
    • matrix4
    • None

    Answer :matrix2

    What is the output of the given code fragment?
    List list = new ArrayList<>();
    list.add("A");
    list.addAll(1, List.of("A"));
    list.clear();
    System.out.println(list);


    Options are :

    • []
    • [A]
    • [A, A]
    • null
    • An IndexOutOfBoundsException is thrown
    • Compilation fails

    Answer :[]

    What's the program's output?
    String string = "Hey";
    StringBuilder builder = new StringBuilder(string);
    System.out.println(string.equals(builder))
    // Line 1
    System.out.println(string.contentEquals(builder))
    // Line 2


    Options are :

    • TRUE TRUE
    • TRUE FALSE
    • FALSE TRUE
    • FALSE FALSE
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :FALSE TRUE

    What is the output of the program?
    public class Test {
       static int number;
        public Test() {
           number = 1;
        }
       {
          number = 2;
        }
        static {
           number = 3;
       }
       public static void main(String[] args) {
            Test test = new Test();
           System.out.println(test.number);
       }
    }


    Options are :

    • 0
    • 1
    • 2
    • 3
    • Compilation fails

    Answer :1

    What is the program's output?
    package f;
    public class Foo {
       Foo() { }
        public void printMessage() {
           System.out.println("Foo");
       }
    }
    And:
    package b;
    import f.Foo;
    public class Bar extends Foo {
        Bar() { } // Line 1
        @Override // Line 2
       public void printMessage() {
          System.out.println("Bar");
        }
       public static void main(String[] args) {
            Foo foo = new Bar();
            foo.printMessage();
       }
    }


    Options are :

    • Foo
    • Bar
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :Compilation fails on line 1

    What is the output of the program?
    int i;
    int j;
    for (i = 1, j = 3; i <= j; i = i + 2, j++) {
        i++;
    }
    System.out.println(i + " " + j);


    Options are :

    • 2 3
    • 4 4
    • 7 5
    • It goes into an infinite loop
    • It fails to compile

    Answer :7 5

    Which types of exceptions/errors must be caught or specified with a throws clause in the method's declaration?


    Options are :

    • Checked exceptions and unchecked exceptions
    • Checked exceptions and errors
    • Unchecked exceptions and errors
    • Errors only
    • Checked exceptions only
    • Unchecked exceptions only

    Answer :Checked exceptions only

    Which expression doesn't set the string variable to "a-b"?


    Options are :

    • String string = String.join("-", "a", "b");
    • String string = String.join("-", {"a", "b"});
    • String string = String.join("-", List.of("a", "b"));
    • String string = String.join("-", new StringBuilder("a"), new StringBuilder("b"));
    • None of the above

    Answer :String string = String.join("-", {"a", "b"});

    What is the output of the given code fragment?
    List strings = new ArrayList<>(List.of("A", "B"));
    Iterator iterator = strings.listIterator(1);
    strings.set(1, "C");
    if (iterator.hasNext()) {
        System.out.println(iterator.next());
    }


    Options are :

    • TRUE
    • FALSE
    • An exception is thrown
    • Compilation fails

    Answer :An exception is thrown

    It's known that the foo and bar modules require the my.module module. Which of the following declarations makes the foo and bar packages visible in those requiring modules?


    Options are :

    • module my.module { exports foo to foo, bar; exports bar to foo, bar; }
    • module my.module { exports foo, bar; }
    • module my.module { exports * to foo; exports * to bar; }
    • module my.module { exports foo & bar to foo, bar; }
    • None of the above

    Answer :module my.module { exports foo to foo, bar; exports bar to foo, bar; }

    Which method when inserted at position 1 allows the Bar class to compile?
    class Foo {
        protected Number getValue(Number number) {
            return number;
        }
    }
    class Bar extends Foo {
       @Override
        /* Position 1 */
    }


    Options are :

    • public Integer getValue(Integer number) { return number; }
    • public int getValue(Number number) { return 0; }
    • public Object getValue(Number number) { return number; }
    • public String getValue(Number number) { return String.valueOf(number); }
    • None of the above

    Answer :None of the above

    What is the given code's output?
    int a = 4, b = 2;
    System.out.println(a + b + "=a+b=" + a + b);


    Options are :

    • 42=a+b=42
    • 42=a+b=6
    • 6=a+b=42
    • 6=a+b=6
    • Compilation fails

    Answer :6=a+b=42

    Which line fails to compile?
    public class Test {
        public Test() {
       text3 = text2 + "c"
    // Line 1
        }
        String text1 = "a" + text2
    // Line 2
        String text2 = "b"
    // Line 3
    String text3
    }


    Options are :

    • Line 1
    • Line 1 and Line 2
    • Line 1, Line 2 and Line 3
    • Line 2
    • Compilation succeeds

    Answer :Line 2

    Which line is a valid variable declaration?
    float f1 = _12.345F
    // Line 1;
    float f2 = 12_.345F
    // Line 2;
    float f3 = 12._345F
    // Line 3;
    float f4 = 12.345_F
    // Line 4;
    float f5 = 12.345F_
    // Line 5


    Options are :

    • Line 1
    • Line 2
    • Line 3
    • Line 4
    • Line 5
    • None

    Answer :None

    What is the output when executing the given code fragment?
    String[] array = new String[5];
    Arrays.fill(array, "Hello");
    System.out.println(array[2]);


    Options are :

    • Hello
    • null
    • An ArrayIndexOutOfBoundsException is thrown

    Answer :null

    What is the program's output?
    class Foo {
        protected String myField = "Foo";
        protected static Object myMethod() {
            return "Foo";
        }
    }
    public class Bar extends Foo {
        public String myField = "Bar";
        public String myMethod() {
            return "Bar";
       }
        public static void main(String[] args) {
            Foo foo = new Bar();
            System.out.println(foo.myField);
            System.out.println(foo.myMethod());
       }
    }


    Options are :

    • Foo Foo
    • Foo Bar
    • Bar Foo
    • Bar Bar
    • Compilation fails

    Answer :Compilation fails

    Which methods modify a field of the Foo class?
    public class Foo {
        String a, b, c, d;
        void setA(String b) {
            a = b;
       }
        void setB(String b) {
            b = this.a;
        }
        void setC(String d) {
            this.c = this.d;
       }
        void setD(String a) {
            this.d = d ;
        }
    }


    Options are :

    • setA and setB
    • setA and setC
    • setB and setD
    • setA, setC and setD
    • setA only
    • setB only

    Answer :setA and setC

    What is the program's output?
    public class Test {
        static boolean x;
        static boolean y;
       public static void main(String[] args) {
            x = x || y && !x;
            y = y && x || y;
            System.out.println(x);
           System.out.println(y);
        }
    }


    Options are :

    • TRUE TRUE
    • TRUE FALSE
    • FALSE TRUE
    • FALSE FALSE
    • Compilation fails

    Answer :FALSE FALSE

    Which method in the Bar class must be removed to make the given code compile?
    class Foo {
        Object myMethod(String argument) {
            // a valid body
        }
    }
    public class Bar extends Foo {
        /* Method 1 */
        String myMethod(String argument) {
            // a valid body
       }
       /* Method 2 */
        String myMethod(Object argument) {
            // a valid body
        }
        /* Method 3 */
        Object myMethod(Object argument) {
            // a valid body
        }
    }


    Options are :

    • Method 1
    • Method 2
    • Method 3
    • Either method 1 or method 2
    • Either method 2 or method 3
    • Either method 3 or method 1

    Answer :Either method 2 or method 3

    What is the program's output?
    public class Test {
        static String text = "foo";
        public static void main(String[] args) {
            String text = "bar";
           if (text.equals("foo")) {
               String text = "fuz";
            } else {
                String text = "baz";
           }
           System.out.println(text);
        }
    }


    Options are :

    • foo
    • bar
    • fuz
    • baz
    • Compilation fails

    Answer :Compilation fails

    What is the output when executing the given code?
    int[] array1 = {1, 2, 3};
    int[] array2 = Arrays.copyOf(array1, 4);
    System.out.println(array2[3]);


    Options are :

    • 0
    • 3
    • 4
    • An ArrayIndexOutOfBoundsException is thrown

    Answer :0

    Which types of exception shouldn't be caught and handled?


    Options are :

    • Checked exceptions
    • Uncheck exceptions
    • Errors
    • Checked and unchecked exceptions
    • Unchecked exceptions and errors

    Answer :Errors

    What is the output if we execute the given program with the following command?

    java Test "1" "2" 3

    public class Test {
       public static void main(String[] args) {
            int sum = 0;
            for (String arg : args) {
                sum += Integer.valueOf(arg);
            }
            System.out.println(sum);
        }
    }


    Options are :

    • 123
    • 15
    • 33
    • 6
    • An exception is thrown

    Answer :6

    What is the output of the given code fragment?

    List<String>  list = new ArrayList<>(List.of("A", "B"));
    String[] array1 = {"C"};
    String[] array2 = list.toArray(array1);
    array1[0] = "D";
    System.out.println(Arrays.toString(array2));


    Options are :

    • [A, B]
    • [C]
    • [D]
    • [D, B]
    • An IndexOutOfBoundsException is thrown

    Answer :[A, B]

    Given a method:

    Which of the following is not an overloaded method of the given one?
    String getValue(int value) {
        return String.valueOf(value);
    }


    Options are :

    • String getValue() { return "Java"; }
    • Object getValue(String value) { return value; }
    • int getValue(int value) { return value; }
    • None of the above

    Answer :int getValue(int value) { return value; }

    Assumes local time of the machine on which the given code fragment runs is 10:10:10 AM. What is the output?

    LocalTime now = LocalTime.of(10, 10, 10);
    LocalTime start = LocalTime.of(8, 30).withMinute(now.getMinute());
    LocalTime end = LocalTime.of(12, 30).withMinute(now.getMinute());
    long timePassed = now.until(start, ChronoUnit.HOURS);
    long timeToGo = now.until(end, ChronoUnit.HOURS);
    System.out.println(timePassed + " " + timeToGo);


    Options are :

    • -2 2
    • -2 1
    • -1 1
    • -1 2
    • 1 1
    • 1 2

    Answer :-2 1

    Which two methods in the Bar class fail to compile?
    class Foo {
        void myMethod(Integer number) {
            // a valid body
        }
    }
    public class Bar extends Foo {
       /* Method 1 */
        int myMethod(Integer integer) {
            // a valid body
        }
       /* Method 2 */
        static void myMethod(Integer integer) {
            // a valid body;     }
        /* Method 3 */
       String myMethod(long number) {
            // a valid body
        }
       /* Method 4 */
        void myMethod(int number) {
           // a valid body
        }
        /* Method 5 */
        void myMethod(Integer number1, Integer number2) {
           // a valid body
       }
    }


    Options are :

    • Method 1
    • Method 2
    • Method 3
    • Method 4
    • Method 5

    Answer :Method 1 Method 2

    Which statement is correct about the say method?
    package foo;
    public class MyFoo {
        protected void say() {
            System.out.println("Hello!");
        }
    }


    Options are :

    • It's accessible to only classes that are in the same package and extend the MyFoo class
    • It's accessible to only classes that extends the MyFoo class, regardless of the containing package
    • It's accessible to only classes that extends the MyFoo class as well as all the classes in the foo package
    • It's accessible to only classes in the same module that are public or protected
    • None of the above

    Answer :It's accessible to only classes that extends the MyFoo class as well as all the classes in the foo package

    What is the output of the given code?
    String text = "hiheyhello";
    int index = text.lastIndexOf("h", 4);
    System.out.println(index);


    Options are :

    • 0
    • 1
    • 2
    • 3

    Answer :2

    Which command prints the module descriptor?


    Options are :

    • java --print-descriptor
    • java -check-module
    • javac --check-descriptor
    • jdeps --check
    • jdeps --check-module

    Answer :jdeps --check

    Which of the following are checked exceptions?


    Options are :

    • ArithmeticException
    • ClassCastException
    • IndexOutOfBoundsException
    • IOException
    • NullPointerException
    • None of the above

    Answer :IOException

    Which line will throw an exception when the given code is run?
    List original = new ArrayList<>();
    original.add(null)
    // Line 1
    list copy = List.copyOf(original)
    // Line 2
    System.out.println(copy.get(0))
    // Line 3


    Options are :

    • Line 1
    • Line 2
    • Line 3
    • No exception is thrown

    Answer :Line 2

    What is the output of the given code fragment?
    Integer[] array1 = new Integer[1];
    int[] array2 = new int[1];
    System.out.println(array1[0] + " " + array2[0]);


    Options are :

    • 0 0
    • null 0
    • null null
    • A NullPointerException is thrown
    • An ArrayIndexOutOfBoundsException is thrown

    Answer :null 0

    What is the output of the given code?
    int x = 1, y = 2, z = 3;
    x += y *= z -= x;
    System.out.println(x + " " + y + " " + z);


    Options are :

    • 1 6 2002
    • 5 4 2002
    • 7 6 2004
    • 8 3 2009
    • Compilation fails

    Answer :5 4 2002

    What is the program's output?

    package foo;
    public class Greeting {
        public String say() {
            return "Foo";
        }
    }
    And:
    package bar;
    public class Greeting {
        public String say() {
            return "Bar";
        }
    }
    And:
    package test;
    import foo.*;
    import bar.*
    // Line 1
    public class MyTest {
        public static void main(String[] args) {
            Greeting greeting = new Greeting()
    // Line 2
            System.out.println(greeting.say());
       }
    }


    Options are :

    • Foo
    • Bar
    • Either Foo or Bar
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :Compilation fails on line 2

    What is the program's output?
    class Foo {
        String getName() {
            return "Foo";
        }
    }
    class Bar extends Foo {
        String getName() {
            return "Bar";
       }
    }
    public class Baz extends Foo, Bar {
        String getName() {
            return "Baz";
       }
       public static void main(String[] args) {
            Baz baz = new Baz();
            String name = baz.getName();
            System.out.println(name);
        }
    }


    Options are :

    • Foo
    • Bar
    • Baz
    • Compilation fails

    Answer :Compilation fails

    What is the program's output?
    class Foo {
        Foo() {
           System.out.println("Foo");
        }
    }
    public class Bar extends Foo {
        Bar() {
            System.out.println("Bar");
            super();
        }
    public static void main(String[] args) {
           new Bar();
        }
    }


    Options are :

    • Bar
    • Foo; Bar
    • Bar; Foo
    • Compilation fails

    Answer :Compilation fails

    What is the output of the given code?
    float addend1 = 4L / 2
    // Line 1
    long addend2 = (long) 1.8
    // Line 2
    var sum = addend1 + addend2
    // Line 3
    System.out.println(sum);


    Options are :

    • 3
    • 4
    • 3
    • 4
    • Compilation fails on line 1 only
    • Compilation fails on line 2 only

    Answer :3

    What is the output of the given code?
    try {
        throw new FileNotFoundException();
    } catch (Exception | IOException e) {
        if (e instanceof FileNotFoundException) {
           System.out.println("FileNotFoundException");
        } else if (e instanceof IOException) {
            System.out.println("IOException");
        } else if (e instanceof Exception) {
            System.out.println("Exception");
        }
    }


    Options are :

    • FileNotFoundException
    • IOException
    • Exception
    • An exception is thrown up the call stack
    • Compilation fails

    Answer :Compilation fails

    Which of the following isn't a command that can be used to create and build application?


    Options are :

    • javac
    • javap
    • jar
    • jlink
    • jmod
    • jshell

    Answer :jshell

    What is the output of the program?
    public class Test {
        public static void main(String[] args) {
            Test test = new Test();
            String text1 = test.convert('a');
            String text2 = test.convert('b');
            System.out.println(text1 + text2);
        }
       String convert(char character) {
            String text;
            switch (character) { // Line 1
                case 'a':
                   text = "A";
               case 'b':
                    text = "B";
                    break;
                case 'c':
                    text = "C";
            }
            return text
    // Line 2
        }
    }


    Options are :

    • AB
    • BB
    • AC
    • BC
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :Compilation fails on line 2

    What is the program's output?
    class Foo {
        void myMethod() {
            System.out.println("Foo");
        }
    }
    public class Bar extends Foo {
        void myMethod() {
            System.out.println("Bar");
        }
       public static void main(String[] args) {
            Foo foo = new Foo();
            Bar bar = (Bar) foo;
            bar.myMethod();
        }
    }


    Options are :

    • Foo
    • Bar
    • A ClassCastException is thrown
    • Compilation fails

    Answer :A ClassCastException is thrown

    After which line all objects created in the main method are eligible for garbage collection?
    public class Test {
        public static void main(String[] args) {
            Test test1 = new Test();
            Test test2 = null;
            Test test3 = new Test();
            Test test4 = null;
            test1 = test2
    // Line 1
            test2 = test3
    // Line 2
            test3 = test4
    // Line 3
            test2 = test4
    // Line 4
           test4 = test1
    // Line 5
        }
    }


    Options are :

    • Line 1
    • Line 2
    • Line 3
    • Line 4
    • Line 5

    Answer :Line 4

    What is the output of the given program?
    public class Test {
        static public void main(String[] args) {
            var result = 0;
           int i1 = 1;
            int i2 = 2;
            float i3 = i1++ + ++i2;
    // Line 1
            result = (i1 + i2) / i3
    // Line 2
            System.out.println(result);
        }
    }


    Options are :

    • 1
    • 1
    • 1.25
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :Compilation fails on line 2

    Which of the following is not correct about a module descriptor?


    Options are :

    • It must be named module-info.java
    • It must contain at least the module keyword and module name
    • It must be located at the root of the module?s source-file hierarchy
    • Any module that the current module depends on must be explicitly specified in the descriptor with the requires directive
    • None of the above

    Answer :Any module that the current module depends on must be explicitly specified in the descriptor with the requires directive

    Which of the following code fragments prints "true" to the console?


    Options are :

    • String[] array1 = {"hi", "hey", "hello"}; String[] array2 = {"hi"}; boolean isEqual = Arrays.equals(array1, 1, array2); System.out.println(isEqual);
    • String[] array1 = {"hi", "hey", "hello"}; String[] array2 = {"hey", "hello", "hi"}; boolean isEqual = Arrays.equals(array1, array2); System.out.println(isEqual);
    • String[] array1 = {"hi", "hey", "hello"}; String[] array2 = {"hey", "hello"}; boolean isEqual = Arrays.equals(array1, 1, 3, array2); System.out.println(isEqual);
    • String[] array1 = {"hi", "hey", "hello"}; Object[] array2 = {"hey", "hello"}; boolean isEqual = Arrays.equals(array1, 1, 3, array2, 0, 2); System.out.println(isEqual);
    • None of the above

    Answer :String[] array1 = {"hi", "hey", "hello"}; Object[] array2 = {"hey", "hello"}; boolean isEqual = Arrays.equals(array1, 1, 3, array2, 0, 2); System.out.println(isEqual);

    What is the output of the given code fragment?
    List list = new ArrayList();
    list.add(1)
    // Line 1
    Object object = list.get(list.size())
    // Line 2
    System.out.println(object);


    Options are :

    • 0
    • 1
    • An ArrayIndexOutOfBoundsException is thrown
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :An ArrayIndexOutOfBoundsException is thrown

    Which lines cause a compile-time error?


    Options are :

    • Line 1 only
    • Line 2 only
    • Line 3 only
    • Line 4 only
    • Line 1 and line 2
    • Line 1 and line 4

    Answer :Line 4 only

    What is the output of the given code?
    String text = "JAVA";
    while (text.equalsIgnoreCase("JAVA")) {
        text = text.toLowerCase();
    }
    System.out.println(text);


    Options are :

    • java
    • JAVA
    • It runs into an infinite loop
    • Compilation fails

    Answer :It runs into an infinite loop

    Given two source files:

    package test;
    public class Foo { }
    And:
    package test;
    public class Bar {
        Foo foo;
    }

    These files are placed in a test folder:

    test

    Foo.java

    Bar.java

    Which statement is correct about compiling these files using the javac command?


    Options are :

    • It must be run in the test directory
    • It must be run in the parent of the test directory
    • The Foo.java file must be specified before the Bar.java file on the command line
    • The Bar.java file must be specified before the Foo.java file on the command line
    • None of the above

    Answer :None of the above

    boolean input1 = true, input2 = false;; boolean output1 = input1 = input2;; boolean output2 = input2 = input1;; boolean output3 = input1 == input2;; System.out.println(input1 + " " + output2 + " " + output3);; What is the output?


    Options are :

    • true true true
    • true true false
    • true false true
    • true false false
    • false true true
    • false false true

    Answer :false false true

    What is the program's output?
    public class Test {
        public static void main(String[] args) {
            try {
                Test test = new Test();
                test.isNegative(-1);
                System.out.println("try");
            } catch (Exception e) {
                System.out.println("catch");
            } finally {
                System.out.println("finally");
            }
        }
        void isNegative(int input) {
            if (input >= 0)
                throw new RuntimeException();
        }
    }


    Options are :

    • try
    • try catch
    • try catch finally;
    • try finally
    • catch
    • catch finally

    Answer :try finally

    What is the program's output?
    class Foo {
        String name = "Foo";
        void print() {
           System.out.println(this.name)
    // Line 1
        }
    }
    class Bar extends Foo {
        String name = "Bar";
        void print() {
            super.print()
    // Line 2
       }
    }
    public class Test {
        public static void main(String[] args) {
            new Bar().print();
        }
    }


    Options are :

    • Foo
    • Bar
    • null
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :Foo

    Which two changes enable the given class to be encapsulated?
    class Data {
        List<Integer> numbers;
        Data(List<Integer> numbers) {
            this.numbers = numbers;
        }
        double getAverage() {
            if (numbers.isEmpty()) {
                return 0;
           }
            double sum = 0;
            for (int i : numbers) {
                sum += i;
        }
            return sum / numbers.size();
        }
    }


    Options are :

    • Declare the numbers field as private
    • Declare the constructor as public
    • Declare the constructor as private
    • Declare the getAverage method as public
    • Change the body of the constructor to this.numbers = List.copyOf(numbers);
    • Add a setter for the numbers field:; ; public void setNumbers(List numbers) {; this.numbers = numbers;; };

    Answer :Declare the numbers field as private Change the body of the constructor to this.numbers = List.copyOf(numbers);

    What is the program's output?
    public class Test {
        public static void main(String[] args) {
            Test test = new Test();
            test.loop(0);
        }
        void loop(int number) {
            while (number < 5) {
                number++;
                continue;
                System.out.print(number + " ");
            }
        }
    }


    Options are :

    • 0
    • 4
    • 5
    • 6
    • Compilation fails

    Answer :Compilation fails

    Which of the following helps Java achieve high performance?


    Options are :

    • The just-in-time (JIT) compiler reorders statements
    • The JVM automatically executes code in parallel with multiple processors
    • The javac compiler optimizes the source code during compilation
    • The garbage collector runs at prime time to boost performance when needed

    Answer:The just-in-time (JIT) compiler reorders statements

    What is the program's output?
    public class Test {
        int decrease(int number) {
            return --number - number--;
        }
        public static void main(String[] args) {
            Test test = new Test();
            int number = test.decrease(10);
            number -= number-- - --number;
            System.out.println(number);
        }
    }


    Options are :

    • -2
    • 0
    • 2
    • 4
    • 7
    • 9

    Answer :-2

    Comment / Suggestion Section
    Point our Mistakes and Post Your Suggestions