LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 961|回复: 10

java interview test

[复制链接]
发表于 2005-7-22 09:36:10 | 显示全部楼层 |阅读模式
For more friendly page,pls see:http://blog.chinaunix.net/articl ... 040&blogId=1096

最近经历过的一些面试的笔试题,凭记忆写下来一些,不上机的情况下你能做出多少。


--------------------------------------------------------------------------------

写出下列各题的输出结果,并简要分析其所以然,指出其考查点。

public class Exam1{
public static String a="example 1";

public final static void main(String[] args ){
Exam1 e=new Exam1();
e=null;
System.out.println("e.a="+e.a);

}


}
public class Exam2{
public static  int step=0;
public Exam2(){}

public void runExam(Exam2 e){
  e.step++;
}

public final static void main(String args[]){
Exam2 e1=new Exam2();
e1.step=25;
e1.runExam(e1);
System.out.println(e1.step);
Exam2 e2=new Exam2();
e2.step=1;
e2.runExam(e2);
System.out.println(e2.step);


}
}
public class Exam3{
String s="a";
public Exam3(){
}

public static final void main(String args[]){
Object o =(Object)new Exam3();
Exam3 e=(Exam3)o;
System.out.println(e.s);
}


}
public class Exam4{
static void setStr(String s){ s="a";}
public static final void main(String args[]){
String ss="hello world";
setStr(ss);
System.out.println(ss);
}

}
public class Exam5{
static void leftshift(int i,int j){
i<<=j;
}

public static final void main(String args[]){
int i=4,j=2;
leftshift(i,j);
System.out.println(i);

}

}
public class Exam6{
static void runSwitch(int i){
switch(i){
  case 1:
    System.out.println("one");
    break;
  case 2:
    System.out.println("two");
  case 3:
    System.out.println("three");
    break;
  default:
  System.out.println("default");
}

}

public static final void main(String args[]){
runSwitch(2);

}
}
public class Exam7{
public Exam7(){

Exam7 e1=this;
Exam7 e2=this;
synchronized(e1){
  try{
      e2.wait();
      System.out.println("e2 waiting!");
    }catch(InterruptedException e){
      System.out.println("interruped exception!");
    }catch(Exception e){
      System.out.println("other exception!");
    }finally{
      System.out.println("finally!");
    }
}
System.out.println("all done!");
}


public final static void main(String args[]){
new Exam7();

}

}
public class Exam8{
  public static final void main(String args[]){
    int i=0,j=5;
    tp:for(;;){
      i++;
      for(;;)
        if(i>--j)
          break tp;
    }
   
    System.out.println("i="+i+",j="+j);

  }

}
public class Exam9{
static String s="Test";

public static final void main(String args[]){
Exam9 e1=new Exam9();
System.out.println(s);
System.out.println(e1.s);
e1.s="test1";
System.out.println(s);
System.out.println(e1.s);

Exam9 e2=new Exam9();
System.out.println(s);
System.out.println(e2.s);



}

}
public class Exam10{

public String s="test";
public char ch[]={'a','b','c'};

void change(String s,char[] ch){
s="no test";
ch[0]='g';
}

public static final void main(String args[]){
Exam10 e=new Exam10();
e.change(e.s,e.ch);
System.out.println("e.s="+e.s+",e.ch"+e.ch);

}
}
public class one{
protected void printA(){
System.out.println("one print A");

}
protected void printB(){
System.out.println("one print B");

}

protected void printAll(){
printA();
printB();
}


}
public class two extends one{
protected void printA(){
System.out.println("two print A");

}
protected void printB(){
System.out.println("two print B");

}

public final static void main(String[] args){
two t=new two();
t.printAll();

}

}
package test;

public class Test1{
private String s="private varible in test1!";
String s1="default varible in test1";

public static final void main(String args[]){
Test1 t=new Test1();
System.out.println(t.s);
System.out.println(t.s1);

}

}
package test;
public class Test2{

public static final void main(String args[]){
Test1 t1=new Test1();
System.out.println(t1.s1);
//System.out.println(t1.s);

}
}
public class While{
public static final void main(String[] args){
int  i=1,j=10;
do {
if(i++<--j)continue;

}while(i<5);
System.out.println("i="+i+",j="+j);

}
}
public class A{
public A(){System.out.println("In structure A.");}
static{
System.out.println("static a");

}

public void printout(){
System.out.println("print a method.");
}

/*public static final void main(String[] args){
A a=new A();   


}
*/
}
public class B extends A{
public B(){System.out.println("In structure B.");}
static {
System.out.println("static b");
}

public void printout(){
System.out.println("print b method.");
}

public static final void main(String[] args){

System.out.println("Begin...");
B b=new B();
b.printout();
A a=new B();
a.printout();
A aa=new A();
aa.printout();
}


}
public class Parent{
protected void test(){
System.out.println("Test in Parent!");
}

}
public class Child extends Parent{
private void test(){
System.out.println("Test in Child!");

}

public static final void main(String args[]){
Parent p=new Child();
p.test();

}
}
发表于 2005-7-22 16:03:35 | 显示全部楼层
Exam 1:
e.a=example 1
回复 支持 反对

使用道具 举报

发表于 2005-7-22 16:05:32 | 显示全部楼层
Exam 2:
26
2
回复 支持 反对

使用道具 举报

发表于 2005-7-22 16:06:36 | 显示全部楼层
Exam 3:
a
回复 支持 反对

使用道具 举报

发表于 2005-7-22 16:08:00 | 显示全部楼层
Exam 4:
hello world
回复 支持 反对

使用道具 举报

发表于 2005-7-22 16:11:08 | 显示全部楼层
Exam 5:
4
回复 支持 反对

使用道具 举报

发表于 2005-7-22 16:17:15 | 显示全部楼层
Exam 6:
two
three
回复 支持 反对

使用道具 举报

发表于 2005-7-22 16:22:30 | 显示全部楼层
Exam 7:
deadlock
回复 支持 反对

使用道具 举报

发表于 2005-7-22 16:27:42 | 显示全部楼层
Exam 9:
Test
Test
test1
test1
test1
test1
回复 支持 反对

使用道具 举报

发表于 2005-7-22 16:29:33 | 显示全部楼层
Exam 10:
e.s=test ,e.ch[g,b,c]
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表