Site Search:

Simulation Test 3 -- 90 question set

Question1. What is the result of executing the following code?



  1 import java.io.*;
  2 class Channel {}
  3 public class TV implements Serializable {
  4   private String brand;
  5   private transient int year;
  6   private Channel channel;
  7 
  8   public String getBrand() {return this.brand;}
  9   public void setBrand(String brand) {this.brand = brand;}
 10   public int getYear() { return year;}
 11   public void setYear(int year) { this.year = year;}
 12   public Channel getChannel() { return channel;}
 13   public void setChannel(Channel channel) {this.channel = channel;}
 14 
 15   public static void main(String[] args) throws IOException {
 16     try(ObjectOutputStream out = new ObjectOutputStream(
 17       new BufferedOutputStream(new FileOutputStream("tv.dat")))) {
 18       TV tv = new TV();
 19       tv.setBrand("jee");
 20       tv.setYear(2009);
 21       tv.setChannel(new Channel());
 22       out.writeObject(tv);
 23     }
 24   }
 25 }

A. It compiles and runs without issue
B. compile error at line 3
C. compile error at line 5
D. compile error at line 10
E. compile error at line 11
F. compile error at line 16 - 17
G. compile error at line 20
H. It compiles but throws an exception at runtime.

Answers: H

Explain: The code compiles, however, when it execute line 22, the following RuntimeException is thrown. Exception in thread "main" java.io.NotSerializableException: Channel. Channel needs to implements Serializable.

Study: Use BufferedReader, BufferedWriter, File, FileReader, FileWriter, FileInputStream, FileOutputStream, ObjectOutputStream, ObjectInputStream, and PrintWriter in the java.iopackage.


Question2

Which of the following statements have valid syntax?

A. List names = new ArrayList();
B. List<String> names = new ArrayList<String>();
C. List<String> names = new ArrayList<>();
D. List names = new ArrayList<String>();
E. List<> names = new ArrayList<String>();
F. List<String> names = new ArrayList(); 
G. List names = new ArrayList<>();
H. List<String> names = new Object();

Answers: A, B, C, D, F, G

Explain: A is pre java5 syntax, the element in the List is Object. B is java5 syntax, C is java 7 syntax, diamond <> at the right side of the assignment tell compiler to inference the type. D is legal, it assigns generic type to raw type List, due to type erasure, the assignment is allowed, though we got compiler warning "uses unchecked or unsafe operations". E uses illegal syntax, diamond can not be used at the left side of the assignment, you have to supply a concrete type (or no type for raw type), otherwise the compiler error is illegal start of type. F is a legal syntax, it assigns raw type to generic type, due to type erasure, the code compiles, despite of the compiler warning uses unchecked or unsafe operations. G is also legal, diamond <> hints the compiler to deduce the type, it assigns generic type to raw type List, due to type erasure, the assignment is allowed, though we got compiler warning "uses unchecked or unsafe operations". H is illegal, the compile error is incompatible types: Object cannot be converted to List<String>.

Question 3