java基础测试题2

6. Test.java程序如下:
1. class Test {
2. public static void main(String [] args) {
3. int x=20;
4. String sup = (x<15)?"small":(x<22)?"tiny":"huge";
5. System.out.println(sup);
6. }
7. }
则输出结果为?(Choose one.)
A. small
B. tiny
C. huge
D. Compilation fails
答案:B
解析:这道题考查是 ?: 的用法如果?之前的为真,则执行?与:之间,如果为假,则执行:后面。
7. RTExcept.java程序如下:
1. public class RTExcept {
2. public static void throwit () {
3. System.out.print("throwit ");
4. throw new RuntimeException();
5. }
6. public static void main(String [] args) {
7. try {
8. System.out.print("hello ");
9. throwit();
10. }
11. catch (Exception re ) {
12. System.out.print("caught ");
13. }
14. finally {
15. System.out.print("finally ");
16. }
17. System.out.println("after ");
18. }
19. }
输出结果为?(Choose one.)
A. hello throwit caught
B. 编译失败
C. hello throwit RuntimeException caught after
D. hello throwit RuntimeException
E. hello throwit caught finally after
F. hello throwit caught finally after RuntimeException
答案:E
解析:正常阅读程序得出结果。需要注意的是finally的用法
8. B.java程序如下:
class A {
public void baz() {
System.out.println("A");
}
}
public class B extends A {
public static void main(String [] args) {
A a = new B();
a.baz();
}
public void baz() {
System.out.println("B");
}
}
编译、运行将得到下面哪一个结果?(Choose one.)
A. A
B. B
C. Compilation fails.
D. An exception is thrown at runtime.
答案:B
解析:子类 父类 调用 没啥解释的吧。
9. 哪一个class或interface定义了wait()、notify()、notifyAll()方法?(Choose one.)
A. Object
B. Thread
C. Runnable
D. Class
答案:A
解析:查API
10. MyRunnable.java程序如下:
1. public class MyRunnable implements Runnable {
2. public void run() {
3. // some code here
4. }
5. }
下面哪一段代码将创建并启动一个线程?(Choose one.)
A. new Runnable(MyRunnable).start();
B. new Thread(MyRunnable).run();
C. new Thread(new MyRunnable()).start();
D. new MyRunnable().start();
答案:C
解析:考查线程。调用start就会自动执行run。参看下面卖票的例子。
package a;

public class A implements Runnable {
public int a=10;

public void run() {
a=a-1;
System.out.println(a);
System.out.println(Thread.currentThread().getName());
// TODO Auto-generated method stub

}

public static void main(String[] args) {
// TODO Auto-generated method stub
A c=new A();
new Thread(c).start();
System.out.println(c.a);
new Thread(c).start();
}

/**
* 十道选择题的最后一道A是大家共卖10 张票。 C是两个窗口各卖各的10 张票
*/
}


/**
*
*/
package a;

/**
* @author Administrator
*
*/
public class C extends Thread {
public int a=10;
public void run(){
a=a-1;
System.out.println(a);
System.out.println(Thread.currentThread().getName());
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
C a1=new C();
a1.start();
C a2=new C();
a2.start();
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值