西电软院java考试_北航软件学院Java历届期末考题整理

本文整理了西电软院和北航软件学院Java课程的历年期末考试题目,涵盖abstract、static、Thread、Exception等多个关键知识点。通过对这些试题的解答和解析,帮助读者理解和掌握Java编程中的核心概念,包括线程、异常处理、数据类型、环境配置以及网络编程等。
摘要由CSDN通过智能技术生成

文章目录

abstract

static

Thread

finally

package

Exception

I/O

子类和父类

关键字

标识符

垃圾收集

数据类型

环境配置

网路编程

initial

匿名类和内部类

语法

网页

abstract

what will be the result of attempting to compile and run the following code?

abstract classMineBase {static inti;abstract voidamethod();

}public class Mine extendsMineBase {public static voidmain(String arg[]){int [] ar=new int[5];for(i=0;i

System.out.println(ar[i]);

}

}

A asequence of 5 0’s will be printed

B Error: ar is used before it is initialized

C Error Mine must be declared abstract

D IndexOutOfBoundes Error

C

c6868edf33400aaab3ab9fb25519d8b4.png

A

828a20f85d2bf4929294d3bfcf1c999c.png

B

92fb3a8663305dd7e92049a4257aa366.png

D

8953f0e020b264b56ce7e5bd245ede82.png

static

写出编译运行后的结果

public classStudent{static intnum;intnumber;public static intadd1()

{

num=num+1;returnnum;

}public intadd2()

{

number=number+1;returnnumber;

}public static voidmain(String argv[])

{

Student zhang=newStudent();

Student wang=newStudent();

Student jiang=newStudent();

System.out.println("zhang num="+add1());

System.out.println("zhang number="+zhang.add2());

System.out.println("wang num="+add1());

System.out.println("wang number="+wang.add2());

System.out.println("jiang num="+add1());

System.out.println("jiang number="+jiang.add2());

}

}

zhang num=1

zhang number=1

wang num=2

wang number=1

jiang num=3

jiang number=1

What will happen if you try to compile and run the following code

public classTest {public static voidmain(String[] arguments) {

aMethod(arguments);

}public voidaMethod(String[] arguments) {

System.out.println(arguments[1]);

}

}

a)error Can’t make static reference to void amethod.

b)error method main not correct

c)error array must include parameter

d)aMethod must be declared with String

B

a818acacb5ac5e08f182027211192682.png

C

35d565bce249b32bb1216d7db36843e9.png

D

ab48e0489d17fe7557f7b592df169309.png

4001a7c05cfac05820b263b39d569ec3.png

Thread

public class Test extendsThread {static int aInt = 10;public static voidmain(String[] argv) {

Test t= newTest();

t.start();

}public voidrun() {for (int i = 0 ; i < 4; i++) {

System.out.println(++aInt);

}

}

}

11

12

13

14

下列说法不正确的是:

a)Java中线程是抢占式的;

b)Java中线程可以是抢占式也可以是分时的;

c)Java中线程可以共享数器;

d)Java中线程可以共享代码。

Which method should you define as the starting point of a new thread in a class from which new threads of execution can be made?

a)public static void main(String[] args)

b)public void run()

c)public void start()

d)public void init()

e)public void runnable()

There are two ways to create a new thread of execution. One is to

declare a class to be a subclass of Thread. This subclass should

override the run method of class Thread.

c0c689a9bc5437e68f47fc1dd78b1537.png

The following code would then create a thread and start it running:

9e82a66d96115d2069923cf87b09fde5.png

The other way to create a thread is to declare a class that implements

the Runnable interface. That class then implements the run method.

28ded463f2c5e56be56ecd38535b0e3a.png

The following code would then create a thread and start it running:

61cc025c614693514b063e8e1f567954.png

下面哪个方法是启动新线程的方法:

a)只需创建

b)创建并调用start()

c)创建并调用begin()

d)创建并调用start Thread()

Java实现多线程的两种方法:

1.扩展java.lang.Tread类

2.实现java.lang.Runnable接口

public class Test extendsThread{static String Sname="Hello";public static voidmain(String argv[])

{

Test t=newTest();

t.start();

}public voidrun()

{for(int i=0;i<4;i++)

{

Sname=Sname+" "+i;

System.out.println(Sname);

}

}

}

Hello 0

Hello 0 1

Hello 0 1 2

