1.importjava.io.*;2.publicclassFooimplementsSerializable{3.publicintx,y;4.publicFoo(intx,inty){this.x=x;this.y=y;}5.6.privatevoidwriteObject(ObjectOutputStreams)7.throwsIOException{8.s.writeInt(x);s.writeInt(y)9.}10.11.privatevoidreadObject(ObjectInputStr

题目

1.importjava.io.*;

2.publicclassFooimplementsSerializable{

3.publicintx,y;

4.publicFoo(intx,inty){this.x=x;this.y=y;}

5.

6.privatevoidwriteObject(ObjectOutputStreams)

7.throwsIOException{

8.s.writeInt(x);s.writeInt(y)

9.}

10.

11.privatevoidreadObject(ObjectInputStreams)

12.throwsIOException,ClassNotFoundException{

13.

14.//insertcodehere

15.

16.}

17.}

Whichcode,insertedatline14,willallowthisclasstocorrectlyserializeand deserialize?()


相似考题
参考答案和解析
参考答案:D
更多“importjava.io.*;2.publicclassFooimplemen ”相关问题
  • 第1题:

    AssumingthattheserializeBanana2()andthedeserializeBanana2()methodswillcorrectlyuseJavaserializationandgiven:

    importjava.io.*;

    classFood{Food(){System.out.print(”1”);}}

    classFruitextendsFoodimplementsSerializable{

    Fruit(){System.out.print(”2”);}}

    publicclassBanana2extendsFruit{intsize=42;

    publicstaticvoidmain(String[]args){

    Banana2b=newBanana2();

    b.serializeBanana2(b);//assumecorrectserialization

    b=b.deserializeBanana2(b);//assumecorrect

    System.out.println(”restored+b.size+);}

    //moreBanana2methods

    }

    Whatistheresult?()


    参考答案:D

  • 第2题:

    阅读下面程序 import java.io.*; public class TypeTransition{ public static void main(String args[]){ char a='h'; int i=100; int j=97; int aa=a+i; System.out.println("aa="+aA) ; char bb=(char)j; System.out.println("bb="+bB) ; } } 如果输出结果的第二行为bb=a,那么第一行的输出是

    A.aa=I

    B.aa=204

    C.aa=v

    D.aa=156


    正确答案:B
    解析:本题考查的是Java的基本数据类型及其运算。程序开始生成了一个字符型变量a和3个整型变量i、j、aa。而整型变量aa的初始值是a+i,其中a是一个字符型变量。如何进行加法运算呢?Java语言规定,char型数据可以自动转换成int类型,转换的结果就是该字符的ASCII码值。因此,整型变量aa的初始值为字符“h”的 ASCH码值加上100。如果记住h的ASCII码值是104,则直接就确定aa的初始值是 204,选项B为正确答案。
      如果记不得h的ASCII码,题目中则给出提示。题目中说“输出结果的第二行为bb=a”,也就是字符比的值为字符a,bb的生成语句是char bb;(char)j,是把整型变量j的值强制转换为字符型。同样,把ASCH码值为.j(97)所对应的字符赋值给比。显然,字符a的ASCII码值为97,字符b的ASCII码值为98,依次类推,字符h的ASCII码为104。
      因此,本题的正确答案是B。

  • 第3题:

    本题提示输入年份,然后判断该年份是否为闰年。 importjava.io.*; public class javal{ public static void main(String[]args){ InputStreamReader ir; BufferedReader in; ir=new InputStreamReader(System.in); in=new BufferedReader(ir); int year=1900; System.out.print("请输入年份:"); try{ String s=in.readLine; ; } (Exception e){ } if( ) System.OUt.println(year+"是闰年"); else System.out.println(year+"不是闰年"); } }


    正确答案:
    第1处:year=Integer.parseInt(s)
    第2处:catch
    第3处:year%4= =0year%100 !=0 ||year%400= =0
    【解析】第1处是将String型的s转换成整型;第2处是捕获异常的catch子句,用来处理由try所抛出的异常事件;第3处是判断是否为闰年的条件,即能被4整除且不能被100整除的或能被400整除的就是闰年。

  • 第4题:

    importjava.io.*;publicclassForestimplementsSerializable{privateTreetree=newTree();publicstaticvoidmain(String[]args){Forestf=newForest();try{FileOutputStreamfs=newFileOutputStream(”Forest.ser”);ObjectOutputStreamos=newObjectOutputStream(fs);os.writeObject(f);os.close();}catch(Exceptionex){ex.printStackTrace();}}}classTree{}Whatistheresult?()

    A.Compilationfails.

    B.Anexceptionisthrownatruntime.

    C.AninstanceofForestisserialized.

    D.AinstanceofForestandaninstanceofTreearebothserialized.


    参考答案:B

  • 第5题:

    从对象流中读取对象,请在下面横线处填写代码完成此程序。

    import java.util. *;

    import java. io. *;

    public class UnSerializeDate

    {

    date d = null;

    UnSerializeDate ( )

    {

    try

    {

    FileInputStream f1 = new FileInputStream("date.ser");

    d = (Date) s. readobject ();

    f1,close();

    }

    catch (Exceptin e)

    {

    e. printStackTrace ();

    }

    }

    public static main(String args [] )

    {

    System.out.println("The date read is: "+ a.d.toString());

    }

    }


    正确答案:ObjeetInputStream s = new ObjectInputStream(f1 ); UnSerilizaDate a = new UnSerializeDate();
    ObjeetInputStream s = new ObjectInputStream(f1 ); UnSerilizaDate a = new UnSerializeDate();

  • 第6题:

    本题读取用户输入的字符流,直到用户输入字符串quit后结束。

    importjava.io.*;

    public class javal{

    public static void main(String[]args){

    BufferedReader in;

    ir=new InputStreamReader(System.in);

    in=new BufferedReader(ir);

    System.OUt.println("please input:");

    while( ){

    try{

    String s=in.readLine();

    System.out.println("echo:"+s);

    if(S.equals("quit"))

    )catch(Exception e){

    }

    }

    }

    }


    正确答案:
    第1处:InputStreamReaderir第2处:true第3处:break【解析】第1处构造一个InputStreamReader对象,把从控制台输入的字节作为参数,构建一个读取数据用的InDutStreamReader流,读取字节将其解码为字符;第2处while条件为真,执行循环;第3处当输入的s中的内容为quit时,跳出循环。