一、判断程序段
1. class Something {
final int i;
public void doSomething() {
System.out.println("i = " + i);
}
}
7. interface Playable {
void play();
}
interface Bounceable {
void play();
}
interface Rollable extends Playable, Bounceable { Ball ball = new Ball("PingPang");
}
class Ball implements Rollable {
private String name;
public String getName() {
return name;
}
public Ball(String name) {
= name;
}
public void play() {
ball = new Ball("Football");
System.out.println(ball.getName());
}
}
二、下面代码编译和运行的结果是什么?
1. class MyThread extends Thread{
public void run(){
System.out.println("MyThread: run()");
}
public void start(){
System.out.println("MyThread: start()");
}
}
class MyRunnable implements Runnable{
public void run(){
System.out.println("MyRunnable: run()");
}
public void start(){
System.out.println("MyRunnable: start()");
}
}
public class MyTest {
public static void main(String args[]){
MyThread myThread = new MyThread();
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
myThread.start();
thread.start();
}
}
A.打印MyThread: start() 后面跟MyRunnable: run()
B.打印MyThread: run() 后面跟MyRunnable: start()
C.打印MyThread: start() 后面跟MyRunnable: start()
D.打印MyThread: run() 后面跟MyRunnable: run()
2. // 文件名:SuperclassX.java
package packageX;
public class SuperclassX{
protected void superclassMethodX(){}
int superclassVarX;
}
//文件名:SubclassY.java
1.package packageX.packageY;
2.
3.public class SubclassY extends SuperclassX
4.{
5. SuperclassX objX = new SubclassY();
6. SubclassY objY = new SubclassY();
7. void subclassMethodY()
8. {
9. objY.superclassMethodX();
10. int i;
11. i = objY.superclassVarX;
12. }
13.}
A.第5行编译错
B.第9行编译错
C.第11行意外
D.都不是
3. 程序段如下:
1.class MyClass
2.{
3. void myMethod(int i) {System.out.println("int version");}
4. void myMethod(String s) {System.out.println("String version");}
5. public static void main(String args[])
6. {
7. MyClass obj = new MyClass();
8. char ch = 'c';
9. obj.myMethod(ch);
10. }
11.}
A.第四行编译出错
B.第九行抛出例外
C.输出结果:int version
D.输出结果:String version
4. public class ThrowsDemo {
static void throwMethod() {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwMethod();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
A.编译错
B.运行错
C.编译成功,但是没有输出结果
D.以上都不是
5. class X {
Y b = new Y();
X() {
System.out.print("X");
}
}
class Y {
Y() {
System.out.print("Y");
}
}
public class Z extends X {
Y y = new Y();
Z() {
System.out.print("Z");
}
public static void main(String[] args) {
new Z();
}
}
A.Z
B.YZ
C.XYZ
D.YXYZ
6. public class MyThread implements Runnable {
String myString = "Yes ";
public void run() {
this.myString = "No ";
}
public static void main(String[] args) {
MyThread t = new MyThread();
new Thread(t).start();
for (int i=0; i < 10; i++)
System.out.print(t.myString);
}
}
A. 打印yes yes yes yes yes yes B. 打印no no no no no no no no
C. 打印yes no yes no ye no ye no D. 不确定
答案:
一、1. 错。
final int i是个final的instant variable (实例变量,或叫成员变量)。
final 的instant variable没有default value,必须在constructor (构造器)结束之前被赋予一个明确的值。
可以修改为"final int i = 0;"。
2. 错。
Ball ball = new Ball("PingPang");"实际上是"public static final Ball ball = new Ball("PingPang");"。
在Ball类的Play()方法中,"ball = new Ball("Football");"改变了ball的reference,所以编译时间错。
二、1.答案A
2.答案D
3.答案C
4.答案:A
5.答案D
6.答案D。