Hello 0 1 2 3

finally

public classTest1 {public static voidmain(String[] args) {

Test1 m= newTest1();

System.out.println(m.aMethod());

}public intaMethod() {try{throw newException();

}catch(Exception ex) {

System.out.println("No such file found");return -1;

}finally{

System.out.println("Doing finally");

}

}

}

No such file found

Doing finally

-1

别漏掉-1

public classTest1 {public static voidmain(String[] args) {

print();

}private static voidprint() {try{

System.out.println("Thank your");

}finally{

System.out.println("I am sorry");

}

}

}

a)Thank you

b)I am sorry

c)Thank you

I am sorry

d)代码不能编译

public classTest1 {public static voidmain(String argv[]) {try{

System.out.println("Hello World!");

}catch(Exception e) {

System.out.println("Exception 1");

}finally{

System.out.println("Thank you!");

}

}

}

Hello World!

Thank you!

package

Package com.co.project;

PublicclassTest {inti;public intj;protected intk;private intl;

}

说法正确的是;

a)其它包中的所有类可以访问变量i;

b)其它包中的所有类可以访问变量j;

c)其它包中的所有类可以访问变量k;

d)其它包中的所有类可以访问变量l;

e)其它包中只有Test子类才能访问l。

Exception

所有异常的基础类是

a)String

b)error

c)Throwable

d)RuntimeException

下面代码的结果是

public classTest {classB{}public static voidmain(String[] args) {newB();

}

}

a)compile time exception

b)runtime exception

c)compile with errors

d)一切正常

C

190deca63587b4cb8c3b7ae101be60e0.png

D

b980deee0dc805081c17b38261bd250d.png

I/O

System是属于java中的哪一个包

a)lang

b)io

c)util

d)awt

关于System.out正确的是:

a)是一个OutputStream;

b)是一个PrintStream;

c)是一个FilterOutputStream;

d)异常时抛出IOException;

java API

55eb05cbfbd4b789068550496c92ac61.png

try{

File f= new File("file.txt");

OutputStream out= new FileOutputStream("f.txt");

}catch(FileNotFoundException e) {

e.printStackTrace();

}

a)不得编

b)可编,文件无改

c)可编,长度为0

d)抛出异常

这是个啥,反正我是没看懂

下面哪个语句能正确地创建一个InputStreamReader的实例:

a)new InputStreamReader(new FileInputStream(“data.txt”));

b)new InputStreamReader(new FileReader(“data.txt”));

c)new InputStreamReader(new BufferReader(“data.txt”));

d)new InputStreamReader( “data.txt”);

InputStreamReader参数必须是InputStream或其子类类型

.txt表示是一个文件(file)

FileReader(); 读入的是字符类型,不是字节(Stream)类型

DataInputStream只读入简单类型的变量

哪个语句可以建立”file.txt”的字节输入流:

a)FilelnputStream in =new FilelnputStream(”file.txt”);

b)InputStream in =new InputStream();

c)InputStream in =new FilefReaxder();

What will be printed out if this code is run with the following command line

public classMyProg {public static voidmain(String argv[]) {

System.out.println(argv[2]);

}

}

a)MyProg

b)good

c)morning

d)Exception raised:”java.lang.ArrayIndexOutOfBoundsException:2”

A

这个实在是不知道该怎么弄出来

B、C

public classMyProg {public static voidmain(String argv[]) {

System.out.println(MyProg.class);

System.out.println(argv);

System.out.println(argv[0]);

System.out.println(argv[1]);

}

}

javac MyProg.java

java MyProg good morning

class MyProg

[Ljava.lang.String;@15db9742

good

morning

29f9ec827197db1a5cb7967a8771ff9c.png

439faf1ccf4ee4a7c130fca893f5cced.png

也就是说java中main函数的命令行传参是不会把类名作为参数传递的,这点与C语言main函数传参不同

子类和父类

看程序输出

interfaceone {

}class Two implementsone {

}class Three implementsone {

}public classTest {public static voidmain(String[] argv) {

one test1= newTwo();

one test2= newThree();

System.out.println(test1instanceofone);

System.out.println(test2instanceofone);

System.out.println(test1.getClass().equals(test2.getClass()));

}

}

true

true

false

What will happen if you attempt to compole and run the following code?

classBase{}class Sub extendsBase{}class Sub2 extendsBase{}public classTest {public static voidmain(String[] argv) {

Base b= newBase();

Sub s=(Sub) b;

System.out.println("everything is fine");

}

}

a)Compile and run without error

