Question 6 of 50

Given the code fragment:

Which action enables the code to compile?

    Correct Answer: E

    The keyword 'record' in Java allows only instance methods and variables to be defined. However, the error here is because a record cannot have non-static instance fields outside of constructor parameters. Therefore, making 'regNo' a static variable will allow the code to compile since static fields are allowed in records. The correct action is to make the 'regNo' variable static.

Question 7 of 50

Given:

What is the result?

    Correct Answer: C

    The given code defines an enum 'Forecast' with three constants: SUNNY, CLOUDY, and RAINY. The toString method is overridden to return 'SNOWY' for any constant of this enum. In the main method, 'Forecast.SUNNY.ordinal()' is called, which returns the index of SUNNY, which is 0. Then 'Forecast.valueOf("cloudy".toUpperCase())' is evaluated, which converts 'cloudy' to 'CLOUDY' and retrieves the CLOUDY enum constant. Because the toString method is overridden, it returns 'SNOWY' when CLOUDY is printed. Therefore, the output is '0 SNOWY'.

Question 8 of 50

Given the code fragment:

What is the result?

    Correct Answer: B

    The code makes use of Java Streams to evaluate various conditions on the specialDays list and outputs the results. The first line of output comes from the allMatch method, which checks if all elements in the stream match the given predicate (equals 'Labour'). This returns false because not all elements in the list are 'Labour'. The second line uses anyMatch to check if any element in the stream matches 'Labour', which is true. The third line uses noneMatch to check if none of the elements match 'Halloween', which is true as 'Halloween' is not in the list. Finally, findFirst returns an Optional describing the first element of the stream, which is 'NewYear'. Therefore, the output is 'false true true Optional[NewYear]'.

Question 9 of 50

Given:

Which statement is true while the program prints GC?

    Correct Answer: B

    In the given code, t1 is initially assigned a new App object with the name 't1', and t2 is assigned a new App object with the name 't2'. When t1 is set to t2, both t1 and t2 reference the same App object with the name 't2'. After setting t1 to null, t2 still references the App object with the name 't2'. The App object with the name 't1' is not referenced anymore and is eligible for garbage collection, but the object referenced by t2 is not eligible for garbage collection. Thus, only the first App object is eligible for garbage collection.

Question 10 of 50

Given:

What is the result?

    Correct Answer: B

    The method new Main().print(0b1101_1010) calls the print method with the binary value 0b1101_1010, which is 218 in decimal. In Java, a binary literal prefixed with 0b is considered an integer. However, 218 exceeds the range of a byte type (which is -128 to 127) and therefore fits within the range of an int. But since both int and long can accommodate 218, Java will prefer the more specific method when possible. Thus, it will choose print(long j) because method resolution in Java chooses the narrowest possible type when an exact match isn't found, leading to the result 'there'.