基础测试题
卷面上不能出现任何的涂写文字,所有的答案要求写在答题纸上,考卷不得带走。
选择题
1、 What will happen when you attempt to compile and run the following code? (D)
public class Static {
static {
int x = 5; // 在static内有效
}
static int x, y; // 初始化为0
public static void main(String args[]) {
x--; // -1
myMethod();
System.out.println(x + y + ++x);
}
public static void myMethod() {
y = x++ + ++x; // y=-1+1 x=1
}
}
A. compiletime error B. prints: 1 C. prints: 2 D、 prints: 3 E、 prints: 7 F、 prints: 8
将以上的
程序分步进行执行:
1、 static int x, y; ? x = 0 ; y = 0 ;
2、 x--; ? x = -1 ; y = 0 ;
3、 y = x++ + ++x; ? x = 1 ; y = 0 ;
4、 System.out.println(x + y + ++x); ? 3
2、 Given the following code, what will be the output? (A)
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 + " " + i);
}
}
A、 15 0 20 B、 15 0 15 C、 20 0 20 D、 0 15 20
3、 What will happen when you attempt to compile and run the following code? (A)
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 E、 compile error F、 none of the above
class MyParent {
int x, y;
MyParent(int x, int y) { // 10 20
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) { // 10 20 30
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;
// 10 10 20 20 30 30 --> 120 + 120
}
public int addMe(MyChild myChi) {
retur
n 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); // 120
int y = myChi.addMe(myChi); // 120
int z = myPar.addMe(myPar); // 60
System.out.println(x + y + z);
}
}
4、 What will happen when you attempt to compile and run the following code? (E)
interface MyInterface {
}
public class MyInstanceTest implements MyInterface {
static String s;
public static void main(String args[]) {
MyInstanceTest t = new MyInstanceTest();
if (t instanceof MyInterface) {
System.out.println("I am true interface");
} el