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

Which two lines cause a compile-time error?
abstract class Foo {
    String getName(); // Line 1
}
class Bar extends Foo {
    String getName() { // Line 2
        return "Bar";
    }
}
public class Baz extends Bar implements Foo { // Line 3
    String getName() { // Line 4
        return "Baz";
    }
    public static void main(String[] args) {
        Baz baz = new Baz(); // Line 5
        String name = baz.getName(); // Line 6
        System.out.println(name);
    }
}


Options are :

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

Answer :Line 1 Line 3

What is the output of the given code?
List strings = new ArrayList<>(List.of("A", "B"));
Iterator iterator = strings.iterator();
ListIterator listIterator = strings.listIterator();
boolean same1 = true;
boolean same2 = true;
for (String element : strings) {
    if (!(element == iterator.next())) same1 = false;
    if (!(element == listIterator.next())) same2 = false;
}
System.out.println(same1 + " " + same2);


Options are :

  • true true
  • true false
  • false true
  • false false
  • An exception is thrown at runtime
  • Compilation fails

Answer :true true

Which of the following is correct about a module?


Options are :

  • Its name mustn't contain underscores (_)
  • Its name must be the same as the root Java package contained in the module
  • When a module is complied, the module-info.java file is copied to the compiler's output directory
  • If a project contains only a single module, a dedicated directory for that module isn't needed
  • A Java package cannot be exported by more than one module
  • None of the above

Answer :A Java package cannot be exported by more than one module

What is the give code's output?
public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        test.loop(0);
    }
    void loop(int number) {
        while (number-- < 0) {
            number++;
        }
        System.out.println(number);
    }
}


Options are :

  • -1
  • 0
  • 1
  • It runs into an infinite loop
  • Compilation fails

Answer :-1

Given a code fragment:

What is the output when executing the given code?
String[] array1 = {"a", "b", "c", "d", "e"};
String[] array2 = Arrays.copyOfRange(array1, 1, 3);
System.out.println(Arrays.toString(array2));


Options are :

  • [a, b, c]
  • [b, c]
  • [b, c, d]
  • [b, c, d, e]

Answer :[b, c]

Which two fragments print out the date right after the first Monday of November this year?


Options are :

  • LocalDate localDate = LocalDate.now(); .withMonth(11); .withDayOfMonth(1); .with(TemporalAdjusters.next(DayOfWeek.TUESDAY)); System.out.println(localDate);
  • LocalDate localDate = LocalDate.now(); .withMonth(Month.NOVEMBER); .withDayOfMonth(1); .with(TemporalAdjusters.next(DayOfWeek.MONDAY)); .plusDays(1); System.out.println(localDate);
  • LocalDate localDate = LocalDate.now(); .withMonth(11); .with(TemporalAdjusters.firstDayOfMonth()); .with(TemporalAdjusters.next(DayOfWeek.MONDAY)); .plusDays(1); System.out.println(localDate);
  • Temporal temporal = TemporalAdjusters; .next(DayOfWeek.TUESDAY); .adjustInto(LocalDate.now(); .withMonth(11); .with(TemporalAdjusters.firstDayOfMonth()); ); System.out.println(temporal);

Answer :LocalDate localDate = LocalDate.now(); .withMonth(11); .withDayOfMonth(1); .with(TemporalAdjusters.next(DayOfWeek.TUESDAY)); System.out.println(localDate); Temporal temporal = TemporalAdjusters; .next(DayOfWeek.TUESDAY); .adjustInto(LocalDate.now(); .withMonth(11); .with(TemporalAdjusters.firstDayOfMonth()); ); System.out.println(temporal);

Given a source file:

Which access modifier can be inserted into the declaration of the Bar class?
package test;
public class Foo {
    // a valid body
}
/* Insert here */ class Bar {
    // a valid body
}


Options are :

  • public only
  • protected only
  • private only
  • public or protected
  • protected or private
  • No access modifier can be declared

Answer :No access modifier can be declared

What is the program's output?
class Foo {
    void myMethod() {
        System.out.println("Foo");
    }
}
class Bar {
    void myMethod() {
        System.out.println("Bar");
    }
}
public class Test {
    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 :Compilation fails

What is the output when compiling and executing the given program with the following command?

java Test Foo Bar

public class Test {
    public static void main(String[] strings) {
        String output = "";
        for (String string : strings) {
            output += output.concat(" ").concat(string);
        }
        System.out.println(output);
    }
}


Options are :

