考试时间:60分钟
public class Static {
static int a = 5;
static String s = “”;
public Static() {
calculate();
}
public static void main(String args[]){
new Static();
new Static();
new Static().show();
}
public void calculate() {
a = a + 1;
s = s + 1;
}
public void show() {
System.out.println(“a = ” + a); //输出结果:____①_______________
System.out.println(“s = ” + s); //输出结果:_____②______________
}
static {
a = a + 10;
s = s + 1;
}
{
a = a + 5;
s = s +1;
}
}
菲波那契数列的定义如下:
f(n) = f (n-1) +f(n-2) ,其中 f(1) = f(0) = 1, n >= 2。用递归算法计算出第n个元素的值。
public class ThreadDemo {
Public static void main(String args[]) throws InterruptedException {
MyQueue queue = new MyQueue();
Producer producer = new Producer(queue);
Consumer consumer = new Consumer(queue);
Thread.sleep(10000);
producer.setExitFlag():
consumer.setExitFlag();
___①________________
___②_________________
}
}
____③______class CommonThread implements Runnable {
MyQueue queue;
boolean exitFlag = false;
Thread thread;
CommonThread (MyQueue queue) {
this.queue = queue;
thread = new Thread(this);
__④__________;
}
public void setExitFlag() {
synchronized (this){
exitFlag = true;
}
}
public boolean testExitFlag() {
synchronized(this) {
return exitFlag;
}
}
class Consumer extends CommonThread {
Consumer(MyQueue queue) {
_⑤______________;
}
public void run() {
while (true) {
queue.get();
if (testExitFlag()) {
break;
}
}
}
}
class Producer extends CommonThread {
Producer (MyQueue queue) {
____⑥____________;
}
public void run() {
long count = 0;
while (true) {
queue.put(new String(“obj” + count));
if (testExitFlag()) {
break;
}
}
}
}
public class MyQueue {
final static int MAX_SIZE = 1024;
private ArrayList list;
public MyQueue() {
_______⑦________________
}
synchronized public void put(Object obj) {
try {
while (list.size() == MAX_SIZE) {
_____⑧_____________;
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
list.add(obj);
notifyAll();
}
synchronized public Object get() {
try {
___⑨_____________________ {
wait();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
Object obj = list.remove(0);
___⑩__________________
return obj;
}
}
程序说明:类DynArray实现了一个动态数组。当数组的空间不够时动态分配空间。
public class DynArray {
private Object[] array;
private int index;
public DynArray() {
array = new Object[16];
index = -1;
}
// 向动态数组中添加一个对象obj。如果空间不够,重新分配空间。
public void add(Object obj) {
if (index == array.length -1) {
_⑴_
_⑵_
_⑶_
}
array[++index] = obj;
}
//从数组中删除一个指定对象。
public void remove(Object obj) {
if (index < 0) {
return;
}
for (int i = 0; i <= index; i++) {
if (array[i].equals(obj)) {
for (int j = i; j < index; j++) {
__⑷__
}
break;
}
}
index–;
}
//返回指定索引处的对象。
public Object getAt(int index) {
return array[index];
}
//返回当前数组的元素个数。
public int size() {
return index + 1;
}
}
//接口MySet定义一个集合操作
import java.util.Iterator;
public interface MySet {
public boolean put(Object obj);
public boolean remove(Object obj);
public Iterator iter();
public boolean contains(Object obj);
}
//类MyHashSet采用哈希表来存储集合元素
public class MyHashSet implements MySet {
private DynArray[] container;
private int count;
public MyHashSet() {
count = 0;
container = new DynArray[16];
}
//向集合中添加一个元素,但不允许重复元素,否则返回false。成功则返回true;
public boolean put(Object obj) {
if (contains(obj)) {
return false;
}
int hashCode = Math.abs(obj.hashCode());
int pos = _____⑸______;
if (container[pos] == null) {
______⑹_____;
}
______⑺____;
count++;
return true;
}
//从集合中删除一个元素,当集合中没有该元素时返回false。成功删除返回true;
public boolean remove(Object obj) {
if (!contains(obj)) {
return false;
}
int hashCode = Math.abs(obj.hashCode());
int pos = hashCode % container.length;
_____⑻____;
count–;
return true;
}
// 返回一个集合的迭代器
public Iterator iter() {
____⑼_____
}
// 判断集合中是否包括指定的元素。
public boolean contains(Object obj) {
Iterator iter = new MyIterator();
while (iter.hasNext()) {
Object theObj = iter.next();
if (theObj.equals(obj)) {
return true;
}
}
return false;
}
//一个实现标准Iterator接口的内部类。
class MyIterator implements Iterator {
private int containerIndex = 0;
private int iterCount = 0;
private int arrayIndex = 0;
public void remove() {
}
public boolean hasNext() {
if (___⑽______) {
return true;
}
else {
return false;
}
}
public Object next() {
Object obj = null;
while (containerIndex < container.length) {
if (container[containerIndex] == null) {
containerIndex++;
continue;
}
if (arrayIndex < container[containerIndex].size()) {
obj = ______⑾_________
arrayIndex++;
iterCount++;
break;
}
else {
containerIndex++;
arrayIndex = 0;
___⑿______
}
}
return obj;
}
}
}
说明:
设计一个学生选课系统,每个学生可以选修多门课程,多个学生可以同时选修同一门课程。学生信息为学号,姓名,课程信息为课程编号,课程名称。
用SQL语句查询出选修3门或3门以上课程,并且其中选修了java课程的学生。(8分)
参考答案:
1-12正确,13-20错误。
① 33
② 1111111
public class Fibo {
public int f(int n) {
if (n == 0 || n == 1) {
return 1;
}
return f(n-1) + f(n-2);
}
}
①producer.thread.join();
②consumer.thread.join();
③abstract
④thread.start();
⑤_super(queue)
⑥super(queue)
⑦list = new ArrayList(MAX_SIZE)
⑧wait()
⑨while(list.size==0)
⑩_notifyAll();
public abstract class Vehicle {
private Engine engine;
public Vehicle() {
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public abstract String go();
public void startEngine() {
if (this.engine != null) {
this.engine.start();
}
}
public void stopEngine() {
if (this.engine != null) {
this.engine.stop();
}
}
public boolean isEngineOn() {
if (this.engine != null) {
return this.engine.isOn();
}
return false;
}
}
public class Car extends Vehicle{
public Car() {
}
public String go() {
if (this.isEngineOn()) {
return “!!!!”;
}
else {
return “…”;
}
}
}
public interface CargoTransport {
public void loadCargo();
}
public class Truck extends Vehicle implements CargoTransport {
public Truck() {
}
public String go() {
if (this.isEngineOn()) {
return “!!!”;
}
else {
return “…”;
}
}
public void loadCargo() {
System.out.println(“truck can load cargo.”);
}
}
public class Engine {
private boolean on;
public Engine() {
}
public void start() {
this.on = true;
}
public void stop() {
this.on = false;
}
public boolean isOn() {
return this.on;
}
}
public class Person {
private Vector vehicles = new Vector();
public Person() {
}
public void addVehicle(Vehicle v) {
vehicles.addElement(v);
}
}
create table student (id number not null, name nvarchar2(10), constraint pk_student primary key (id));
create table course(id number not null, name nvarchar2(10), constraint pk_course primary key (id));
create table enrollment(studentid number not null, courseid number not nul, constraint pk_enrollment primary key(studentid,courseid),constraint fk_student foreign key (studentid) references student(id),constraint fk_course foreign key (courseid) references course(id));
select course.name,count(course.name) from enrollment,course
where enrollment.courseid = course.id group by course.name
select * from student where student.id in
(
select enroll.studentid from student,enroll
where student.id = enroll.studentid group by enroll.studentid,student.name
having count(enroll.studentid) >= 3
)
and student.id in
(
select enroll.studentid from enroll,course where enroll.courseid = course.id and course.name = ‘java’
)