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

    Which command lists all observable modules on the system?


    Options are :

    • java --list-modules
    • java --show-modules
    • javac -list
    • javac -modules
    • jdeps --observable-modules

    Answer :java --list-modules

    Given:

    What is the given program's output?
    class Person {
        String name;
          public Person(String name) {
            this.name = name;
        }
        @Override
        public String toString() {
            return name;
        }
    }
    public class Test {
        static List<Person> filter(List<Person> input, Predicate<Person> predicate) {
            var output = new ArrayList<Person>();
            for (Person person : input) {
                if (predicate.test(person))
                    output.add(person);
                return output;
            }
            return output;
        }
        public static void main(String[] args) {
            var john = new Person("John");
            var jane = new Person("Jane");
            var mark = new Person("Mark");
            var input = List.of(john, jane, mark);
            var output = filter(input, p -> p.name.startsWith("J"));
            System.out.println(output);
        }
    }


    Options are :

    • null
    • []
    • [John]
    • [Jane]
    • [Mark]
    • [John, Jane]

    Answer :[John]

    Given:

    What is the program's output?
    public class Test {
        public static void main(String[] args) {
            int x, y = 2, z = 4;
            try {
                x = z % y;
                y /= x;
                z *= y;
            } catch (RuntimeException e) {
                x = y / z;
                z += y;
            }
            System.out.println(x + " " + y + " " + z);
        }
    }


    Options are :

    • 2000 2 4
    • 2000 2 6
    • 2000 4 4
    • 2000 4 6
    • 2 4 2004
    • 2 6 2008

    Answer :2000 2 6

    Which statement is correct about a module declaration?


    Options are :

    • It can be given a name other than module-info.java
    • It's contained in the default package
    • It's must be located in the root directory of the module
    • If the module-info.java file doesn't have a module name, this name is inferred from the containing directory
    • None of the above

    Answer :It's must be located in the root directory of the module

    Given:

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


    Options are :

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

    Answer :Compilation fails

    Given:

    What is the given program's output?
    package test;
    import java.lang.StringBuilder; // Line 1
    class StringBuilder { // Line 2
        String content;
        StringBuilder(String content) {
            this.content = content;
        }
    }
    public class Test {
       public static void main(String[] args) {
            test.StringBuilder builder1 = new test.StringBuilder("test"); // Line 3
            java.lang.StringBuilder builder2 = new java.lang.StringBuilder("test");
            System.out.println(builder1.equals(builder2));
        }
    }


    Options are :

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

    Answer :Compilation fails on line 2

    Given:

    What happens when compiling and executing the given code fragment?
    int i = 0;
    for (; i < 1; i = 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

    Answer :It prints "Inside" repeatedly forever

    Given:

    What is the output of the given code fragment?
    int i1 = 1000;
    int i2 = 1_000; // Line 1
    int i3 = 1,000; // Line 2
    System.out.println(i1 == i2);
    System.out.println(i1 == i3);


    Options are :

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

    Answer :Compilation fails on line 2

    What is the default garbage collector in Java 11?


    Options are :

    • Serial Collector
    • Parallel Collector
    • Concurrent Mark Sweep Collector
    • Garbage-First Collector
    • Z Garbage Collector

    Answer :Garbage-First Collector

    Given:

    What is the program's output?
    class Foo {
        protected String myField = "Foo";
        protected Object myMethod() {
            return myField;
        }
    }
    public class Bar extends Foo {
        public String myField = "Bar";
        public String myMethod() {
            return myField;
        }
       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 :Foo Bar

    Given a method:

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


    Options are :

    • Hi
    • Hello
    • Nothing
    • Compilation fails

    Answer :Compilation fails

    Given a class:

    What is the output of the program?
    public class Test {
        public static void main(String[] args) {
            int i, j = 1, k; // Line 1
            k = i + j; // Line 2
            System.out.println(k);
        }
    }


    Options are :

    • 1
    • 2
    • Compilation fails on line 1
    • Compilation fails on line 2
    • Compilation fails on line 1 and line 2

    Answer :Compilation fails on line 2

    Given three module declarations:

    module test {
        requires foo;
    }
    And:
    module foo {
        requires bar;
        exports my.foo;
    }
    And:
    module bar {
        exports bar;
    }

    Which graph describes the given modules' dependencies:


    Options are :

    • A
    • B
    • C
    • D

    Answer :B

    Given:

    Which line fails to compile?
    interface Foo {
        String myField; // Line 1
        String myMethod(); // Line 2
    }
    public abstract class Bar implements Foo { // Line 3
        abstract void myMethod(String arg);  // Line 4
    }


    Options are :

    • Line 1
    • Line 2
    • Line 3
    • Line 4
    • Compilation succeeds

    Answer :Line 1

    Given:

    public class Test {
        public static void main(String[] args) {
            try {
                throwException(); // Line 1
            } catch (IOException e) { // Line 2
                e.printStackTrace();
            }
        }
        static void throwException() throws RuntimeException { // Line 3
            throw new RuntimeException();
        }
    }

    Which lines of code cause a compile-time error?


    Options are :

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

    Answer :Line 2 only

    Which of the following describes polymorphism in Java?


    Options are :

    • Subclasses inherit all accessible fields and methods from the parent class
    • Subclasses can define their own methods to override behaviors of the parent method
    • Subclasses can define their own unique behaviors and yet share some of the same functionality of the parent class
    • None of the above

    Answer :Subclasses can define their own unique behaviors and yet share some of the same functionality of the parent class

    Which option indicates the destination directory for class files when executing the javac command?


    Options are :

    • -d
    • -dir
    • -directory
    • --directory
    • -destination
    • --destination

    Answer :-d

    Given:

    What is the output?
    String text = "hello";
    text.concat("bye");
    text.concat("hello");
    text.concat("bye");
    System.out.println(text);


    Options are :

    • hello
    • hellobye
    • hellobyehellobye
    • bye

    Answer :hello

    Given:

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


    Options are :

    • Floating point
    • Integer
    • Nothing
    • Compilation fails

    Answer :Compilation fails

    Given:

    What is the output when executing the given code fragment?
    int[] array1 = {1, 2, 3};
    int[] array2 = {4, 5};
    int[][] matrix = new int[3][2];
    for (int i = 0; i < array1.length; i++) {
        matrix[i][0] = array1[i];
    }
    for (int i = 0; i < array2.length; i++) {
        matrix[i][1] = array2[i];
    }
    for (int[] row : matrix) {
        for (int element : row) {
            System.out.print(element + " ");
        }
        System.out.println();
    }


    Options are :

    • 1 2 3 4 5
    • 1 4 2 5 3
    • 1 4 2 5 3 0
    • An ArrayIndexOutOfBoundsException is thrown
    • A NullPointerException is thrown

    Answer :1 4 2 5 3 0

    Given:

    What of the following statements is correct?
    interface Foo {
        int myField = 0;
        void myMethod();
    }


    Options are :

    • All classes that implement the Foo interface must override the myMethod method
    • An implementation class of the Foo interface cannot define a field with name myField
    • All methods in an implementation class of Foo that has name myMethod must be public
    • The myField field can be changed in an object whose class implements the Foo interface
    • None of the above

    Answer :None of the above

    Given:

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


    Options are :

    • Foo: test
    • Bar: test
    • Foo: test Bar: test
    • Bar: test Foo: test
    • Compilation fails

    Answer :Compilation fails

    Given:

    What is printed to the console when the main method runs?
    public class Test {
        static String text1 = printAndEcho("a")
        static {
            printAndEcho("b");
        }
        static String text2 = printAndEcho("c");
        static String printAndEcho(String text) {
            System.out.print(text);
            return text;
        }
        public static void main(String[] args) { }
    }


    Options are :

    • Nothing
    • abc
    • ac
    • Compilation fails

    Answer :abc

    Given:

    What is the given code's output?
    try {
        throw new IOException();
    } catch (IOException e) {
        System.out.println("IOException");
    } finally {
        System.out.println("finally");
    } catch (Exception e) {
        System.out.println("Exception");
    }


    Options are :

    • IOException Exception finally
    • IOException finally Exception
    • Exception IOException finally
    • Exception finally IOException
    • Compilation fails

    Answer :Compilation fails

    Given:

    What is the program's output?
    StringBuilder builder = new StringBuilder("ABCDE")
            .delete(1, 2)
            .deleteCharAt(3);
    System.out.println(builder);


    Options are :

    • ABCDE
    • ACD
    • ADE
    • DE
    • Compilation fails

    Answer :ACD

    Given:

    int x = 1, y = -2;; x -= 3;
    y /= 4;
    System.out.println(x + " " + y);

    What is the output of the given code?


    Options are :

    • -2 0
    • -2 2
    • 2 2
    • 2 4

    Answer :-2 0

    Given the foo module:

    module foo {
        exports foo to test;
    }
    With a class:
    package foo;
    public class MyFoo { }
    And the bar module:
    module bar {
        exports bar;
        requires java.base;
    }
    With another class:
    package bar;
    public class MyBar { }
    These MyFoo and MyBar classes are used in another class:
    public class MyTest {
        public static void main(String[] args) {
            new MyFoo(); // Line 1
            new MyBar(); // Line 2
        }
    }

    Here's the declaration of the module containing MyTest:

    module test {

    requires bar;

    }

    What happens when compiling and executing the main method?


    Options are :

    • Compilation fails on line 1 only
    • Compilation fails on line 2 only
    • Compilation fails on line1 and line 2
    • Compilation succeeds, but the program fails to start
    • The program runs without any problem

    Answer :Compilation fails on line 1 only

    Given:

    interface Foo {
        abstract void methodA();
        void methodB();
        static void methodC() {
            // a valid body
       }
    }
    abstract class Bar implements Foo {
        @Override
        public abstract void methodB();
        @Override
        public static void methodC() {
            // a valid body
       }
    }

    Which of the following changes when applied independently makes the Bar class compile?


    Options are :

    • Remove all the @Override annotations
    • Remove the abstract keyword on methodA in the Foo interface
    • Remove the static keyword on methodC in the Bar class
    • Declare a method in the Bar class to override methodA in the Foo interface
    • Provide a body to methodB in the Bar class to make it a concrete method
    • Both options B and C

    Answer :Remove all the @Override annotations

    Which of the following class declarations can be compiled?


    Options are :

    • public class Foo { void myMethod();; }
    • public class Foo { abstract void myMethod();; }
    • public abstract class Foo { void myMethod() { } }
    • public abstract class Foo { static abstract void myMethod(); }
    • None of the above

    Answer :public abstract class Foo { void myMethod() { } }

    Given:

    After which line the object created on line 0 is eligible for garbage collection?
    public class Test {
        public static void main(String[] args) {
            Test test1 = new Test(); // Line 0
            Test test2 = test1; // Line 1
            Test test3 = new Test(); // Line 2
            test2 = test3; // Line 3
            test3 = test1; // Line 4
            test1 = test2; // Line 5
            test3 = test2; // Line 6
        }
    }


    Options are :

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

    Answer :Line 6

    Given:

    What's the output of the program?
    String string = "abcabcabc";
    int index1 = string.lastIndexOf("cab");
    int index2 = string.lastIndexOf("bca", index1);
    System.out.println(index1 + " " + index2);


    Options are :

    • 43647
    • 7 6
    • 5 4
    • 5 1
    • 43586
    • 4 3

    Answer :5 4

    Given:

    What is the program's output?
    public class Test {
        public static void main(String[] args) {
            Test test = new Test();
            String result = test.identifyNumber(1);
            System.out.println(result);
        }
        String identifyNumber(int number) {
            switch (number) {
                default:
                    return "Zero";
                case 1:
                    return "Positive";
                case -1:
                    return "Negative";
            }
        }
    }


    Options are :

    • Positive
    • Negative
    • Zero
    • Compilation fails

    Answer :Positive

    Given:

    What is the output?
    int[] array1 = {1, 2, 3};
    int[] array2 = {1, 3};
    int result = Arrays.compare(array1, array2);
    System.out.println(result);


    Options are :

    • -1
    • 1
    • 2
    • 3

    Answer :-1

    Given:

    What happens when executing the given program?
    public class Test {
        public static void main(String[] args) {
            try {
                Test test = new Test();
               String text = test.convertToUpperCase(null);
                System.out.println(text);
            } catch (Exception e) {
                try {
                    if (e instanceof RuntimeException)
                        throw e;
                } finally {
                    System.out.println("inner");
                }
            } finally {
                System.out.println("outer");
            }
        }
        String convertToUpperCase(String input) {
            return input.toUpperCase();
        }
    }


    Options are :

    • It prints "NULL", "inner", "outer", then exits normally
    • It prints "NULL", "outer", "inner", then exits normally
    • It prints "inner", "outer", then throws an exception
    • It prints "outer", "inner", then throws an exception
    • It throws an exception without printing anything
    • It throws an exception, then prints "inner" and "outer"

    Answer :It prints "inner", "outer", then throws an exception

    Given:

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


    Options are :

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

    Answer :Compilation fails on line 2

    Assuming required class files of a program are stored in two directories: dir1 and dir2. Which of the following is a correct way to launch that program?


    Options are :

    • java -classpath dir1 dir2 MainClass
    • java -classpath dir1:dir2 MainClass
    • java -classpath dir1;dir2 MainClass
    • java -classpath dir* MainClass
    • None of the above

    Answer :java -classpath dir1;dir2 MainClass

    Given:

    What is the output?
    String string = "foo:and:bar";
    String[] array = string.split(":", 2);
    System.out.println(Arrays.toString(array));


    Options are :

    • [foo, and, bar]
    • [foo:and, bar]
    • [foo, and:bar]
    • [foo:and:bar]
    • Compilation fails

    Answer :[foo, and:bar]

    Given:

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


    Options are :

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

    Answer :Compilation fails on line 2

    Given:

    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) {
                if (number % 2 == 0) break;
                System.out.print(number + " ");
                number++;
            }
        }
    }


    Options are :

    • 0
    • 0 1 2 3 4
    • 2000 2 4
    • 1
    • 1 3
    • Nothing

    Answer :Nothing

    Given the output:

    foo -> bar

    foo -> java.base

    Which command can produce that result?


    Options are :

    • jdeps --module-path . -s foo
    • jdeps --module-path . --summary foo
    • jdeps --module-path . -v foo
    • jdeps --module-path . -verbose:package foo
    • jdeps --module-path . -verbose:class foo
    • jdeps --module-path . --api-only foo

    Answer :jdeps --module-path . -s foo

    Given:

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


    Options are :

    • Foo
    • Bar
    • A ClassCastException is thrown
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :Compilation fails on line 2

    Given:

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


    Options are :

    • [A, B, C]
    • [A, A, B, C]
    • [A, C, B]
    • [B, A, C]
    • [B, C]
    • Compilation fails

    Answer :[A, C, B]

    Given:

    What is the program's output?
    public class Test {
        public static void main(String[] args) {
            String text;
            if (text == null) {// Line 1
                text = "foo"; // Line 2
            }
            System.out.println(text); // Line 3
        }
    }


    Options are :

    • null
    • foo
    • Compilation fails on line 1
    • Compilation fails on line 2
    • Compilation fails on line 3

    Answer :Compilation fails on line 1

    Given:

    What is the program's output?
    public class Test {
        void modify(String string, StringBuilder builder, int number) {
            string = string.toUpperCase();
            builder = builder.append("builder");
            number++;
        }
        static public void main(String[] args) {
            Test test = new Test();
            String text = "string";
            StringBuilder builder = new StringBuilder();
           int number = 0;
            test.modify(text, builder, number);
            System.out.println(text + builder + number);
        }
    }


    Options are :

    • string0
    • string1
    • stringbuilder0
    • stringbuilder1
    • STRING0
    • STRING 1

    Answer :stringbuilder0

    Given:

    What is the output of the given code fragment?
    List<Object> list = List.of("A", 'B');
    for (Object element : list) {
        System.out.println(element);
        continue;
    }


    Options are :

    • 0
    • 1
    • Compilation fails

    Answer :Compilation fails

    Which of the following must be installed to set up a Java development environment on a computer?


    Options are :

    • The Java Development Kit (JDK)
    • The Java Runtime Environment (JRE)
    • An Integrated Development Environment (IDE)
    • Nothing is required as a development environment is provided by operating systems by default

    Answer :The Java Development Kit (JDK)

    Given:

    List list = new ArrayList<>(List.of("A", "A", "B", "B"));

    System.out.println(list.indexOf("B") + " " + list.lastIndexOf("A"));

    What is the output of the given code?


    Options are :

    • 1 1
    • 1 2
    • 2 1
    • 2 2
    • 2 3
    • 3 2

    Answer :2 1

    Given:

    What is the given code fragment's output?
    LocalDateTime dateTime = LocalDateTime.parse("2019-01-01"); // Line 1
    String formatted = dateTime.format(DateTimeFormatter.ISO_DATE_TIME); // Line 2
    System.out.println(formatted);


    Options are :

    • 43466
    • 2019-01-01T00:00:00
    • An exception is thrown at runtime on line 1
    • An exception is thrown at runtime on line 2
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :An exception is thrown at runtime on line 1

    Given:

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


    Options are :

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

    Answer :Foo

    Given:

    Which two lines of code cause compile-time errors?
    public class Test {
        void methodA() throws FileNotFoundException {
            throw new RuntimeException(); // Line 1
        }
        void methodB() throws NullPointerException {
            throw new IOException(); // Line 2
        }
        void methodC() throws FileNotFoundException {
            methodA(); // Line 3
           methodB(); // Line 4
        }
        public static void main(String[] args) {
            Test test = new Test();
            test.methodC(); // Line 5
        }
    }


    Options are :

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

    Answer :Line 2 Line 5

    Given:

    What is the output of the given code?
    int i, j;
    for (i = 0, j = 0; i + j < 10; i++) {
        j = i;
        while (true) {
            if (j % 2 == 0) {
                break;
            }
        }
    }
    System.out.println(i + " " + j);


    Options are :

    • 0 0
    • 5 4
    • 5 5
    • 6 5
    • It runs into an infinite loop
    • Compilation fails

    Answer :It runs into an infinite loop

    Which is not correct about benefits of the modular JDK?


    Options are :

    • Applications can be packaged with smaller size
    • Internal JDK APIs are hidden
    • Missing packages can be detected at deployment time
    • None of the above

    Answer :None of the above

    Given:

    Which line of code successfully compiles?
    public class Person {
        static var name; // Line 1
        var age; // Line 2
        public Person(var name) { // Line 3
            this.name = name;
        }
           public static var getName() { // Line 4
           return name;
        }
        public void setAge(var age) { // Line 5
            this.age = age;
        }
        public var getAge() { // Line 6
            return age;
        }
    }


    Options are :

    • Line 1
    • Line 2
    • Line 3
    • Line 4
    • Line 5
    • None of the above

    Answer :None of the above

    Which of the following array declarations is valid?


    Options are :

    • Integer[] array1 = new int[2];
    • Number[] array2 = new Integer[2];
    • long[] array3 = new int[2];
    • int[] array4 = new int[2][2];
    • int[] array5 = new int[2] {0, 1} ;
    • None of the above

    Answer :Number[] array2 = new Integer[2];

    Given:

    What is the output of the given code fragment?
    List list = new ArrayList<>(List.of(1, 2, 3, 4, 5));
    System.out.print(list.subList(1, 3).size() + " elements: ");
    System.out.println(list);


    Options are :

    • 2 elements: [1, 2, 3, 4, 5]
    • 2 elements: [2, 3]
    • 2 elements: [2, 3, 4]
    • 3 elements: [1, 2, 3]
    • 3 elements: [2, 3]
    • 3 elements: [2, 3, 4]

    Answer :2 elements: [1, 2, 3, 4, 5]

    Given:

    On which line the @Override annotation can be added?
    abstract class Foo {
        protected String x;
        protected static String y;
        static void methodA() { }
        void methodB() { }
    }
    abstract class Bar extends Foo {
        // Line 1
        public String x;
        // Line 2
        public static String y;
        // Line 3
        static void methodA() { }
        // Line 4
        abstract void methodB();
    }


    Options are :

    • Line 1 only
    • Line 2 only
    • Line 3 only
    • Line 4 only

    Answer :Line 4 only

    Given:

    Which statement is correct about the given code?
    package test;
    import java.lang.*
    // Line 1
    public class MyTest {
        public static void main(String[] args) {
            java.lang.String text = "test"
    // Line 2
            System.out.println(text);
        }
    }


    Options are :

    • All classes and interfaces in the test package are loaded when the program is executed
    • All classes and interfaces in the java.lang package are loaded when the program is executed
    • The import statement is redundant and can be removed without affecting the program
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :The import statement is redundant and can be removed without affecting the program

    Given:

    What is the program's output?
    public class Test {
        public static void main(String[] args) {
            Test test = new Test();
            String[] array = {"Abc", "aBc", "abC"};
            String string = test.appendCharacter(array);
            System.out.println(string);
        }
        String appendCharacter(String[] array) {
            StringBuilder builder = new StringBuilder();
           myLabel:
            for (String element : array) {
                for (int i = 0; i < element.length(); i++) {
                    char c = element.charAt(i);
                    if (c < 'A' || c > 'Z') continue myLabel;
                    builder.append(c);
                }
            }
            return builder.toString();
        }
    }


    Options are :

    • ABC
    • bc
    • bcacab
    • Nothing
    • It runs into an infinite loop
    • Compilation fails

    Answer :ABC

    Given:

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


    Options are :

    • 0
    • 1
    • 2
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :0

    Given:

    What is the program's output?
    public class Person {
        static final String name; // Line 1
        Person(String name) {
            this.name = name; // Line 2
        }
        public static void main(String[] args) {
           Person person1 = new Person("John");
            Person person2 = new Person("Jane");
            System.out.println(person1.name); // Line 3
        }
    }


    Options are :

    • John
    • Jane
    • Compilation fails on line 1
    • Compilation fails on line 2
    • Compilation fails on line 3

    Answer :Compilation fails on line 1

    Given:

    What is the output of the given code?
    List original = new ArrayList<>();
    original.add("A");
    original.add("A");
    List copy = List.copyOf(original);
    copy.remove("A");
    System.out.println(original);


    Options are :

    • []
    • [A]
    • [A, A]
    • An UnsupportedOperationException is thrown

    Answer :An UnsupportedOperationException is thrown

    Given:

    Which change must be applied to make the given class adhere to the encapsulation concept?
    public class Person {
        private String name;
        public Person(String name) {
            this.name = name;
        }
        String retrieveName() {
            return name.toUpperCase();
        }
    }


    Options are :

    • Declare the retrieveName method with the public scope
    • Rename the retrieveName method to getName
    • Delete the invocation of toUpperCase() in the retrieveName method
    • Add a method to set the value of the name field
    • Nothing is needed as the given class is already encapsulated

    Answer :Nothing is needed as the given class is already encapsulated

    Given:

    What is the given program's output?
    public class Test {
        static boolean b1;
        static Boolean b2;
        static boolean[] a1 = new boolean[1];
        static Boolean[] a2 = new Boolean[1];
        public static void main(String[] args) {
            Boolean check;
            if (b1 || b2)
                check = a1[0] = a2[0];
            else if (b1 && b2)
                check = a2[0] = a1[0];
            else
                check = a1[0] == a2[0];
            System.out.println(check);
        }
    }


    Options are :

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

    Answer :An exception is thrown

    Given:

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


    Options are :

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

    Answer :Foo Bar

    Given:

    What is the program's output?
    public class Test {
        public static void main(String[] args) {
            int a = 0, b, c = 2;
            int sum = 0;
            try {
               if (a > c)
                    throw new RuntimeException();
                b = 1;
            } catch (RuntimeException e) {
                sum = a + b + c;
            }
            System.out.println(sum);
        }
    }


    Options are :

    • 2
    • 3
    • 4
    • An exception is thrown up the call stack
    • Compilation fails

    Answer :Compilation fails

    Given:

    What happens when compiling and executing the given code fragment?

    do {
        System.out.println("hi");
    } while (1 < 2);
    System.out.println("bye");


    Options are :

    • It prints "bye"
    • It prints "hi", then "bye"
    • It prints "hi" repeatedly forever
    • It runs indefinitely without printing anything
    • Compilation fails

    Answer :Compilation fails

    Which two options specify the location of application modules when compiling source files with the javac command?


    Options are :

    • -m
    • -p
    • -cp
    • -mp
    • -modulepath
    • --module-path

    Answer :-p --module-path

    Given:

    How many objects are created when the given code fragment is executed?
    String string1 = new String("hello").intern().toUpperCase();
    String string2 = "hello";
    String string3 = "HELLO".toLowerCase();


    Options are :

    • 2
    • 3
    • 4
    • 5
    • 6

    Answer :4

    Given:

    Which of the following statement is valid?
    interface Foo {
        boolean myMethod(String arg1, Integer arg2);
    }


    Options are :

    • Foo foo = () -> true;
    • Foo foo = (arg1, arg2) -> arg1 + arg2;
    • Foo foo = (arg1, arg2) -> {return arg2.toString().equals(arg1)};
    • Foo foo = (String arg1, int arg2) -> Integer.valueOf(arg1).equals(arg2);
    • None of the above

    Answer :None of the above

    It's known that the access array[2][3] doesn't throw an exception. Which of the following is the correct way to declare and initialize the array variable?


    Options are :

    • String[][] array = {?{"a", "b", "c"}, {"d", "e", "f"}};Array 2 only
    • String[][] array = {new String[3], new String[4]};
    • String[][] array = new String[3][];; for (String[] element : array) { element = new String[4]; }
    • String[][] array = {?{}, {}, {}}; for (int i = 0; i < array.length; i++) { array[i] = new String[4]; }
    • None of the above

    Answer :String[][] array = {?{}, {}, {}}; for (int i = 0; i < array.length; i++) { array[i] = new String[4]; }

    Given:

    Which of the following changes to the FooBar class enables the given code to compile?
    interface Foo {
        void m();
    }
    class Bar {
        public static void m() {
            System.out.println("Bar");
        }
    }
    class FooBar extends Bar implements Foo {
        public void m() {
            System.out.println("FooBar");
        }
    }


    Options are :

    • Add the abstract keyword to its declaration
    • Declare the m method as static
    • Remove the m method entirely
    • It fails to compile, but none of the above solutions works
    • No change is needed as the existing code compiles successfully

    Answer :It fails to compile, but none of the above solutions works

    Given:

    Which import statement, when inserted into the Test class, enables it to compile?
    package foo;
    public class MyFoo {
        public static String myField = "Foo";
    }
    And:
    package test;
    // Insert here
    public class Test {
        public static void main(String[] args) {
            System.out.println(myField);
        }
    }


    Options are :

    • import foo.*;
    • import foo.MyFoo.*;
    • import foo.MyFoo.foo;
    • import static foo.MyFoo.*;
    • import static foo.*;
    • None of the above

    Answer :import static foo.MyFoo.*;

    Given:

    Which of the following changes enables the given program to compile?
    public class Test {
        void m(int argument) throws IOException /* Position 1 */ {
            throw new FileNotFoundException();
        }
        void m(String argument) throws NullPointerException /* Position 2 */ {
            throw new NullPointerException();
        }
        public static void main(String[] args) /* Position 3 */ {
            Test test = new Test();
            if (true)
                test.m("0");
            else
                test.m(0);
        }
    }


    Options are :

    • Replace IOException at position 1 with FileNotFoundException
    • Remove NullPointerException at position 2
    • Insert throws IOException at position 3
    • It fails to compile, but none of the above solutions works
    • No change is needed as the existing code compiles successfully

    Answer :Insert throws IOException at position 3

    Given:

    It's known that the given program is valid, and the return type of the select method is a primitive data type. Which of the following is correct about this type?
    public class Test {
        /* Insert here */ select(short x) {
            return x * 10;
        }
        public static void main(String[] args) {
            Test test = new Test();
            short s = 10
            switch (test.select(s)) {
                default:
                    System.out.println("switch");
            }
        }
    }


    Options are :

    • It must be short
    • It must be int
    • It must be short or int
    • It must be int or long
    • It must be short, int or long
    • It must be short, int, or double

    Answer :It must be int

    Given:

    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
    • true true false
    • true false true
    • true false false
    • false true true
    • false true false

    Answer :true false false

    Given:

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


    Options are :

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

    Answer :Method A

    Given:

    Which two expressions when inserted independently into the given program allow it to compile?
    interface Foo {
        boolean compareData(Data d1, Data d2);
    }
    class Data {
        int input;
        Data(int input) {
            this.input = input;
        }
    }
    public class Test {
        static boolean compare(Foo foo, Data data1, Data data2) {
            return foo.compareData(data1, data2);
        }
        public static void main(String[] args) {
            Data d1 = new Data(1);
            Data d2 = new Data(2);
            boolean result = /* Insert here */;
            System.out.println(result);
        }
    }


    Options are :

    • compare((data1, data2) -> data1.input == data2.input, d1, d2)
    • compare((var d1, var d2) -> d1.input == d2.input, d1, d2)
    • compare((int d1.input, int d2.input) -> d1.input == d2.input)
    • compare((Data d1, Data d2) -> d1.input == d2.input, d1, d2)
    • compare((Data data1, Data data2) -> d1.input == d2.input, d1, d2)

    Answer :compare((data1, data2) -> data1.input == data2.input, d1, d2) compare((Data data1, Data data2) -> d1.input == d2.input, d1, d2)

    Given:

    Which of the following cannot be inserted into the given code?
    class Foo {
        void m() throws IOException { }
    }
    class Bar extends Foo {
        void m() throws /* Insert here */ { }
    }


    Options are :

    • Exception
    • FileNotFoundException
    • IOException
    • NullPointerException
    • None of the above

    Answer :Exception

    Given:

    What is the output of the given code fragment?
    String[] array1 = {"a", "b"};
    String[] array2 = {"a", "a"};
    String[] array3 = {"a"};
    int i1 = Arrays.mismatch(array1, array2);
    int i2 = Arrays.mismatch(array2, array3);
    int i3 = Arrays.mismatch(array3, array1);
    System.out.println(i1 + " " + i2 + " " + i3);


    Options are :

    • 0 0 0
    • 0 0 1
    • 0 1 0
    • 2000 1 1
    • 1 0 0
    • 1 1 2001

    Answer :1 1 2001

    Given:

    What happens when compiling and executing the given code fragment?
    List<String> list = List.of("a", "b");
    for (String element : list) {
        switch (element) {
            case "b":
            default:
                continue;
        }
        System.out.println(element);
    }


    Options are :

    • It fails to compile
    • It prints "a" only
    • It prints "b" only
    • It prints "a" and "b"
    • It prints nothing

    Answer :It fails to compile

    Given:

    What is the output of the given code fragment?
    int i = 0;
    i += (i = 10) * ++i;
    System.out.println(i);


    Options are :

    • 10
    • 11
    • 100
    • 101
    • 110
    • 111

    Answer :110

    Given:

    What happens when compiling and executing the given program?
    public class Test {
        static int m() throws RuntimeException {
            throw new ArithmeticException();
        }
        public static void main(String[] args) {
            int[] arr = null;
            int i = arr[m()];
        }
    }


    Options are :

    • It fails to compile
    • It throws a NullPointerException
    • It throws an ArrayIndexOutOfBoundsException
    • It throws an ArithmeticException
    • It throws a RuntimeException

    Answer :It throws an ArithmeticException

    Given:

    What happens when compiling and executing the given code fragment?
    do {
        int i = 0;
        while (i < 10) {
            System.out.println(i);
            i++;
        }
        ++i;
    } while (i < 10);


    Options are :

    • It prints all integers from 0 to 9
    • It prints all even numbers from 0 to 9
    • It prints all odd numbers from 0 to 9
    • It runs into an infinite loop
    • Compilation fails

    Answer :Compilation fails

    Given:

    Which of the following is incorrect about the given code?
    interface Foo {
       int number = 0; // Line 1
        void modify(int arg); // Line 2
    }
    class Bar implements Foo {
        int number = 10; // Line 3
       public void modify(int arg) { // Line 4
            number += arg;
        }
    }


    Options are :

    • The value of the variable defined on line 1 cannot be changed
    • The method defined on line 2 is visible outside of its containing package
    • Commenting out line 3 makes the code unable to compile
    • The public modifier on line 4 cannot be removed
    • None of the above

    Answer :The method defined on line 2 is visible outside of its containing package

    Given:

    On which line the given program fails to compile?
    interface Foo {
        int count = 0;
        void increase();
    }
    public class Bar implements Foo {
        @Override // Line 1
        public void increase() {
            count++; // Line 2
        }
        public static void main(String[] args) {
            Foo foo = new Bar(); // Line 3
            foo.increase(); // Line 4
            System.out.println(foo.count); // Line 5
        }
    }


    Options are :

    • Line 1
    • Line 2
    • Line 3
    • Line 4
    • Line 5
    • The given program compiles successfully

    Answer :Line 2

    Given:

    What is the program's output?
    class Foo {
        protected static String myField = "Foo";
        protected String myMethod() {
            return myField;
        }
    }
    public class Bar extends Foo {
        public String myField = "Bar";
        public static String myMethod() {
            return new Bar().myField;
        }
        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

    Given:

    What is the program's output?
    public class Test {
        public static void main(String[] args) {
            Test test = new Test();
            test.compare(1, 2, 3);
        }
        private void compare(int x, int y, int z) {
            var in = x > y - z && (x > z - y && x < y + z);
            var out = x < y - z && (x < z - y || x < y + z);
            System.out.println(in);
            System.out.println(out);
        }
    }


    Options are :

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

    Answer :FALSE FALSE

    Given:

    What is the output when executing the code fragment?
    String string1 = new String("Java11");
    String string2 = "Java11";
    String string3 = string1.intern();
    System.out.println(string1 == string2);
    System.out.println(string2 == string3);


    Options are :

    • TRUE TRUE
    • TRUE FALSE
    • FALSE TRUE
    • FALSE FALSE

    Answer :FALSE TRUE

    Given:

    What is the output of the given code?
    try {
        throw new FileNotFoundException();
    } catch (IOException e) {
        throw new RuntimeException(e); // Line 1
    } catch (Exception e) { // Line 2
        System.out.println("Test");
    }


    Options are :

    • Test
    • A FileNotFoundException is thrown up the call stack
    • A RuntimeException is thrown up the call stack
    • Compilation fails on line 1
    • Compilation fails on line 2

    Answer :A RuntimeException is thrown up the call stack

    Given:

    Which three of the following changes when made independently allows the given code to compile?
    class Foo {
        void methodA() {
            // a valid body
        }
    }
    abstract class Bar extends Foo {
        abstract void methodB();
    }
    public class Baz extends Bar {
        void methodC() {
            // a valid body
        }
    }


    Options are :

    • Add the abstract modifier to the declaration of the Baz class
    • Declare a concrete method in the Baz class to override methodA in the Foo class
    • Declare a concrete method in the Baz class to override methodB in the Bar class
    • Remove methodC from the Baz class
    • Convert methodB in the Bar class to a concrete method
    • Remove the extends Foo clause on the declaration of the Bar class

    Answer :Add the abstract modifier to the declaration of the Baz class Declare a concrete method in the Baz class to override methodB in the Bar class Convert methodB in the Bar class to a concrete method

    Comment / Suggestion Section
    Point our Mistakes and Post Your Suggestions