  • Test
  • Foo
  • Test Foo
  • Foo Bar
  • Foo Foo Bar
  • Test Foo Bar

Answer :Foo Foo Bar

What is the output of the given code?
List list = new ArrayList<>(List.of("A", "A"));
String[] array = list.toArray();
for (int i = 0; i < list.size(); i++) {
    list.set(i, list.get(i).toLowerCase());
}
System.out.println(Arrays.toString(array));


Options are :

  • [A]
  • [A, A]
  • [a]
  • [a, a]
  • Compilation fails

Answer :Compilation fails

It's known that this program compiles:

Which of the following can be the body of methodC?
public class Test {
    void methodA() throws RuntimeException {
        throw new ArithmeticException();
    }
    void methodB() throws IOException {
        throw new FileNotFoundException();
    }
    void methodC() {
       /* Insert here */
    }
    public static void main(String[] args) {
        Test test = new Test();
        test.methodC();
    }
}


Options are :

  • try { methodA(); methodB(); } catch (IOException e) { }
  • methodA(); try { methodB(); } catch (FileNotFoundException | IOException e) { }
  • methodA(); try { methodB(); } catch (RuntimeException | IOException e) { throw e; }
  • try { methodA(); methodB(); } catch (ArithmeticException | FileNotFoundException e) { }
  • None of the above

Answer :try { methodA(); methodB(); } catch (IOException e) { }

What is the program's output?
public class Test {
    public static void main(String[] args) {
        Test test = new Test();
        test.loop(1);
    }
   void loop(int number) {
        do {
            System.out.println(number--);
        } while (number < 0);
    }
}


Options are :

  • 0
  • 1
  • Nothing
  • It runs into an infinite loop
  • Compilation fails

Answer :1

What is the output of the given program?

public class Person {
    String name;
    int age;
    public String toString() {
        return name + ": " + age;
    }
}
//And:
public class Test {
    static public void main(String[] args) {
        Person person = new Person();
        person.name = "John";
        person.age = 30;
        String string = String.valueOf(person);
        person.name = "Jane";
        person.age = 20;
        System.out.println(string);
    }
}


Options are :

  • John: 30
  • John: 20
  • Jane: 30
  • Jane: 20
  • A string containing the memory address of the Person object
  • Compilation fails

Answer :John: 30

What is the program's output?
public class Test {
   Test(String arg1, String arg2) {
        System.out.println("Two parameters");
        this(arg1 + " " + arg2);
    }
    Test(String arg) {
        System.out.println("One parameter");
        System.out.println(arg);
    }
    public static void main(String[] args) {
       new Test("Foo", "Bar");
    }
}


Options are :

  • One parameter Two parameters Foo Bar
  • Two parameters One parameter Foo Bar
  • Two parameters One parameter
  • Compilation fails

Answer :Compilation fails

Which option specify the location of input source files when compiling modules with the javac command?


Options are :

  • -m
  • -module
  • -source
  • --module-path
  • --module-source
  • --module-source-path

Answer :--module-source-path

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


Options are :

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

Answer :An UnsupportedOperationException is thrown

What is the output of the given code fragment?
Object[][] strings = new String[2][2]; // Line 1
strings[1][1] = "test";; strings[0] = new String[1]; // Line 2
strings[1] = new String[3]; // Line 3
System.out.println(strings[1][1]);


Options are :

