当前位置:文档之家› java试卷(2)

java试卷(2)

教研室主任(签字)学院院长(系主任)(签字)华东理工大学试卷B)abstract类中只能有abstract方法。

C)abstract类中可以有非abstract方法,但该方法不可以用final修饰。

D)不可以同时用final和abstract修饰一个方法。

答:3.不同对象的实例变量分配的内存空间地址一定不同吗?答:4.类的static方法可以用类名调用吗?答:5.abstract类中可以有非abstract方法吗?答:6.源文件中声明编写的类一定在同一包中吗答:7.子类在什么情况下可以继承父类的友好成员?答:8.一个线程执行完run方法后,进入了什么状态?答:9.一个处于新建状态的线程调用isAlive()方法返回的结果是什么?答:10.ServerSocket对象调用什么方法来建立服务器端的Socket对象?答:装订线二、阅读理解题(请在指定位置写出答案,否则无效。

本大题共6小题,每小题10分,总计60分)1.请给出E 类中标记的【结果1】、【结果2】。

class B { int n;static int sum=0; void setN(int n) { this.n=n; }int getSum(){ for(int i=1;i<=n;i++) sum=sum+i; return sum; }}public class E{ public static void main(String args[]) { B b1=new B(),b2=new B(); b1.setN(3); b2.setN(5);int s1=b1.getSum(); int s2=b2.getSum();System.out.println(s1); //【结果1】 System.out.println(s2);//【结果2】 } }2.请给出E 类中标记的【结果1】、【结果2】。

class A{ double f(double x,double y) { return x+y; } }class B extends A { double f(int x,int y) { return x*y; } }public class E{ public static void main(String args[]) { B b=new B();System.out.println(b.f(5,8)); //【结果1】 System.out.println(b.f(8.0,12.0));// 【结果2】 } }考生注意: 考试时间100分钟 试卷总分 100 分 共 4 页 第 1 页3.请给出E 类中标记的【结果】。

import java.util.*; class GetToken { String s[];public String getToken(int index,String str){ StringTokenizer fenxi=new StringTokenizer(str); int number=fenxi.countTokens(); s=new String[number+1]; int k=1;while(fenxi.hasMoreTokens()) { String temp=fenxi.nextToken(); s[k]=temp;k++; }if(index<=number) return s[index]; elsereturn null; } } class E{ public static void main(String args[]){ String str="public static void main"; GetToken token=new GetToken(); String s1=token.getToken(2,str), s2=token.getToken(4,str);System.out.println(s1+":"+s2); //【结果】 } }4.请给出E 类中标记的【结果1】、【结果2】。

class AAA{ int add(int x,int y){ return x+y;} }class Student2004 extends AAA { int add(int x,int y) { return x-y; } }public class E{ public static void main(String args[]) { AAA a=new AAA();System.out.println(a.add(55,33)); //【结果1】 a=new Student2004();System.out.println(a.add(55,33)); //【结果2】 } }装订线5.请给出E类中标记的【结果1】、【结果2】。

import java.awt.*;public class E implements Runnable{ StringBuffer buffer=new StringBuffer();Thread t1,t2,t3;E(){ t1=new Thread(this);t2=new Thread(this);t3=new Thread(this);}public synchronized void addString(String c){ if(Thread.currentThread()==t1){ while(buffer.length()==0)try{ wait();}catch(Exception e){}buffer.append(c);}if(Thread.currentThread()==t2){ while(buffer.length()<15)try{ wait();}catch(Exception e){}buffer.append(c);}if(Thread.currentThread()==t3){ buffer.append(c);}notifyAll();}public void run(){if(Thread.currentThread()==t1){ addString("今天是一月十五号,") ;}if(Thread.currentThread()==t2){ addString("天气不错,") ;}if(Thread.currentThread()==t3){ addString("我们考试的科目是Java,") ;}}public static void main(String s[]){ E hello=new E();System.out.println(hello.t1.isAlive()+","+hello.t2.isAlive()); //【结果1】hello.t2.start();hello.t1.start();hello.t3.start();while(hello.t1.isAlive()||hello.t2.isAlive()||hello.t3.isAlive()){ }System.out.println(hello.buffer); //【结果2】}}共4 页第 2 页6.请说出E 类中System.out.println 的输出结果。

//本题不会考的 超过了老师给的提纲了import java.io.*; public class E {public static void main(String args[]) { try{FileOutputStream out=new FileOutputStream("hello.txt"); FileInputStream in=new FileInputStream("hello.txt"); byte content[]="ABCDEFG".getBytes();StringBuffer bufferOne=new StringBuffer(),bufferTwo=new StringBuffer();//本题不会考的 超过了老师给的提纲了int m=-1;byte tom[]=new byte[3]; out.write(content); out.close();while((m=in.read(tom,0,3))!=-1) {String s1=new String (tom,0,m);bufferOne.append(s1);String s2=new String (tom,0,3); bufferTwo.append(s2); }in.close();System.out.println(bufferOne); //【结果1】 System.out.println(bufferTwo); //【结果2】 }catch(IOException e){} } }//本题不会考的 超过了老师给的提纲了装订线四、模板编程题(请按模板要求,在指定位置写出【代码】答案,否则无效。

本大题共2小题,每小题10分,总计20分))【代码1】:text=new TextField(12);【代码2】:label=new Label(“I love this game”);【代码3】:setLayout(new FlowLayout());【代码4】:text.addActionListener(this);【代码5】:label.setText(text.getText());import java.awt.*;import java.awt.event.*;class IenFrame extends Frame implements ActionListener{ TextField text;Label label;IenFrame(){【代码1】 //创建TextField对象:text,要求text的可见字符数为12个机器字符。

【代码2】 //创建 Label对象:label,要求label上的名字是"I love this game"。

【代码3】 //设置窗口的布局为FlowLayout型布局。

【代码4】 //将当前窗口作为text的ActionEvent事件的监视器。

add(label);add(text);}public void actionPerformed(ActionEvent e){【代码5】 //label调用方法将自己名字设置为text中的文本}}2.【代码1】:str.indexOf("1236");【代码2】:str.length();【代码3】:str.charAt(3);【代码4】:str.substring(5);【代码5】:Integer.parseInt(temp);class IenString{ public static void main(String args[]){ int index=-1,length=0,n=0;String str="我非常喜欢1236" ;index=【代码1】 //str调用方法返回"1236"在str出现的位置length=【代码2】 //str调用方法返回str中字符的个数char c=【代码3】 //str调用方法返回str中的字符:'喜'String temp=【代码4】 //str调用方法返回字符串"1236"n=【代码5】 //将temp转化为int型数据。

相关主题