Java语言程序设计---期末复习试题

一、选择题(每道题只有一个正确答案,每小题2分,共30分)15道题
1、关于Java语言叙述错误的是:( C)
A.Java语言具有跨平台性
B.Java是一种面向对象的语言
C.Java语言中的类可以多继承
D.Java的垃圾收集机制自动回收程序已不再使用的对象
2、以下叙述正确的是:( B)的确台a
A.构造方法必须是public方法
B.main方法必须是public方法
C.Java应用程序的文件名可以是任意的
D.构造方法应该声明为void类型
3、关于Java中数据类型叙述正确的是:( B)
A、整型数据在不同平台下长度不同
B.boolean类型数据只有2个值,true和false
C.数组属于简单数据类型
D.Java中的指针类型和C语言的一样
4、设 int x=1,float y=2,则表达式 x / y的值是:( D)
A.0
B.1
C.2
D.以上都不是
5、以下语句有语法错的是:( A)
A.int x=1;y=2;z=3
B.for (int x=10,y=0;x>0;x++);
C.while (x>5);
6、关于类和对象的叙述正确的是:( A)
A.Java的类分为两大部分:系统定义的类和用户自定义的类
B.类的静态属性和全局变量的概念完全一样,只是表达形式不同
C.类的成员至少有一个属性和一个方法
D.类是对象的实例化
7、以下有关构造方法的说法,正确的是:( A)
A.一个类的构造方法可以有多个
B.构造方法在类定义时被调用
C.构造方法只能由对象中的其它方法调用
D.构造方法可以和类同名,也可以和类名不同
8、以下有关类的继承的叙述中,正确的是:( D)
A.子类能直接继承父类所有的非私有属性,也可通过接口继承父类的私有属性
B.子类只能继承父类的方法,不能继承父类的属性
C.子类只能继承父类的非私有属性,不能继承父类的方法
D.子类不能继承父类的私有属性
9、下列属于容器的组件有:( B)
A.JButton
B.JPane
C.Canvas
D.JTextArea
10、void的含义:( C)
A.方法体为空
B.定义的方法没有形参
C.定义的方法没有返回值
D.方法的返回值不能参加算术运算
11、关于Java中异常的叙述正确的是:( D)
A.异常是程序编写过程中代码的语法错误
B.异常是程序编写过程中代码的逻辑错误
C.异常出现后程序的运行马上中止
D.异常是可以捕获和处理的
12、所有的异常类皆继承哪一个类?( C)
A.java.io.Exception
B.java.lang.Throwable
C.java.lang.Exception
D.java.lang.Error
13、下面哪个不是java语言中的关键字?( B)
A.long
B.sizeof
C.instanceof
D.const
14、为实现进程之间的通信,需要使用下列那种流才合适?(D)
A.Data stream
B.File stream
C.Buffered stream
D.Piped stream
15、在复选框中移动鼠标,然后单击一选项,要捕获所选项必需实现哪个接口?(D)
A.ActionListener
B.MouseListener
C.MouseMotionListern
D.ItemListener
二、填空题(每空1分,共20分)
1、面向对象程序设计所具有的基本特征是:抽象性,封装性__,继承性_,多态性_
2、数组x定义如下
int x[ ][ ]=new int[3][2]
则 x…length的值为____3________,
x[0].length的值为_____2_______。
3、Java中实现多线程一般使用两种方法,一是___继承Thread类_________,二是_____实现Runnable方法__________
4、Java的图形用户界面设计中,有很多布局管理器用来摆放组件的位置,一般用到的布局管理器有(列出四种即可)FlowLayout___,GridLayout__,BorderLayout___,CardLayout____
5、Applet常用的方法是:__init()_、run()、__stop()和destroy()。
三、阅读程序,写出程序的输出结果(每题5分,共20分)
1、class A{
private int privateVar;
A(int privateVar){
privateVar=privateVar;
   }
   boolean isEqualTo(A anotherA){
    if(this.privateVar == anotherA.privateVar)
    return true;
    else
    return false;
   }
}
public class B{
public static void main(String args[]){
A a = new A(1);
A b = new A(2);
System.out.println(a.isEqualTo(b));
   }
}
程序的输出结果为:false
2、class A {
double f(double x, double y) {
return x * y;
}
}
class B extends A {
double f(double x, double y) {
return x + y;
}
}
public class Test {
public static void main(String args[]) {
B obj = new B();
System.out.println("The program output is " + obj.f(4, 6));
}
}
程序的输出结果为: The program output is 10
3、public class Unchecked {
public static void main(String[] args) {
try {
method();
} catch (Exception e) {
System.out.println(“A”);
} finally {
System.out.println(“B”);
}
}
static void method() {
try {
wrench();
System.out.println(“C”);
} catch (ArithmeticException e) {
System.out.println(“D”);
} finally {
System.out.println(“E”);
}
System.out.println(“F”);
}
static void wrench() {
throw new NullPointerException();
}
}
程序的输出结果为:
E
A
B
4、public class Test {
public static void main(String[] args) {
int x;
int a[] = { 0, 0, 0, 0, 0, 0 };
calculate(a, a[5]);
System.out.println("the value of a[0] is " + a[0]);
System.out.println("the value is a[5] is " + a[5]);
}

static int calculate(int x[], int y) {
for (int i = 1; i < x.length; i++)
if (y < x.length)
x[i] = x[i - 1] + 1;
return x[0];
}
}
程序的输出结果为:
the value of a[0] is 0
the value is a[5] is 5
一、单选择题(每小题2分,共10分)
1、编译Java Application 源程序文件将产生相应的字节码文件,这些字节码文件的扩展名为( )。
A…java B. .class
C…html D. .exe
2、设 x = 1 , y = 2 , z = 3,则表达式 y+=z--/++x 的值是( )。
A. 3 B. 3. 5
C. 4 D. 5
3、在Java Applet程序用户自定义的Applet子类中,一般需要重载父类的( )方法来完成一些画图操作。
A. start( ) B. stop( )
C. init( ) D. paint( )
4、不允许作为类及类成员的访问控制符的是( )。
A. public B. private
C. static D. protected
5、为AB类的一个无形式参数无返回值的方法method书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为( )。
A. static void method( ) B. public void method( )
C. final void method( ) D. abstract void method( )

