一.不定先择题(每小题4分,少选得2分,错选不得分,共计64分)
1.下面的哪些程序片段可能导致错误(B,D)
A.
String s = “Gone with the wind”;
String t = “good”;
String k = s + t;
B.
String s = “Gone with the wind”;
String t;
t = s[3] + “one”;
C.
String s = “Gone with the wind”;
String standard = “c”;
D.
String s = “home directory”;
String t = “s-“directory”;
2.在//point x 处的哪些申明是句法上合法的?(D,E)
Class Person
{
Private int a;
Public int change(int m) {return m;}
}
Public ciass Teacher extends Person
{
Public int b;
Public static void main(String arg[])
{
Person p = new Person();
Teacher t = new Teacher();
Int i;
//point x
}
}
A.i = m; B. i =b; C. i= p.a; D. = p.change(30); E. i = t.b;
3.
1) class Person {
2) public void print Value(int i ,int j ) {/*…*/}
3) public void print Value(int i) {/*…*/}
4) }
5) public class Teacher extends Persan {
6) public void print Value() {/*…*/}
7) public void print Value(int i) {/*…*/}
8) public static void main (String args[]){
9) person t = new Teacher();
10) t.print Value(10);
11) }
12) }
第十行的声明将调用声明方法?(C)
A. 第 2 行 B. 第 3 行 C. 第 6 行 D. 第 7 行
4.给出下面的不完整的类代码:
Class Person
{
Sting name , department;
int age;
public Person(String n) { name = n; }
public Person(String n,int a) { name = n; age = a; }
public Person(String n,String d, int a)
{
//doing the same as two arguments version of constructor
// including assignment name = n,age=a
Department = d;
}
}
下面的哪些表达式可以加到构造方法中的” doing the same as…”处?(C)
A.Person(n,a); B. this(Person(n,a));
C.this(n,a); D.this (name,age).
5.对于Java数组的理解下面说法正确的是:(A)
A.数组是一个从Object 中继承的对象 B.数组是一种基本数据类型
C.数组的长度可以动态改变 D.数组的元素可以是对象
6.
Public class Parent
{
Public int addValue( int a,int b)
{
int s;
s = a + b
return s;
}
class Child extends Parent {
}
哪些方法可以加入类Child中?(B,C)
A. int addValue (int a, int b) { //do something…}
B. public void add Value () { //do something…}
C. public int addValue ( int a) { //do something…}
D. public int addValue ( int a, int b) throws MyException { //do something…}
7. 一个类中定义的成员变量只能被同一包中的类访问。下面的哪些修饰符可以获得需要的访问控制?(B,C,D)
A.private B.无修饰符 C. public D. protected
8.
Public class English
{
Public English ( )
{
system. out. printIn (“create an English object.”)
}
}
public class Person
{
private int age = 100;
public Person ( ) { printAge(); }
public void printAge ( ) { System. out. printIn (“person age is” + this.age); }
}
public class Teacher extends Person
{
private int age = 200;
private static English English = new English ( );
public void printAge ( ) { System. out. printIn (“teacher age is” + age); }
public static void main (String args[ ]) { Person t = new Teacher( ); }
}
下面哪个输出正确 (A)
A、r
create an English object.
teacher age is 0
B、create
create an English object.
teacher age is 100
C、
create an English object.
teacher age is 200
D、
teacher age is 100
create an English object.
E、
teacher age is 200
create an English object.
9.
String s = “hello”
String t = “hello”
char c [] = { ‘h’,’e’,’l,’l’,’o’};
哪些返回 true?(A,B,C,D)
-
s.equals(t); b. t.equals(c); C. s==t;
D. t.equals(new String(“hello”)); E. t==c.
10. 下面的哪些声明是合法的?(A,B,D)
A. long 1 = 4990;
B. int I =4L;
C. float f = 1.1;
D. double d = 34.4;
E. double t = 0.9F.
11.
class Parent
{
String one, two;
Public Parent (String a, String b)
{
one = a;
two = b;
}
public void print () { System.out.printIn(one); }
}
public class Child extends Parent
{
public Child(String a, String b) { super(a,b); }
public int print(){ System.out.printIn(one + “to”+two);return 1;}
public static void main(String arg[])
{
Parent p = new Parent(“south”,”north”);
Parent t = new Child(“east”,”west”);
p.print();
t.print();
}
}
下面的哪个输出正确?(A)
-
在编译时出错
B.
south
east
C.
south to north
east to west
D.
south to north
east
E.
south
east to west
12.给出下面的代码:
1) class Parent {
2) private String name;
3) public String getValue(){return name;}
5) }
6) public class Child extends Parent {
7) private String department;
8) public Child() {}
9) public String getValue(String name){ return name + department;}
10) public void main(String arg[]) {
11) Parent p = new Child();
12) p.getValue(“Tom”);
13) }
14) }
哪些行将导致错误?(A,E)
-
line 6 B. line 9 C. line 10 D. line 11 E. line 12
13. 变量“result”是一个boolean型的值,下面的哪些表达式是合法的?(A,B)
A. result = true;
B. if (result) {//do something…}
C. if (result!= 0) {//so something…}
D. result = 1
14. 类Teacher和Student都是类Person的子类(C)
Person p;
Teacher t;
Student s;
p,t和s都是非空值.
If(t instance of Person) { s = (Student)t; }
这个语名导致的结果是什么?
-
将构造一个Student对象。
-
表达式合法。
-
编译时非法。
-
编译时合法而在运行时可能非法。
15. 下面这段代码的输出结果是什么?(A)
class ValHold{
public int I = 10
}
public class ObParm{
public void amethod(){
ValHold v = new ValHold();
Another(v);
System.out.printIn(v.i);
}
public void another(ValHold v){
v.i = 20;
valHold vh = new ValHold();
v = vh;
System.out.printIn(v.i);
}
public static void main(String[] argv){
ObParm o = new ObParm();
o.amethod();
}
}
A.
10
20
B.
10
10
C.
20
10
D.
20
20
16. 给出下面的类:
1) public class Stack implements Cloneable{
2) private Object[] elements;
3) private int size = 0
4) public Stack(int initialCapacity) {
5) this.elements = new Object[initialCapacity];
6) }
7) public void push(Object e) {
8) ensureCapacity();
9) elements[size++] = e;
10) }
11) public Object pop() {
12) if (size =0) {
13) throw new EmptyStackException();
14) }
15) return elements[–size];
16) }
17) private void ensureCapacity(){
18) if(elements.length = size){
19) Object[] oldElements = elements;
20) elements = new Object[2*elements.length + 1];
21) System.arraycopy(oldElements,0,elements,0,size);
22) }
23) }
24) }
哪些语句会引起内存泄漏?(D)
A.第5行 B. 第9行 C. 第15行 D. 第21行
二、下面这段代码是一段简单的socket服务器程序,服务器每监听到一个连接请求就新建一个线程跟客户端进行通信,通信过程是服务器将接收到的内容再返回给客户端,直到接收”END”消息为止。请在空格处填写完整的代码(每空2分,共16分)
import java. Io. *;
import java. net. *;
class ServeOneJabber extends Thread
{
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public ServeOneJabber(Socket s) throws IOException
{
In = new BufferedReader(
New InputStreamReader ( socket.getInputStream()));
// Enable auto-flush;
Out = new PrintWriter( new BufferedWriter(new OutputStreamWriter
socket.getOutputStream())). True);
start(); //Calls run()
}
public void run() {
try {
while (true) {
String str = in.readLine();
If (str.equals(“END”)) break;
Out.printIn(str);
}
} catch (IOException e) {
} finally {
Try {
if (null != in) {
in.close();
}
if (null != out) {
out.flush();
out.close();
}
if (null != socket) {
socket.close();
}
}catch(IOException e) {}
}
}
}
public class MultiJabberServer
{
static final int PORT = 8080;
public static void main(String[] args)throws IOException
{
ServerSocket s= new ServerSocket(PORT);
try
{
while (true)
{
Socket socket = s.accept();
try {
new ServerOneJabber(socket)
} catch(IOException e) {
socket.close();
} finally {
s.close();
}
}
}
三、案例操作题
对于银行中的帐户管理:一个用户可以开设多个帐户,对于每个帐户可以有存款(deposit)和取款(withdraw)的操作。对帐户的每个操作都必须记录下来。每个这些操作由出纳(Teller)来完成。相应的实体的属性用java类型表示如下:
—Customer:
customerid: int //标识
name: String
address: String
—-Account:
accountid: int //标识
customer: Customer
balance: BigDecimal
—-TransRecord:
account: Account
amt: BigDecimal
transType: String{“deposit” | “withdraw”}
transid: java.sql.TimeStamp //标识
任务:
-
写出建表的sql脚本
ORACLE 建表语句:
— customer 客户表 —
CREATE
TABLE customer(
customer_id NUMBER(11)
CONSTRAINT customer_customer_id_pk PRIMARY
KEY,
name
VARCHAR(10)
CONSTRAINT customer_name_nn NOT
NULL,
address VARCHAR(20)
CONSTRAINT customer_name_nn NOT
NULL
)
— Account 账户表 —
CREATE
TABLE
account(
account_id NUMBER(11)
CONSTRAINT account_account_id_pk PRIMARY
KEY,
customer_id VARCHAR(10)
NOT
NULL
CONSTRAINT fk_account_customer_id FOREIGN
KEY
(customer_id)
REFERENCES customer (customer_id),
balance NUMBER(20)
)
— TransRecord 记录表 —
CREATE
TABLE trans_record(
account_id NUMBER(11)
NOT
NULL,,
amt NUMBER(20)
NOT
NULL,
trans_type VARCHAR(10)
NOT
NULL
CONSTRAINT ck_trans_record_trans_type CHECK(trans_type in
(‘deposit’,‘withdraw’)),
trans_id TIMESTAMP(4)
NOT
NULL
CONSTRAINT trans_record_trans_id PRIMARY
KEY,
)
b. 编写三个CMP Entity Bean: CustomerEJB, AccountEJB和TransRecordEJB和一个Session Bean: TellerEJB来实现以下的功能:
createCustomer
createAccount
deposit
withdraw