Menu
Question Index
...

What is the output of the following program?

import java.io.*;
class Gen<T> implements Serializable {
    T ob;
    public Gen(T ob) { this.ob = ob;  }
    T getOb() { return ob; }
}
class Gen2<T, U> extends Gen<T> {
    U os;
    public Gen2(T ob, U os) {
        super(ob); // LINE A
        this.os = os;
    }
    T getOb() { return ob; }
    U getOs() { return os; }
    public static U myMethod() { return os; }
}
public class Complex {
    public static void main(String[] args) {
        Gen<Integer> gen = new Gen<Integer>(50);
        Gen2<String, Integer> gen2 = new Gen2<String, Integer>("Merit Campus", 155);
        try {
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("file.txt"));
            os.writeObject(gen);
            os.writeObject(gen2);
        } catch (Exception e) { System.out.println(e);  }
        try {
            ObjectInputStream os = new ObjectInputStream(new FileInputStream("file.txt"));
            gen = (Gen<Integer>) os.readObject();
            gen2 = (Gen2<String, Integer>) os.readObject();
        } catch (Exception e) { System.out.println(e); }
        System.out.print(gen.getOb() + " " + gen2.getOb() + " " + gen2.getOs() + " " +   Gen2.myMethod());
    }
}


50 Merit Campus 155 Merit Campus
50 Merit Campus 155
Compilation Error at LINE A since Gen is Integer type but we are passing String type
Compilation Error because of some other reason
NotSerializableException

Doubts

Problems

Topic: Java ObjectInputStream

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App