二、填空题(每空格1分,共20分)
1、开发与运行Java程序需要经过的三个主要步骤为 、
和 。
2、如果一个Java Applet源程序文件只定义有一个类,该类的类名为MyApplet,则类MyApplet必须是 类的子类并且存储该源程序文件的文件名为 。
3、如果一个Java Applet程序文件中定义有3个类,则使用Sun公司的JDK编译
器 编译该源程序文件将产生 个文件名与类名相同而扩展名为 的字节码文件。
4、在Java的基本数据类型中,char型采用Unicode编码方案,每个Unicode码占
用 字节内存空间,这样,无论是中文字符还是英文字符,都是占
用 字节内存空间。
5、设 x = 2 ,则表达式 ( x + + )/3 的值是 。
6、若x = 5,y = 10,则x < y和x >= y的逻辑值分别为 和 。
7、 方法是一种仅有方法头,没有具体方法体和操作实现的方法,该方法必须在抽象类之中定义。 方法是不能被当前类的子类重新定义的方法。
8、创建一个名为 MyPackage 的包的语句是 ,
该语句应该放在程序的位置为: 。
9、设有数组定义:int MyIntArray[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70}; 则执行以下几个语句后的输出结果是 。
int s = 0 ;
for ( int i = 0 ; i < MyIntArray.length ; i + + )
if ( i % 2 = = 1 ) s += MyIntArray[i] ;
System.out.println( s );
10、在Java程序中,通过类的定义只能实现 重继承,但通过接口的定义可以实现 重继承关系。

三、写出下列程序完成的功能。(每小题5分,共20分)

1、public class Sum
{ public static void main( String args[ ])
{ double sum = 0.0 ;
for ( int i = 1 ; i <= 100 ; i + + )
sum += 1.0/(double) i ;
System.out.println( “sum=”+sum );
}
}

2、 import java.io.* ;
public class Reverse
{ public static void main(String args[ ])
{ int i , n =10 ;
int a[ ] = new int[10];
for ( i = 0 ; i < n ; i ++ )
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
a[i] = Integer.parseInt(br.readLine( )); // 输入一个整数
} catch ( IOException e ) { } ;
for ( i = n-1 ; i >= 0 ; i ―― )
System.out.print(a[i]+" ");
System.out.println( );
}
}

3、 import java.awt.*;
public class abc
{ public static void main(String args[])
{ new FrameOut(); }
}
class FrameOut extends Frame // Frame为系统定
{ Button btn; // 义的窗框类
FrameOut( )
{ super(“按钮”);
btn = new Button(“按下我”);
setLayout(new FlowLayout( ));
add(btn);
setSize(300,200);
show( );
}
}

4、import java.io.*;
public class abc
{ public static void main(String args[])
{ SubClass sb = new SubClass( );
System.out.println(sb.max( ));
}
}
class SuperClass
{ int a = 10 , b = 20 ; }
class SubClass extends SuperClass
{ int max( ) { return ((a>b)?a:b); } }

四、写出下面程序的运行结果(每小题10分,共30分)
1、 import java.io.*;
public class abc
{ public static void main(String args[ ])
{ AB s = new AB(“Hello!”,“I love JAVA.”);
System.out.println(s.toString( ));
}
}
class AB {
String s1;
String s2;
AB( String str1 , String str2 )
{ s1 = str1; s2 = str2; }
public String toString( )
{ return s1+s2;}
}

2、 import java.io.* ;
public class abc
{
public static void main(String args[ ])
{ int i , s = 0 ;
int a[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 };
for ( i = 0 ; i < a.length ; i ++ )
if ( a[i]%3 = = 0 ) s += a[i] ;
System.out.println(“s=”+s);
}
}

3、import java.io.* ;
public class abc
{
public static void main(String args[ ])
)
{ System.out.println(“a=”+a+"\nb="+b); }
}
class SubClass extends SuperClass
{ int c;
SubClass(int aa,int bb,int cc)
{ super(aa,bb);
c=cc;
}
}
class SubSubClass extends SubClass
{ int a;
SubSubClass(int aa,int bb,int cc)
{ super(aa,bb,cc);
a=aa+bb+cc;
}
void show()
{ System.out.println(“a=”+a+"\nb="+b+"\nc="+c); }
}

