Search Tutorials


Oracle Java 17 1Z0-829 Certification Exam Practice Test 1 (2024) | JavaInUse

Oracle Java 17 1Z0-829 Certification Exam Practice Test 1

Q. Given code:

    
package com.JavaInUse.ocp;

import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String> celebrations =
                List.of("HALLOWEEN", "THANKSGIVING", "NEW YEAR");

        celebrations.removeIf(str -> str.length() == 9);

        System.out.println(celebrations);
    }

What is the result?
A. The code will compile and run successfully, and the output will be [HALLOWEEN, NEW YEAR] as the elements with a length of 9 are removed.
B. A compilation error will occur because the removeIf method is not supported on immutable lists created using List.of.
C. An exception is thrown at runtime
D. [HALLOWEEN,THANKSGIVING,NEW YEAR]

Q. Consider below code of Test.java file:

package com.JavaInUse.ocp;

public class Test {
    public static void main(String[] args) {
        int value = 7;

        if (++value == value++) {
            System.out.println("MATCH " + value);
        } else {
            System.out.println("NO MATCH" + value);
        }
    }
    

What is the result?
A. MATCH 9
B. NO MATCH 9
C. NO MATCH 8
D. MATCH 8

Q. Given code of Test.java file:

    
package com.JavaInUse.ocp;

class CustomResource implements AutoCloseable {
    @Override
    public void close() {
        System.out.println("Resource Closed");
    }
}

public class Example {
    public static void main(String[] args) {
        try (AutoCloseable myResource = new CustomResource()) {
            // Some code here
        }
    }

What is the result?
A. Compilation error in Example class
B. Resource Closed
C. Compilation error in CustomResource Class
D. None of the above

Q. Given code of Test.java file:

     
package com.JavaInUse.ocp;

public class BaseClass {
    int identifier = 1000; // Line n1

    BaseClass() {
        initBase(); // Line n2
    }

    void initBase() { // Line n3
        System.out.println("BaseClass ID: " + (++identifier)); // Line n4
    }
}

class DerivedClass extends BaseClass {
    int identifier = 2000; // Line n5

    DerivedClass() {}// Line n6

    void initBase() { // Line n7
        System.out.println("DerivedClass ID: " + (--identifier)); // Line n8
    }
}

public class Example {
    public static void main(String[] arguments) {
        BaseClass baseObject = new DerivedClass(); // Line n9
    }

What is the result?
A. DerivedClass ID: -1
B. DerivedClass ID: 0
C. DerivedClass ID: 999
D. exception error

Q. Given Code of Test.java file:

     
package com.JavaInUse.ocp;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

enum Shade {
    CRIMSON, EMERALD, AZURE;
}

record StringRope(int size, Shade shade) {}

record RopeGroup(Map<Shade, List<StringRope>> ropeMap,
                 Map<Integer, List<StringRope>> sizeMap) {}

public class Example {
    public static void main(String[] arguments) {
        var strings = Stream.of(
            new StringRope(100, Shade.CRIMSON),
            new StringRope(200, Shade.AZURE),
            new StringRope(200, Shade.CRIMSON),
            new StringRope(300, Shade.CRIMSON),
            new StringRope(100, Shade.AZURE));

        var outcome = strings.collect(
            Collectors.teeing(
                Collectors.groupingBy(StringRope::shade),
                Collectors.groupingBy(StringRope::size),
                RopeGroup::new
            ));

        System.out.println(outcome.ropeMap().size() +
                                outcome.sizeMap().size());
    }

What is the result?
A. 5
B. 4
C. 2
D. 1

Q. What is the result?

package com.JavaInUse.ocp;

public class Test {
    public static void main(String[] arguments) {
        StringBuilder builder = new StringBuilder("BREATHE");
        String result = builder.toString() + (builder.append("OUT"));
        System.out.println(result.strip().length());
    }
}
A. 20
B. 12
C. 13
D. 23

Q. What is the result?

package com.JavaInUse.ocp;

public class Test {
    private static String text;

    public static void main(String[] arguments) {
        try {
            System.out.println(text.length());
        } catch (NullPointerException | RuntimeException exception) {
            System.out.println("COMPLETED");
        }
    }
}
A. COMPLETED
B. Executes but no result
C. Compilation error
D. None of the Above

Q. What is the result?

package com.JavaInUse.ocp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
    public static void main(String[] arguments) {
        List<String> wordList = new ArrayList<>(Arrays.asList("Apple", "Banana", "Orange"));

        String selectedWord = wordList.stream()
                .sorted()
                .findFirst()
                .get();

        System.out.println("Selected Fruit: " + selectedWord);
    }
}
A. Selected Fruit: Apple
B. Selected Fruit: Banana
C. Selected Fruit: Orange
D. Compilation error

Q. On execution, Test class prints 40 on to the console. Which of the following statement can replace Line n1 such that there is no change in the output?

package com.JavaInUse.ocp;

@FunctionalInterface
interface MathOperation {
    void performOperation(int operand1, int operand2);
}

class Arithmetic {
    public static void addition(int operand1, int operand2) {
        System.out.println("Sum: " + (operand1 + operand2));
    }
}

public class Test{
    public static void main(String[] arguments) {
        MathOperation operation = (operand1, operand2) ->
                System.out.println("Result: " + (operand1 + operand2)); // Line n1

        operation.performOperation(15, 25);
    }
}
A. MathOperation operation = Arithmetic::subtraction;
B. MathOperation operation = Arithmetic::addition;
C. Arithmetic instance = new Arithmetic();\nMathOperation operation = instance::addition;
D. MathOperation operation = (operand1, operand2) -> \nSystem.out.println("Difference: " + (operand1 - operand2));

Q. F: is accessible for reading/writing and currently doesn't contain any files/directories.
What is the result?

package com.JavaInUse.ocp;

import java.io.*;

class Person {
    private String fullName;
    private int years;

