容易犯错的面试题

  1     public static void main(String[] args) {
  2 // Exams 1:
  3         {
  4             System.out.println("examples  "+count++ +":   ");
  5             x--;
  6             System.out.println(x);
  7             myMethod();
  8             System.out.println(x+y+++x);
  9         }
 10 //Exams 2:
 11         {
 12             System.out.println("examples  "+count++ +":   ");
 13             int j=0,temp=0;
 14             for(int i=0;i<100;i++)
 15             {
 16                  j=j++;
 17                  //java中间缓存变量机制 与以下等同
 18     //             temp=j;
 19     //             j=j+1;
 20     //             j=temp;
 21             }
 22             System.out.println(j);
 23         }
 24 //Exams 3:
 25         System.out.println("examples  "+count++ +":   ");
 26         {
 27             int a=5,b=5;
 28             if(!(a==b)&&(a==1+b++))                //前半段为false的话,后半段就不执行了
 29             {}
 30             System.out.println("a="+a+"    b="+b);
 31         }
 32 //Exams 4:
 33         {
 34             System.out.println("examples  "+count++ +":   ");
 35             char a='0';
 36             int b=a;
 37             int l=3;
 38             short s=1;
 39             s+=1;            
 40             //    a=a+1会报错,因为a+1为int转换,而 a为short,不能直接赋值给short
 41             //    a+=1是可以的
 42             String bb="";
 43             assert(l==4):bb;
 44             System.out.println(bb);
 45             
 46         }
 47 //Exams 5:        
 48         {
 49             System.out.println("examples  "+count++ +":   ");
 50             //注意到10.9 所以9也会进行转换变成9.0
 51             int a=5;
 52             System.out.println("value is "+((a<5)?10.9:9));
 53         }
 54 //Exams 6:        
 55         {
 56             System.out.println("examples  "+count++ +":   ");
 57             
 58             //i是一个int类型的,所以x的int值为120,x被提升为int类型了
 59             
 60             //java规范提到:当后两个表达式有一个是常量表达式(本题10),另一个类型是T,
 61             //而常量表达式可以被T表示的时候,输出结果是T类型
 62             char x='x';
 63             int i=10;
 64             System.out.println(false?i:x);
 65             System.out.println(false?10:x);
 66             
 67         }
 68 //Exams 7:        
 69         {
 70             System.out.println("examples  "+count++ +":   ");
 71             //移位运算的参数要先进行模的32运算,并且是对二进制的操作
 72             Integer num=32;
 73             System.out.println(num>>6);
 74             
 75             System.out.println(Integer.toBinaryString(num>>32));
 76         }
 77         
 78 //Exams 8:        
 79         {
 80             System.out.println("examples  "+count++ +":   ");
 81             
 82             try
 83             {
 84                 System.out.println("to the try");
 85                 int i=3/0;
 86                 //finally 依然执行的
 87                 //try中有return,finally会被执行且在return之前执行,
 88                 //因为return了之后函数会被销毁,所以是之前
 89                 return ;
 90             }
 91             catch(Exception e)
 92             {
 93                 System.out.println("to the catch");
 94             }
 95             finally
 96             {
 97                 System.out.println("execute finally");
 98             }
 99         }
100 //Exams 9:        
101             {
102                 System.out.println("examples  "+count++ +":   ");
103                 Testiplusplus x1=new Testiplusplus(1);
104                 changeValue(x1);
105                 System.out.println(x1.testNum);
106             }
107 //Exams 10:    
108             
109             {
110                 System.out.println("examples  "+count++ +":   ");
111                 //    static int p;  编译会出错,局部变量,要放类内,算全局变量不赋初值才行
112                 System.out.println(p);
113             }
114             
115 //Exams 11:    
116             
117             {
118                 System.out.println("examples  "+count++ +":   ");
119             
120             }
121     }

 

posted on 2014-03-07 18:31  wf110  阅读(453)  评论(0编辑  收藏  举报