}
请问运行完test类后stu对象的age值是多少?15分 答案: 1. 2. 3.
2、写出如下代码运行效果并请分析出原因(20分) class student{ String name; int age;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public student returnS(){ return this; } }
public class test {
public static void main(String[] args){ student s2=new student(); s2.setAge(40);
s2.setName(\张三\
student s3=s2.returnS(); s3.setAge(200);
System.out.println(\ } } 答案:
3、请分析程序分别运行到第一处和第二处的内存中各对象的状态。25分 public class Count {
private int serialNumber; private static int counter = 0; public Count() { counter++;
serialNumber = counter; System.out.println(\ } public static void main(String args[]) { System.out.println(\ Count count1=new Count();
System.out.println(\creat count1, counter=\//第一处 Count count2=new Count(); System.out.println(\
System.out.println(\
System.out.println(\
System.out.println(\
System.out.println(\//第二处
System.out.println(\ } }
答案:
4、请分析实例化disp对象时,JVM的加载执行过程20分 public class disp { static{ System.out.println(\类加载\}
disp(){ System.out.println(\实例初始化\} public static void main(String[] args) { System.out.println(\开始\ disp d1=new disp();
disp d2=new disp();
System.out.println(\结束\ } }
答案:
5、请指出下列代码的错误处5分
1 public class GeneralFunction { 2 private int x; 3 private static int y; 4 public static int addUp(int x) { 5 return x + y;
6 }
7 public int add (int x) { 8 return x + y; 9 }
10 public int add_1 (int x) { 11 return x + this.x; 12 }
13 public static int add_2 (int y) { 14 return x + y; 15 } 16 } 答案:
6、单例模式的设计(此题不计入总分)
请设计一个类”onlyInstance”,该类有个方法”public static onlyInstance getOnlyInstance()”用来获得该类的实例对象。现要求通过该方法获得的实例对象是唯一的、只要一个。即在 onlyInstance only1= onlyInstance.getOnlyInstance(); onlyInstance only2= onlyInstance.getOnlyInstance();
后,only1= =only2。同时要求该类不可以在类外部被实例化!