扫描二维码关注公众号,回复: 12426791 查看本文章

五、使用Java语言编写程序。(每小题10分,共20分)

1、编写一个字符界面的Java Application 程序,接受用户输入的10个整数,并输出这10个整数的最大值和最小值。

2、编写一个完整的Java Applet 程序使用复数类Complex验证两个复数 1+2i 和3+4i 相加产生一个新的复数 4+6i 。
复数类Complex必须满足如下要求:
(1) 复数类Complex 的属性有:
RealPart : int型,代表复数的实数部分
ImaginPart : int型,代表复数的虚数部分
(2) 复数类Complex 的方法有:
Complex( ) : 构造函数,将复数的实部和虚部都置0
Complex( int r , int i ) : 构造函数,形参 r 为实部的初值,i为虚部的初值。
Complex complexAdd(Complex a) : 将当前复数对象与形参复数对象相加,所得的结果仍是一个复数值,返回给此方法的调用者。
String ToString( ) : 把当前复数对象的实部、虚部组合成 a+bi 的字符串形式,其中a 和 b分别为实部和虚部的数据。

《JAVA语言程序设计》期末考试模拟试题
参考答案及评分标准

一、单选择题(每小题2分,共10分)
1、B 2、A 3、D 4、C 5、A

二、填空题(每空格1分,共20分)
1、编辑源程序、编译生成字节码、解释运行字节码
2、Applet、MyApplet
3、javac.exe、3、 . class
4、2 、2
5、0
6、true 、 false
7、抽象(abstract)方法、最终(final)方法
8、package MyPackage ; 应该在程序第一句。
9、120
10、单、多

三、写出下列程序完成的功能。(每小题5分,共20分)
1、计算 1/1+1/2+1/3+…+1/100 的值。
2、从标准输入(即键盘)读入10个整数存入整型数组a中,然后逆序输出这10个整数。
3、创建一个标题为"按钮"的窗框,窗框中显示有"按下我"字样的按钮。
4、求两个数的最大值。

四、写出下面程序的运行结果(每小题10分,共30分)
1、Hello! I love JAVA.
2、s = 180
3、a=60
b=20
c=30

五、使用Java语言编写程序。(每小题10分,共20分)
1、参考程序如下:
import java.io.* ;
public class abc
{
public static void main(String args[ ])
{ int i , n = 10 , max = 0 , min = 0 , temp = 0;
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
max = min = Integer.parseInt(br.readLine( ));
} catch ( IOException e ) { } ;
for ( i = 2 ; i <= n ; i ++ ) {
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
temp = Integer.parseInt(br.readLine( ));
if (temp > max ) max=temp;
if (temp < min) min=temp;
} catch ( IOException e ) { } ;
}
System.out.println(“max=”+max+"\nmin="+min);
}
}

2、参考程序如下:
import java.applet.* ;
import java.awt.* ;
public class abc extends Applet
{
Complex a,b,c ;
public void init( )
{
a = new Complex(1,2);
b = new Complex(3,4);
c = new Complex();
}

public void paint(Graphics g)
{
c=a.complexAdd(b);
g.drawString(“第一个复数:”+a.toString(),10,50);
g.drawString(“第二个复数:”+b.toString(),10,70);
g.drawString(“两复数之和:”+c.toString(),10,90);
}
}

class Complex
{
int RealPart ; // 复数的实部
int ImaginPart ; // 复数的虚部
Complex() { RealPart = 0 ; ImaginPart = 0 ; }
Complex(int r , int i)
{ RealPart = r ; ImaginPart = i ; }
Complex complexAdd(Complex a)

{
Complex temp = new Complex( ); // 临时复数对象
temp.RealPart=RealPart+a.RealPart;
temp.ImaginPart=ImaginPart+a.ImaginPart;
return temp;
}
public String toString( )
{ return ( RealPart+" + “+ImaginPart+” i "); }
1
2
3
4
5
6
7
8
}

一、 判断题
2、Java的源代码中定义几个类,编译结果就生成几个以“.class”后缀的字节码文件。(Y)
3、Java Applet是由独立的解释器程序来运行的。(N)
4、Java Application是由独立的解释器程序来运行的。(Y)
5、Java Applet的字节码文件必须嵌入HTML的文件中并由负责解释HTML文件的WWW浏览器充当解释器来解释运行。(Y)
7、Java源程序是由类定义组成的,每个程序可以定义若干个类,但只有一个类是主类。(Y)
8、Java中数组的元素只能是简单数据类型的量。(N)
9、Vector类中的对象不能是简单数据类型。(Y)
10、Java的String类的对象既可以是字符串常量,也可以是字符串变量。(N)
11、Java中用户自定义的图形界面元素也可以响应用户的动作,具有交互功能。(N)
12、Java的屏幕坐标是以像素为单位,容器的左上角被确定为坐标的起点。(Y)
13、在Java中,并非每个事件类都只对应一个事件。(Y)
14、一个类只能有一个父类,但一个接口可以有一个以上的父接口。(Y)
15、由继承性可知,程序中子类拥有的成员数目一定大于等于父类拥有的成员数目。(N)
16、Java语言中的数组元素下标总是从0开始,下标可以是整数或整型表达式。(N)
17、注释的作用是使程序在执行时在屏幕上显示//之后的内容。(N)
18、Java的字符类型采用的是Unicode编码,但所占字节由具体软硬件环境决定。(N)
19、所有的鼠标事件都由MouseListener监听接口的监听者来处理。(N)
20、Applet是一种特殊的Panel,它是Java Applet程序的最外层容器。(N)
二、 填空题
1、根据结构组成和运行环境的不同,Java程序可分为两类:
  Java Applet        、
  Java application       。
