中兴 Java 开发笔试试题及答案
1.类 A,B 和 C 的定义如下:
public class A {
public void f() {
System.out.println(“A.f()”);
}
}
public class B extends A {
public void f() {
System.out.println(“B.f()”);
}
}
public class C {
public void g(A a) {
System.out.println(“g(A a)”);
a.f();
}
public void g(B b) {
System.out.println(“g(B b)”);
b.f();
}
}
运行下面程序:
C c = new C();
A a = new B();
c.g(a);
输出的结果是:()。
A. g(A a)
A.f()
B. g(A a)
B.f()
C. g(B b)
A.f()
D. g(B b)
B.f()
正确答案:B
2.执行下列语句:
int a = 0x9af700; //1001 1010 1111 0111 0000 0000
a <<= 2;
变量 a 的值为:()。
A. 0x26bdc00
B. 0xc6bdc00
C. 0x3fa0000
D. 0x7e02ffff
正确答案:A
3.下列代码的输出结果是()。
int j=0;
for(int i=0;i<100;i++){
j=j++;
}
System.out.println(j);
A.0
B.99
C.100
D.101
正确答案:A
4.请看下列代码:
public static void main(String[] args) {
< 插入代码>
set.add(new Integer(2));
set.add(new Integer(1));
System.out.println(set);
}
如果想保证程序的输出结果是[1,2],那么<插入代码>处应填入的代码是()。
A.Set set = new TreeSet();
B.Set set = new HashSet();
C.Set set = new SortedSet();
D.Set set = new LinkedHashSet();
正确答案:A
5.列代码的运行结果是()。
public class Forest implements Serializable {
private Tree tree = new Tree();
public static void main(String[] args) {
Forest f = new Forest();
try {
FileOutputStream fs = new FileOutputStream(“Forest.ser”);
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(f);
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class Tree {}
A.编译失败
B.运行时,抛出异常
C.Forest 的实例被序列化到文件
D.Forest 的实例和 Tree 的实例都被序列化到文件
正确答案:B
6. 请看下列代码:
class Payload {
private int weight;
public Payload(int wt) {
weight = wt;
}
public Payload() {}
public void setWeight(int w) {
weight = w;
}
public String toString() {
return Integer.toString(weight);
}
}
public class TestPayload {
static void changePayload(Payload p) {
< 插入代码>
}
public static void main(String[] args) {
Payload p = new Payload();
p.setWeight(1024);
changePayload(p);
System.out.println(“The value of p is ” + p);
}
}
假设运行后输出“The value of p is 420”,那么<插入代码>处应填入代码是:
A. p.setWeight(420);
B. Payload.setWeight(420);
C. p = new Payload(420);
D. p = new Payload();
p.setWeight(420);
正确答案:A
7. 题目代码实现的功能是:把放入到 TreeSet 集合中的 Student 进行排序,首先按照 num
升序,如果 num 相同,再按照 name 降序。请问《插入代码 1》和《插入代码 2》处应填入的
代码分别是:
public class SortStudent {
public static void main(String[] args) {
TreeSet set=new TreeSet();
set.add(new Student(19,”tom”));
set.add(new Student(20,”jessica”));
set.add(new Student(19,”terry”));
}
}
class Student implements 《插入代码 1》{
private int num;
private String name;
public Student(int num,String name){
this.name=name;
this.num=num;
}
《插入代码 2》
}
A. Comparable
public int compareTo(Object o) {
Student stu=null;
if(o instanceof Student){
stu=(Student)o;
}
int result=this.num>stu.num?1:(this.num==stu.num?0:-1);
if(result==0){
result=this.name.compareTo(stu.name);
}
return result;
}
B. Comparable
public int compareTo(Object o) {
Student stu=null;
if(o instanceof Student){
stu=(Student)o;
}
int result=this.num>stu.num?1:(this.num==stu.num?0:-1);
if(result==0){
result=stu.name.compareTo(this.name);
}
return result;
}
C. Compartor
public int compare(Object o) {
Student stu=null;
if(o instanceof Student){
stu=(Student)o;
}
int result=this.num>stu.num?1:(this.num==stu.num?0:-1);
if(result==0){
result=this.name.compareTo(stu.name);
}
return result;
}
D. Compartor
public int compare(Object o) {
Student stu=null;
if(o instanceof Student){
stu=(Student)o;
}
int result=this.num>stu.num?1:(this.num==stu.num?0:-1);
if(result==0){
result=stu.name.compareTo(this.name);
}
return result;
}
正确答案:B
8.
下列语句创建对象的总个数是:()。
String s=”a”+”b”+”c”+”d”+”e”;
A.
1
B.
2
C.
3
D.
4
正确答案:A
9. 下列代码的输出结果是:
public static void main(String[] args) {
BigDecimal d1 = new BigDecimal(“3.0″);
BigDecimal d2 = new BigDecimal(“2.9″);
BigDecimal d3 = d1.subtract(d2);
System.out.println(d3);
}
A. 0
B. 0.1
C. 0.10000000000000009
D. 0.10
正确答案:B
10.
运行下面程序:
public class Foo{
public static void main(String[] args) {
try {
test();
System.out.println(“condition 1″);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(“condition 2″);
} catch (Exception e) {
System.out.println(“condition 3″);
} finally {
System.out.println(“finally”);
}
}
public static void test() {
String str = “cc”;
str.compareTo(“abc”);
}
}
输出的结果是:()。
A.
condition 1
finally
B.
condition 2
finally
C.
condition 1
condition 3
finally
D.
condition 1
condition 2
finally
正确答案:A
11. 关于下列代码说法正确的是:
public class A {
private int counter = 0;
public static int getInstanceCount() {
return counter;
}
public A() {
counter++;
}
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
A a3 = new A();
System.out.println(A.getInstanceCount());
}
}
A. 该类编译失败
B. 输出:1
C. 输出:3
D. 输出:0
正确答案:A
12. 运行下列代码发生的异常或错误是:
public class ClassB {
public void count(int i) {
count(++i);
}
public static void main(String[] args) {
ClassB a = new ClassB();
a.count(3);
}
}
A. java.lang. StackOverflowError
B. java.lang.IllegalStateException
C. java.lang.ExceptionlnlnitializerError
D. java.lang.ArraylndexOutOfBoundsException
正确答案:A
13.
类 A,B 的定义如下:
class A {
private int a = 100;
A() {
System.out.print(“A()”);
System.out.println(a);
}
}
class B extends A {
private int a = 200;
B() {
System.out.print(“B()”);
System.out.println(a);
}
}
运行下面的代码:
new B();
输出的结果是:()。
A. A() 100
B() 200
B. A() 200
B() 200