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<Student> set=new TreeSet<Student>();
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
C. B() 200
A() 100
D. B() 200
A() 200
正确答案:A
14. 下列类的定义,错误的是()。
A.
public class Test extends Object{……}
B.
final class Operators{……}
C.
class Point{……}
D.
void class Point{……}
正确答案:D
15.
下列代码的作用说法不正确的是:()。
class Card implements java.io.Serializable{}
A.
开启序列化功能,使得Card类的对象可以存储到文件中
B.
开启序列化功能,使得Card类的对象可以在网络上传输
C.
使得Card类的子类的对象可以被序列化
D.
导致Card的子类的对象不可以被反序列化
正确答案:D
16. 程序员需要创建一个泛型类MinMax,MinMax类的属性需要实现Comparable接口,下列选项中的MinMax类的实现能编译通过的是:
A. class MinMax<E extends Comparable<E>> {
E min=null;
E max=null;
public MinMax() { }
public void put(E value) { /* store min or max */ }
}
B. class MinMax<E implements Comparable<E>> {
E min=null;
E max=null;
public MinMax() { }
public void put(E value) { /* store min or max */ }
}
C. class MinMax<E extends Comparable<E>> {
<E> E min = null;
<E> E max = null;
public MinMax() { }
public <E> void put(E value) { /* store min or max */ }
}
D. class MinMax<E implements Comparable<E>> {
<E> E min = null;
<E> E max = null;
public MinMax() { }
public <E> void put(E value) { /* store min or max */ }
}
正确答案:A
17. 请看下列代码:
public class Plant {
private String name;
public Plant(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class Tree extends Plant {
public void growFruit() {
}
public void dropLeaves() {
}
}
下列说法正确的是:
A. 在Tree类中添加代码:public Tree() { Plant(); },编译将通过
B. 在Plant类中添加代码:public Plant() { Tree(); },编译将通过
C. 在Plant类中添加代码:public Plant() { this(”fern”); },编译将通过
D. 在Plant类中添加代码:public Plant() { Plant(”fern”); },编译将通过
正确答案:C
18. 运行下列代码:
int[] oneArr = { 2, 11, 26, 27, 37, 44, 48, 60 };
int[] twoArr = { 19, 35, 49, 55, 58, 75, 83, 84, 91, 93 };
int[] threeArr = new int[oneArr.length + twoArr.length];
int p = 0, q = 0;
while (p < oneArr.length && q < twoArr.length) {
threeArr[p + q] =
oneArr[p] < twoArr[q] ? oneArr[p++] : twoArr[q++];
}
if (p < oneArr.length) {
System.arraycopy(oneArr, p, threeArr, p + q, oneArr.length – p);
}
else if (q < twoArr.length) {
System.arraycopy(twoArr, q, threeArr, p + q, twoArr.length – q);
}
System.out.println(Arrays.toString(threeArr));
输出的结果是:()。
A. [2,11,26,27,37,44,48,60,19,35,49,55,58,75,83,84,91,93];
B. [2,11,19,26,27,35,37,44,48,49,55,58,60,75,83,84,91,93];
C. [19,35,49,55,58,75,83,84,91,93,2,11,26,27,37,44,48,60];
D. [2,19,11,35,26,49,27,55,37,58,44,75,48,83,60,84,91,93];
正确答案:B
19. 请看下列代码:
2. public class Test {
3. public static void main(String[] args) {
4. List<String> strings = new ArrayList<String>();
5. <插入代码>
6. }
7. }
下列选择中放在第5行编译失败的是:
A. String s = strings.get(0);
B. Iterator i1 = strings.iterator();
C. String[] array1 = strings.toArray();
D. Iterator<String> i2 = strings.iterator();
正确答案:C
20.
下列代码的输出结果是()
public static void main(String[] args) {
String test = “a1b2c3”;
String[] tokens = test.split(“\d”);
for (String s : tokens)
System.out.print(s + ” “);
}
A.
a b c
B.
1 2 3
C.
a1b2c3
D.
a1 b2 c3
正确答案:A
21. 数据类型int、char和double所占用内存字节数分别是:()。
A. 4、2和8
B. 2、2和4
C. 2、1和8
D. 4、4和4
正确答案:A
22. 如下方法声明中,错误的是()。
A.
public void say()
{ System.out.print(“Hi”); }
B.
public void say()
{ System.out.print(“Hi”); return; }
C.
public int say()
{ System.out.print(“Hi”); return; }
D.
public int say()
{ System.out.print(“Hi”); return 0; }
正确答案:C
23.
运行下面的语句:
String s=””;
if(s==s+0){
System.out.println(“Hello World”);
}
编译,运行的结果是:()。
A.
Hello World
B.
无输出
C.
编译错误
D.
抛出运行时异常
正确答案:B
24. List类的对象list中的元素为:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],现在想返回该list对象的子集合[5,6,7,8],需要做的操作是:
A. list.subList(5, 8);
B. list.subList(5, 9);
C. list.subList(4, 8);
D. list.subList(4, 9);
正确答案:B
25. 请看下列代码:
interface Foo {
int bar();
}
public class Sprite {
public int fubar(Foo foo) {
return foo.bar();
}
public void testFoo() {
fubar(
<插入代码>
);
}
}
使类Sprite编译通过,在<插入代码>处应填入的代码是:
A. Foo { public int bar() { return 1; } }
B. new Foo { public int bar() { return 1; } }
C. new Foo() { public int bar(){return 1; } }
D. new class Foo { public int bar() { return 1; } }
正确答案:C
26. 需要读取一个比较大的文本文件,这个文件里有很多字节的数据,那么下列最合适读这个文件的选项是:()。
A.
new FileInputStream(“fileName”);
B.
new InputStreamReader(new FileInputStream(“fileName”));
C.
new BufferedReader(new InputStreamReader(new FileInputStream(“fileName”)));
D.
new RandomAccessFile(“fileName”,”+rw”);
正确答案:C
27. 下列不属于Collection接口的方法的是:
A. clear
B. contains
C. remove
D. listIterator
正确答案:D
28. 下列代码运行的结果是:
public static void main(String[] args) {
List list = new ArrayList();
list.add(“b”);
list.add(“a”);
list.add(“c”);
System.out.println(Collections.binarySearch(list, “a”));
}
A. 0
B. 1
C. a
D. b
正确答案:B
29. 下列选项中,不能包含重复元素的容器是:()。
A.
List
B.
ArrayList
C.
Set
D.
Collection
正确答案:C
30. 下面关于interface,叙述错误的是:()
A.
一个interface可以继承多个interface
B.
接口中的方法可以由private修饰
C.
interface中可以定义static final 常量
D.
interface中可以无任何方法定义
正确答案:B
31. 下面关于final说法正确的是:()。
A.
final修饰类时,该类能被继承。
B.
final修饰方法时,该方法能被重写。
C.
当使用static final 修饰的常量时,将采用编译期绑定的方式。
D.
当使用final和abstract共同修饰一个类时,final应至于abstract之前。
正确答案:C
32. 下列不属于Java运算符的是()。
A. >>
B. >>>
C. <<
D. <<<
正确答案:D
33. 下面不属于Java语言特点的是:
A. 平台无关
B. 面向对象
C. 支持指针类型
D. 垃圾回收机制
正确答案:C
34. 请看下列代码:
public interface A {
String DEFAULT_GREETING = “Hello World”;
public void method1();
}
现有接口B,是A接口的子接口,下列选择中B接口的声明正确的是:
A. public interface B extends A { }
B. public interface B implements A {}
C. public interface B instanceOf A {}
D. public interface B inheritsFrom A { }
正确答案:A
35. 在Java语言中,下列说法正确的是:()。
A. Java访问修饰符按照访问范围由低到高的排列顺序是public,default,protected,private
B. private可以用于外部类的声明
C. 一个Java源文件中声明为public的外部类只能有一个
D. protected声明的方法不可以被子类重写
正确答案:C
36. 请看下列代码:
Map<String,Integer> map=new HashMap<String,Integer>();
map.put(“one”,100);
map.put(“two”,200);
map.put(“three”,300);
遍历map对象中的每一个元素,下列选项正确的是:
A. Set<String> set=map.keySet();
for(String key:set){
Integer value=map.get(key);
System.out.println(key+”:”+value);
}
B. List<String> list=map.keyList();
for(String key:list){
Integer value=map.getKey(key);
System.out.println(key+”:”+value);
}
C. Set<Map.Entry<String, Integer>> set = map.entrySet();
for (Map.Entry<String, Integer> per : set) {
System.out.println(per.getKey() + “:” + per.getValue());
}
D. List<Entry> list=map.entryList();
for(Entry per:list){
System.out.println(per.getKey() + “:” + per.getValue());
}
正确答案:AC
37. 关于下列代码说法正确的是:
Set<Integer> set = new HashSet<Integer>();
Random r = new Random();
int i = 0;
while (set.size() <10) {
set.add(r.nextInt(100)); i++;
}
A. 代码循环执行的次数一定为10次,重复的整数可以放入set集合中。
B. 代码将随机产生10个100以内的可重复的整数,并将其放入集合中。
C. 代码循环执行的次数可能会大于10次,重复的整数无法放入set集合中。
D. 代码将随机产生元素个数为10个的100以内不重复整数集合。
正确答案:CD
38. 类A、B及接口C的定义如下:
class A {}
final class B {}
interface C {}
下列语句有编译错误的是:()
A. A a = (A)new Object();
B. B b = (B)new A();
C. C c1 = (C) new A();
D. C c2 = (C)new B();
正确答案:BD
39. 以下创建线程的方式正确的是:
A. class Runner implements Runnable {
public void run() {线程体…}
public static void main(String args[]) {
Runner r = new Runner();
Thread t = new Thread(r);
t.start();
}
}
B. class Runner implements Runnable {
public void run() {线程体…}
public static void main(String args[]) {
Runner t = new Runner();
t.start();
}
}
C. class Runner extends Thread {
public void run() {线程体… }
public static void main(String args[]){
Thread t = new Runner();
t.start();
}
}
D. class Runner {
public static void main(String args[]){
Thread t = new Thread(){
public void run() {线程体… }
};
t.start();
}
正确答案:ACD
40. 请看下列代码
public class Foo {
public void method(String str,int age){}
}
和Foo类中method方法重载的方法是:
A. public int method(String str,int age){}
B. public void method(String s,int year){}
C. public void method(int year,String s){}
D. public int method(int year,String s){}
正确答案:CD
41.
ScoreManager类实现了成绩管理系统。该系统有如下功能供选则:录入成绩 ,成绩列表 ,查询成绩,退出。当用户在控制台输入1,用户选择的功能为录入成绩;输入2,用户选择的功能为成绩列表;输入3,用户选择的功能为根据姓名查找成绩;输入4,退出。
ScoreManager类代码如下:
public class ScoreManager {
public static void main(String[] args) {
int numOfStudents = 10;
//学生名字信息数组
String[] students = NameUtils.randomNames(numOfStudents);
int[] scores = new int[numOfStudents];//学生成绩数组
//使用Scanner接收控制台的输入信息
空白处1
System.out.println(“**********欢迎进入成绩管理系统**********”);
while (true) {
System.out.println(”
请选择功能:1——成绩录入,2——成绩列表,3——成绩查询,4——退出”);
String c = scanner.next().trim();
if (“1”.equals(c)) {
System.out.println(“开始录入成绩: “);
for (int i = 0; i < students.length; i++) {
System.out.print((i + 1) + “. 学生姓名:” + students[i]
+ “, 成绩:?”);
//从控制台接收到学生成绩
空白处2
}
System.out.println(“录入完毕。”);
} else if (“2”.equals(c)) {
int avg = 0;
for (int i = 0; i < scores.length; i++) {
avg += scores[i];
}
//计算学生的评价成绩
空白处3
System.out.println(StringUtils.rightPad(“编号”, 20, ” “)
+ StringUtils.rightPad(“姓名”, 20, ” “)
+ StringUtils.rightPad(“成绩”, 20, ” “));
System.out.println(StringUtils.repeat(“-“, 30));
for (int i = 0; i < students.length; i++) {
System.out.println(StringUtils
.rightPad(i + 1 + “”, 10, ” “)
+ StringUtils.rightPad(students[i], 10, ” “)
+ StringUtils.rightPad(scores[i] + “”, 10, ” “));
}
System.out.println(StringUtils.repeat(“-“, 30));
System.out.println(“平均成绩: ” + avg);
} else if (“3”.equals(c)) {
System.out.print(“请输入您要查询的学生姓名: “);
String student = scanner.next().trim();
int i = 0;
for (; i < students.length; i++) {
//如果查找到某个学生的信息,退出当前循环
空白处4
}
if ( 空白处5 ) {
System.out.println((i + 1) + “. 学生姓名:” + students[i]
+ “, 成绩:” + scores[i]);
} else {
System.out.println(“对不起,找不到学员的信息。”);
}
} else if (“4”.equals(c)) {
System.out.println(“**********谢谢使用**********”);
break;
}
}
scanner.close();
}
}
(1). 下列选项中,能填入空白处1的代码是( )
A.
Scanner scanner = new Scanner(System.out);
B.
Scanner scanner = new Scanner(System.in);
C.
Scanner scanner = new Scanner(new FileInputStream(System.in));
D.
Scanner scanner = new Scanner(new FileOutputStream(System.out));
正确答案:B
(2). 下列选项中,能填入空白处2的代码是( )
A.
scores[i] = scanner.nextInt();
B.
scores[i] = scanner.next();
C.
scores[i] = scanner.nextDouble();
D.
scores[i] = scanner.nextString();
正确答案:A
(3). 下列选项中,能填入空白处3的代码是( )
A.
avg *= scores.length;
B.
avg /= scores.length;
C.
avg *= scores.length-1;
D.
avg /= scores.length-1;
正确答案:B
(4). 下列选项中,能填入空白处4的代码是( )
A.
if (student.equalsIgnoreCase(students[i-1])) {
break;
}
B.
if (student.equalsIgnoreCase(students[i])) {
continue;
}
C.
if (student.equalsIgnoreCase(students[i-1])) {
continue;
}
D.
if (student.equalsIgnoreCase(students[i])) {
break;
}
正确答案:D
(5). 下列选项中,能填入空白处5的代码是( )
A.
scores.length < scores.length-1
B.
scores.length < scores.length
C.
i < students.length
D.
i < students.length-1
正确答案:C
42.
歌德巴赫猜想的近似证明
歌德巴赫猜想是说任何一个大于2的偶数都能表示为两个素数之和,请编写一个Java程序,验证1~100内歌德巴赫猜想的正确性。
public class Guess {
public static void main(String[] args) {
System.out.println(“在1~100范围内,现在开始证实哥德巴赫猜想:”);
if (testifyGuess(1, 100)) {
System.out.println(“在 1~100范围内,哥德巴赫猜想是正确的。”);
} else {
System.out.println(“哥德巴赫猜想是错误的”);
}
}
/**
* 判断1~100范围内的所有偶数是否符合哥德巴赫猜想,符合则返回true,反之则返回false
*/
public static boolean testifyGuess(int low, int high) {
int i, j = 0;
boolean flag = true;
for (i = low; i <= high; i++)
if ( 空白处1 ) // 在1~100之间选取大于2的偶数进行哥德巴赫猜想测试
if (isGoldbach(i)) {
j++; // j用来控制输出格式 ,每行输出5个数据
if (j == 5) {
System.out.println();
j = 0;
}
} else {
flag = false;
break;
}
return flag;
}
/**
*判断参数a是否符合哥德巴赫猜想
*/
public static boolean isGoldbach(int a) {
int i;
boolean flag = false;
for (i = 1; 空白处2 ; i++) {
// 根据试题分析中的表达式,传入相关的两个参数
if ( 空白处3 ) {
flag = true;
System.out.print(a + “=” + i + “+” + (a – i) + ” “);
空白处4
}
}
return flag;
}
/**
* 判断参数i是否是素数,是则返回true反之则返回false
*/
public static boolean isPrime(int i) {
int n;
boolean flag = true;
// 1本身不是素数,因此需把这个特殊的数字抛出
if (1 == i)
flag = false;
/*
* 质数又称素数。指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数整除数
* 判断i是否是素数的一个方法是看2~i-1之间有其因子(能被2整除),
* 有则不是素数返回false,反之则返回true
*/
for ( 空白处5 )
if (i % n == 0) {
flag = false;
break;
}
return flag;
}
}
(1).
下列选项中,能填入空白处1的代码是( )
A.
i % 2 == 0 && i > 2
B.
i % 2 == 0 && i < 2
C.
i / 2 == 0 && i > 2
D.
i / 2 == 0 && i < 2
正确答案:A
(2).
下列选项中,能填入空白处2的代码是( )
A.
i <= a % i;
B.
i <= a / i;
C.
i <= a % 2;
D.
i <= a / 2;
正确答案:D
(3).
下列选项中,能填入空白处3的代码是( )
A.
isPrime(i-1) && isPrime(a – i)
B.
isPrime(i) && isPrime(a + i)
C.
isPrime(i) && isPrime(a – i)
D.
isPrime(i) && isPrime(a)
正确答案:C
(4).
下列选项中,能填入空白处4的代码是( )
A.
final;
B.
break;
C.
continue;
D.
static;
正确答案:B
(5).
下列选项中,能填入空白处5的代码是( )
A.
n = 2; n <= i – 1; n++
B.
n = 2; n <= i; n++
C.
n = 1; n <= i – 1; n++
D.
n = 1; n <= i; n++
正确答案:A