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?()
第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?()
第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
第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+"不是闰年"); } }
第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.
第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());
}
}
第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){
}
}
}
}