我的JAVA习题集2 联系客服

发布时间 : 星期日 文章我的JAVA习题集2更新完毕开始阅读8c0779814693daef5ff73d1a

23.以下程序片段,下列哪个选项插入到第2行将引起编译错误?( ) 1. package mypackage; 2.

3. class MyClass{

4. //do something 5. }

A. public class MainClass{ } B. package mypackage1; C. class B{ } D. import java. util.*;

24.以下程序代码,无法通过编译的是( )。 A. class A{

int i=O;

public void method(){

System.out.println (i); } }

B. class A{

int i=0; }

class B{

public void method(){

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

C. class A{

public int i=0;

public static void method(){

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

D. class A{

public static int i=0;

public static void method(){ System.out.println (i); ) }

25.下列代码在编译时会发生错误,下面哪种修改可以更正错误?( ) class Test {

private int m;

public static void fun(){ System.out.println(m); }

5

}

A.将private int m改为protected int m B.将private int m改为public int m C.将phvate int m改为static int m D.将private int m改为int m

三、程序阅读题

1.阅读下面的程序,程序保存为Test.java:

1. public class Test{ 2. short mValue;

3. public static void main(String[] args){ 4. int a=32; 5. int b=56;

6. Test os=new Test (a+b); 7. os.Show (); 8. }

9. protected Test (short aValue) { 10. mValue= aValue; 11. }

12. public void Show() {

13. System. out .println (mValue); 14. } 15. }

上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编译正确,运行结果是什么?

2.阅读下面的程序:

1 public class Test{

2 public static void main (String argv [ ] ) { 3 Bird b=new Bird(); 4 b. fly (3); 5 } 6 }

7 class Bird{

8. static int Type=2;

9. private void fly(int an_Type) { 10. Type= an_Type;

11. System. out .println (\12. } 13. }

6

上面的程序编译是否成功?如果编译出错,指出哪行出错,并说明理由;如果编译正确, 运行结果是什么?

3.仔细阅读下面的程序代码,若经编译和运行后,请写出打印结果。 class StaticTest { static int x=l; int y;

StaticTest () { y++; }

public static void main (String args [] ) { StaticTest st=new StaticTest (); System.out.println (\

System. out.println (\ st=new StaticTest ();

System.out.println (\ System. out.println (\}

static { x++; }

4.写出下列程序代码的运行结果。

public class PassTest{ float ptValue;

public void changeInt(int value){ value=il; }

public void changeStr(String value){ value=new String(\ }

public void changeObjValue( PassTest ref){ ref.ptValue=22; }

public static void main(String args[ ] ){ String str; int val;

PassTest pt=new PassTest(); val=33;

pt.changeInt (val);

System.out.println(\ str=new String (\ pt.changeStr (str);

System. out.println(\ pt.ptValue=44;

7

pt.changeObjValue(pt);

System.out.println(\;

} }

5.写出下列程序代码的运行结果:

class Test {

public static String ss=\杭州电子科技大\ public String ms= \计算机学院\ public void printInfo () {

System. out.println (\ }

public static void main (String [] args) { Test obj1, obj2; obj1=new Test(); obj2=new Test();

obj1.ss=\浙江工业大学\obj1.ms= \软件学院\obj1.printInfo(); obj2.printInfo(); } }

6.阅读以下程序,回答问题。 package packagel; class ClassA{

public void printInfo(){

System.out.println (\ } }

package package2;

import packagel.ClassA; public class ClassB{

public static voidmain(String [] args){ new ClassA().printInfo(); } }

(1)上面两个类是在同一个源程序文件中吗?

(2)以上程序编译是否正常?若编译正常则运行结果是什么?

8