Java习题二

更新时间:2024-03-03 22:04:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

1. 指出下列程序的运行结果

public class Example {

String str=new String(\ char[]ch={'a','b','c'};

public static void main(String args[])

{

Example ex=new Example(); ex.change(ex.str,ex.ch);

System.out.print(ex.str+\ Sytem.out.print(ex.ch); }

public void change(String str, char ch[])

{

str=\ ch[0]='g'; } }

传值和传地址 ① good and abc ② good and gbc ③ test ok and abc ④ test ok and gbc 2. 给出下面代码:

public class Person {

static int arr[] = new int[10];

public static void main(String a[]) {

System.out.println(arr[1]); } }

哪个语句是正确的? ① 编译时将产生错误;

② 编译时正确,运行时将产生错误; ③ 输出零; ④ 输出空。

3.下列哪些语句关于内存回收的说明是正确的?

① 程序员必须创建一个线程来释放内存;

② 内存回收程序负责释放无用内存

③ 内存回收程序允许程序员直接释放内存

④ 内存回收程序可以在指定的时间释放内存对象

4.1) public void modify() {

2) int I, j, k; 3) I = 100;

4) while ( I > 0 ) { 5) j = I * 2;

6) System.out.println (\7) k = k + 1; 8) I--; 9) } 10) } ① line 4 ② line 6

③ line 7 局部变量必须初始化,成员变量不需初始化,数组不需初始化 ④ line 8

5.指出下列哪些方法与方法public void add(int a){}为合理的重载方法。

① public int add(int a) ② public void add(long a)

③ public void add(int a,int b) ④ public void add(float a) 6.给出下面代码段

1) public class Test { 2) int m, n;

3) public Test() {}

4) public Test(int a) { m=a; }

5) public static void main(String arg[]) { 6) Test t1,t2; 7) int j,k;

8) j=0; k=0;

9) t1=new Test(); 10) t2=new Test(j,k); 11) } 12) }

哪行将引起一个编译时错误?

① ine 3 ② line 5 ③ line 6 ④ line 10

7.main()方法的返回类型是:

① int ② void

③ Boolean ④ static

8.System类在哪个包中? ① java.util ② java.io ③ java.awt ④ java.lang 9.给出下面代码:

public class Test {

static int a[] = new a[10];

public static void main(String args[]) {

System.out.println(a[10]); } }

那个选项是正确的? ① 译时将产生错误;

② 编译时正确,运行时将产生错误; ③ 输出零;

④ 输出空。

10.String s = \ String t = \

char c[] = {'h','e','l','l','o'} ;

下列哪些表达式返回true? ① s.equals(t);t ② t.equals(c);f ③ s==t;t

