Question 1. What is the output of the following program?
1 public abstract class Email { 2 public String recipient; 3 void sendEmail(){}; 4 public static void main(String args[]) { 5 Email email = new XyzEmail(); 6 email.recipient = "John@xyznetwork.com"; 7 email.sendEmail(); 8 } 9 static class XyzEmail extends Email { 10 public final void sendEmail() { 11 System.out.println("Email is sent to " + recipient); 12 }}}
A. Email is sent to John@xyznetwork.com
B. Email is sent to null
C. A compile error at line 3
D. A compile error at line 9
E. A compile error at line 11
F. None of the above
Answer: A
Explain: an abstract class can have any number of methods including zero. The methods can be abstract or concrete. A static nested class can not access the local variables of enclosing class, however it can access variables defined in the enclosing class like a regular class without a reference of the outer class (line 11).
Study: Develop code that uses abstract classes and methods
===end===Explain: an abstract class can have any number of methods including zero. The methods can be abstract or concrete. A static nested class can not access the local variables of enclosing class, however it can access variables defined in the enclosing class like a regular class without a reference of the outer class (line 11).
Study: Develop code that uses abstract classes and methods
===start===
Question2. What is the output of the following program?
1 import java.io.*; 2 class Channel { 3 int number = 3; 4 public Channel(int number) {this.number = number;} 5 } 6 public class TV implements Serializable { 7 public TV() {brand="jeep";year=2008;channel=new Channel();} 8 private transient String brand; 9 private int year; 10 private static Channel channel; 11 public String getBrand() {return this.brand;} 12 public void setBrand(String brand) {this.brand = brand;} 13 public int getYear() { return year;} 14 public void setYear(int year) { this.year = year;} 15 public Channel getChannel() { return channel;} 16 public void setChannel(Channel channel) {this.channel = channel;} 17 public static void main(String[] args) throws Exception { 18 try(ObjectOutputStream out = new ObjectOutputStream( 19 new BufferedOutputStream(new FileOutputStream("tv.dat")))){ 20 TV tv = new TV(); 21 tv.setBrand("jee"); 22 tv.setYear(2009); 23 tv.setChannel(new Channel(5)); 24 out.writeObject(tv);} 25 try(ObjectInputStream in = new ObjectInputStream( 26 new BufferedInputStream(new FileInputStream("tv.dat")))){ 27 TV tv = (TV)in.readObject(); 28 System.out.println(tv.getBrand()); 29 System.out.println(tv.getYear()); 30 System.out.println(tv.getChannel().number); 31 }}}
A. jee
B. null
C. 2009
D. 2008
E. 5
F. 3
G. The code does not compile
H. The code compiles but throws Exception at runtime
Answer: G
Explain: The code is long, but it is a trick question, Channel didn't provide default constructor, line 7 will generate compiler error TV.java:7: error: constructor Channel in class Channel cannot be applied to given types;. If the line 7 is changed to public TV() {brand="jeep";year=2008;channel=new Channel(6);}, the answers are B, C, E. The value of year will be 2009 because during deserialization, no class constructor or default initializations are used. The value of brand will be null because it is marked as transient. When a field is marked as transient, it is left out of the serialized file. The value of tv.getChannel().number will be 5, because besides transient instance variables, static class members will also be ignored during the serialization and deserialization process. As static class reference variable channel are shared by all instances of the class and new Channel(5) is the last value in our program.
Study: Use BufferedReader, BufferedWriter, File, FileReader, FileWriter, FileInputStream, FileOutputStream, ObjectOutputStream, ObjectInputStream, and PrintWriter in the java.iopackage
===end===
===start===
Question 3. What is the output of the following program?
1 class Tool<T> {
2 public String name;
3 public Tool(T t) {this.name = t.toString() + "Solver";}
4 }
5 public class ToolBox<U> {
6 public static <T> Tool<T> solve(T t) {
7 return new Tool<T>(t);
8 }
9 public static <T> T desc(T t) {
10 return t;
11 }
12
13 public static <T> void idle(T t){}
14
15 public static void main(String...args) {
16 System.out.print(ToolBox.desc("hammer"));
17 System.out.print(ToolBox.solve("hammer").name);
18 System.out.print(ToolBox.<Integer>solve(10).name);
19 ToolBox.<String>idle("xxx");
20 }
21 }
A. compile error at line 3
B. compile error at line 6
C. compile error at line 9
D. compile error at line 13
E. compile error at other lines
F. code compile but throw exception at runtime
G. hammerhammerSolver10Solver
H. nullSolver0Solver
Answer: G
Explain: The class have 3 static generic functions, since the formal type parameter <T> are declared immediately before the return type, they are valid syntax. When calling them, the parameter type can be optionally supplied immediately before the method name. The compiler can figure out the type from the input parameters. Line 18 supplied an int 10, which is auto boxing into Integer.
Study: Create and use a generic class
===end===
===start===
Question 4. Which lines of the following problem won't compile?
1 import java.util.*;
2 public class test<T> {
3 public static <U> void grabs(T t) {}
4 public static <T, U>void grab(U t) {}
5 public static <T>T self(T t) {return t;}
6 public static <T, U> T process(T t, List<U> s) {return t;}
7 public static <T> T processes(T t, T s) {return t;}
8 public void takes(T t) {}
9 public T check(T t) {return T;}
10 public <U>U checks(U u) {return u;}
11 }
A. line 3
B. line 4
C. line 5
D. line 6
E. line 7
F. line 8
G. line 9
H. line 10
I. code compiles without issue
Answer: A, G
Explain: Unless a method is obtaining the generic formal type parameter from the class/interface, it is specified immediately before the return type of the method. line 3 is incorrect because T has no formal type parameter declaration. it declared U as type variable, didn't declare T as type variable, even though T is declared in class level, it can not be used by static method, the compiler error is: non-static type variable T cannot be referenced from a static context. By putting <U, T> before return type void, the static class then declare T as a parameter type for this static method. Line 8 and line 9 is correct because T is declared in the enclosing generic class. Line 10 is correct because even for non-static method, you can declare U as type parameter, despite the fact that it isn't declared in the enclosing class.
Study: Create and use a generic class
===end===
===start===