2、类的修饰符分为 abstract 、 public 。
3、程序中定义类使用的关键字是 class ,每个类的定义由类头定义、类体定义两部分组成,其中类体部分包括 属性 、 方法 。
4、Java程序中定义接口所使用的关键字是 interface,接口中的属性都是 静态变量 ,接口中的方法都是 抽象方法 。
5、 Object 是Java程序中所有类的直接或间接父类,也是类库中所有类的父类。
三、 选择题
1、 下列哪一项不是域的非访问控制符。 ( D)
(A) static (B) final © volatile (D) public
2、 关于被私有保护访问控制符private protected修饰的成员变量,以下说法正确的是(B )
(A) 可以被三种类所引用:该类自身、与它在同一个包中的其他类、在其他包中的该类的子类
(B)可以被两种类访问和引用:该类本身、该类的所有子类
©只能被该类自身所访问和修改
(D)只能被同一个包中的类访问
3、 关于被保护访问控制符protected修饰的成员变量,以下说法正确的是(A )
(A) 可以被三种类所引用:该类自身、与它在同一个包中的其他类、在其他包中的该类的子类
(B) 可以被两种类访问和引用:该类本身、该类的所有子类
© 只能被该类自身所访问和修改
(D) 只能被同一个包中的类访问
4、下列关于修饰符混用的说法,错误的是( D)
(A) abstract不能与final并列修饰同一个类
(B) abstract中不可以有private的成员
© abstract方法必须在abstract类中
(D) static方法中能处理非static的属性
5、容器Panel和Applet缺省使用的布局编辑策略是(B )
(A) BorderLayout (B) FlowLayout © GridLayout (D) CardLayout
6、以下标识符中哪项是不合法的 (D)
(A) BigOlLong$223 (B) _utfint © $12s (D) 3d
7、main方法是Java Application程序执行的入口点,关于main方法的方法头以下哪项是合法的( B)
(A) public static void main
(B) public static void main(String[] args)
© public static int main(String[] args)
(D) public void main(String args[])
8、行完以下代码int[] x = new int[5];后,以下哪项说明是正确的(A )
(A) x[4]为0 (B) x[4]未定义 © x[5]为0 (D) x[0]为空
9、以下代码段执行后的输出结果为( C)
int x=3; int y=8;System.out.println(y%x);
(A) 0 (B) 1 © 2 (D) 3
10、以下哪个表达式是不合法的( B)
(A) String x = “Sky”; int y = 5; x += y;
(B) String x = “Sky”; int y = 5; if(x==y){}
© String x = “Sky”; int y=5; x=x+y;
(D) String x = null; int y=(x!=null) && (x.length()>0)?x.length:0
11、编译运行以下程序后,关于输出结果的说明正确的是 ( C)
public class Conditional{
public static void main(String args[]){
int x = 2;
System.out.println(“value is “((x<1)?2.2:2));;
}
}
(A) 输出结果为:value is 22.2 (B) 输出结果为:value is 2
© 输出结果为:value is 2.0 © 编译错误
12、以下声明合法的是( B)
(A) default String s; (B) public final static native int w()
© abstract double d; © abstract final double hyperbolicCosine()
13、关于以下application的说明,正确的是( C)

