|
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();
}
} |
|