北航软件学院Java历届期末考题整理
文章目录
- 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 class MineBase { static int i; abstract void amethod(); } public class Mine extends MineBase { public static void main(String arg[]){ int [] ar=new int[5]; for(i=0;i<ar.length;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
A
B
D
static
写出编译运行后的结果
public class Student{ static int num; int number; public static int add1() { num=num+1; return num; } public int add2() { number=number+1; return number; } public static void main(String argv[]) { Student zhang=new Student(); Student wang=new Student(); Student jiang=new Student(); 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 class Test { public static void main(String[] arguments) { aMethod(arguments); } public void aMethod(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
C
D
Thread
public class Test extends Thread { static int aInt = 10; public static void main(String[] argv) { Test t = new Test(); t.start(); } public void run() { 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.
The following code would then create a thread and start it running:
The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method.
The following code would then create a thread and start it running:
下面哪个方法是启动新线程的方法:
a)只需创建
b)创建并调用start()
c)创建并调用begin()
d)创建并调用start Thread()
Java实现多线程的两种方法:
1.扩展java.lang.Tread类
2.实现java.lang.Runnable接口
public class Test extends Thread{ static String Sname="Hello"; public static void main(String argv[]) { Test t=new Test(); t.start(); } public void run() { 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 class Test1 { public static void main(String[] args) { Test1 m = new Test1(); System.out.println(m.aMethod()); } public int aMethod() { try { throw new Exception(); } 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 class Test1 { public static void main(String[] args) { print(); } private static void print() { 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 class Test1 { public static void main(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; Public class Test { int i; public int j; protected int k; private int l; }
说法正确的是;
a)其它包中的所有类可以访问变量i;
b)其它包中的所有类可以访问变量j;
c)其它包中的所有类可以访问变量k;
d)其它包中的所有类可以访问变量l;
e)其它包中只有Test子类才能访问l。
Exception
所有异常的基础类是
a)String
b)error
c)Throwable
d)RuntimeException
下面代码的结果是
public class Test { class B{} public static void main(String[] args) { new B(); } }
a)compile time exception
b)runtime exception
c)compile with errors
d)一切正常
C
D
I/O
System是属于java中的哪一个包
a)lang
b)io
c)util
d)awt
关于System.out正确的是:
a)是一个OutputStream;
b)是一个PrintStream;
c)是一个FilterOutputStream;
d)异常时抛出IOException;
java API
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 class MyProg { public static void main(String argv[]) { System.out.println(argv[2]); } }
a)MyProg
b)good
c)morning
d)Exception raised:”java.lang.ArrayIndexOutOfBoundsException:2”
A
这个实在是不知道该怎么弄出来
B、C
public class MyProg { public static void main(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
也就是说java中main函数的命令行传参是不会把类名作为参数传递的,这点与C语言main函数传参不同
子类和父类
看程序输出
interface one { } class Two implements one { } class Three implements one { } public class Test { public static void main(String[] argv) { one test1 = new Two(); one test2 = new Three(); System.out.println(test1 instanceof one); System.out.println(test2 instanceof one); System.out.println(test1.getClass().equals(test2.getClass())); } }
true
true
false
What will happen if you attempt to compole and run the following code?
class Base{} class Sub extends Base{} class Sub2 extends Base{} public class Test { public static void main(String[] argv) { Base b = new Base(); 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
请问下面代码的结果
class base{} class mine extends base{} public class ss { public static void main(String[] args) { mine x = new mine(); base mm = (base)x; System.out.println("sss"); } }
a)compile time exception
b)runtime exception
c)sss
写输出
interface a{} class AA implements a{} class B implements a{} public class Test{ public static void main(String[] args) { a x = new AA(); a y = new B(); System.out.println(x instanceof a); System.out.println(y instanceof a); 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 class Fatherclass { public Fatherclass() { System.out.println("Father class create"); } } public class Childclass extends Fatherclass{ public Childclass() { System.out.println("Child class create"); } public static void main(String[] args) { Fatherclass fc = new Fatherclass(); Childclass cc = new Childclass(); } }
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 class StaticTest { public static int printStr1() { System.out.println("这是静态方法!"); return 0; } public int printStr2() { System.out.println("这是普通方法!"); return 0; } public static void main(String[] args) { StaticTest.printStr1(); StaticTest st = new StaticTest(); st.printStr2(); } }
这是静态方法!
这是普通方法!
需要注意:
public class StaticTest { public static int printStr1() { System.out.println("这是静态方法!"); return 0; } public int printStr2() { System.out.println("这是普通方法!"); return 0; } public static void main(String[] args) { StaticTest st = new StaticTest(); st.printStr2(); StaticTest.printStr1(); } }
这是普通方法!
这是静态方法!
What is the result with the following code?
class A { public A() { System.out.print("A"); } } class B extends A { public B() { System.out.print("B"); A a = new A(); } } public class Test { public static void main(String[] argv) { B b = new B(); } }
a)ABA
b)BA
c)A
d)Compile with some errors
以下代码的输出是
public class Test { public static int a; public static void main(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 class MyClass { static int i; public static void main(String[] argv) { System.out.println(i); } }
a)Error Variable i may not have been initialized
b)null
c)1
d)0
public class MyClass { static int i; static int j; static String k; //int l; //无法从静态上下文中引用非静态变量,无法通过编译 public static void main(String[] argv) { int l; 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 class Q { public static void main(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
B
匿名类和内部类
What will happen if you attempt to compile and run the following code?
public class Test { public static void main(String[] argv) { new B(); } class B { 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 class Test { public static void main(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