java面试题1

来源:互联网 发布:淘宝造物节线上活动 编辑:程序博客网 时间:2024/05/28 23:11
  1. import java.util.Random;  
  2. /** 
  3.  *  
  4.  * @author Alexs 
  5.  */  
  6. /* 
  7.  * Demo1 
  8.  */  
  9. class Demo1 {  
  10.     public static void main(String[] args) {  
  11.         int num=2147483647;  
  12.         long temp=num+2L;  
  13.         System.out.println(temp);//2147483649  
  14.     }  
  15. }  
  16. /* 
  17.  * Demo2 
  18.  */  
  19. class Demo2{  
  20.     public static void main(String[] args) {  
  21.         char c='A';  
  22.         int num=10;  
  23.         switch(c) {  
  24.         case 'B':  
  25.             num++;  
  26.         case 'A':  
  27.             num++;  
  28.         case 'Y':  
  29.             num++;  
  30.             break;  
  31.         default:  
  32.             num--;  
  33.         }  
  34.         System.out.println(num);//12  
  35.     }  
  36. }  
  37. /* 
  38.  * Demo3 
  39.  */  
  40. class Demo3{  
  41.         public static void main(String[] args) {  
  42.             int x=10;  
  43.             double y=20.2;  
  44.             long z=10L;  
  45.             String str=""+x+y*z;  
  46.             System.out.println(str);//10202.0  
  47.         }  
  48. }  
  49. /* 
  50.  * Demo4 
  51.  */  
  52. class Demo4{  
  53.     public static void main(String[] args) {  
  54.         int sum=0;  
  55.         for(int x=0;x<10;x++) {  
  56.             sum+=x;  
  57.             if(x%3==0) {  
  58.                 break;  
  59.             }  
  60.         }  
  61.         System.out.println(sum);//0  
  62.     }  
  63. }  
  64. /* 
  65.  * Demo5 
  66.  */  
  67. class Demo5{  
  68.     public static void main(String[] args) {  
  69.         System.out.println(inc(10)+inc(8)+inc(-10));//35  
  70.     }  
  71.     public static int inc(int temp) {  
  72.         if(temp>0) {  
  73.             return temp*2;  
  74.         }  
  75.         return -1;  
  76.     }  
  77. }  
  78. /* 
  79.  * Demo6 
  80.  */  
  81. class Demo6{  
  82.     public static void main(String[] args) {  
  83.         boolean flag=10%2==1 && 10/3==0 && 1/0==0;  
  84.         System.out.println(flag?"A":"B");//B  
  85.     }  
  86. }  
  87. /* 
  88.  * Demo7 
  89.  */  
  90. class Demo7{  
  91.     public static void main(String[] args) {  
  92.         int num=50;  
  93.         num=num++*2;  
  94.         System.out.println(num);//100  
  95.     }  
  96. }  
  97. /* 
  98.  * Demo8 
  99.  */  
  100. class Demo8{  
  101.     public static void main(String[] args) {  
  102.         int i=1;  
  103.         int j=i++;  
  104.         if(i==(++j)&&((i++)==j)) {  
  105.             i+=j;  
  106.             System.out.println("i="+i);//5  
  107.         }  
  108.     }  
  109. }  
  110. /* 
  111.  * Demo9 
  112.  */  
  113. class Demo9{  
  114.     public static void main(String[] args) {  
  115.         String str="";  
  116.         for(int x=0;x<5;x++) {  
  117.             str+=x;  
  118.         }  
  119.         System.out.println(str);//01234  
  120.     }  
  121. }  
  122. /* 
  123.  * Demo10 
  124.  */  
  125. class Demo10{  
  126.     public int a() {  
  127.         int i=0;   
  128.         i++;  
  129.         return i;  
  130.     }  
  131.     public static void main(String[] args) {  
  132.         Demo10 d=new Demo10();  
  133.         d.a();  
  134.         int j=d.a();  
  135.         System.out.println(j);//1  
  136.     }  
  137. }  
  138. /* 
  139.  * Demo11 
  140.  */  
  141. class supers{  
  142.     String name;  
  143.     public supers(){};  
  144.     public supers(String name) {  
  145.         this.name=name;  
  146.     }  
  147.     public void fun1() {  
  148.         System.out.println("this is class super!"+name);  
  149.     }  
  150. }  
  151.   
  152. class sub extends supers{  
  153.       
  154.     public void fun1() {  
  155.         System.out.println("this is class sub!"+name);  
  156.     }  
  157. }  
  158.   
  159. class Demo11{  
  160.     public static void main(String[] args) {  
  161.         supers s=new sub();  
  162.         s.fun1();//this is class sub!null  
  163.     }  
  164. }  
  165. /* 
  166.  * Demo12 
  167.  */  
  168. class Demo12{  
  169.     public static void main(String[] args) {  
  170.         int i=9;  
  171.         switch (i) {  
  172.         default:  
  173.             System.out.println("default");  
  174.         case 0:  
  175.             System.out.println("zero");  
  176.         case 1:  
  177.             System.out.println("one");  
  178.         case 2:  
  179.             System.out.println("two");  
  180.         }  
  181.         /* 
  182.          * default 
  183.          * zero 
  184.          * one 
  185.          * two 
  186.          */  
  187.     }  
  188. }  
  189. /* 
  190.  * Demo13 写一个单例模式 
  191.  */  
  192. class Demo13{  
  193.     //饿汉式  
  194.     private static final Demo13 d=new Demo13();  
  195.     private Demo13() {}  
  196.     public static Demo13 getInstance() {  
  197.         return d;  
  198.     }  
  199. }  
  200. class Demo13Test{  
  201.     public static void main(String[] args) {  
  202.         Demo13 d1=Demo13.getInstance();  
  203.         Demo13 d2=Demo13.getInstance();  
  204.         System.out.println(d1==d2);//true  
  205.     }  
  206. }  
  207. /* 
  208.  * Demo14 
  209.  * 生成一个随机密码,密码包含大小写字母及数字,并且可根据输入参数对密码长度进行调整 
  210.  */  
  211. class Demo14{  
  212.     //定义一下字符串常量  
  213.     private static final String CHAR_ALL="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";  
  214.     public String getRandomPwd(int num) {  
  215.         String result="";  
  216.         //实例化Random对象  
  217.         Random r=new Random();  
  218.         for(int i=0;i<num;i++) {  
  219.             //生成0-62之间的(小于62)int值  
  220.             int j=r.nextInt(CHAR_ALL.length());  
  221.             //截取下标跟跟int值对应的字符  
  222.             String singleChar=CHAR_ALL.substring(j, j+1);  
  223.             //拼接  
  224.             result+=singleChar;  
  225.         }  
  226.         return result;  
  227.     }  
  228.     public static void main(String[] args) {  
  229.         Demo14 d=new Demo14();  
  230.         System.out.println(d.getRandomPwd(6));  
  231.     }  
  232. }  
  233. /* 
  234.  * Demo15 
  235.  */  
  236. class Demo15 {  
  237.      public static void main(String []args) {  
  238.             int i=9;  
  239.             int j=9;  
  240.             //System.out.println(i==j);//true  
  241.             //System.out.println(j==i++);//true  
  242.             //System.out.println(j==++i);//false  
  243.             //System.out.println(j++==i);//true  
  244.             //System.out.println(++j==i);//false  
  245.      }  
  246. }
原创粉丝点击