b)Compole time Exception

c)Runtime Exception

d)”everything is okay”

D

b2d75408172760928ce9f85b02689f15.png

请问下面代码的结果

classbase{}class mine extendsbase{}public classss {public static voidmain(String[] args) {

mine x= newmine();

base mm=(base)x;

System.out.println("sss");

}

}

a)compile time exception

b)runtime exception

c)sss

写输出

interfacea{}class AA implementsa{}class B implementsa{}public classTest{public static voidmain(String[] args) {

a x= newAA();

a y= newB();

System.out.println(xinstanceofa);

System.out.println(yinstanceofa);

System.out.println(x.getClass().equals(y.getClass()));

}

}

true

true

false

关于接口的说法不正确的是:

a)接口所包含的方法既可以有时限,也可以没有实现;

(接口包含的方法不能有实现,抽象类才可以)

b)    接口没有构造函数;

c)    实现一个接口必须实现接口的所有方法;

d)接口间可以有继承关系。

下面关于方法覆盖不正确的是

a)在一个子类中一个方法不是public的就不能被重载;

b)子类重写父类的方法不能低于其在父类中的访问权限;

c)子类抛出的异常类型不能比父类抛出的异常类型更宽泛;

d)子类对父类方法重写时,要求重写方法参数列表和返回类型必须与被重写方法的相同。

Which of the following statements are true?

a)Methods cannot be overriden to be more private

b)static methods cannot be overloaded

c)private methods canot be overload

d)an overloaded method cannot throw exceptions not checked in the base class

overload(方法重载:方法名相同,参数不同) Static methods cannot be overriden but they

can be overloaded. There is no logic or reason why private methods

should not be overloaded. Option 4 is a jumbled up version of the

limitations of exceptions for overriden methods.

override(重写,覆盖)方法名,参数,返回值等都相同,是父类子类之间的关系

下列正确的是:

a)子类必须通过super关键字才能调用父类有参数构造的方法;

b)子类必须通过this关键字才能调用父类有参数构造的方法;

c)子类无条件继承父类不含参数的构造方法;

d)如果子类定义自己含参数的构造方法,就不能再调用父类的构造方法。

public classFatherclass {publicFatherclass() {

System.out.println("Father class create");

}

}public class Childclass extendsFatherclass{publicChildclass() {

System.out.println("Child class create");

}public static voidmain(String[] args) {

Fatherclass fc= newFatherclass();

Childclass cc= newChildclass();

}

}

Father class create

Father class create

Child class create

实现继承的关键字是:

extends

Java中实现多继承的方法是:

a)继承多个类

b)继承多个抽象类

c)多个接口

d)不可以实现多继承

关键字

不能被继承的类是用___关键字修饰的:

a)static

b)print

c)final

d)protected

哪个关键字可抛出异常:

a)transient

b)finally

c)throw

d)static

Which modifier should be

applied to a method for the lock of the object(对象锁) this to be obtained

prior to excuting any of the method body?

a)abstract

b)final

c)protected

d)static

e)synchronized

标识符

类名和文件名的关系是:

Java保存的文件名必须与类名一致

如果文件中只有一个类,文件名必须与类名一致

一个Java文件中只能有一个public类

不止一个类,文件名必须与public类名一致

文件中不止一个类,且没有public类,那么文件名可与任一类名一致

垃圾收集

关于Java垃圾收集,下列哪个是正确的:

a)垃圾收集机制将检查并回收不再使用的内存;

b)垃圾收集机制允许开发者明确制定并释放该内存;

c)程序开发者必须自己创建一个线程运行内存释放工作;

d)垃圾收集机制能在期望时间内释放被Java对象使用的内存。

数据类型

下面哪个赋值语句是不正确的:

a)long a=6;

b)double-=99l;

c)float z=12.414f;

d)float z=12.414;

下面哪个不是Java的原始数据类型:

a)“abc”

b)’x’

c)100.09f

d)false

Which of the following lines will compile without warning or error.

a)float f = 1.3;(double)

b)char c = “a”;(java.lang.String)

c)byte b = 257;(int)

d)boolean b = null;(null)

e)int i = 10;

阅读以下程序,选择结果:

boolean a=false;

