Oracle
1Z0-829 · Question #18
1Z0-829 Question #18: Real Exam Question with Answer & Explanation
Sign in or unlock 1Z0-829 to reveal the answer and full explanation for question #18. The question stem and answer options stay visible for context.
Using Java I/O API
Question
Given the product class:
import java.io.*;
public class Product implements Serializable {
private static float averagePrice = 2.99f;
private String description;
private transient float price;
public Product(String description, float price) {
this.description = description;
this.price = price;
}
public void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject();
price = averagePrice;
}
public String toString() {
return description+" "+price+" "+averagePrice;
}
}
And the shop class:
import java.io.*;
public class Shop {
public static void main(String[] args) {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("p.ser")));
out.writeObject(p);
ObjectInputStream in = new ObjectInputStream(new FileInputStream("p.ser"))) {
p = (Product)in.readObject();
} catch (Exception e) { e.printStackTrace(); }
System.out.println(p);
}
}
What is the result?
Options
- ACookie 2.99 2.99
- BCookie 3.99 2.99
- CCookie 0.0 0.0
- DAn exception is produced at runtime
- ECompilation fails
- FCookie 0.0 2.99
Unlock 1Z0-829 to see the answer
You've previewed enough free 1Z0-829 questions. Unlock 1Z0-829 for full answers, explanations, the timed quiz mode, progress tracking, and the master PDF. Question stem and options stay visible so you can still see what's on the exam.
Topics
#serialization#readObject contract#try-with-resources syntax#compilation errors