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

    What is the output of the given code?
    String language = "java";
    while (language.equals("java")) {
        if (language.equals("java")) {
            language = language.toUpperCase();
        }
        if (language.equals("JAVA")) {
           language = language.toLowerCase();
        }
    }
    System.out.println(language);


    Options are :

    • java
    • JAVA
    • It runs into an infinite loop (Correct)
    • Compilation fails

    Answer :It runs into an infinite loop

    Which two of the following statement are correct about exception handling?


    Options are :

    • A try block must always be followed by a catch block
    • There cannot be more than one finally block in an exception handler (Correct)
    • No code can be inserted between two adjacent blocks in an exception handler (Correct)
    • If existent, a finally block always runs no matter what happens when the try block is executed
    • A finally block mustn't throw any exception

    Answer :There cannot be more than one finally block in an exception handler No code can be inserted between two adjacent blocks in an exception handler

    What is the output?
    String string = "	 
    ";
    System.out.println(string.isEmpty() + " " + string.isBlank());


    Options are :

    • true true
    • true false
    • false true (Correct)
    • false false

    Answer :false true

    abstract class Foo {
        abstract void methodA();
    }
    abstract class Bar extends Foo {
        void methodA() {
            // a valid body
        }
        void methodB() {
            // a valid body
        }
        abstract void methodC();
    }
    public class Baz extends Bar {
       // method declarations
    }

    For the Baz class to compile, which concrete methods it must define?


    Options are :

    • methodA only
    • methodB only
    • methodC only (Correct)
    • methodA and methodB
    • methodB and methodC
    • methodC and methodA

    Answer :methodC only

    What is the output?
    String[] array1 = {"a", "b", "c"};
    String[] array2 = {"a"};
    int result = Arrays.mismatch(array1, array2);
    System.out.println(result);


    Options are :

    • 0
    • 1 (Correct)
    • 2
    • Compilation fails

    Answer :1

    Given the synopsis of the jdeps command:

    jdeps [options] path _

    Which three of the following can be specified as a path value?


    Options are :

    • A .class file (Correct)
    • A .java file
    • A .dot file
    • A directory (Correct)
    • A JAR file (Correct)
    • A module's name

    Answer :A .class file A directory A JAR file

    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) {
            loop: // Line 1
            while (number < 5) {
                number++;
                if (number % 2 == 0)
                    continue loop; // Line 2
                System.out.print(number + " ");
            }
        }
    }


    Options are :

    • 2000 2 4
    • 1
    • 1 2 3 4 5
    • 1 3 2005 (Correct)
    • 2 4
    • Compilation fails on line 1

    Answer :1 3 2005

    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 (Correct)
    • Compilation succeeds

    Answer :Line 2

    What line causes a compile-time error?
    interface Foo {
        void myMethod();
    }
    abstract class Bar implements Foo { // Line 1
    }
    public class Test implements Foo extends Bar { // Line 2
        @Override // Line 3
        public void myMethod() {
            // a valid body
        }
        public static void main(String[] args) {
            Foo foo = new Test(); // Line 4
            foo.myMethod(); // Line 5
        }
    }


    Options are :

    • Line 1
    • Line 2 (Correct)
    • Line 3
    • Line 4
    • Line 5
    • Compilation succeeds

    Answer :Line 2

    Which two of the following class declarations apply the encapsulation concept?


    Options are :

    • public class Person { String name; String getName() { return name; } void setName(String name) { this.name = name; } }
    • public class Person { private String name; Person(String name) { this.name = name; } String getName() { return name; } } (Correct)
    • public class Person { String name; private String getName() { return name; } private void setName(String name) { this.name = name; } }
    • public class Person { private String firstName; private String lastName; public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getFullName() { return firstName + " " + lastName; } } (Correct)

    Answer :public class Person { private String name; Person(String name) { this.name = name; } String getName() { return name; } } public class Person { private String firstName; private String lastName; public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getFullName() { return firstName + " " + lastName; } }

    Which two of the following statements are correct about the trim and strip methods in the String class?


    Options are :

    • Both the trim and strip method remove leading and trailing spaces (Correct)
    • The strip method removes all the characters whose Unicode codepoint returns true when passed to the Character#isWhitespace method
    • The strip method was recently added to the API, starting with Java 11 (Correct)
    • The trim method is preferred to the strip method

    Answer :Both the trim and strip method remove leading and trailing spaces The strip method was recently added to the API, starting with Java 11

    After which line the Data object passed to the playWithData method is eligible for garbage collection?
    class Data { }
    class Test {
        void playWithData(Data a) {
            Data b = new Data();
            Data c = new Data();
            Data d = a; // Line 1
            b = c; // Line 2
            c = d; // Line 3
            a = null; // Line 4
            d = null; // Line 5
            c = null; // Line 6
        }
    }


    Options are :

    • Line 1
    • Line 2
    • Line 3
    • Line 4
    • Line 5
    • None of the above (Correct)

    Answer :None of the above

    Which of the following is not a correct way to reference the text field defined in the Foo class?
    class Foo {
        static String text = "Foo";
    }
    class Bar extends Foo {
        void printText() {
            System.out.println(/* Insert here */);
        }
    }


    Options are :

    • text
    • super.text
    • this.text
    • Foo.text
    • Bar.text
    • None of the above (Correct)

    Answer :None of the above

    What happens when executing the given program?
    public class Test {
        public static void main(String[] args) {
            try {
                Test test = new Test();
                int quotient = test.divide(1, 0);
                System.out.println(quotient);
            } catch (RuntimeException e) {
                try {
                    throw e;
                } finally {
                    System.out.println("finally");
                }
            } catch (Exception e) {
                System.out.println("catch");
            }
        }
        int divide(int dividend, int divisor) {
            return dividend / divisor;
        }
    }


    Options are :

    • It prints "catch", then exits normally
    • It prints "finally", then exits normally
    • It prints "catch", "finally", then exits normally
    • It prints "finally", "catch", then exits normally
    • It prints "catch", then throws an ArithmeticException
    • It prints "finally", then throws an ArithmeticException (Correct)

    Answer :It prints "finally", then throws an ArithmeticException

    Which of the following code fragment compiles?


    Options are :

    • for (int i = 0, j = 0; i < 10; i++, j++) { continue; } System.out.println(i + " " + j);
    • int i, j; for (int i = j = 0; i < 10; i++, j++) { System.out.println(i + " " + j); }
    • for (int i, j = 0; i < 10; j++) { System.out.println(i + " " + j); }
    • int i, j; for (i = 0, j = 1; i < 10; j++) { if ((i + j) % 2 == 0) continue; else break; System.out.println(i + " " + j); }
    • None of the above (Correct)

    Answer :None of the above

    Which of the following statements are correct?


    Options are :

    • A type must be defined in a package
    • A type can be defined without a package, and such a no-package type cannot reference any type in any named package
    • A type can be defined without a package, and such a no-package type cannot be referenced from any type in any named package (Correct)
    • None of the above

    Answer :A type can be defined without a package, and such a no-package type cannot be referenced from any type in any named package

    What is the output of the given code?
    List list = new ArrayList<>(List.of(1, 2));
    list.add(1, 3);
    System.out.println(list);


    Options are :

    • [1, 1, 2]
    • [1, 2, 3]
    • [1, 3, 2] (Correct)
    • [3, 1, 2]
    • An IndexOutOfBoundsException is thrown

    Answer :[1, 3, 2]

    Given an output:

    bar,java.base

    Which command may produce such an output on the console?


    Options are :

    • jdeps --module-path . --list-deps foo
    • jdeps --module-path . --list-reduced-deps foo
    • jdeps --module-path . -p foo
    • jdeps --module-path . --print-deps foo
    • jdeps --module-path . --print-module-deps foo (Correct)

    Answer :jdeps --module-path . --print-module-deps foo

    Which statement when inserted into the Test class allows it to compile?
    package foo;
    public class MyFoo {
        // a valid body
    }
    And:
    package foo.bar;
    public class MyBar {
        // a valid body
    }
    And:
    package test;
    // Insert here
    public class Test {
        public static void main(String[] args) {
            new MyFoo();
            new MyBar();
        }
    }


    Options are :

    • import *;
    • import foo.*;
    • import foo.bar.*;
    • import foo.MyFoo.*
    • import foo.bar.MyBar.*; (Correct)
    • None of the above

    Answer :import foo.bar.MyBar.*;

    What can be the program's output?
    class MyException extends RuntimeException {
        MyException(String message) {
           super(message);
        }
    }
    public class Test {
        static void methodA() throws MyException {
            throw new MyException("A custom exception")
       }
        static void methodB() {
            methodA();
       }
        public static void main(String[] args) {
            try {
                methodB();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }


    Options are :

    • A custom exception
    • MyException: A custom exception (Correct)
    • java.lang.RuntimeException: A custom exception
    • MyException: A custom exception at Test.methodA(Test.java:9) at Test.methodB(Test.java:13) at Test.main(Test.java:18)
    • java.lang.Exception: A custom exception at Test.methodA(Test.java:9) at Test.methodB(Test.java:13) at Test.main(Test.java:18)
    • Compilation fails

    Answer :MyException: A custom exception

    What is the program's output?
    public class Test {
        static public void main(String[] args) {
            String string1 = "hello";
            var chars = string1.toCharArray(); // Line 1
            StringBuilder builder = new StringBuilder();
            for (var c : chars) { // Line 2
                builder.append(c);
            }
            String string2 = builder.toString();
            System.out.println(string1 == string2);
            System.out.println(string1.equals(string2));
        }
    }


    Options are :

    • TRUE TRUE
    • TRUE FALSE
    • FALSE TRUE (Correct)
    • FALSE FALSE
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :FALSE TRUE

    On which line the program fails to compile?
    public class Test {
        void Test(String input) { // Line 1
            System.out.println(input);
        }
        static public void main(String[] args) {
            new Test(); // Line 2
            new Test("hello"); // Line 3
        }
    }


    Options are :

    • Line 1 only
    • Line 2 only
    • Line 3 only (Correct)
    • Line 1 and line 2
    • Line 2 and line 3
    • Line 3 and line 1

    Answer :Line 3 only

    What is the output of the given code?
    List original = List.of("A");
    List derived = new ArrayList<>(original);
    original.add(1, "B");
    System.out.println(derived);


    Options are :

    • [A]
    • [A, B]
    • An UnsupportedOperationException is thrown (Correct)
    • An IndexOutOfBoundsException is thrown

    Answer :An UnsupportedOperationException is thrown

    What happens when compiling and executing the given code fragment?
    for (int i = 0; false; i++) {
        System.out.println("Inside");
    }
    System.out.println("Outside");


    Options are :

    • It prints "Inside"
    • It prints "Outside"
    • It prints "Inside", then "Outside"
    • It prints "Inside" repeatedly forever
    • It runs indefinitely without printing anything
    • Compilation fails (Correct)

    Answer :Compilation fails

    Which statement when inserted at line 1 allows the program to print "John 25" to the console?
    public class Person {
        String name = "John";
        static int age = 25;
        static String getName() {
            return new Person().name;
        }
        int getAge() {
            return age;
        }
        public static void main(String[] args) {
            Person person = new Person();
            // Line 1
            System.out.println(nameAndAge);
        }
    }


    Options are :

    • String nameAndAge = person.name + " " + person.getAge(); (Correct)
    • String nameAndAge = person.getName() + " " + person.age; (Correct)
    • String nameAndAge = Person.name + " " + Person.age;
    • String nameAndAge = Person.getName() + " " + Person.age; (Correct)
    • String nameAndAge = Person.name + " " + Person.getAge();

    Answer :String nameAndAge = person.name + " " + person.getAge(); String nameAndAge = person.getName() + " " + person.age; String nameAndAge = Person.getName() + " " + Person.age;

    What is the program's output?
    public class Test {
        public static void main(String[] args) {
            CharSequence sequence = "test"; // Line 1
            String string = (String) sequence.toUpperCase(); // Line 2
            System.out.println(string);
        }
    }


    Options are :

    • test
    • TEST
    • An exception is thrown
    • Compilation fails on line 1
    • Compilation fails on line 2 (Correct)

    Answer :Compilation fails on line 2

    Which of the following is correct about module path of the java command?


    Options are :

    • It must be specified using the --module-path option
    • Multiple directories can be used to indicate the module path - these directories must be separated by semi-colons (;) (Correct)
    • When no value is specified, the module path defaults to the current directory
    • None of the above

    Answer :Multiple directories can be used to indicate the module path - these directories must be separated by semi-colons (;)

    Given a source file named Foo.java:

    package test;
    class Foo {
        int f;
        Foo(int f) {
            this.f = f;
       }
    }
    // Insert here

    Which of the following class declarations can be inserted into the given file?


    Options are :

    • class Bar extends Foo { }
    • class Bar extends Foo { Bar() { } }
    • class Bar { Bar() { super(); } } (Correct)
    • public class Bar extends Foo { Bar() { super(0); } }
    • None of the above

    Answer :class Bar { Bar() { super(); } }

    Which two of the following lambda expressions are valid?


    Options are :

    • (var arg1, var arg2) -> {return;} (Correct)
    • arg1, arg2 -> System.out.println(arg1 + " " + arg2)
    • (String arg1, arg2) -> System.out.println(arg1 + " " + arg2)
    • (String arg1, var arg2) -> System.out.println(arg1 + " " + arg2)
    • (String arg1, String arg2) -> {System.out.println(arg1 + " " + arg2);} (Correct)

    Answer :(var arg1, var arg2) -> {return;} (String arg1, String arg2) -> {System.out.println(arg1 + " " + arg2);}

    int[][] array1 = {?{1}, {2, 3}, {4, 5, 6}};
    int[] array2 = array1[1];
    System.out.println(array2[2][3]);

    What is the output of the given code fragment?


    Options are :

    • 1
    • 3
    • 6
    • An ArrayIndexOutOfBoundsException is thrown
    • Compilation fails (Correct)

    Answer :Compilation fails

    What is the given code fragment's output?
    String string1 = new String("ja") + new String("va");
    String string2 = "ja" + "va";
    String string3 = "java";
    boolean b1 = string1 == string2;
    boolean b2 = string2 == string3;
    System.out.println(b1 + " " + b2);


    Options are :

    • true true
    • true false
    • false true (Correct)
    • false false
    • Compilation fails

    Answer :false true

    The compiled class file Test.class is stored in a folder called test. How can we execute the given program using the command line?
    package test;
    public class Test {
        public static void main(String[] args) {
            System.out.println("Hello");
        }
    }


    Options are :

    • Run java test.Test in the parent of the test folder (Correct)
    • Run java test/Test in the parent of the test folder
    • Run java test/Test.class in the parent of the test folder
    • Run java test.Test in test folder
    • Run java Test in test folder
    • Run java Test.class in test folder

    Answer :Run java test.Test in the parent of the test folder

    What happens when compiling and executing the given code fragment?
    String s = "2";
    switch (s) {
        case 1:
            System.out.println(1);
        case 2:
            System.out.println(2);
    }


    Options are :

    • It fails to compile (Correct)
    • It prints 1 only
    • It prints 2 only
    • It prints 1 and 2
    • It prints nothing

    Answer :It fails to compile

    What is the program's output?
    public class Test {
        public static void main(String[] args) {
            String x = "hello";
            try {
                throw new IOException(x = "good bye");
            } catch (Exception e) {
                System.out.println(x);
            }
            System.out.println(x);
        }
    }


    Options are :

    • hello hello
    • hello good bye
    • good bye hello
    • good bye good bye (Correct)
    • Compilation fails

    Answer :good bye good bye

    What is the program's output?
    public class Test {
        void aMethod(long... argument) {
            System.out.println("Method A");
        }
        void aMethod(float argument) {
            System.out.println("Method B");
        }
        void aMethod(Integer argument) {
            System.out.println("Method C");
        }
        public static void main(String[] args) {
            Test test = new Test();
            int number = 'a'; // Line 1
            test.aMethod(number); // Line 2
        }
    }


    Options are :

    • Method A
    • Method B (Correct)
    • Method C
    • An exception is thrown
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :Method B

    What is the output of the given code fragment ?
    String s1 = "java";
    String s2 = s1.replace('A', 'a');
    String s3 = s1.replace("A", "a");
    String s4 = s1.replaceAll("A", "a");
    System.out.print((s1 == s2) + " ");
    System.out.print((s1 == s3) + " ");
    System.out.print(s1 == s4);


    Options are :

    • true true true (Correct)
    • true true false
    • true false true
    • true false false
    • false true true
    • false true false

    Answer :true true true

    What is the given program's output?
    interface MyInterface {
        boolean m(Integer i1, Integer i2);
        boolean m(long l1, long l2);
    }
    public class Test {
        public static void main(String[] args) {
            MyInterface func1 = (i1, i2) -> i1 > i2;
            MyInterface func2 = (i1, i2) -> i1 < i2;
            System.out.println(func1.m(1, 2));
            System.out.println(func2.m(2, 1));
        }
    }


    Options are :

    • TRUE TRUE
    • TRUE FALSE
    • FALSE TRUE
    • FALSE FALSE
    • Compilation fails (Correct)

    Answer :Compilation fails

    What is the given program's output?
    public class Test {
        int getNumber() {
            return 0;
        }
       float getNumber() {
            return 1;
       }
        public static void main(String[] args) {
            Test test = new Test();
            long l = test.getNumber();
            double d = test.getNumber();
            System.out.println(l + d);
        }
    }


    Options are :

    • 1
    • 1.0
    • 01
    • 01.0
    • Compilation fails (Correct)

    Answer :Compilation fails

    What is the given program's output?
    public class Test {
        public static void main(String[] args) {
            int[] array = {1, 2, 3, 4};
            for (int element : array) {
                if (element % 2 == 0)
                    continue;
                else
                    break;
                element *= 2;
            }
            System.out.println(Arrays.toString(array));
        }
    }


    Options are :

    • [1, 2, 3, 4]
    • [2, 4, 6, 8]
    • [1, 4, 3, 8]
    • [2, 2, 6, 4]
    • Compilation fails (Correct)

    Answer :Compilation fails

    Which variable declarations are valid?
    var a1[] = new int[]{1, 2};
    var a2[] = {1, 2};
    var a3 = {1, 2};


    Options are :

    • a1 only
    • a2 only
    • a3 only
    • a1 and a2
    • a1 and a3
    • None of those variables (Correct)

    Answer :None of those variables

    Which of the following is correct about the m abstract method in the Foo interface?
    interface Foo {
        // declaration of the m method
    }
    class Bar implements  Foo {
        void m() throws IOException { }
    }


    Options are :

    • It must specify IOException or one of its superclasses (Correct)
    • It must specify IOException or one of its subclasses
    • It must catch or specify IOException
    • It cannot specify any exception
    • None of the above

    Answer :It must specify IOException or one of its superclasses

    Given the class directory structure of a module:

    my.foo
    moduleinfo.class
    foo
    Test.class

    Which command starts the Test program when executed in the parent folder of my.foo?


    Options are :

    • java -p . -m my.foo/foo.Test (Correct)
    • java -p . my.foo/foo.Test.class
    • java --module-class-path . -m my.foo Test
    • java -path . my.foo/Test
    • java --module-path my.foo Test

    Answer :java -p . -m my.foo/foo.Test

    What happens when compiling and executing the given program?;
    public class Test {
        public static void main(String[] args) {
            var var = new ArrayList<>(); // Line 1
            Data input = new Data("My data");
            var.add(input); // Line 2
            Data output = var.get(0); // Line 3
            System.out.println(output.content);
        }
    }


    Options are :

    • It prints "My data" to the console
    • It fails to compile on line 1
    • It fails to compile on line 2
    • It fails to compile on line 3 (Correct)
    • It throws an exception at runtime

    Answer :It fails to compile on line 3

    Which methods modify fields of a Foo object?
    public class Foo {
        int a, b, c;
        void methodX(int a) {
           a = setA(a);
            setBC(a, a);
        }
        void methodY(int a, int b) {
            b = setBC(a, b);
            setA(b);
        }
        private int setA(int a) {
            return a = a;
        }
        private int setBC(int a, int b) {
           c = b = a;
           return b;
        }
    }


    Options are :

    • methodX
    • methodY
    • methodX and methodY (Correct)
    • Neither methodX nor methodY modifies a field
    • Compilation fails

    Answer :methodX and methodY

    What is the given code's output?
    int x = 1, y = 2;
    x += x < y ? y : x;
    y = x < y ? y - x : y + x;
    System.out.println(x + " " + y);


    Options are :

    • 1 0
    • 1 3
    • 2 0
    • 2 4
    • 3 1
    • 3 5 (Correct)

    Answer :3 5

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


    Options are :

    • Foo
    • Bar
    • Compilation fails on line 1
    • Compilation fails on line 2 (Correct)
    • Compilation fails on lines 1 and 2
    • Compilation fails on line 3

    Answer :Compilation fails on line 2

    What is the output of the given code?
    LocalDate d1 = LocalDate.of(2019, 1, 1);
    LocalDate d2 =  LocalDate.parse("2019-1-1", DateTimeFormatter.ISO_DATE);
    LocalDate d3 = LocalDate.now().withDayOfMonth(1).withMonth(1).withYear(2019);
    System.out.println(d1.equals(d2) + " " + d2.equals(d3));


    Options are :

    • true true
    • true false
    • false true
    • false false
    • An exception is thrown at runtime (Correct)
    • Compilation fails

    Answer :An exception is thrown at runtime

    What is the output of the given code?
    LocalTime localTime = LocalTime.of(1, 15, 30);
    localTime.withHour(2)
    // Line 1
    localTime.with(ChronoField.MINUTE_OF_HOUR, 30)
    // Line 2
    localTime.plusSeconds(15)
    // Line 3
    System.out.println(localTime);


    Options are :

    • 0.052430556 (Correct)
    • 0.1046875
    • 0.146006944
    • Compilation fails on line 1
    • Compilation fails on line 2
    • Compilation fails on line 3

    Answer :0.052430556

    Given two classes:

    package test1;
    class Foo { }
    And:
    package test2;
    import test1.Foo;
    public class Bar {
        public static void main(String[] args) {
            Foo foo = new Foo();
            foo = new Foo();
            foo = null;
           foo = new Foo();
        }
    }

    How many instances of the Foo class are created when the main method is compiled and run?


    Options are :

    • 1
    • 2
    • 3
    • Compilation fails (Correct)

    Answer :Compilation fails

    Which two of the following are valid options to specify a classpath for the javac command?


    Options are :

    • -cp (Correct)
    • -class
    • --classpath
    • --class-path (Correct)
    • -path

    Answer :-cp --class-path

    Which option instructs the javac command to generate class files with all debugging information, including source file information, line numbers and local variables?


    Options are :

    • -d
    • -g (Correct)
    • -debug
    • -debugging
    • -info

    Answer :-g

    int i = 0, j = 5, k;

    do {

    k = i;

    } while (i++ < --j);

    System.out.println(k);

    What is the output of when executing the code fragment?


    Options are :

    • 0
    • d a e b f c
    • 2 (Correct)
    • 3
    • Compilation fails

    Answer :2

    Given a code fragment:

    byte b = 100

    // Line 1

    short s = 'A'

    // Line 2

    char c = "A"

    // Line 3

    float f = 1.0

    // Line 4

    double d = 1

    // Line 5

    Which two statements fail to compile?


    Options are :

    • Line 1
    • Line 2
    • Line 3 (Correct)
    • Line 4 (Correct)
    • Line 5

    Answer :Line 3 Line 4

    What is the output when executing the given code?
    String[][] strings = {?{"a", "d"}, {"b", "e"}, {"c", "f"}};
    for (int i = 0; i < strings.length; i++) {
       for (int j = 0; j < strings[i].length; j++) {
            System.out.print(strings[i][j] + " ");
        }
        System.out.println();
    }


    Options are :

    • a b c d e f
    • a b c d e f
    • a d b e c f (Correct)
    • An ArrayIndexOutOfBoundsException is thrown
    • Compilation fails

    Answer :a d b e c f

    Which method in the Test class doesn't overload the only method in the SuperTest?
    class SuperTest {
        public Object myMethod(Object... args) {
            // A valid body
        }
    }
    class Test extends SuperTest {
        // Method 1
        public Object myMethod(String... args) {
            // A valid body
        }
       // Method 2
        public Object myMethod(Integer[] args) {
            // A valid body
       }
       // Method 3
        public Object myMethod(Object arg) {
            // A valid body
        }
        // Method 4
        public String myMethod(Object[] args) {
            // A valid body
        }
    }


    Options are :

    • Method 1
    • Method 2
    • Method 3
    • Method 4 (Correct)
    • None of the above

    Answer :Method 4

    Which method is valid?
    interface Foo {
        void methodA() { }
        static void methodB();
        protected void methodC();
       public void methodD();
    }


    Options are :

    • methodA
    • methodB
    • methodC
    • methodD (Correct)
    • None of the above

    Answer :methodD

    What is the output of the program?
    public class Test {
        static String text;
        public static void main(String[] args) {
           String string1 = text + "Java";
           String string2 = "Java";
           System.out.println(string1 == string2);
           System.out.println(string1.equals(string2));
        }
    }


    Options are :

    • TRUE TRUE
    • TRUE FALSE
    • FALSE TRUE
    • FALSE FALSE (Correct)
    • A NullPointerException is thrown

    Answer :FALSE FALSE

    Given a method:

    What is printed to the console if the switchString method is called with argument "B"?
    void switchString(String arg) {
        switch (arg) {
            case "A": case "a":
                System.out.println("Apple");
            case "B":
                System.out.println("Beef");
           default:
                System.out.println("No match");
        }
    }


    Options are :

    • Beef
    • Beef No match (Correct)
    • No match
    • Compilation fails

    Answer :Beef No match

    Given three module declarations:

    module bar {
        exports bar to test;
    }
    And:
    module foo {
       requires transitive bar;
        exports foo;
    }
    And:
    module test {
        requires foo;
    }

    Which of the following statements is correct about the bar package?


    Options are :

    • It's visible in the foo module but not in the test module
    • It's visible in the test module but not in the foo module (Correct)
    • It's visible in both the foo and test modules
    • It's invisible in both the foo and test modules

    Answer :It's visible in the test module but not in the foo module

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


    Options are :

    • Foo
    • Bar (Correct)
    • Compilation fails on line 1
    • Compilation fails on line 2
    • Compilation fails on line 3

    Answer :Bar

    What is the output of the program?
    public class Test {
        public static void main(String[] args) {
           try {
               foo();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
     static void foo() throws Exception {
            throw new Exception("Foo");
       }
    }


    Options are :

    • Foo
    • java.lang.Exception
    • java.lang.Exception: Foo (Correct)
    • java.lang.Exception: Foo; ; at Test.foo(Test.java:11); ; at Test.main(Test.java:4);

    Answer :java.lang.Exception: Foo

    What is the program output?
    class Foo {
        public String toString() {
            return "Foo";
        }
    }
    public class Bar {
       public static void main(String[] args) {
            StringBuilder builder = new StringBuilder();
            Foo foo = new Foo();
            builder.append(foo)
    // Line 1
           builder.append(' ')
    // Line 2
           builder.append("Bar");
           System.out.println(builder);
        }
    }


    Options are :

    • Bar
    • Foo Bar (Correct)
    • Nothing
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :Foo Bar

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


    Options are :

    • Foo Foo
    • Foo Bar (Correct)
    • Bar Foo
    • Bar Bar
    • Compilation fails

    Answer :Foo Bar

    public class Test {
        public static void main(String[] args) {
            String concatenated = Arrays.toString(args);
            System.out.println(concatenated);
       }
    }
    What happens when compiling and running the given program with the following command?

    java Test "1 2" 3


    Options are :

    • [1 2 3]
    • [1, 2, 3]
    • [1 2, 3] (Correct)
    • ['1 2', 3]
    • ["1 2", 3]
    • An exception is thrown at runtime

    Answer :[1 2, 3]

    Comment / Suggestion Section
    Point our Mistakes and Post Your Suggestions