class StaticStuff
{
static int x=15;
static {x*=3;}
public static void main(String args[])
{
System.out.println(“x=”+x);
}
static {x/=3;}
}
(A) 4行与9行不能通过编译,因为缺少方法名和返回类型
(B) 9行不能通过编译,因为只能有一个静态初始化器
© 编译通过,执行结果为:x=15
(D) 编译通过,执行结果为:x=3
14、关于以下程序代码的说明正确的是( C)
class HasStatic{
private static int x = 100;
public static void main(String args[]){
HasStatic hs1 = new HasStatic();
hs1.x++;
HasStatic hs2 = new HasStatic();
hs2.x++;
hs1=new HasStatic();
hs1.x++;
System.out.println(“x = ”+x);
}
}
(A) 5行不能通过编译,因为引用了私有静态变量
(B) 10行不能通过编译,因为x是私有静态变量
© 程序通过编译,输出结果为: x=103
(D) 程序通过编译,输出结果为:x=100
15、以下选项中循环结构合法的是( C)
(A) while(int i<7){
i++;
System.out.println(“i is ”+i);
}
(B) int j=3;
while(j){
System.out.println(“ j is ”+j);
}
© int j=0;
for(int k=0;j+k!=10;j++,k++){
System.out.println(“j is ”+j+ “ k is ”+k );
}
(D) int j=0;
do{
System.out.println(“j is ”+j++);
if (j==3){continue loop;}
}while(j<10);
16、类Text1定义如下:(B)
public class Test1{
public float aMethod(Float a, float b){}
}
将以下哪种方法插入行 *** 是不合法的。( )
(A) public float aMethod(float a,float b,float c){}
(B) public float aMethod(float c,float d){}
© public int aMethod(int a,int b){}
(D) public float aMethod(int a,int b,int c){}
17、创建字符串s: s=new String(“xyzy”);以下哪条语句将改变s( D)
(A) s.append(“a”) (B) s.concat(s)
© s.substring(3) (D) 以上语句都不会
18、关于以下程序段,正确的说法是( B)
String s1 = “ac”+ “def”;
Strimg s2 = new String(s1);
if(s1.equals(s2))
System.out.println(“succeeded”);
if(s1s2)
System.out.println(“.equals() succeeded”);
(A) 行4与行6都将执行 (B)行44执行,行6不执行
© 行6执行,行4不执行 (D) 行4、行6都不执行
19、关于以下代码段的说法正确的是(D )
String s = “abcde”;
StringBuffer s1 = new StringBuffer(“abcde”);
if(s.equals(s1))
s1 = null;
if(s1.equals(s))
s=null;
(A) 第1行编译错误,String的构造器必须明确调用
(B) 第3行编译错误,因为s1与s2有不同的类型
© 编译成功,但执行时在第5行有异常抛出
(D) 编译成功,执行过程中也没有异常抛出
20、以下说法哪项是正确的( A)
class MyListener extends MouseAdapter implements MouseListener{
public void mouseEnter(MouseEvent mev){
System.out.println(“Mouse entered.”);
}
}
(A) 以上代码可通过编译
(B) 不能通过编译,因为没有实现MouseListener接口中的所有方法
© 不能通过编译,因为类头定义不能分行
(D) 能通过编译,若组件用该类作为Mouse的监听者并且接收了mouse-exited事件,则在执行过程中会抛出异常
21、关于以下程序段的说法,正确的是( A)

class MyListener implements
ActionListener,ItemListener{
public void actionPerformed(ActionEvent ae){
System.out.println(“Action”);}
1
public void itemStateChanged(ItemEvent ie){
System.out.println(“Item”);
1
}
1
}
(A) 可通过编译
(B) 第2行产生编译错误
© 第3行产生编译错误
(D) 第5行产生编译错误
22、通过调用new JList(10,false)创建一个列表,关于该列表的几个说法哪项是错误的。( D)
(A) 该列表不支持复选
(B) 该列表一次可显示10个选项
© 根据需要该列表可能有垂直滚动条
(D) 该列表有10个选项
23、以下哪项可能包含菜单条( B)
(A) JPanel (B) JFrame © Applet (D) JDialog
24、以下代码完成画线功能,指出所画线的颜色( C)
g.setColor(Color.red.green.cyan.yellow);
g.drawLine(0,0,100,100);
(A) red (B) green © yellow (D) cyan
25、关于以下代码所画图形的说明,正确的是( B)
1.g.setColor(Color.black);
g.drawLine(10,10,10,50);
g.setcolor(color.red);
g.drawRect(100,100,150,150);
(A) 一条40像素长的垂直红线,一个边长为150像素的红色四方形
(B) 一条40像素长的垂直黑线,一个边长为150像素的红色四方形
© 一条40像素长的垂直黑线,一个边长为150像素的红色四方形
(D) 一条40像素长的垂直红线,一个边长为150像素的红色四方形
四、 程序阅读
1、阅读以下程序,输出结果为 。(21)
class D{
public static void main(String args[])
int d=21;
Dec dec=new Dec( );
dec.decrement(d);
System.out.println(d);
}
classs Dec{
public void decrement(int decMe){
decMe = decMe - 1;
}
}
2、以下程序的输出结果为 。(结果为: Boy)
public class Short{
public static void main(String args[ ]) {
StringBuffer s = new StringBuffer(“Boy”);
if((s.length( ))&& (s.append(“男孩”) . equals(“False”)))
;
System.out.println(“结果为: ”+s);
}
}
3、以下程序段的输出结果为 。(Message four)
int x=0,y=4, z=5;
if ( x>2){
if (y<5){
System.out.println(“Message one”);
}
else {
System.out.println(“Message two”);
}
}
else if(z>5){
System.out.println(“Message three”);
}
else {
System.out.println(“Message four”);
}
4、以下程序段的输出结果为 。(two.three.)
int j=2;
switch ( j ) {
case 2:
System.out.print(“two.”);
case 2+1 :
System.out.println(“three.”);
break;
default:
System.out.println(“value is “+j);
break;
}
5、以下程序段的输出结果为 。(int)
class Cruncher{
void crunch( int i ){
System.out.println(“int”);
}
void crunch(String s){
System.out.println(“String”);
}
public static void main(String args[ ]){
Cruncher crun=new Cruncher ( );
char ch=’p’;
crun.crunch(ch);
}
}
6、阅读以下程序,写出输出结果 。(99)
class Q6{
public static void main(String args[ ]){
Holder h=new Holder( );
h.held=100;
h.bump(h);
System.out.println(h.held); }
}
class Holder{
public int held;
public void bump(Holder theHolder){
theHolder.held - -;
}
}
7、阅读以下程序,请写出输出结果 。(true)
public class EqualsMethod {
public static void main(String[] args) {
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1.equals(n2));
}
}
1、 Java application中的主类需包含main方法,main方法的返回类型是什么?( D )
A、int B、float C、double D、void
2、 有以下方法的定义,请选择该方法的返回类型( D )。
ReturnType method(byte x, double y)
{
return (short)x/y2;
}
A、byte B、short C、int D、double
3、 在编写Java Applet程序时,若需要对发生的事件作出响应和处理,一般需要在程序的开头写上( D )语句。
A、import java.awt. ; B、import java.applet.* ;
C、import java.io.* ; D、import java.awt.event.* ;
4、 容器Panel和Applet缺省使用的布局编辑策略是( A )
A、BorderLayout B、FlowLayout C、GridLayout D、CardLayout
5、 以下哪个不是Java的关键字?( A )
A、TRUE B、const C、super D、void
6、 有程序如下,关于程序的描述哪个是正确的?( A )
public class Person{
i. static int a[] = new int[10];
ii. public static void main(String a[])
iii. {   System.out.println(arr[1]);  }
}
A、编译将产生错误 B、编译时正确,但运行时将产生错误
C、正确,输出0 D、正确,输出 null
7、 以下关于构造函数的描述错误的是( A )。
A、构造函数的返回类型只能是void型。
B、构造函数是类的一种特殊函数,它的方法名必须与类名相同。
C、构造函数的主要作用是完成对类的对象的初始化工作。
D、一般在创建新对象时,系统会自动调用构造函数。
8、 有语句String s=”hello world”; ,以下操作哪个是不合法的?( )
A、int i=s.length(); B、s>>>=3; C、String ts=s.trim(); D、String t=s+”!”;
9、 有程序片段如下,以下哪个表示式的结果为true?( A )
Float s=new Float(0.1f);
Float t=new Float(0.1f);
Double u=new Double(0.1);
A、s==t B、s.equals(t) C、u.equals(s) D、t.equals(u)
10、 欲构造ArrayList类的一个实例,此类继承了List接口,下列哪个方法是正确的 ?(B)
A、ArrayList myList=new Object()
B、List myList=new ArrayList()
C、ArrayList myList=new List()
D、List myList=new List();
11、 paint()方法使用哪种类型的参数? (A)
A、Graphics B、Graphics2D C、String D、Color
12、 指出正确的表达式 (C)
A、 byte=128; B、Boolean=null; C、 long l=0xfffL; D、 double=0.9239d;
13、 指出下列程序运行的结果 (B)
public class Example{
String str=new String(“good”);
  char[]ch={‘a’,‘b’,‘c’};
  public static void main(String args[]){
    Example ex=new Example();
    ex.change(ex.str,ex.ch);
    System.out.print(ex.str+" and “);
    Sytem.out.print(ex.ch);
  }
  public void change(String str,char ch[]){
    str=“test ok”;
    ch[0]=‘g’;
  }
}
A、 good and abc B、good and gbc C、 test ok and abc D、test ok and gbc
14、 运行下列程序, 会产生什么结果 (A)
public class X extends Thread implements Runable{
 public void run(){
System.out.println(“this is run()”);
 }
 public static void main(String args[])
 {
  Thread t=new Thread(new X());
  t.start();
 }
}
A、 第一行会产生编译错误 B、 第六行会产生编译错误
C、 第六行会产生运行错误 D、 程序会运行和启动
15、 要从文件” file.dat"文件中读出第10个字节到变量C中,下列哪个方法适合? (B)
A、FileInputStream in=new FileInputStream(“file.dat”); in.skip(9); int c=in.read();
B、FileInputStream in=new FileInputStream(“file.dat”); in.skip(10); int c=in.read();
C、FileInputStream in=new FileInputStream(“file.dat”); int c=in.read();
D、RandomAccessFile in=new RandomAccessFile(“file.dat”); in.skip(9); int c=in.readByte();
16、 容器被重新设置大小后,哪种布局管理器的容器中的组件大小不随容器大小的变化而改变? (B)
CardLayout
FlowLayout
BorderLayout
GridLayout
17、 给出下面代码: (C)
public class Person{
static int arr[] = new int[10];
  public static void main(String a[])
  {
   System.out.println(arr[1]);
  }
}

那个语句是正确的?
A、 编译时将产生错误; B、编译时正确,运行时将产生错误;
C、 输出零; D、输出空。
18、 下列哪些语句关于内存回收的说明是正确的? (B)
A、 程序员必须创建一个线程来释放内存;
B、 内存回收程序负责释放无用内存
C、 内存回收程序允许程序员直接释放内存
D、 内存回收程序可以在指定的时间释放内存对象
19、 下列代码哪几行会出错: (C)

public void modify() {
int I, j, k;
I = 100;
while ( I > 0 ) {
j = I * 2;
System.out.println (" The value of j is " + j );
k = k + 1;
I–;
}
}
A、 line 4 B、 line 6 C、 line 7 D、 line 8
一、 程序阅读
1、 阅读以下程序:
class Exp1
{
public static void main(String[] args)
{
String s,s1="";
char c;
s=args[0];
for (int i=0;i<s.length();i++)
{ c=s.charAt(i);
if(c>=‘a’ && c<=‘z’){
s1=s1+Character.toUpperCase©;
}else{
s1=s1+Character.toLowerCase©; }
}
System.out.println(s1);
}
}
若在dos命令行输入:java Exp1 cHINA,则输出为 China 。
2、 阅读以下程序:
import java.io.* ;
public class AboutFile{
public static void main(String[ ] args)throws IOException{
BufferedReader b=new BufferedReader (new InputStreamReader(System.in));
String s;
System.out.flush();
s=b.readLine();
System.out.println(“String is:”+s);
}
}
运行以上程序,若从键盘输入:
java<回车>
则输出结果为 String is: java 。
3、 以下程序段的输出结果为 。
public class C
{
public static void main(String args[ ]){
int i , j ;
int a[ ] = { 5,1,3,9,7};
for ( i = 0 ; i < a.length-1; i ++ ) {
int k = i;
for ( j = i ; j < a.length ; j++ )
if ( a[j]<a[k] ) k = j;
int temp =a[i];
a[i] = a[k];
a[k] = temp;
}
for ( i =0 ; i<a.length; i++ )
System.out.print(a[i]+" ");
System.out.println( );
}
}
4、 阅读以下程序,写出输出结果 Animal Cat 。
class Animal {
Animal() {
System.out.print ("Animal "); }
}
public class Cat extends Animal {
Cat() {
System.out.print ("Cat "); }
public static void main(String[] args) {
Cat kitty= new Cat(); }
}
5、 阅读以下程序,输出结果为 1 。
import java.io.;
public class ATest{
public static void main(String args[]) {
SubClass sb = new SubClass( );
System.out.println(sb.funOfMod( )); }
}
class SuperClass{
int a = 10 , b = -3 ;
}
class SubClass extends SuperClass{
int funOfMod( ) { return a%b; }
}
6、 以下程序段的输出结果为 int, int 。
class Cruncher{
void crunch( int i ){
System.out.print(“int”); }
void crunch(String s){
System.out.print(“String”); }
public static void main(String args[ ]){
Cruncher crun=new Cruncher ( );
char ch=’h’;
int i=12;
crun.crunch(ch);//应该是返回ASCII码的值
System.out.print(“,”);
crun.crunch(i); }
}
7、 阅读以下程序,输出结果为 cooljava 。
import java.io.;
public class TestString
{ public static void main(String args[ ])
{ StringC s = new StringC (“cool”,“java”);
System.out.println(s); }
}
class StringC {
String s1;
String s2;
StringC( String str1 , String str2 )
{ s1 = str1; s2 = str2; }
public String toString( )
{ return s1+s2;}
}
8、 写出以下程序的运行结果为 s1!=s2 。
class StringTest1
{
public static void main(String[] args)
{
String s1=“hello”;
String s2=new String(“hello”);
if(s1s2){
System.out.println(“s1s2”);
}else{
System.out.println(“s1!=s2”);}
}
}
1、 编译Java Applet 源程序文件产生的字节码文件的扩展名为( B )。
A、java B、class C、html D、exe
2、 Java application中的主类需包含main方法,main方法的返回类型是什么?( D )
A、int B、float C、double D、void
3、 当浏览器暂时离开含applet 程序的页面时,以下选项中的哪个方法将被执行?( )
A、init() B、start() C、destroy() D、stop()
C、Applet D、Dialog
4、 以下标识符中哪项是不合法的( A )
A、const B、$double C、hello
B、D、BigMeaninglessName
5、 以下哪项可能包含菜单条( B )
A、Panel B、Frame C、Applet D、Dialog
6、 以下关于构造函数的描述错误的是( )。
A、构造函数的返回类型只能是void型。
B、构造函数是类的一种特殊函数,它的方法名必须与类名相同。
C、构造函数的主要作用是完成对类的对象的初始化工作。
D、一般在创建新对象时,系统会自动调用构造函数。
7、 以下关于继承的叙述正确的是( A )。
A、在Java中类只允许单一继承
B、在Java中一个类只能实现一个接口
C、在Java中一个类不能同时继承一个类和实现一个接口
D、在Java中接口只允许单一继承
8、 在编写Java Applet程序时,若需要对发生的事件作出响应和处理,一般需要在程序的开头写上( D )语句。
A、import java.awt.* ;
B、B、import java.applet.* ;
C、import java.io.* ;
D、import java.awt.event.* ;
9、 下列哪些语句关于Java内存回收的说明是正确的? ( B )
A、程序员必须创建一个线程来释放内存
B、内存回收程序负责释放无用内存
C、内存回收程序允许程序员直接释放内存
D、内存回收程序可以在指定的时间释放内存对象
10、 有以下方法的定义,请选择该方法的返回类型( D )。
ReturnType method(byte x, double y)
{
return (short)x/y2;
}
A、byte B、short C、int D、double
11、 有以下程序片段,下列哪个选项不能插入到行1。( D )
1.
2.public class Interesting{
3.//do sth
4. }
A、import java.awt.; B、package mypackage;
C、class OtherClass{ } D、public class MyClass{ }
12、 以下哪项是接口的正确定义?( B )
A、 interface B
{ void print() { } ;}
B、 abstract interface B
{ void print() ;}
C、 abstract interface B extends A1,A2 //A1、A2为已定义的接口
{ abstract void print(){ };}
D、 interface B
{ void print();}
13、 下面哪个函数是public void aMethod(){…}的重载函数?( D )
A、void aMethod( ){…}
B、public int aMethod(){…}
C、public void aMethod ( ){…}
D、public int aMethod ( int m){…}
14、 A派生出子类B ,B派生出子类C,并且在Java源代码中有如下声明:

A a0=new A();
A a1 =new B();
A a2=new C();
问以下哪个说法是正确的? ( )
A、只有第1行能通过编译
B、第1、2行能通过编译,但第3行编译出错
C、第1、2、3行能通过编译,但第2、3行运行时出错
D、第1行、第2行和第3行的声明都是正确的
15、 假设A类有如下定义,设a是A类的一个实例,下列语句调用哪个是错误的?( D )
class A
{ int i;
static String s;
void method1() { }
static void method2() { }
}
A、System.out.println(a.i);B、a.method1(); C、A.method1(); D、A.method2()
16、 有如下代码段:
{ if(unsafe()){//do something…}
else if(safe()){//do the other…}
}
其中,方法unsafe() 将抛出IOException, 请问可将以下哪项填入第1行?( D )
A、 public IOException methodName()
B、 public void methodName()
C、public void methodName() throw IOException
D、public void methodName() throws IOException
一、 程序阅读
1、 写出以下程序的运行结果。
public class EqualOrNot
{ public static void main(String[] args)
{ B b1=new B(5);
B b2=new B(5);
System.out.println(b1==b2);
System.out.println(b1.equals(b2));
}
}
class B
{ int x;
B( int y){ x=y; }
}
2、 写出以下程序的运行结果。
import java.io.;
public class TestString
{ public static void main(String args[ ])
{ StringC s = new StringC (“hello “,“world!”);
System.out.println(s); }
}
class StringC {
String s1;
String s2;
StringC( String str1 , String str2 )
{ s1 = str1; s2 = str2; }
public String toString( )
{ return s1+s2;}
}
3、 写出以下程序的功能。
import java.io.;
public class C {
public static void main(String[] args) throws IOException {
File inputFile = new File(“a.txt”);
File outputFile = new File(“b.txt”);
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read() ) != -1) out.write©;
in.close();
out.close(); }
}
4、 写出以下程序的功能。
class Test
{ public static void main(String[] args)
{ String s;
char c;
int upper,lower;
upper=lower=0;
s=args[0];
for (int i=0;i<s.length();i++)
{ c=s.charAt(i);
if(c>=‘a’ && c<=‘z’) lower++;
if(c>=‘A’ && c<=‘Z’) upper++; }
System.out.println(upper+”,”+lower); }
}
5、 写出以下程序的运行结果。
import java.util.*;
public class Vec{
public static void main(String[] args) {
String[] s;
s=new String[2];
s[0]=new String(“no1”);
s[1]=new String(“no2”);
Vector v = new Vector();
for(int i = 0; i <2; i++) v.addElement(s[i]);
v.insertElementAt(new String(“no3”),2);
Enumeration e = v.elements();
while(e.hasMoreElements()) System.out.print(e.nextElement()+" “);
System.out.println();
v.removeElement(“no2” );
for(int i = 0; i < v.size() ; i++) System.out.print(v.elementAt(i) +” ");
System.out.println(); }
}
6、 写出以下程序的运行结果。
class StaticTest {
static int x=1;
int y;
StaticTest()
{ y++; }
public static void main(String args[ ]){
StaticTest st=new StaticTest();
System.out.println(“x=” + x);
System.out.println(“st.y=” + st.y);
st=new StaticTest();
System.out.println(“st.y=” + st.y);
}
static { x++;}
}
7、 阅读以下程序,写出输出结果。
class First{
public First(){
aMethod(); }
public void aMethod(){
System.out.println(“in First class”);}
}
public class Second extends First{
public Second(){
aMethod(); }
public void aMethod(){
System.out.println(“in Second class”);}
public static void main(String[ ] args){
new Second( ); }
}
8、 写出以下程序的运行结果。
public class A
{
public static void main(String[ ] args)
{ System.out.println( test(15,26,4) ); }

static int test(int x, int y, int z)
{ return test( x, test(y,z) ); }

static int test(int x,int y)
{ if(x>y) return x;
else return y; }
}
1
2
3
4
9、 写出以下程序的运行结果。
class MyException extends Exception{
public String toString( ){ return “negative”; }
}
public class ExceptionDemo{
public static void mySqrt(int a) throws MyException {
if( a<0 ) throw new MyException();
System.out.println(Math.sqrt(a));
}
public static void main( String args[] ){
try{ mySqrt(25 ); mySqrt(-5 ); }
catch( MyException e ){ System.out.println("Caught “+e); }
}
}
10、 写出以下程序的功能。
public class TestArray
{ public static void main(String args[ ]){
int i , j ;
int a[ ] = {1,3,2,5,4};
for ( i = 0 ; i < a.length-1; i ++ ) {
int k = i;
for ( j = i ; j < a.length ; j++ )
if ( a[j]>a[k] ) k = j;
int temp =a[i];
a[i] = a[k];
a[k] = temp; }
for ( i =0 ; i<a.length; i++ )
System.out.print(a[i]+” ");
System.out.println( );
}
}

猜你喜欢

转载自blog.csdn.net/qq_44528283/article/details/111768335