第五章单元测试
关于以下程序的说明,正确的是( )
1. class StaticStuff
2. {
3. static int x=10;
4. static { x+=5;}
5. public static void main(String args[ ])
6. {
7. System.out.println("x=" + x);
8. }
9. static { x/=3;}
10. }
以下程序编译和运行会发生什么
public class Q8 {
int i = 20;
static { int i = 10; }
public static void main(String[] args) {
Q8 a = new Q8();
System.out.println(a.i);
}
}
给出如下类定义:
public class test {
test(int k) { }
}
如果要创建一个该类的对象,正确的语句是:有如下代码:
public class Person { … }
下列哪个符合该类的构造方法定义
以下代码的输出结果?
public class Test{
static int x=5;
public static void main(String argv[]){
change(x);
x++;
System.out.println(x);
}
static void change(int m){
m+=2;
}}
设有如下程序:
public class Test5 {
public static void main (String args []) {
/* This is the start of a comment
if (true) {
Test5 = new test5();
System.out.println("Done the test");
}
/* This is another comment */
System.out.println ("The end");
}
}
结果为?
给出下面的不完整的类代码:
class Person {
String name, department;
int age;
public Person(String n){ name = n; }
public Person(String n, int a){ name = n; age = a; }
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;
}
}
下面的哪些表达式可以加到构造方法中的"doing the same as..."处?考虑如下类:
public class Test {
int j,k;
public Test(int j ) {
this(j,0);
}
public Test(int j, int k) {
this.j=j;
this.k=k;
}
}
以下哪些可正确创建Test对象?
A:4行与9行不能通过编译,因为缺少方法名和返回类型 B:编译通过,执行结果为:x=3 C:编译通过,执行结果为:x=5 D:9行不能通过编译,因为只能有一个静态初始化器
答案:编译通过,执行结果为:x=5
A:编译错误,静态初始化只能用于初始化目的 B:编译错误,变量 'i' 定义2次. C:输出 20. D:输出 10.
A:test obj1 = new test(); B:test obj1 = new test(5); C:test obj1 = new test(3.4); D:test obj1 = new test('5 ');
A:public Person() {…} B:public static void Person() {…} C:public void Person() {…} D:public int Person() {…}
A:6 B:8 C:7 D:5
A:程序输出"Done the test"和 "The end" B:输出 "Done the test". C:程序编译错误. D: 程序输出"The end"
A:this(n,a); B:name=n;age=a; C:this(name,age); D:Person(n,a);
A:Test t = new Test(); B:Test t = new Test(1); C:Test t = new Test(1, 2); D:Test t = new Test(1, 2, 3);