The code fragment will fail to compile because the parseInt method of the Integer class is a static method, which means that it can be invoked without creating an object of the class. However, the code is trying to invoke the parseInt method on an object of type Integer, which is not allowed. The correct way to invoke the parseInt method is by using the class name, such as Integer.parseInt (s). Therefore, the code fragment will produce a compilation error. References: Integer (Java SE 17 & JDK 17) - Oracle
Question 2
Given the code fragment:
What is the result?
Options:
A.
81111
B.
8109
C.
777
D.
71010
E.
888
F.
7107
Answer:
B
Explanation:
Explanation:
The code fragment is creating a string variable “a” with the value “Hello! Java”. Then, it is printing the index of “Java” in “a”. Next, it is replacing “Hello!” with “Welcome!” in “a”. Then, it is printing the index of “Java” in “a”. Finally, it is creating a new StringBuilder object “b” with the value of “a” and printing the index of “Java” in “b”. The output will be 8109 because the index of “Java” in “a” is 8, the index of “Java” in “a” after replacing “Hello!” with “Welcome!” is 10, and the index of “Java” in “b” is 9. References: Oracle Java SE 17 Developer source and documents: [String (Java SE 17 & JDK 17)], [StringBuilder (Java SE 17 & JDK 17)]
Question 3
Given the product class:
And the shop class:
What is the result?
Options:
A.
Cookie 2.99 2.99
B.
Cookie 3.99 2.99
C.
Cookie 0.0 0.0
D.
An exception is produced at runtime
E.
Compilation fails
F.
Cookie 0.0 2.99
Answer:
E
Explanation:
Explanation:
The code fragment will fail to compile because the readObject method in the Product class is missing the @Override annotation. The readObject method is a special method that is used to customize the deserialization process of an object. It must be declared as private, have no return type, and take a single parameter of type ObjectInputStream. It must also be annotated with @Override to indicate that it overrides the default behavior of the ObjectInputStream class. Without the @Override annotation, the compiler will treat the readObject method as a normal method and not as a deserialization hook. Therefore, the code fragment will produce a compilation error. References: Object Serialization - Oracle, [ObjectInputStream (Java SE 17 & JDK 17) - Oracle]