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 fragments:
Which action prints Wagon : 200?
Options:
A.
At line n1, implement the java.io, Serializable interface.
B.
At line n3, replace readObject (0 with readLine().
C.
At Line n3, replace Car with LuxurayCar.
D.
At Line n1, implement the java.io.AutoCloseable interface
E.
At line n2, in the main method signature, add throws IOException, ClassCastException.
F.
At line n2, in the main method signature, add throws IoException, ClassNotFoundException.
Answer:
F
Explanation:
Explanation:
The code fragment is trying to read an object from a file using the ObjectInputStream class. This class throws an IOException and a ClassNotFoundException. To handle these exceptions, the main method signature should declare that it throws these exceptions. Otherwise, the code will not compile. If the main method throws these exceptions, the code will print Wagon : 200, which is the result of calling the toString method of the LuxuryCar object that was written to the file. References: ObjectInputStream (Java SE 17 & JDK 17) - Oracle, ObjectOutputStream (Java SE 17 & JDK 17) - Oracle
Question 3
Given the code fragment:
Which action enables the code to compile?
Options:
A.
Replace record with void.
B.
Remove the regNO initialization statement.
C.
Make the regNo variable static.
D.
Replace thye regNo variable static
E.
Make the regNo variable public
Answer:
E
Explanation:
Explanation:
The code will compile if the regNo variable is made public. This is because the regNo variable is being accessed in the main method of the App class, which is outside the scope of the Product class. Making the regNo variable public will allow it to be accessed from outside the class. References: