java考试习题及答案

来源:文书网 2.55W

  选择题

java考试习题及答案

3、在Java Applet程序用户自定义的Applet子类中,一般需要重载父类的( D )方法来完成一些画图操作。

A. start() B. stop()

C. init() D. paint()

3、Java语言具有许多优点和特点,下列选项中,哪个反映了Java程序并行机制的特点?B

A)安全性 B)多线程 C)跨平台 D)可移植

4、下列哪个类声明是正确的?D

A)abstract final class HI{···} B)abstract private move(){···}

C)protected private number; D)public abstract class Car{···}

6、在Java语言中,下列哪些语句关于内存回收的说明是正确的? B

A.程序员必须创建一个线程来释放内存;

B.内存回收程序负责释放无用内存

C.内存回收程序允许程序员直接释放内存

D.内存回收程序可以在指定的时间释放内存对象

10、下列Object类中的方法,哪一项不是完全跟线程有关: A

ng toString()

notify()

notifyAll()

wait()

11、给出下面代码:C

public class Person{

static int arr[] = new int[10];

public static void main(String a[])

{

tln(arr[1]);

}

}

下列说法中正确的是?

A.编译时将产生错误;

B.编译时正确,运行时将产生错误;

C. 输出零;

D. 输出空。

12、字符串是Java已定义的类型,关于它的构造函数,下面说法不正确的是: B

ng(char[] value, int offset, int count)

ng(int[] codePoints,int offset, int count)

ng(String original)

ng(StringBuffer buffer)

13、下列说法中正确的是: C

A.导入包会影响程序的性能

B.包存储在类库中

C.包是类的容器D.上述说法都不对

14、下列不是String类的常用方法是:C

A、boolean equals(String str)

B、int compareTo(String str)

C、int SetindexOf(String str)

D、int lastIndexOf(String str)

17、表达式:(x>y) ? (z>w) ?x:z:w,(其中x=5,y=9,z=1,w=9)的值为:D

A、5 B、8

C、1 D、9

1、main方法是Java Application程序执行的入口点,关于main方法的方法头以下哪项是合法的( B)?

A、public static void main( )

B、public static void main( String args[] )

C、public static int main(String [] arg )

D、public void main(String arg[] )

5、欲构造ArrayList类的一个实例,下列哪个方法是正确的 ? B

A. ArrayList myList=new Object();

B. ArrayList myList=new ArrayList();

C. myList= new Object();

D. myList= new ArrayList();

7、哪个关键字可以对对象加互斥锁? B

sient

hronized

alize

ic

9、下列叙述中,错误的是: D

A、父类不能替代子类

B、子类能够替代父类

C、子类继承父类

D、父类包含子类

10、下列关于Java多线程并发控制机制的叙述中,错误的是:BC

A、Java中对共享数据操作的并发控制是采用加锁技术

B、线程之间的交互,提倡采用suspend()/resume()方法

C、共享数据的访问权限都必须定义为private

D、Java中没有提供检测与避免死锁的专门机制,但应用程序员可以采用某些策略防止死锁的发生

12、下列哪一个方法不是applet程序的组成部分:D

A、init() B、start()

C、stop() D、new()

15、Java应用程序中,程序执行的总入口方法是:B

A、start() B、main()C、run() D、init()

16、在Java实现线程有多少种方法:B

A、1 B、2

C、3 D、4

18、下列Applet类的方法中,在Applet的整个生命周期里至多只执行一次的是:A

A、init() B、start()

C、stop() D、paint()

19、启动一个线程所调用的方法是:C

A、run() B、init()

C、start() D、new()

  判断题

1.一个Java类可以有多个父类。

小程序Applet的主类的父类必须是类Applet。

k语句可以用在循环和switch语句中。

6.在Java中,异常Exception是指程序在编译和运行进出现的错误。

7.可以用new来创建一个类的实例,即对象。

使用16位的Unicode字符集,而不仅仅为ASCII字符集,因此Java字符是一个16位的无符号整数。

10.子类的成员变量能与其父类的`成员变量同名。

类中不能存在同名的两个成员函数。

语言是编译性语言。

l方法不能被覆盖。

20.多数I/O方法在遇到错误是会抛出异常,因此在调用这些方法时必须对异常进行处理。

ract 是抽象修饰符,可以用来修饰类及其属性和方法。

4.一个Java Applet源程序的主类能有多个父类。

6.用javac编译Java源文件后得到代码叫字节码。

7.可以用类名调用实例方法。

9.要想在类中实现多线程,类必须继承Thread类。

语言是平台无关的语言。

12.在类的静态方法中可以访问该类的非静态数据成员。

中方法调用时参数传递都是按值传递的,因此从方法退出时,参数的值不会改变。

14.声明为final的方法不能在子类中重载。

15.在子类中可以覆盖一个private方法。

19.在方法定义中,所有可能发生的异常都必须用try{}catch(){}捕捉并处理

1.请写出输出结果

class change{

void changeint(int x){

x++;

} public static void main(String args[]){

int a=2;

tln("Before changed: "+"a="+a); //请系统输出Before changed: a=

change cxz=new change();

geint(a);

tln("After changed: "+"a="+a);

}

}

答案:

Before changed: a= 2

After changed: a= 3

2. 下面程序运行的最终结果i是:___2_____

public class Foo {

public static void main (String []args) {

int i = 1;

int j = i++;

if ((i>++j) && (i++ ==j)) {

i +=j;

}

}

}

3、阅读以下程序,请写出输出结果

import .*;

public class StrCompare

{

public static void main(String[] args)

{

String

str1 = "Hello, Java!",

str2 = "hello, java!";

tln(areToIgnoreCase(str2));

tln(ls(str2)); tln(lsIgnoreCase(str2)); }

}

答:

0

False

True

4、阅读以下程序,请写出输出第一行结果

public class abc

{

public static void main(String args[ ])

{ int i =3,j ;

while (i>0){

j=3;

while (j>0){ if (j<2)

break;

tln(

"j+and"+i);

j--;

}

i--;

}

}

}

答:

j+and3

j+and3

j+and2

j+and2

j+and1

j+and1

5、 import .*;

public class abc

{ public static void main(String args[ ])

{ AB s = new AB("Hello!","I love JAVA.");

tln(ring( ));

}

}

class AB {

String s1;

String s2;

AB( String str1 , String str2 )

{ s1 = str1; s2 = str2; }

public String toString( )

{ return s1+s2;}

}

答:

Hello!I love JAVA.

6、阅读以下程序,请写出输出c.x=是多少

public class withstaticdata {

static int x;

int y;

public static void main(String[] args) {

withstaticdata a=new withstaticdata();

a.x=1;

tln(a.x);withstaticdata b=new withstaticdata();

b.x=2;

tln(b.x); tln(a.x); withstaticdata c=new withstaticdata();

tln(

"c.x="+c.x); }

}

答:2

public class StaticFun {

static void incr(){

withstaticdata.x++

;

}

public static void main( String [] args )

{

();

}

}

1、在横线上填上适当的内容

import .*;

import et.*;

public class AppletLife extends Applet //声明为Apple类

{

Color r;

public AppletLife() //初始化

{

tln("Applet initing...");

r = ;

}

public void start()

{

tln("Applet starting...");

}

public void stop()

{

tln("Applet stopping...");

}

public void paint(Graphics g)

{

tln("Applet painting...");

olor(r);

String("Painting here!", 50, 60 );

}

public void destroy()

{

tln("Applet destroy...");

}

}

2. 阅读以下程序,请写出输出结果

public class tt {

public static void main(String[] args) {

String s=new String("Bicycle");

int iBegin=1;

int iEnd=3;

tln(tring(iBegin,i

End));}

}

答:ic

3、请在相应的划线上填上代码

public class CircumferenceTester {

public static void main(String args[]) {

Circle c1 = new Circle();

us = 50;

Circle c2 = new Circle();

us = 10;

double circum1 = umference();

double circum2 = umference();

tln(

"Circle 1 has circumference " + circum1);

tln(

"Circle 2 has circumference " + circum2);

}

}

public class Circle

{

private static double PI = 3.141; //静态double 常量PI为 3.141

int radius;

public double circumference()

{

return 2*PI*radius;

}

public double area()

{

return PI * radius * radius;

}

public void enlarge(int factor) {

radius = radius * factor;

}

public boolean fitsInside (Rectangle r) {

return (2 * radius < h) && (2 * radius < ht);

}

}

4、阅读以下程序,请写出输出结果

class father{

void speak(){

tln(

"I am father!");

}

}

public class son extends father{

void speak(){

k();

tln(

"I am son!");

}

public static void main(String args[]){

son cxz=new son();

k();

}}

答:

I am father!

I am son!

5.写出下面程序的运行结果

import .*;

class Parent

{

void printMe()

{

tln("parent");

}

}

class Child extends Parent

{

void printMe()

{

tln("child");

}

void printAll()

{

tMe();

tMe();

printMe();

}

}

public class Class1

{

public static void main(String args[ ])

{

Child myC = new Child( );

tAll( );

}

}

答:

parent

child

child

6.写出下面程序的运行结果

import .*;

public class abc

{

public static void main(String args[])

{

String s1 = "Hello!";

String s2 = new String("World!");

tln(at(s2));

} }

答:

Hello!World!定义能计算圆的面积、周长的类circle,半径r为私有数据分量,其值由类circle的构造函数初始化,类circle提供读取半径r的方法getr();计算面积的方法area();计算周长的方法circlelength()。再定义类circlecomputer其包含程序运行所需的方法main,圆的半径为1,请设计程序, 必须使用类circle的构造函数及方法初始化数据分量和计算圆的面积、周长并打印出如下形式的结果。

半径 面积 周长

答:public

class circle {

private int r;

private static double PI = 3.141;

public circle(int r) {

this.r = r;

}

public int getR() {

return r;

}

public double area() {

return PI * r * r;

}

public double circlelength() {

return 2 * PI * r;

}

}

public class circlecomputer {

public static void main(String[] args) {

circle c = new circle(1);//

实例化

circle

并设置半径为

1

tln("

半径 面积 周长

");

t(());

t(" ");

t(());t(" ");

t(lelength());

}

}定义能

计算三角形的面积、周长的类js,三边a,b,c都为私有数据分量,其值由类sj的构造函数初始化,类js提供读取各边长度a,b,c的方法分别为geta(); getb(); getc(); 计算面积的方法area();计算周长的方法sjlength()。

(三角形面积公式:c)

-b)(s-a)(s-s(s 其中s=(a+b+c)/2)

再定义类jxcomputer其包含程序运行所需的方法main,矩三角形的三边a为3,b为6,c为8请设计程序必须使用类sj的构造函数及方法初始化数据分量和计算的三角形的面积、周长并打印出如下形式的结果。

a b c 面积 周长

答:public

class js {

private int a;

private int b;

private int c;

public js(int a, int b, int c) {

this.a = a;

this.b = b;

this.c = c;

}

public int getA() {

return a;

}

public int getB() {

return b;

}

public int getC() {

return c;

}

public double area() {

double s = this.s();return (s * (s - a) * (s - b) * (s - c));

}

public double sjlength() {

return a + b + c;

}

public double s() {

return ngth() / 2;

}

}

import malFormat;

public class jxcomputer {

public static void main(String[] arg) {

js j = new js(3, 6, 8);

tln("a b c

面积 周长

");

t(());

t(" ");

t(());

t(" ");

t(());

t(" ");

DecimalFormat df = new DecimalFormat("#.00"); //

保留两位小数,此句可以不用

t(at(()));

t(" ");

t(ngth());

}

}

热门标签