boolean b=true;

boolean c=(a&&b)&&!b;

int result = c == false?1:2;

a)c:false;result:1;

b)c:false;result:2;

b)c:true;result:1;

d)c:true;result:2;

下列对String类说法正确的是

a)是基本数据类型(不是基本数据类型)

b)可以继承(String类有final修饰符,不可以被继承)

c)length()是String的属性(length是数组的一个属性,而length()是字符串的一个方法)

d)不允许使用“==”判断相等

环境配置

系统环境变量配置中%JAVA_HOME%代表:

指向Java的JDK的安装目录的根目录

网路编程

下列写法正确的URL地址是

a)http:166.111.136.3:-10/index.html

b)ftp://166.11.136.3/incoming

c)ftp://166.111.136.3:-1/

d)http://166.111.136.3.3

URL结构

protocol(协议名):// host(主机名) [:port(端口号)] / path(文件目录或文件名) / [;parameters] [?query] #fragment

initial

public classStaticTest {public static intprintStr1() {

System.out.println("这是静态方法!");return 0;

}public intprintStr2() {

System.out.println("这是普通方法!");return 0;

}public static voidmain(String[] args) {

StaticTest.printStr1();

StaticTest st= newStaticTest();

st.printStr2();

}

}

这是静态方法!

这是普通方法!

需要注意:

public classStaticTest {public static intprintStr1() {

System.out.println("这是静态方法!");return 0;

}public intprintStr2() {

System.out.println("这是普通方法!");return 0;

}public static voidmain(String[] args) {

StaticTest st= newStaticTest();

st.printStr2();

StaticTest.printStr1();

}

}

这是普通方法!

这是静态方法!

What is the result with the following code?

classA {publicA() {

System.out.print("A");

}

}class B extendsA {publicB() {

System.out.print("B");

A a= newA();

}

}public classTest {public static voidmain(String[] argv) {

B b= newB();

}

}

a)ABA

b)BA

c)A

d)Compile with some errors

以下代码的输出是

public classTest {public static inta;public static voidmain(String[] args) {

System.out.println(a);

}

}

a)0

b)1

c)null

d)error

What will happen when you compile and run the following code?

public classMyClass {static inti;public static voidmain(String[] argv) {

System.out.println(i);

}

}

a)Error Variable i may not have been initialized

b)null

c)1

d)0

public classMyClass {static inti;static intj;staticString k;//int l;//无法从静态上下文中引用非静态变量,无法通过编译

public static voidmain(String[] argv) {intl;

System.out.println(i);//0

System.out.println(++j);//1

System.out.println(k);//null//System.out.println(l);//可能尚未初始化,运行时错误

}

}

What will happen if you try to compile and run the following code?

public classQ {public static voidmain(String[] args) {int anArray[] = new int[5];

System.out.println(anArray[0]);

}

}

a)Error:anArray is referenced before it is initialized

b)null

c)0

d)5

别看他没在这初始化,而且是局部的,但是他不是个简单数据类型,它的那个new就初始化了,所以不能选A

A

34cf67847f2473cbbc2a664c4ff9ab45.png

B

b04292e3fd4fb04d4692f40b9a50be98.png

匿名类和内部类

What will happen if you attempt to compile and run the following code?

public classTest {public static voidmain(String[] argv) {newB();

}classB {

B() {

System.out.println();

}

}

}

a)Compile without error

b)Compile time Exception

c)Running time Exception

d)Compile with errors

语法

What will be the result of attempting to compile and run the following code?

public classTest {public static voidmain(String[] args) {int i = 1;switch(i) {case 0:

System.out.println("zero");break;case 1:

System.out.println("one");case 2:

System.out.println("two");default:

System.out.println("default");

}

}

}

a)one

b)one, default

c)one, two, default

d)default

不要忘记了default,因为2后面也没有break,就像一根一通到底的水管。

网页

What should you use to position

a Button within an application Frame so that the size of the Butten is

not affected by the Frame size?

a) a FlowLayout

b)a GridLayout

c)the center area of a BorderLayout

d)the East or West area of a BorderLayout

e)the North or South area of a Borderlayout

What should you use to position

a Button within an application frame so that the width of the Button is

affected by the Frame size but the height is not affected.

a)FlowLayout

b)GridLayout

c)Center area of a BorderLayout

d)North or South of a BorderLdyou

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值