④ t.equals(new String(\11.下述哪些说法是正确的?

① 实例变量是类的成员变量

② 实例变量是用static关键字声明的 ③ 方法变量在方法执行时创建

④ 方法变量在使用之前必须初始化( 见test24.java)

12.String s = new String(\创建了几个String Object? 13.int 和 Integer 有什么区别? 14.Java有没有goto?是关键字吗?

15.用最有效率的方法算出2乘以8等於几? 16. a = null 和 a.length() = 0 有什么区别? 17.public class Something

{

void doSomething ()

{

private String s = \方法变量没有访问权限

int l = s.length(); }

}

有错吗?若有,错在哪里? 18.public class Something {

public static void main(String[] args) {

Something s = new Something();

System.out.println(\或者创建实例再调用,或者改成static方法

}

public String doSomething() {

return \ } }

有错吗?若有,错在哪里?

19. 此处,Something类的文件名叫OtherThing.java

class Something

{编译没错,执行有错

private static void main(String[] something_to_do)

{

System.out.println(\

}

}

有错吗?若有,错在哪里? 20. class Test

{

public static void main(String[] args) {

String s=new String(\ String ss=method(s); System.out.println(ss); }

public static String method(String s) {

s+=\没有返回值,应该加return }

}

程序返回的结果是什么?

21.class Compare

{

public static void main(String [] args) {

String str1=new String(\ String str2=new String(\

String str3=str1; String str4=”bcd”; String str5=”bcd”;

if(str1==str2) {

System.out.println(\ } else {

System.out.println(\ }

if(str1==str3) {

System.out.println(\ } else {

System.out.println(\

}

if(str4==str5) {

System.out.println(\ } else {

System.out.println(\

}

if(str1.equals(str2)) {

System.out.println(\ } else {

System.out.println(\ }

if(str1.equals(str3)) {

System.out.println(\ }

else {

System.out.println(\ } }

}

程序的运行结果是什么?

str1!=str2 str1==str3 str4==str5 str1 equal str2 str1 equal str3

23.给出下面的代码片断

1) public void create() { 2) Vector myVect;

3) myVect = new Vector(); 4) }

下面的哪些陈述为true(真)?

① 第二行的声明不会为变量myVect分配内存空间。

② 第二行的声明分配一个到Vector对象的引用的内存空间。 ③ 第二行语句创建一个Vector类对象。 ④ 第三行语句创建一个Vector类对象。

⑤ 第三行语句为一个Vector类对象分配内存空间。 创建对象就是分配空间么? 24.给出下面的不完整的类代码

class Person {

String name, department; int age;

public Person(String n){ name = n; }

public Person(String name, int age){ this.name = name; age = age; } public Person(String n, String d, int a) {

// doing the same as two arguments version of constructor // including assignment name=n,age=a department = d; } }

下面的哪些表达式可以加到构造方法中的\处? ① Person(n,a);

② this(Person(n,a)); ③ this(n,a);

④ this(name,age).

25.下面关于变量及其范围的陈述哪些是对的?

① 实例变量是类的成员变量。

② 实例变量用关键字static声明。

③ 在方法中定义的局部变量在该方法被执行时创建。 ④ 局部变量在使用前必须被初始化。 26.给出下面的代码:

1)public void modify() {

2) int i, j, k; 3) i = 100;

4) while ( i > 0 ) { 5) j = i * 2;

6) System.out.println (\7) k = k + 1; 8) i--; 9) } 10) }

哪些行在编译时可能产生错误。 ① line 4 ② line 6 ③ line 7 ④ line 8

27.下面有关变量及其作用域的陈述哪些是对的?

① 在方法里面定义的局部变量在方法退出的时候被撤销。 ② 局部变量也叫自动变量。

③ 在方法外面定义的变量(实例变量)在对象被构造时创建。 ④ 在方法中定义的方法的参变量只要该对象被需要就一直存在。 28.类的设计要求它的某个成员变量不能被外部类直接访问。应该使用下面的哪些修饰符获得需要的访问控制?

① public

② no modifier ③ protected ④ private

29.下面有关java代码安全性的叙述哪些是对的?

① 字节码校验器加载查询执行需要的所有类。 ② 运行时解释器执行代码。

③ 在运行时,字节码被加载,验证然后在解释器里面运行。

④ 类加载器通过分离本机文件系统的类和从网络导入的类增加安全性。

SL275中描述的Java程序运行的过程是这样的:类加载器(class loader)加载程序运行所需要的所有类,它通过区分本机文件系统的类和网络系统导入的类增加安全性,这可以限制任何的特洛伊木马程序,因为本机类总是先被加载,一旦所有的类被加载完,执行文件的内存划分就固定了,在这个时候特定的内存地址被分配给对应的符号引用,查找表(lookuo table)也被建立,由于内存划分发生在运行时,解释器在受限制的代码区增加保护防止未授权的访问;然后字节码校验器(byte code verifier)进行校验,主要执行下面的检查:类符合JVM规范的类文件格式,没有违反访问限制,代码没有造成堆栈的上溢或者下溢,所有操作 代码的参数类型都是正确的,没有非法的数据类型转换(例如将整型数转换成对象类型)发生;校验通过的字节码被解释器(interpreter)

执行,解释器在必要时通过运行时系统执行对底层硬件的合适调用。后三个答案是SL275中的原话。 30.给出下面的代码

public class Person{

static int arr[] = new int[10];

public static void main(String a[]) { System.out.println(arr[1];) } }

① 编译时将发生错误。

② 编译时正确但是运行时出错。 ③ 输出为0。 ④ 输出为null 31.给出下面的代码

public class Person {

double d1;

public static void main(String [] args) {

float f1=0.2f;

System.out.println(f1); System.out.println(f2);

} }

有错吗?若有,错在哪里?f2没定义 32.给出下面的代码

class Person {

private String name; Private int age;

void private Person(String name,int age)//有错误 {

this.name=name; this.age=age;

}

void getInfo() {

System.out.println(“I am chinese”); } }

class TestPerson {

public static void main(String [] args) {

Person p1=new Person();

P1.getInfo();//P大写了。。。

Person p2=new Person(“张三”,21);//此构造方法未定义 System.out.println(p2.name);

} }

有错吗?若有,错在哪里? 34.class StaticCode

{

static String country; static {

country=\

System.out.println(\ }

}

class TestStaticCode {

static {

System.out.println(\ }

public static void main(String [] args) {

System.out.println(\ new StaticCode(); new StaticCode(); }

}

程序是否有错?若有,错在哪里,若没有,程序执行的结果是什么?为什么? TestStaticCode is loading begin executing main method StaticCode is loading

35.class Outer

{

private int size=10; public class Inner {

public void doStuff() {

System.out.println(++size); } }

}

class TestInner {

public static void main(String[] args) {

Outer outer=new Outer();

Outer.Inner inner= new outer.Inner();//先实例外部类,再 实例内

部类,改为Outer.Inner inner= outer.new Inner();

outer.new Inner()

inner.doStuff(); }

}

程序是否有错?若有,错在哪里?

36.class InOut

{

String str=new String(\public void amethod(final int iArgs) {

int abc=8; class Bicycle {

public void sayHello() {

System.out.println(str); System.out.println(iArgs);

System.out.println(abc);//方法中定义的内部类只能访问方法的

final变量

}

}//End of Bicycle class

}//End of amethod }//End of InOut class

程序是否有错?若有,错在哪里,若没有,程序执行的结果是什么?为什么? 37.编写一个Java程序,定义一个表示学生的类Student,包括成员变量“学号”、“班级”、“姓名”、“性别”、“年龄”;方法“获得学号”、“获得班级”、“获得性别”、“获得姓名”、“获得年龄”和“修改年龄”。

本文来源:https://www.bwwdw.com/article/f4ta.html

Top