试题库-1

更新时间:2023-12-16 03:31:01 阅读量: 教育文库 文档下载

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

一、选择题

1. 当我们定义变量时,下列哪个是错误的?( C ) A、_sys B 、$change C 、1name D、thisOne 2. 当我们定义类名,下列哪个是错误的?( B ) A、Employee B 、3G C 、helloworld D、Hello$World 3. 在Java中,整型int存储的字节长度为( C )

A、1 B、2 C、4 D、8

4. 查看下列代码,说法正确的是( B )。 byte b = 10; short s = 200; short c = b + s;

A、程序执行正确。 B、程序编译错误。

5. 当我们定义不同类型的变量时,哪个是错误的?( A )

A、byte b = 128 B、char a = 'a' C、double d = 1.3d D、float f = 3.1415f

6. MyClass类的默认构造器是( B )

A、new MyClass() B、MyClass(){} C、MyClass{} D、public class MyClass 7. 构造函数何时被调用?( A )

A、创建对象时 B、类定义时

C、使用对象的方法时 D、使用对象的属性时

8. 下列定义数组正确的是( ABD )选三项

A、String[] s1 = new String[4]; B、String[] s2 = {\ C、String[] s4 = String[4]; D、String s3[] = new String[4]; E、char[] s5 = {null};

9. 下面定义一个String值是正确的是( BCE )选三项

A、String a = I'm a String; B、String b = \ C、String c = new String(); D、String d = 'I'm as String'; E、String e = new String(\a String\

10. 以下关于构造函数的描述错误的是( A )。 A、构造函数的返回类型只能是void型。

B、构造函数是类的一种特殊函数,它的方法名必须与类名相同。 C、构造函数的主要作用是完成对类的对象的初始化工作。 D、一般在创建新对象时,系统会自动调用构造函数。 11. 设有下面两个赋值语句:

a = Integer.parseInt(“12”); b = Integer.valueOf(“12”).intValue(); 下述说法正确的是( D )。 A、a是整数类型变量,b是整数类对象。 B、a是整数类对象,b是整数类型变量。 C、a和b都是整数类对象并且值相等。 D、a和b都是整数类型变量并且值相等。 12. 在Java中,一个类可同时定义许多同名的方法,这些方法的形式参数个数、类型或顺

序各不相 同,传回的值也可以不相同。这种面向对象程序的特性称为( C )。 A、隐藏 B、覆盖 C、重载 D、Java不支持此特性

13. 下列X,Y,和Z都是接口。下列正确的接口声明是什么( AC )。选二项 A、public interface A extends X{void aMethod();} B、interface B implements Y {void aMethod();}

C、interface C extends X,Y,Z {void aMethod();} D、interface C extends X {protected void aMethod();} 14. 以下关于继承的叙述正确的是( A )。 A、在Java中类只允许单一继承

B、在Java中一个类只能实现一个接口

C、在Java中一个类不能同时继承一个类和实现一个接口 D、在Java中接口只允许单一继承

15. 给出下面代码,关于该程序以下哪个说法是正确的?( A ) public class Person{ int arr[] = new int[5];

public static void main(String a[]) {

System.out.println(arr[0]); } } A、编译时将产生错误 B、编译时正确,运行时出错 C、输出零 D、输出空

16. 下列抽象类定义正确的是哪个?( C ) A、class MyClass { abstract void hello();} B、abstract Myclass { abstract void hello();} C、abstract class MyClass { abstract void hello();} D、abstract class MyClass { abstract void hello({System.out.println(\17. 查看下面的程序LoginApp.java class User{ private String name; private String pwd; private User(){ } }

public class LoginApp{ public static void main(String[] args){ User user = new User(); } }

以下说法正确的是( A )。 A、程序不能正确编译。 B、程序能正确编译,user是已实例化的对象。 C、程序能正确编译,user不能被实例化对象。 D、程序能正确编译。 18. 查看下列代码 public class MyClass{ private String loginName;

public static void main(String[] args) { System.out.println(this.loginName); } }

以下说法正确的是( A )。 A、程序不能正确编译。 B、程序能正确编译,打印loginName的内容。 C、程序能正确编译,不能打印loginName,因为loginName没有给初始值。 D、程序能正确编译。

19. 下列哪些是正确的( ABC )。选三项 A、private final String loginName = \ B、final int i = 0; C、static final String s = \ D、const final int m = 0; E、private final String name; 20. 下列说法正确的有( C ) A、class中的constructor不可省略。 B、 constructor必须与class同名,但方法不能与class同名。 C、 constructor在一个对象被new时执行。 D、一个class只能定义一个constructor。

21. 下面哪个流类属于面向字符的输入流( D )。 A、BufferedWriter B、FileInputStream C、ObjectInputStream D 、InputStreamReader

22. Java接口的修饰符可以为( CD )。选二项 A、private B、 protected C、 final D、 abstract 23. 当我们新建一个流对象,下面哪个选项的代码是错误的?( B ) A、new BufferedWriter(new FileWriter(\ B、new BufferedReader(new FileInputStream(\ C、new GZIPOutputStream(new FileOutputStream(\ D、new ObjectInputStream(new FileInputStream(\24. 查看下列代码: public class foo{

public static void main (String[] args){ String s1;

System.out.println(\ } }

下列哪个说法是正确的( C )。 A、 代码得到编译,并输出“s=”。 B、 代码得到编译,并输出“s=null”。

C、 由于String s没有初始化,代码不能编译通过。

D、 代码得到编译,但捕获到 NullPointException异常。

25. 下列哪种异常是检查型异常,需要在编写程序时声明 ( C ) A、NullPointerException B、ClassCastException

C、FileNotFoundException D 、IndexOutOfBoundsException

26. 选项中哪一行代码可以替换题目中//add code here而不产生编译错误?( A ) 查看下列代码:

public abstract class MyClass { public int constInt = 5; //add code here

public void method() { } }

A、public abstract void method(int a); B、 constInt = constInt + 5; C、 public int method();

D、 public abstract void anotherMethod() {}

27. importCustomerInfo()方法如下,try中可以捕获三种类型的异常,如果在该方法运行中

产生了一个IOException,将会输出什么结果( )。 查看下列代码:

public void importCustomerInfo() { try {

// do something that may cause an Exception } catch (java.io.FileNotFoundException ex) {

System.out.print(\ } catch (java.io.IOException ex) {

System.out.print(\ } catch (java.lang.Exception ex) { System.out.print(\ } }

哪个是正确的输出( A )。 A、IOException!

B、IOException!Exception!

C、FileNotFoundException!IOException!

D、FileNotFoundException!IOException!Exception! 28. 关于异常处理机制的叙述哪些正确( BC )。

A、catch部分捕捉到异常情况时,才会执行finally部分。

B、当try区段的程序发生异常时,才会执行catch区段的程序。

C、不论程序是否发生错误及捕捉到异常情况,都会执行finally部分。 D、以上都是。

29. 在Java Applet程序用户自定义的Applet子类中,一般需要重载父类的( D )方法

来完成一些画图操作。

A、start( ) B、stop( ) C、init( ) D、paint( )

30. 下面哪些是Thread类的方法( ABD )。选三项

A 、start() B、 run() C、 exit() D、 getPriority()

二、代码阅读题

1. 阅读下列代码,请写出它的运行输出结果: 程序:

public class TestOper{ public static void main(String[] args){ int i = 0; System.out.println(\ System.out.println(\ i = 0; System.out.println(\ } }

结果: i=0;(i++)=0 i=1

i=0;(++i)=1

2. 阅读下列程序,请写出运行结果并说明原因。 String str1 = \

String str2 = \System.err.println(str1 == str2); 结果: false,

因为str2中的llo是新申请的内存块,而==判断的是对象的地址而非值,所以不一样。 3. 阅读下列程序,请写出运行结果。 当i输入值为2时。

public static int getValue(int i) { int result = 0; switch (i) { case 1:

result = result + i; case 2:

result = result + i * 2; case 3:

result = result + i * 3; }

return result; } 结果:10

4. 阅读下列程序,请写出运行结果。 class HelloA {

public HelloA() {

System.out.println(\ }

{ System.out.println(\ static { System.out.println(\}

public class HelloB extends HelloA { public HelloB() {

System.out.println(\ }

{ System.out.println(\ static { System.out.println(\ public static void main(String[] args) { new HelloB(); } }

结果: static A static B I'm A class HelloA I'm B class HelloB

5. 阅读下列程序,请写出运行结果。 class Father{ public void show(){ System.out.println(\ } }

class son extends Father{ public void show(){ System.out.println(\ } }

public class Sample1 { public static void main(String[] args) { Father f1 = new son(); Father f2 = new Father(); f1.show(); f2.show(); } }

结果: sun show...

father show...

6. 阅读下列程序,请写出运行结果。 public class Sample2 { String str = new String(\ char[] ch = { 'a', 'b', 'c' }; public static void main(String[] args) { Sample2 s2 = new Sample2(); s2.change(s2.str, s2.ch);

System.out.print(s2.str + \ System.out.print(s2.ch); } public void change(String str, char ch[]) { str = \ ch[0] = 'g'; } }

结果: ok and gbc

7. 阅读下列程序,请写出运行结果。 public class StaticTest { static int x=1; int y; StaticTest() { y++; } public static void main(String[] args) { StaticTest st=new StaticTest(); System.out.println(\ System.out.println(\ st=new StaticTest(); System.out.println(\ } static { x++;} }

结果: x=2 st.y=1 st.y=1

8. 阅读下列程序,请写出结果: class Meal{ Meal(){System.out.println(\}

class Bread{ Bread(){System.out.println(\

}

class Lunch extends Meal{ Lunch(){System.out.println(\}

class PortableLunch extends Lunch{ PortableLunch(){System.out.println(\}

public class Sandwich extends PortableLunch{ private Bread b = new Bread(); public Sandwich(){ System.out.println(\ } public static void main(String[] args) { new Sandwich(); } }

结果: Meal() Lunch()

PortableLunch() Bread() Sandwich()

9. 阅读下列程序,请写出结果: abstract class Base{ public Base(int i){ System.out.println(\ } public abstract void f(); }

public class AnonymousConstructor { public static Base getBase(int i){ return new Base(i){ public void f() { System.out.println(\ } }; } public static void main(String[] args) { Base base = getBase(40); base.f(); } }

结果:

Base(),i=40 f()

10. 阅读下列程序,请写出结果:

class ThreeException extends Exception{}; public class SampleException { static int count = 0; public static void main(String[] args) { while(true){ try{ if(count++ == 0) throw new ThreeException(); System.out.println(\ }catch(ThreeException ex){ System.out.println(\ }finally{ System.out.println(\ if(count == 2) break; } } } }

结果:

ThreeException finally

No Exception. finally

三、编程题 1)

? 创建两个带有默认构造方法(空参数列表)的类ClassA和类ClassB。 ? 从ClassA中继承产生一个类名为ClassC的新类,并在ClassC内创建一个ClassB

类的成员。

? 不要给ClassC编写构造方法。创建一个ClassC类的对象。

2)

? 编写具有两个(重载)构造方法的类,并在第一个构造方法中通过this调用第二个

构造方法。

? 类名为Employee(员工)。

? 可以依员工名创建Employee对象。

? 可以依员工名、联系方式创建Employee对象。 ? main方法里实例化Employee对象。

3)

? 创建一个类,然后创建一个用你的类的对象进行过初始化的List.

? 通过使用subList()方法,创建你的List的子集,然后在你的List中移除这个子集。 4)

? 编写一个类,在其main()方法里try块里抛出一个Exception类的对象。传递一个

字符串参数给Exception的构造方法。在catch里捕获此异常对象,并且打印字符串参数。增加一个finally子句,打印一条信息以证明这里确实得到了执行。

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

Top