  • null
  • test
  • Compilation fails on line 1
  • Compilation fails on line 2 and line 3
  • An exception is thrown on line 1
  • An exception is thrown on line 2

Answer :null

What is the program's output?
public class Test {
    static int number;
    static public void main(String[] args) {
        int number = 1; // Line 1
        Test test = new Test();
        if (test != null) {
            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 :Compilation fails on line 2

After which line the object created on line 0 is eligible for garbage collection?
class Data { }
class Test {
    Data createData() {
        Data a = new Data(); // Line 0
        Data b = new Data();
        Data c = a; // Line 1
        b = c; // Line 2
        a = null; // Line 3
        c = null; // Line 4
        return b; // Line 5
    }
}


Options are :

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

Answer :None of the above

What is the output of the given code?
for (int i = 0; i < 6; i++) {
    if (i % 3 != 0) {
        continue;
    }
    System.out.println(i);
}


Options are :

  • 0 3
  • 2000 3 6
  • 1 2 4 5
  • Nothing
  • Compilation fails

Answer :0 3

Which kinds of multiple inheritance does Java support?


Options are :

  • Multiple inheritance of state only
  • Multiple inheritance of implementation only
  • Multiple inheritance of type only
  • Multiple inheritance of state and implementation
  • Multiple inheritance of state and type
  • Multiple inheritance of implementation and type

Answer :Multiple inheritance of type only

Which two field declarations allow the given code to compile?
package foo;
public class MyFoo {
    // Insert here
}
//And:
package bar;
import foo.MyFoo;
public class MyBar extends MyFoo {
    void printText() {
        System.out.println(super.text);
    }
}


Options are :

  • String text = "Foo";
  • static String text = "Foo";
  • public String text = "Foo";
  • protected static String text = "Foo";

Answer :public String text = "Foo"; protected static String text = "Foo";

What is the program's output?
class Data {
    String input;
    Data(String input) {
        this.input = input;
    }
}
public class Test {
    Data echo(boolean nullified, Data data) {
        return nullified ? null : data;
    }
    public static void main(String[] args) {
        Test test = new Test();
        String input = "hello";
        String output = null;
        try {
            output = test.echo(true, new Data(input = "bye")).input;
        } catch (Exception e) { }
        System.out.println(input + " " + output);
    }
}


Options are :

  • hello hello
  • hello bye
  • hello null
  • bye hello
  • bye bye
  • bye null

Answer :bye null

What is the program's output?
public class Test {
    static public void main(String[] args) {
        int i = (i = 1) + (i = 2) - ++i + i-- * 3;
        System.out.println(i);
    }
}


Options are :

  • 1
  • 2
  • 5
  • 6
  • 9
  • 10

Answer :9

Given the source directory structure of a module:

my.foo

moduleinfo.java

foo

Test.java

Which command compiles the given module when executed in the parent folder of my.foo?


Options are :

  • javac --module-source-path . --module my.foo
  • javac -d out --module-source-path . -m my.foo
  • javac --module-path . -module my.foo
  • javac --module-path . -m my.foo
  • javac my.foo

Answer :javac -d out --module-source-path . -m my.foo

Which of the following expressions when inserted into the given code doesn't product the output [1, 2, 3]?
Integer[] array = {3, 2, 1};
Arrays.sort(array, /* Insert here */);
System.out.println(Arrays.toString(array));


Options are :

  • Integer::compare
  • Integer::compareTo
  • (int i1, int i2) -> i1 - i2
  • (var i1, var i2) -> i1 - i2
  • None of the above

Answer :(int i1, int i2) -> i1 - i2

What is the output of the given code?
for (int i = 0; i < 5; i++, System.out.print(i + " ")) {
    i++;
}


Options are :

  • 2000 2 4
  • 1 3 2005
  • 2 4 2006
  • Compilation fails

Answer :2 4 2006

Given a class that is compiled and packaged in a JAR file called foo.jar:
package foo;
public class StringFactory {
    public static String createString() {
        return "Hello World!";
    }
}
And a source file:
package test;
import foo.StringFactory;
public class Test {
    public static void main(String[] args) {
        String output = StringFactory.createString();
        System.out.println(output);
    }
}

The JAR file is put in the working directory, while the source file is located inside the test folder:

foo.jar

test

Test.java

Which statement is correct about the following command when it's executed:

java -cp . test/Test.java


Options are :

  • It fails, and the -cp option must be replaced by --class-path
  • It fails, and the -cp option must be replaced by -classpath
  • It fails, and the . value of the -cp option must be replaced by foo.jar
  • It fails, and the Test.java file must be replaced by its compiled class file
  • It prints "Hello World!" to the console

Answer :It prints "Hello World!" to the console

Which variable doesn't reference a "Hello World!" string?
String string1 = new String("Hello").concat(" ").concat("World!");
String string2 = "  Hello World!".stripLeading(); // 2 leading white-spaces
String string3 = new StringBuilder("World!")
        .append("Hello", 0, 5).append(" ").toString();
String string4 = new StringBuilder(" ")
        .insert(0, "Hello").append("World!").toString();


Options are :

  • string1
  • string2
  • string3
  • string4
  • None of the above

Answer :string3

What is the given code fragment's output?
String[] strings = {"b", "c", "a"};
Arrays.sort(strings, 1, 3);
System.out.println(Arrays.toString(strings));


Options are :

  • [a, b, c]
  • [b, a, c]
  • [b, c, a]
  • [c, b, a]
  • A NullPointerException is thrown
  • An ArrayIndexOutOfBoundsException is thrown

Answer :[b, a, c]

What is the program's output?
class Foo {
    int number = 1;
    int getNumber() {
        return number;
    }
}
public class Bar extends Foo {
    int number = 2;
    int getNumber() {
        return number;
    }
    public static void main(String[] args) {
        Foo foo = new Bar();
        System.out.print(foo.number + " " + foo.getNumber() + " ");
        Bar bar = (Bar) foo;
        System.out.print(bar.number + " " + bar.getNumber());
    }
}


Options are :

  • 1 1 1 1
  • 1 1 1 2
  • 1 1 2 2
  • 1 2 1 1
  • 1 2 1 2
  • 1 2 2 2

Answer :1 2 2 2

Which lines cause a compile-time error?
public class Foo {
    static int number;
    String text;
    void methodA() {
        System.out.println(this.number); // Line 1
        System.out.println(this.text); // Line 2
    }
    static void methodB() {
        System.out.println(this.number); // Line 3
        System.out.println(this.text); // Line 4
    }
}


Options are :

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

Answer :Line 3 and line 4

What can be the program's output?
class MyException extends Exception {
    MyException(String message) {
        super(message);
    }
}
public class Test {
    static void methodA() throws MyException {
        throw new MyException("A custom exception");
    }
    static int methodB(int argument) throws MyException {
        int result = 1 / argument;
        methodA();
        return result;
    }
   public static void main(String[] args) {
        try {
            String output = "The result is " + methodB(0);
            System.out.println(output);
        } catch (MyException e) {
            System.out.println(e);
        }
    }
}


Options are :

  • The result is NaN
  • java.lang.Exception: A custom exception
  • MyException: A custom exception
  • java.lang.ArithmeticException: / by zero
  • Exception in thread "main" java.lang.ArithmeticException: / by zero at Test.methodB(Test.java:13) at Test.main(Test.java:20)
  • Compilation fails

Answer :Exception in thread "main" java.lang.ArithmeticException: / by zero at Test.methodB(Test.java:13) at Test.main(Test.java:20)

In JDK 11, is it allowed to create a class in default package (with missing package statement)?


Options are :

  • Yes
  • No

Answer :Yes

What will be the result of compiling and executing Test class?
package com.udayankhattry.ocp1;

public class Test {
 private static void div(int i, int j) {
  try {
   System.out.println(i / j);
  } catch (ArithmeticException e) {
   throw (RuntimeException) e;
  }
 }
 public static void main(String[] args) {
  try {
   div(5, 0);
  } catch (ArithmeticException e) {
   System.out.println("AE");
  } catch (RuntimeException e) {
   System.out.println("RE");
  }
 }
}


Options are :

  • AE is printed on to the console and program terminates successfully
  • Compilation error
  • RE is printed on to the console and program terminates successfully
  • Program ends abruptly

Answer :AE is printed on to the console and program terminates successfully

Which of these access modifiers can be used for a top level interface?


Options are :

  • protected
  • private
  • All of the other options
  • public

Answer :public

What will be the result of compiling and executing Test class?
package com.udayankhattry.ocp1;

public class Test {
 public static void main(String[] args) {
  String fName = "Joshua";
  String lName = "Bloch";
  System.out.println(fName = lName);
 }
}


Options are :

  • true
  • Compilation error
  • None of the other options
  • false

Answer :None of the other options



Given code of Test.java file:

Does above code compile successfully?
package com.udayankhattry.ocp1;

public class Test {
 public static void main(String[] args) {
  System.out.println(new RuntimeException()); //Line n1
  System.out.println(new RuntimeException("HELLO")); //Line n2
  System.out.println(new RuntimeException(new RuntimeException("HELLO"))); //Line n3
 }
}


Options are :

  • Yes
  • No

Answer :Yes

What will be the result of compiling and executing Test class?
package com.udayankhattry.ocp1;

class M {}
class N extends M {}
class O extends N {}
class P extends O {}

public class Test {
 public static void main(String args[]) {
  M obj = new O();
  if (obj instanceof M)
   System.out.print("M");
  if (obj instanceof N)
   System.out.print("N");
  if (obj instanceof O)
   System.out.print("O");
  if (obj instanceof P)
   System.out.print("P");
 }
}


Options are :

  • MNO
  • MNP
  • O
  • MNOP
  • MOP
  • NOP

Answer :MNO

Interface java.util.function.Predicate<T> declares below non-overriding abstract method: boolean test(T t);
package com.udayankhattry.ocp1;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class Test {
 public static void main(String[] args) {
  List < String > list = new ArrayList < > (List.of("A", "E", "I", "O", "U"));
  if (verify(list, l -> l.remove("I"))) //Line n1
   if (verify(list, l -> l.add("I"))) {} //Line n2

  System.out.println(list);
 }

 private static boolean verify(List < String > list, Predicate < List < String >> predicate) {
  return predicate.test(list); //Line n3
 }
}
What will be the result of compiling and executing Test class?


Options are :

  • [A, E, I, O, U]
  • Line n1 causes compilation error
  • Line n3 causes compilation error
  • [A, E, O, U, I]
  • [A, E, O, U]
  • Line n2 causes compilation error

Answer :[A, E, O, U, I]

    Which of the following options, if used to replace /*INSERT*/, will compile successfully and on execution will print MONEY DOESN'T GROW ON TREES on to the console?

    package com.udayankhattry.ocp1;
    
    class Parent {
     String quote = "MONEY DOESN'T GROW ON TREES";
    }
    
    class Child extends Parent {
     String quote = "LIVE LIFE KING SIZE";
    }
    
    class GrandChild extends Child {
     String quote = "PLAY PLAY PLAY";
    }
    
    public class Test {
     public static void main(String[] args) {
      GrandChild gc = new GrandChild();
      System.out.println( /*INSERT*/ );
     }
    }

    Select ALL that apply.


    Options are :

    • ((Parent)gc).quote
    • (Parent)gc.quote
    • (Parent)(Child)gc.quote
    • ((Parent)(Child)gc).quote
    • gc.quote

    Answer :((Parent)gc).quote ((Parent)(Child)gc).quote

    What will be the result of compiling and executing Test class?
    package com.udayankhattry.ocp1;
    
    public class Test {
     private static String s;
     public static void main(String[] args) {
      try {
       System.out.println(s.length());
      } catch (NullPointerException | RuntimeException ex) {
       System.out.println("DONE");
      }
     }
    }


    Options are :

    • None of the other options
    • DONE
    • Executes successfully but no output
    • Compilation error

    Answer :Compilation error



    Consider below code fragment:

    Which 3 modifications, done independently, enable the code to compile?
    package com.udayankhattry.ocp1;
    
    abstract class Food {
     protected abstract double getCalories();
    }
    
    class JunkFood extends Food {
     double getCalories() {
      return 200.0;
     }
    }


    Options are :

    • Make the getCalories() method of JunkFood class public
    • Make the getCalories() method of JunkFood class protected
    • Make the getCalories() method of JunkFood class private
    • Make the getCalories() method of Food class private
    • Remove the protected access modifier from the getCalories() method of Food class
    • Make the getCalories() method of Food class public

    Answer :Make the getCalories() method of JunkFood class public Make the getCalories() method of JunkFood class protected Remove the protected access modifier from the getCalories() method of Food class

    Range of short data type is from -32768 to 32767.
    Which of the following code segments, written inside main method will compile successfully?

    Select ALL that apply.


    Options are :

    • final int i5 = 10; short s5 = i5 + 100;
    • short s1 = 10;
    • short s2 = 32768;
    • final int m = 25000; final int n = 25000; short s6 = m + n;
    • final int i4 = 40000; short s4 = i4;
    • final int i3 = 10; short s3 = i3;
    • int i7 = 10; short s7 = i7;

    Answer :final int i5 = 10; short s5 = i5 + 100; short s1 = 10; final int i3 = 10; short s3 = i3;

    Which of the following statements are correct regarding below code?
    package com.udayankhattry.ocp1;
    
    public class Test {
     int i1 = 10;
     static int i2 = 20;
     private void change1(int val) {
      i1 = ++val; //Line n1
      i2 = val++; //Line n2
     }
     private static void change2(int val) {
      i1 = --val; //Line n3
      i2 = val--; //Line n4
     }
     public static void main(String[] args) {
      change1(5); //Line n5
      change2(5); //Line n6
      System.out.println(i1 + i2); //Line n7
     }
    }

    Select ALL that apply.


    Options are :

    • Line n1 causes compilation error
    • Line n6 causes compilation error
    • Line n7 causes compilation error
    • Line n2 causes compilation error
    • Above code compiles successfully
    • Line n4 causes compilation error
    • Line n5 causes compilation error
    • Above code prints 8 on execution
    • Above code prints 30 on execution
    • Line n3 causes compilation error

    Answer :Line n7 causes compilation error Line n5 causes compilation error Line n3 causes compilation error

    What will be the result of compiling and executing Test class?
    package com.udayankhattry.ocp1;
    
    public class Test {
     public static void main(String[] args) {
      StringBuilder sb = new StringBuilder();
      System.out.println(sb.append(null).length());
     }
    }


    Options are :

    • Compilation error
    • 1
    • 4
    • An exception is thrown at runtime

    Answer :Compilation error

    What will be the result of compiling and executing Test class?
    package com.udayankhattry.ocp1;
    
    interface Profitable {
     double profitPercentage = 42.0;
    }
    
    class Business implements Profitable {
     double profitPercentage = 50.0; //Line n1
    }
    
    public class Test {
     public static void main(String[] args) {
      Profitable obj = new Business(); //Line n2
      System.out.println(obj.profitPercentage); //Line n3
     }
    }


    Options are :

    • Test class compiles successfully and on execution prints 50.0 on to the console
    • Line n2 causes compilation error
    • Test class compiles successfully and on execution prints 42.0 on to the console
    • Line n3 causes compilation error
    • Line n1 causes compilation error

    Answer :Test class compiles successfully and on execution prints 42.0 on to the console

      Which of the following statements is true?

      package com.udayankhattry.ocp1;
      
      import java.util.ArrayList;
      import java.util.List;
      
      interface Sellable {}
      abstract class Animal {}
      class Mammal extends Animal {}
      class Rabbit extends Mammal implements Sellable {}
      
      public class Test {
       {
        List < Animal > list = new ArrayList < > ();
        list.add(new Rabbit());
       } {
        List < Animal > list = new ArrayList < > ();
        list.add(new Mammal());
       } {
        List < Mammal > list = new ArrayList < > ();
        list.add(new Rabbit());
       } {
        List < Sellable > list = new ArrayList < > ();
        list.add(new Mammal());
       } {
        List < Sellable > list = new ArrayList < > ();
        list.add(new Rabbit());
       }
      }


      Options are :

      • Only one initializer block causes compilation error
      • Three initializer blocks cause compilation error
      • Four initializer blocks cause compilation error
      • Five initializer blocks cause compilation error
      • Two initializer blocks cause compilation error

      Answer :Only one initializer block causes compilation error

      What will be the result of compiling and executing Test class?
      package com.udayankhattry.ocp1;
      
      public class Test {
       public static void main(String[] args) {
        String[] arr = new String[7];
        System.out.println(arr);
       }
      }


      Options are :

      • Compilation Error
      • It prints some text containing @ symbol
      • It prints null
      • An exception is thrown at runtime

      Answer :It prints some text containing @ symbol

      Which of the following options is correct?
      class A {
       public static void main(String[] args) {
        System.out.println("A");
       }
      }
      
      class B {
       public static void main(String[] args) {
        System.out.println("B");
       }
      }
      
      class C {
       public static void main(String[] args) {
        System.out.println("C");
       }
      }
      
      class D {
       public static void main(String[] args) {
        System.out.println("D");
       }
      }


      Options are :

      • To print C on to the console, execute below commands: javac C.java java C
      • To print C on to the console, execute below commands: javac Test.java java Test
      • To print C on to the console, execute below commands: javac Test.java java C
      • Test.java file will compile successfully but expected output is not possible
      • Test.java file is not a valid java file as it doesn't contain code for class Test

      Answer :To print C on to the console, execute below commands: javac Test.java java C

      What will be the result of compiling and executing Test class?
      package com.udayankhattry.ocp1;
      
      public class Test {
       char c;
       double d;
       float f;
       public static void main(String[] args) {
        Test obj = new Test();
        System.out.println(">" + obj.c);
        System.out.println(">" + obj.d);
        System.out.println(">" + obj.f);
       }
      }


      Options are :

      • >null >0.0 >0.0f
      • >null >0.0 >0.0
      • > >0.0 >0.0
      • > >0.0 >0.0f

      Answer :> >0.0 >0.0

      _________ modifier is most restrictive and __________ modifier is least restrictive.
      Which of the following options (in below specified order) can be filled in above blank spaces?


      Options are :

      • default (with no access modifier specified), public
      • default (with no access modifier specified), protected
      • public, private
      • private, public
      • protected, public

      Answer :private, public

      What will be the result of compiling and executing Test class?
      package com.udayankhattry.ocp1;
      
      import java.util.ArrayList;
      import java.util.List;
      
      public class Test {
       public static void main(String[] args) {
        Boolean[] arr = new Boolean[2];
        List < Boolean > list = new ArrayList < > ();
        list.add(arr[0]);
        list.add(arr[1]);
        if (list.remove(0)) {
         list.remove(1);
        }
        System.out.println(list);
       }
      }


      Options are :

      • Compilation error
      • NullPointerException is thrown at runtime
      • [false]
      • []
      • ArrayIndexOutOfBoundsException is thrown at runtime
      • [true]

      Answer :NullPointerException is thrown at runtime

        package com.udayankhattry.ocp1;
        
        interface I1 {
         void m1();
        }
        
        interface I2 extends I1 {
         void m2();
        }
        
        interface I3 extends I2 {
         void m3();
        }
        
        interface I4 {
         String toString();
        }
        
        public class Test {
         public static void main(String[] args) {
          /*INSERT*/
         }
        }

        And the statements:

        1. I1 i1 = () -> System.out.println(1);

        2. I2 i2 = () -> System.out.println(2);

        3. I3 i3 = () -> System.out.println(3);

        4. I4 i4 = () -> System.out.println(4);

        5. I4 i5 = () -> "";

        Which of the above statements can replace /*INSERT*/ such that there is no compilation error?


        Options are :

        • Only one statement
        • Three statements
        • Two statements
        • All Five statements
        • Four statements
        • None of the statements

        Answer :Only one statement

          What will be the result of compiling and executing Test class?
          package com.udayankhattry.ocp1;
          
          import java.util.Arrays;
          import java.util.List;
          
          public class Test {
           public static void main(String[] args) {
            StringBuilder[] arr = {
             new StringBuilder("A"),
             new StringBuilder("A")
            };
            List < StringBuilder > list = Arrays.asList(arr);
            for (int i = 0; i < 2; i++)
             if (i == 0)
              list.forEach(sb -> sb.append("B"));
             else
              list.forEach(sb -> sb.append("C"));
          
            list.forEach(sb -> System.out.println(sb));
           }
          }


          Options are :

          • AB AB
          • A A
          • AC AC
          • Runtime exception is thrown
          • ABB ACC
          • BC BC
          • ABC ABC

          Answer :ABC ABC

          What will be the result of compiling and executing Test class?
          package com.udayankhattry.ocp1;
          
          import java.util.ArrayList;
          
          public class Test {
           public static void main(String[] args) {
            ArrayList < Integer > original = new ArrayList < > (); //Line n1
            original.add(10); //Line n2
            ArrayList < Integer > cloned = (ArrayList < Integer > ) original.clone();
            Integer i1 = cloned.get(0);
            ++i1;
            System.out.println(cloned);
           }
          }


          Options are :

          • [10]
          • [11]
          • Compilation error
          • An exception is thrown at runtime

          Answer :[10]

          What will be the result of compiling and executing Test class?
          package com.udayankhattry.ocp1;
          
          public class Test {
           public static void main(String[] args) {
            StringBuilder sb = new StringBuilder("Breathe Deeply");
            String str1 = sb.toString();
            String str2 = "Breathe Deeply";
            System.out.println(str1 == str2);
           }
          }


          Options are :

          • false
          • Compilation error
          • An exception is thrown at runtime
          • true

          Answer :false

          What is the result of compiling and executing Test class?
          package com.udayankhattry.ocp1;
          
          public class Test {
           public static void main(String[] args) {
            var score = 30; // Line n1
            var grade = 'F'; // Line n2
            if (50 <= score < 60) // Line n3
             grade = 'D';
            else if (60 <= score < 70) // Line n4
             grade = 'C';
            else if (70 <= score < 80) // Line n5
             grade = 'B';
            else if (score >= 80)
             grade = 'A';
            System.out.println(grade);
           }
          }


          Options are :

          • A
          • C
          • D
          • Compilation error
          • B
          • F

          Answer :Compilation error

          What will be the result of compiling and executing Test class?

          1. package com.udayankhattry.ocp1;
            
            public class Test {
             public static void main(String[] args) {
              String fruit = "mango";
              switch (fruit) {
               case "Apple":
                System.out.println("APPLE");
               case "Mango":
                System.out.println("MANGO");
               case "Banana":
                System.out.println("BANANA");
                break;
               default:
                System.out.println("ANY FRUIT WILL DO");
              }
             }
            }


          Options are :

          • MANGO ANY FRUIT WILL DO
          • MANGO BANANA
          • MANGO
          • MANGO BANANA ANY FRUIT WILL DO
          • ANY FRUIT WILL DO

          Answer :ANY FRUIT WILL DO

          When does a class get the default constructor?


          Options are :

          • All classes in Java get a default constructor
          • If the class does not define any constructors explicitly
          • If you define parameterized constructor for the class
          • You have to define at least one constructor to get the default constructor

          Answer :If the class does not define any constructors explicitly

          Consider below codes of 3 java files:
          //Super.java
          package com.udayankhattry.ocp1;
          
          public interface Super {
           String name = "SUPER"; //Line n1
          }
          
          //Sub.java
          package com.udayankhattry.ocp1;
          
          public interface Sub extends Super { //Line n2
          
          }
          
          //Test.java
          package com.udayankhattry.ocp1;
          
          public class Test {
           public static void main(String[] args) {
            Sub sub = null;
            System.out.println(sub.name); //Line n3
           }
          }
          Which of the following statements is correct?


          Options are :

          • Line n3 causes compilation error
          • Line n1 causes compilation error
          • Test class compiles successfully and on execution prints SUPER on to the console
          • Line n3 throws an exception at runtime
          • Line n2 causes compilation error

          Answer :Test class compiles successfully and on execution prints SUPER on to the console

          1. package com.udayankhattry.ocp1;
            
            public class Test {
             public static void main(String[] args) {
              try {
               main(args);
              } catch (Exception ex) {
               System.out.println("CATCH-"); //Line n1
              }
              System.out.println("OUT"); //Line n2
             }
            }
          What will be the result of compiling and executing Test class?


          Options are :

          • System.out.println statements at Line n1 and Line n2 are not executed and program ends abruptly
          • OUT is printed and program terminates successfully
          • Compilation error
          • CATCH-OUT is printed and program terminates successfully

          Answer :System.out.println statements at Line n1 and Line n2 are not executed and program ends abruptly

            Following options are available to replace /*INSERT*/:
            package com.udayankhattry.ocp1;
            
            public class Test {
             public static void main(String[] args) {
              String text = null;
              /*INSERT*/
             }
            }

            1. System.out.println(text.repeat(3));

            2. System.out.println(null + null + null);

            3. System.out.println(null + "null" + null);

            4. System.out.println(text *= 3);

            5. System.out.println(text += "null".repeat(2));

            6. System.out.println(text + text + text);

            7. text += null; System.out.println((text.concat(null)));

            How many of the above options can be used to replace /*INSERT*/ (separately and not together) such that nullnullnull is printed on to the console?


            Options are :

            • Four options only
            • All seven options
            • Five options only
            • One option only
            • Three options only
            • Six options only
            • Two options only

            Answer :Three options only

            Comment / Suggestion Section
            Point our Mistakes and Post Your Suggestions