Java程序设计习题集(3)

2019-02-15 15:56

}

5. class Something { int i;

public void doSomething() { System.out.println(\} }

二、下面代码编译和运行的结果是什么? 1. class Value{ public int i = 15; }

public class Test{

public static void main(String argv[]){ Test t = new Test(); t.first(); }

public void first(){ int i = 5;

Value v = new Value(); v.i = 25; second(v, i);

System.out.println(v.i); }

public void second(Value v, int i){ i = 0; v.i = 20;

Value val = new Value(); v = val;

System.out.println(v.i + \} }

A.15 0 20 B.15 0 15 C.20 0 20 D.0 15 20 2. class FatherClass { public FatherClass(){

System.out.println(\} }

public class ChildClass extends FatherClass{ public ChildClass() {

System.out.println(\}

public static void main(String[] args) {

FatherClass fc = new FatherClass();

ChildClass cc = new ChildClass(); } }

3. public class Static{ static{ int x = 5; }

static int x,y;

public static void main(String args[]){ x--;

myMethod();

System.out.println(x + y + ++x); }

public static void myMethod(){ y = x++ + ++x; } }

A.编译错误 B.输出1 C.输出2 D.输出3 4. class MyParent { int x, y;

MyParent(int x, int y){ this.x = x; this.y = y; }

public int addMe(int x, int y){ return this.x + x + y + this.y; }

public int addMe(MyParent myPar){ return addMe(myPar.x, myPar.y); } }

class MyChild extends MyParent{ int z;

MyChild (int x, int y, int z){ super(x,y); this.z = z; }

public int addMe(int x, int y, int z){ return this.x + x + this.y + y + this.z + z; }

public int addMe(MyChild myChi){

return addMe(myChi.x, myChi.y, myChi.z); }

public int addMe(int x, int y){

return this.x + x + this.y + y; } }

public class MySomeOne{

public static void main(String args[]){

MyChild myChi = new MyChild(10, 20, 30); MyParent myPar = new MyParent(10, 20); int x = myChi.addMe(10, 20, 30); int y = myChi.addMe(myChi); int z = myPar.addMe(myPar); System.out.println(x + y + z); } }

A.300 B.240 C.120 D.180 答案:

一、1. 错。abstract method必须以分号结尾,且不带花括号。

2. 错。abstract的methods不能以private修饰。abstract的methods就是让子类implement(实现)具体细节。

3. 错。int x被修饰成final,意味着x不能在addOne method中被修改。

4. 正确。在addOne method中,参数o被修饰成final。如果在addOne method里我们修改了o的reference(比如: o = new Other();),那么如同上例

这题也是错的。但这里修改的是o的member vairable(成员变量),而o的reference并没有改变。

5. 正确。输出的是\。 二、1. 正确答案A 2. 答案:

FatherClass Create FatherClass Create ChildClass Create 3. 正确答案 D 4. 正确答案:A

第八单元 试题 一、判断程序段 1. class Something { final int i;

public void doSomething() { System.out.println(\} }

7. interface Playable { void play(); }

interface Bounceable { void play(); }

interface Rollable extends Playable, Bounceable { Ball ball = new Ball(\}

class Ball implements Rollable { private String name;

public String getName() { return name; }

public Ball(String name) { this.name = name; }

public void play() {

ball = new Ball(\

System.out.println(ball.getName()); } }

二、下面代码编译和运行的结果是什么? 1. class MyThread extends Thread{ public void run(){

System.out.println(\}

public void start(){

System.out.println(\} }

class MyRunnable implements Runnable{ public void run(){

System.out.println(\}

public void start(){

System.out.println(\} }

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(\

4. void myMethod(String s) {System.out.println(\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(\


Java程序设计习题集(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:黔之驴教案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: