达内JSD1412第一次月考试题及答案

更新时间:2023-11-23 04:02:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

考试场次:2015_01月月考_01月29日_JAVA 试卷名称:2015年01月_JSD_JSD1412 1.

运行下面的程序:

int a = 100;

int b = 200;

a = a + b;

b = a - b;

a = a - b;

System.out.println(\

输出的结果是:()。

A. a=100, b=300 B. a=100, b=200 C. a=200, b=100 D. a=300, b=200

正确答案:C

2. 下面关于数组的声明语句中,有编译错误的是:()。 A. int[] arr = new int[]{1,2,3}; B. int[] arr = null; arr = {1,2,3,4,5};

C. int[][] arr = new int[][]{{1,2,3},{4,5},{6}} D. int[][] arr = new int[2][]; 正确答案:B

3. 分析如下代码,输出结果为()。 public static void main(String[] args) { int i = 0; boolean re = false; re = ((++i) + i == 2) ? true : false; System.out.println(\ A. i=1,re=true B. i=0,re=true C. i=1,re=false D. i=0,re=false 正确答案:A

4. 请看下列代码: 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

5. 程序的执行结果是: public class Test {

public static void main(String [] args){ String str1= new String(\ String str2 = new String(\ String str3=str1;

if(str1.equals(str2)){

System.out.println(\ }else{

System.out.println(\ }

if(str1==str3){

System.out.println(\ }else{

System.out.println(\ } }

A. true true B. true false

C. false true

D. false false

正确答案:A

6. 下列代码的输出结果是:()。 public class StaticFoo { int num; static int x; public static void main(String[] args)

{ StaticFoo foo1 = new StaticFoo (); foo1.num++; foo1.x++; StaticFoo foo2 = new StaticFoo (); foo2.num++; foo2.x++; StaticFoo foo3 = new StaticFoo

(); foo3.num++; foo3.x++; StaticFoo.x++; System.out.print(foo3.num+\System.out.println(foo3.x); } } A. 3,3 B. 1,3 C. 3,4 D. 1,4 正确答案:D

7. 运行下面的程序:

Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, 2012);

c.set(Calendar.MONTH, Calendar.SEPTEMBER); c.set(Calendar.DATE, 31);

SimpleDateFormat sdf = new SimpleDateFormat(\System.out.println(sdf.format(c.getTime())); 输出的结果是:()。 A. 2012-10-1 B. 2012-10-01 C. 2012-09-30 D. 2012-9-30 正确答案:B

8. 下列关于JVM说法,错误的是()。

A. JVM通过专门的线程实现内存的回收。

B. 使用java命令时,可以通过参数来设置分配JVM的内存大小。 C. JRE包括JVM及Java核心类库。

D. 目前主流版本JVM通过纯解释的方式运行Java字节码。 正确答案:D

9. 请看下列代码: 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

10. 运行下列程序:

String str = \ String str1 = \ int index = 0;

while ((index = str.indexOf(str1, index)) != -1) { System.out.print(index+””); index += str1.length(); }

控制台输出的结果是:()。 A. 1 8 17 B. 2 9 18 C. 5 12 21 D. 6 13 22 正确答案:B

11. 下列语句创建对象的总个数是:()。 String s=”a”+”b”+”c”+”d”+”e”; A. 1 B. 2 C. 3 D. 4

正确答案:A

12. 下列代码的输出结果是()。

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

13. 下列代码编译和运行的结果是() public class Foo { public static void main(String[] args) { java.util.List list = new java.util.ArrayList(); list.add(new B()); list.add(new C()); for (A a : list) { a.x(); a.y(); } } } interface A { void

x(); } class B implements A { public void x() {} public void y() {} } class C extends B { public void x() {} } A. 代码运行没有输出 B. 运行时抛出异常

C. 代码a.y();行,编译错误

D. 代码java.util.List list = new java.util.ArrayList();行,编译错误 正确答案:C

14. 下面的程序可以输出1~100内前10个3的倍数: for (int i = 1, count = 0; i < 100; i++) { if (i % 3 == 0) {

System.out.println(i); (空白处) } }

下列选项中,空白处可以填入的代码是()。 A. if (count++ >= 10) { break; }

B. if (++count >= 10) { break; }

C. if (count++ >= 10) { continue; }

D. if (++count >= 10) { continue; }

正确答案:B

15. 请看下列代码: public class Person { private String name;

public Person(String name) { this.name = name; } public boolean equals(Person p) { return p.name.equals(this.name); } }

下列说法正确的是:

A. Person类的equals方法没有覆盖Object类的equals方法 B. 编译错误,因为私有属性不能在equals方法中被访问

C. 基于Hash的数据结构可以正确工作,但是Person类必须覆盖hashCode方法 D. 当向Set集合中添加Person对象时,equals方法中的return语句能避免重复 正确答案:A

16. 有变量声明如下: short b = 120; 下列语句中,错误的是()。 A. short s = b;

B. int i = b; C. byte s1 = b; D. long l = b; 正确答案:C

17. Java程序的执行过程中用到一套JDK工具,其中javac.exe是指()。 A. Java语言编译器 B. Java字节码解释器 C. Java文档生成器 D. Java类分解器 正确答案:A

18. 以下程序的输出结果是: ()。 public class Super { public Super() { System.out.println(\\} public class Sub extends Super{ public Sub() { System.out.println(\ } public static void main(String[] args) { Super fc = new Super(); Sub cc = new Sub(); } } A. Super Super Sub B. Super Sub C. Sub Super D. Super Sub Sub 正确答案:A

19. 下列代码的输出结果是()。 public static void main(String[] args) { int[] one=new int[]{4,6,8}; int[] two=new int[]{1,3,5,7,9}; System.arraycopy(one, 1, two, 2, 2); System.out.println(Arrays.toString(two)); } A. [1, 3, 7, 4, 6] B. [1, 3, 5, 7, 8] C. [1, 3, 5, 6, 9] D. [1, 3, 6, 8, 9] 正确答案:D

20. 实现Point类的equals方法,具体逻辑为:“成员变量x和y分别相等的Point对象被视为相等”。

public class Point { private int x; private int y; ...

public boolean equals(Object obj) { 《填入代码》 } }

《插入代码》处应填入的代码正确的是:

A. if(obj.x == this.x || obj.y == this.y){ return true; }

return false;

B. if(obj.x == this.x && obj.y == this.y){ return true; }

return false;

C. if(!(obj instanceof Point)) return false;

if(((Point)obj).x == ((Point)obj).y && this.x == this.y){ return true; }

return false;

D. if(!(obj instanceof Point)) return false;

if(((Point)obj).x == this.x && ((Point)obj).y == this.y){ return true; }

return false;

正确答案:D

21. 下列数组声明语句中,错误的是:()。 A. int[] arr = new int[]{}; B. int[] arr = new int[]; C. int[] arr = {};

D. int[][] arr = new int[2][] 正确答案:B

22. 下面关于interface,叙述错误的是:() A. 一个interface可以继承多个interface B. 接口中的方法可以由private修饰

C. interface中可以定义static final 常量 D. interface中可以无任何方法定义 正确答案:B

23. 关于下列代码说法不正确的是: 10. interface Foo { 11. int bar(); 12. } 13.

14. public class Beta { 15.

16. class A implements Foo {

17. public int bar() { return 1; } 18. } 19.

20. public int fubar( Foo foo) { return foo.bar(); } 21.

22. public void testFoo() {

23.

24. class A implements Foo {

25. public int bar() { return 2; } 26. } 27.

28. System.out.println( fubar( new A())); 29. } 30.

31. public static void main( String[] argv) { 32. new Beta().testFoo(); 33. } 34. }

A. 编译错误

B. 运行代码输出:2

C. 如果删除16,17,18行,运行代码应然输出:2 D. 如果删除24,25,26行,运行代码输出:1 正确答案:A

24. 下列代码的输出结果是: class Cup { }

class PoisonCup extends Cup { public void takeCup(Cup c) { if (c instanceof PoisonCup) {

System.out.println(\ } else if (c instanceof Cup) {

System.out.println(\ } else {

System.exit(0); } } }

public class TestCup {

public static void main(String[] args) { Cup cup = new PoisonCup();

PoisonCup poison=new PoisonCup(); poison.takeCup(cup); } }

A. Inconceivable! B. Dizzying intellect!

C. 代码正常运行,但是无输出 D. 抛出运行时异常 正确答案:A

25. 下列关于HashMap的方法描述正确的是:

A. containsKey(Object key): 判断集合中是否包含指定的Value B. containsValue (Object value): 判断集合中是否包含指定的Key

C. get(Object key):返回与参数Key所对应的Value对象,如果不存在则返回null

D. put(K key, V value):将Key-Value对存入Map,如果在集合中已经包含该Key,则操作将替换该Key所对应的Value,返回值为该Key当前所对应的Value(如果没有则返回null) 正确答案:C

26. 查看如下代码:

1. class HasStatic{ 2. private static int x=100; 3. public static

void main(String args[ ]){ 4. HasStatic hs1=new HasStatic( ); 5. hs1.x++; 6. HasStatic hs2=new HasStatic( ); 7. hs2.x++; 8. hs1=new HasStatic( ); 9. hs1.x++; 10. HasStatic.x--; 11. System.out.println(“x=”+x); 12. } 13.}

对于此代码,下列描述中,正确的是()。

A. 5行不能通过编译,因为引用了私有静态变量 B. 10行不能通过编译,因为x是私有静态变量 C. 程序通过编译,输出结果为:x=103 D. 程序通过编译,输出结果为:x=102 正确答案:D

27. 在Java语言中,下列说法正确的是:()。

A. Java访问修饰符按照访问范围由低到高的排列顺序是public,default,protected,private

B. private可以用于外部类的声明

C. 一个Java源文件中声明为public的外部类只能有一个 D. protected声明的方法不可以被子类重写 正确答案:C

28. 下列代码运行的结果是()。

public class Base { public static final String FOO = \main(String[] args) { Base b = new Base(); Sub s = new Sub(); System.out.print(Base.FOO); System.out.print(Sub.FOO); System.out.print(b.FOO); System.out.print(s.FOO); System.out.print(((Base) s).FOO); } } class Sub extends Base { public static final String FOO = \ A. foofoofoofoofoo B. foobarfoobarbar C. foobarfoofoofoo D. foobarfoobarfoo 正确答案:D

29. 在Java中,Integer.MAX_VALUE表示: A. double型最大值 B. int最大值 C. long型最大值 D. char型最大值 正确答案:B

30. 请看下列代码:

public static void main(String[] args) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, 2013);

c.set(Calendar.MONTH, Calendar.FEBRUARY); c.set(Calendar.DATE, 28); <插入代码> }

在<插入代码>处填入将Calendar表示的日期转换为Date表示的日期: A. Date d=c.getDate(); B. Date d=c.getCalendar(); C. Date d=c.getNow(); D. Date d=c.getTime(); 正确答案:D

31. 题目代码的功能为:输出每个字符在一个字符串中出现的次数(不区分大小写)。 String str = \str=str.toLowerCase(); int max_length = 0;

while (str.length() > 0) { 《插入代码》 }

A. int length = str.length(); char first=str.charAt(0);

String strNew = str.replaceAll(String.valueOf(first), \if (length>strNew.length()) {

max_length = length - strNew.length(); System.out.println(first+\}

B. int length = str.length(); char first=str.charAt(0);

String strNew = str.replaceAll(String.valueOf(first), \if (length>strNew.length()) {

max_length = length - strNew.length(); str = strNew;

System.out.println(first+\}

C. int length = str.length();

String first = str.substring(0, 1);

String strNew = str.replaceAll(first, \if (length>strNew.length()) {

max_length = length - strNew.length(); str = strNew;

System.out.println(first+\}

D. int length = str.length();

String first = str.substring(0, 1);

String strNew = str.replaceAll(first, \if (length>strNew.length()) {

max_length = length - strNew.length(); System.out.println(first+\}

正确答案:BC

32. 请看下列代码: public class Old {

public static Object get(List list) { return list.get(0); } }

以下选项调用get方法,能编译通过的是: A. Object o = Old.get(new LinkedList()); B. Object o = Old.get(new LinkedList());

C. String s = Old.get(new LinkedList());

D. String s = (String)Old.get(new LinkedList()); 正确答案:AD

33. 下列赋值语句中,会有编译错误的是()。 A. int a = 8888888888; B. char b = 1000+300; C. byte c = 100+30; D. int d = 'a'+'b'+'c'; 正确答案:AC

34. 所谓“水仙花”数是一个整数等于各位数字立方的和,例如:153 = 1*1*1+5*5*5+3*3*3,下面的程序用于输出2~1000内的水仙花数: for (int n = 2; n <= 1000; n++) { 空白处 if (s == n) {

System.out.println(n); } }

下列选项中,空白处可以填入的代码是:()。 A. int s = 0, n1 = n; while (n1 > 0) { int t = n1 % 10; s += t * t * t; n1 /= 10;

}

B. int s = 0, n1 = n; while (n1 > 0) { int t = n1 / 10; s+= t * t * t; n1 %= 10; }

C. int s = 0;

for(int n1 = n; n1>0; n1 /= 10) { int t = n1; s += t * t * t; }

D. int s = 0; for(int n1 = n; n1>0; n1 %= 10) { int t = n1 / 10; s += t * t * t; } 正确答案:AC

35. 请看下列代码: class One {

public One foo() { return this; } }

class Two extends One { public One foo() { return this; } }

class Three extends Two { <插入代码> }

下列选项中的代码,放置在<插入代码>处无编译错误的是: A. public void foo() { }

B. public Object foo() { return this; } C. public Two foo() { return this; } D. public One foo() { return this; } 正确答案:CD

36. 在Java语言中,下列说法正确的是:()。

A. StringBuffer和StringBuilder的区别在于:StringBuffer是线程安全的而StringBuilder不是。

B. String是不可变对象,而StringBuffer中封装的字符串数据是可以动态改变的。

C. 判断两个StringBuilder对象的字符序列是否相同,可以调用其equlas方法进行比较。 D. String的重写了equals方法,重写的逻辑是:字符序列相同的String对象equals方法返回true。 正确答案:ABD

37. 下列关于HashMap的描述正确的是:

A. HashMap的Key和Value是以链表的方式存入对应的bucket

B. HashMap的查找方式是获取Key的hashCode值,通过hash算法确定存储的bucket,调用equals方法依次与bucket中的Key进行比较

C. 放入HashMap集合中的元素按照key的自然顺序排序 D. HashMap中的key是不可以的重复的 正确答案:ABD

38. 在Java语言中,下列说法正确的是()。 A. 一个接口可以继承多个接口 B. 一个类可以继承多个类 C. 一个类可以实现多个接口 D. 一个类可以有多个子类 正确答案:ACD

39. 请看下列代码:

public abstract class Shape { int x; int y;

public abstract void draw();

public void setAnchor(int x, int y) { this.x = x; this.y = y; } }

下列选项中能正确使用Shape类的是:

A. public class Circle implements Shape { private int radius; }

B. public abstract class Circle extends Shape { private int radius; }

C. public class Circle extends Shape { private int radius; public void draw(); }

D. public class Circle extends Shape { private int radius;

public void draw() {/* code here */} }

正确答案:BD

40. 在<插入代码>处,填入下列代码编译正确的是: public void foo(int[] x) { <插入代码> }

A. foreach(int z : x) System.out.println(z); B. for(int z : x) System.out.println(z);

C. while( x.hasNext()) System.out.println( x.next());

D. for( int i=0; i< x.length; i++ ) System.out.println(x[i]); 正确答案:BD

41. 阅读理解 public class A { public A() { System.out.print(\\public A(String s) { System.out.print(s); } public void fun() { System.out.println(\ } } public class B extends A { public B() { System.out.print(\{ super(s); } public void fun() { System.out.println(\ } public void sun(){ System.out.println(\} public static void Main() { A a = new B(); a.fun(); } } (1). 下列关于上述代码中构造方法的描述,错误的是()。 A. 实例化对象 a 时,将默认调用父类的无参构造方法

B. 类 B中使用 super 关键字,是为了调用父类的含有一个参数的构造方法 C. 实例化对象 a 时,父类A和子类B的构造方法都会被调用到

D. 实例化父类对象时,调用父类 A 的构造方法;实例化子类对象时,则只调用子类B的构造方法

正确答案:D

(2). 该代码运行后,输出为:()。 A. A B A.fun() B. A B B.fun() C. B A A.fun() D. B A B.fun() 正确答案:B

(3). 如果 main 方法中如此调用: public static void main(String[] args) { A a = new B(\其他代码不变,该代码运行后,输出为:()。 A. A A.fun() B. B A.fun() C. Hello,A.fun() D. Hello,B.fun() 正确答案:D

(4). 如果 main 方法中如此调用: public static void main(String[] args) { A a = new A(); a.sun(); } 其它代码不变,下列说法正确的是:()。 A. 运行输出结果为:A B.sun() B. 运行输出结果为:A B B.sun() C. 运行输出结果为:B A B.sun() D. 编译错误 正确答案:D

(5). 下列关于上述代码的描述,正确的是()。

A. 如果将A类定义成public abstract class A,那么方法fun必须定义成抽象方法 B. 如果将A类定义成public abstract class A,那么A类中必须有一个抽象方法

C. 如果将A类中的方法fun定义成public abstract void fun(),那么A类必须是抽象类 D. 如果将A类定义成public abstract class A,那么A类应然可以实例化对象 正确答案: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范围内,哥德巴赫猜想是正确的。\哥德巴赫猜想是错误的\判断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 + \空白处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

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

本文来源:https://www.bwwdw.com/article/o3gv.html

Top