    public Person(String fullName, int years) {
        this.fullName = fullName;
        this.years = years;
    }

    public String getFullName() {
        return fullName;
    }

    public int getYears() {
        return years;
    }
}

class Learner extends Person implements Serializable {
    private String major;

    public Learner(String fullName, int years, String major) {
        super(fullName, years);
        this.major = major;
    }

    public String getMajor() {
        return major;
    }
}

public class Test {
    public static void main(String[] arguments)
            throws IOException, ClassNotFoundException {
        var student = new Learner(
                "John", 20, "Data Science");

        try (var oos = new ObjectOutputStream(
                     new FileOutputStream(("F:\\learner.ser")));
             var ois = new ObjectInputStream(
                     new FileInputStream("F:\\learner.ser")))
        {
            oos.writeObject(student);

            var learner = (Learner) ois.readObject();
            System.out.printf("%s, %d, %s", learner.getFullName(),
                    learner.getYears(), learner.getMajor());
        }
    }
}
A. John,0,Data Science
B. John,20,Data Science
C. Runtime Exception
D. null,0,null

Q. What is the result?

package com.JavaInUse.ocp;

import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] arguments) {
        List<String> letters = new ArrayList<>();
        letters.add("A");
        letters.add("B");
        letters.add("C");

        List<String> selectedLetters = letters.subList(1, 2);
        selectedLetters.set(0, "X");
        System.out.println("Result: " + String.join("-", letters));
    }
}
A. A-X-C
B. A-B-C
C. Exception error at line n2
D. Compilation error

Q. Given code:

package com.JavaInUse.ocp;

    public class Test{
        public static void main(String[] arguments) {
            String text1 = new String("EVERYTHING IS FINE");
            String text2 = new String("EVERYTHING IS FINE");
            System.out.println(text1.equals(text2));
        }
    }

Which of the following changes, done independently, allows the code to compile and on execution prints true?

Replace Line n3 with: System.out.print(text1.toString()==text2.toString());
Replace Line n1 with: String text1 = "EVERYTHING IS FINE";
Replace Line n2 with: String text2 = "EVERYTHING IS FINE";
Replace Line n2 with: String text2 = text1.toString();
Replace Line n1 with: String text1 = new String("EVERYTHING IS FINE").intern();
Replace Line n2 with: String text2 = new String("EVERYTHING IS FINE").intern();

Q. Given code of Example.java file:

package com.JavaInUse.ocp;

    public class Example{
        public static void main(String[] arguments) {
            try {
                validate();
            } catch(RuntimeException e) {
                System.out.println(e.getClass().getSimpleName());
            }
        }

        private static void validate() {
            try {
                RuntimeException runtimeError = new RuntimeException();
                throw runtimeError;
            } catch(RuntimeException e) {
                System.out.println("Error Code: 1");
                ArithmeticException conversionError = (ArithmeticException) e;
                System.out.println("Error Code: 2");
                throw conversionError;
            }
        }
    }

What is the result?

Error Code: 1
Error Code: 2
RuntimeException is thrown
Error Code: 1
Error Code: 2
ArithmeticException is thrown
Error Code: 1
ArithmeticException is thrown
Error Code: 1
ClassCastException is thrown

Q. Given code of Example.java file:

package com.JavaInUse.ocp;

    public class Example {
        public static void main(String[] arguments) {
            Boolean[] booleanArray = new Boolean[2];
            System.out.println(booleanArray[0] + " - " + booleanArray[1]);
        }
    }

What is the result?

T[True]-T[True]
F[False]-F[False]
null-null
Runtime Exception

Q. Given code:

package com.JavaInUse.ocp;

    import java.util.Date;
    import java.util.function.Supplier;

    public class Example {
        public static void main(String[] args) {
            /*INSERT*/ supplier = Date::new; //Constructor reference for no-argument Date() constructor
            Date result = supplier.get(); //Creates an instance of Date class.
            System.out.println(result);
        }
    }

Which of the following options can replace /*INSERT*/ such that on executing Test class, current date and time is displayed in the output?

Supplier<Date>
Consumer
Consumer<Date>
Supplier<Object>