注意:
考试时间:120分钟
请将答案写在答题纸上,写在试卷上不作批改,视无效处理。本试卷共计10题。
public class A {
①public static void method(String str){};
②protected int method(int i){return 0;};
③private void method(String str, int i) {};
④public int method(){return 0;};
⑤void method(A a){};
}
class B extends A {
⑥public static void method(String str) {};
⑦public int method(int i) {return 0;};
⑧public void method(String str,int i) {};
⑨int method() {return 0;};
⑩public method(A a) {};
}
public class ObjectArray {
public static void main(String args[]) {
Person personList[] = new Person[4];
for (int i = 0; i < personList.length; i++) {
_ ①__
personList[i].setId(“id ” + i);
personList[i].setName(“name ” + i);
__②__
for (int j=0; j < 2;j++){
Person kid = new Person();
kid.setId(“kid id” + j);
kid.setName(“kid name” + j);
__③__
personList[i].getKids().add(kid);
}
_④___
personList[i].getMother().setId(“mother id”);
personList[i].getMother().setName(“mother name”);
_⑤___
personList[i].getMother().getKids().add(__⑥___);
}
}
}
class Person {
private String id;
private String name;
HashSet kids;//子女
Person mother;//母亲
// setter 和 getter 方法省略。
}
public class A {
protected static int i = 10;
protected static String s = “”;
private static A a;
//写出A的构造函数
__①_
public static void main(String[] args) {
A a = null;
System.out.println(a.i); //输出结果:__②__
a = A.getInstance();
System.out.println(a.s); //输出结果:__③__
}
// 写出方法getInstance()的定义。
__④__
static {
i += 10;
s += 10;
a = new A();
}
}
package j2setest.access;
import j2setest.access.access1.*;
public class A extends B{
public byte i = 1;
public A() {
}
public void test() {System.out.println(“In A”);}
public static void main(String args[]) {
A a = new A(); // a包含哪些实例变量: __①__
// a能访问哪些实例变量: __②__
C c = new C(); // c包含哪些实例变量:____③__
// c能访问哪些实例变量: __④__
B b = new A();
A a1 = (A)b; // 此句强制转化是否正确?__⑤__
C c2 = (C)a; // 此句强制转化是否正确?___⑥__
b.test(); // 打印结果:______________⑦__
System.out.println(b.i); //打印结果:________⑧__
}
}
class B {
public int i = 5;
private String s;
protected double d;
B b;
public void test(){System.out.println(“In B”);}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
package j2setest.access.access1;
import j2setest.access.*;
public class C extends A{
public C() {
}
public void test() {System.out.println(“In C”);};
}
public class MyList {
private Element head=null;
private Element tail = null;
private int length=0;
private static final int MAX_LENGTH = 1024;
class Element {
Object data;
__①__;
Element(Object data) {
this.data = data;
}
}
synchronized public void addElement(Object d)
{
Element e = new Element(d);
if (length == 0) {
__②___;
__③___;
}
else {
__④___ {
wait();
}
e.next = null;
tail.next = e;
tail = e;
}
length++;
_⑤_
}
synchronized public Object removeElement() {
Object obj;
while (length == 0 ) {
__⑥___;
}
else if (length == 1) {
obj = head.data;
head = tail = null;
}
else {
___⑦__;
____⑧___;
}
length–;
notifyAll();
return obj;
}
synchronized public Object get(int index) {
Element ptr = head;
int sequence = 0;
_⑨____ {
wait();
}
while (ptr != null) {
if (sequence == index) {
__⑩_;
return ptr.data;
}
ptr = ptr.next;
sequence++;
}
notfiyAll();
return null;
}
}
web.xml:
__①_______
net.umltech.train.java.test.MyServlet
__②________
____③_______
login.jsp:
________”>
query.jsp:
MyServlet.java:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
____⑥___);
if (url.equals(“/login.do”)) {
___⑦__
___⑧___
}
else if (url.equals(“/query.do”)) {
___⑨___
___⑩___
}
}
login.jsp:
struts-config.xml:
②___” type=”net.umltech.train.form.LoginForm” />
③__” path=”/loginAction” scope=”session” type=”net.umltech.train.action.LoginAction”>
User.java:
public class User{
private String userId;
private String userName;
private Set groups = new HashSet();
//getter,setter method are omitted.
}
Group.java:
public class Group{
private String groupId;
private String groupName;
private Set rights = new HashSet();
// getter,setter method are omitted.
boolean equals(Group group) {
__④__
}
}
Right.java:
public class Right{
private String rightId;
private String rightName;
}
写出数据库中和User,Group,Right类分别对应的users,groups和rights表的定义:___⑤____
LoginAction.java:
public class LoginAction extends Action {
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
try {
User user = findByUserName(actionForm.getUserName());
httpServletRequest.getSession.setAttribute(“user_right”,user);
} catch(Exception e) {}
return actionMapping.findForward(“success”);
}
public User findByUserName(String userName) throws Exception{
String mysql =
“select users.USER_ID,groups.GROUP_ID,groups.NAME,rights.RIGHT_ID,rights.NAME
from users,user_group,groups,group_right,rights where users.NAME =
and users.USER_ID = user_group.USER_ID
and user_group.GROUP_ID = groups.GROUP_ID
and groups.GROUP_ID = group_right.GROUP_ID
and group_right.RIGHT_ID = rights.RIGHT_ID”;
PreparedStatement pstmt = connection.PrepareStatement(mysql);
pstmt.setString(1,userName);
ResultSet rs = pstmt.executeQuery();
User user = new User();
while(rs.next()) {
String userId = rs.getString(1);
String groupId = rs.getString(2)
String groupName =rs.getString(3);
String rightId = rs.getString(4);
String rightName = rs.getString(5);
Group group = new Group();
group.setGroupId(groupId);
group.setGroupName(groupName);
Set groups = user.getGroups();
iterator iter = groups.iterator();
boolean found = false;
while(iter.hasNext()) {
Group theGroup = (Group)iter.next();
if (theGroup.equals(group)) {
____⑥___
found=true;
break;
}
}
if (__⑦____) {
groups.add(group);
}
Right right = new Right();
right.setRightId(rightId);
right.setRightName(rightName);
_____⑧_______
} // while(rs.next())
__⑨___
__⑩___
connection.close();
return user;
}// findByUsername
}
success.jsp:将用户的组权限的名称显示出来:
⑾___” items=”____⑿____”>
⒀____” items=”___⒁____”>
⒂____” />
程序说明:类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;
}
}
}
说明:
写出各个类、接口的定义,包括实例变量的定义,方法的定义和方法的示例性实现。
参考答案:
java应聘教师测试试题二答案
前40题正确,后20题错误。
3.
4.
5.
6.
7.
8.
9:
10.
将此题答案写在第二页答题纸的反面。
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);
}
}