zoukankan      html  css  js  c++  java
  • 笔试题

     巨人网络2013校园招聘Java程序员笔试题:

    1、改错题(指出错误之处并对其进行修改)

      1.1、下列代码的错误之处

      --

      --

    复制代码
     1 public class Question1 {
     2     /**
     3      * 判断是否为奇数
     4      * @param i
     5      * @return true 为奇数 false 为偶数
     6      */
     7     public static boolean isOdd(int i){
     8         return i%2==1;
     9     }
    10     /**
    11      * @param args
    12      */
    13     public static void main(String[] args) {
    14         for(int i=Integer.MIN_VALUE;i<=Integer.MAX_VALUE;++i){
    15             boolean isOdd=isOdd(i);
    16             System.out.println(String.format("i=%d,isOdd=%b", i, isOdd));
    17         }
    18     }
    19 } 
    复制代码

      1.2、下列代码的错误之处

      --

      --

    复制代码
    1 public class Question2 {
    2     public static void main(String[] args) {
    3         final long MICROS_PER_DAY=24*60*60*1000*1000;
    4         final long MILLIS_PER_DAY=24*60*60*1000;
    5         System.out.println(MICROS_PER_DAY/MILLIS_PER_DAY);
    6     }
    7 }
    复制代码

      1.3、下列代码的错误之处

      --

      --

    复制代码
    1 public class Question3 {
    2     public static void main(String[] args) {
    3         for(byte b=Byte.MIN_VALUE;b<Byte.MAX_VALUE;b++){
    4             if(b==0x90)
    5                 System.out.println("Joy!");
    6         }
    7     }
    8 }
    复制代码

      1.1错误:

        1、for语句是死循环; 

        2、判断int型是否为奇数return i%2==1错误,应该为:return i%2!=0;

        3、Mysuny这位朋友提出判断是否为奇数用 i&1最好,我表示赞成,return (i&1)==1;

      测试代码1:

    复制代码
     1 public class Question1Test2 {
     2     /**
     3      * 判断是否为奇数
     4      * @param i
     5      * @return true 为奇数 false 为偶数
     6      */
     7     public static boolean isOdd(int i){
     8         return i%2==1;
     9     }
    10     /**
    11      * @param args
    12      */
    13     public static void main(String[] args) throws Exception {
    14         for(int i=Integer.MAX_VALUE-5;i<=Integer.MAX_VALUE;++i){
    15             boolean isOdd=isOdd(i);
    16             System.out.println(String.format("i=%d,isOdd=%b", i, isOdd));
    17             Thread.sleep(500);
    18         }
    19     }
    20 }
    复制代码

      结果:

    复制代码
    1 i=2147483642,isOdd=false
    2 i=2147483643,isOdd=true
    3 i=2147483644,isOdd=false
    4 i=2147483645,isOdd=true
    5 i=2147483646,isOdd=false
    6 i=2147483647,isOdd=true
    7 i=-2147483648,isOdd=false
    8 i=-2147483647,isOdd=false
    9 i=-2147483646,isOdd=false   注意:这将引出此程序的第二个错误!
    复制代码
    复制代码
     1 public class Question1Test3 {
     2     public static void main(String[] args) {
     3         System.out.println("整数的最小值:"+Integer.MIN_VALUE);
     4         //    整数的最小值:-2147483648
    5 System.out.println("整数的最大值:"+Integer.MAX_VALUE); 6 // 整数的最大值:2147483647
    7 System.out.println("Integer.MAX_VALUE+1:"+(Integer.MAX_VALUE+1)); 8 // Integer.MAX_VALUE+1:-2147483648 9 } 10 }
    复制代码

      测试代码二:

    复制代码
     1 public class Question1Test5 {
     2     /**
     3      * 判断是否为奇数
     4      * @param i
     5      * @return true 为奇数 false 为偶数
     6      */
     7     public static boolean isOdd(int i){
     8         int j=i%2;
     9         System.out.println("i%2="+j);
    10         return j==1;
    11     }
    12     /**
    13      * @param args
    14      */
    15     public static void main(String[] args) {
    16         for(int i=-10;i<=0;++i){
    17             boolean isOdd=isOdd(i);
    18             System.out.println(String.format("i=%d,isOdd=%b", i, isOdd));
    19         }
    20     }
    21 }
    复制代码

      运行结果:

    复制代码
     1 i%2=0
     2 i=-10,isOdd=false
     3 i%2=-1
     4 i=-9,isOdd=false
     5 i%2=0
     6 i=-8,isOdd=false
     7 i%2=-1
     8 i=-7,isOdd=false
     9 i%2=0
    10 i=-6,isOdd=false
    11 i%2=-1
    12 i=-5,isOdd=false
    13 i%2=0
    14 i=-4,isOdd=false
    15 i%2=-1
    16 i=-3,isOdd=false
    17 i%2=0
    18 i=-2,isOdd=false
    19 i%2=-1
    20 i=-1,isOdd=false
    21 i%2=0
    22 i=0,isOdd=false
    复制代码

      通过测试二及其运行结果我们可以看到当为int型的数据时,与2的余数为   -1而不是1。

      正确写法:

    复制代码
     1 public class Question1Test4 {
     2     /**
     3      * 判断是否为奇数
     4      * @param i
     5      * @return true 为奇数 false 为偶数
     6      */
     7     public static boolean isOdd(int i){
     8         return i%2 != 0;
     9     }
    10     /**
    11      * @param args
    12      */
    13     public static void main(String[] args) {
    14         for(int i=Integer.MIN_VALUE;i<=Integer.MAX_VALUE;++i){
    15             boolean isOdd=isOdd(i);
    16             System.out.println(String.format("i=%d,isOdd=%b", i, isOdd));
    17             if(i == Integer.MAX_VALUE)
    18                 break;
    19         }
    20     }
    21 }
    复制代码

    public class odd {
    /**
    * 判断是否为奇数
    * @return true 为奇数 false 为偶数
    */
    public static boolean isOdd(int i){
    return i%2 != 0;
    }


    public static void main(String[] args) {

    for(int i=Integer.MIN_VALUE;i<=Integer.MAX_VALUE;++i){

    boolean isOdd=isOdd(i);
    System.out.println(String.format("i=%d,isOdd=%b", i, isOdd));

    if(i == Integer.MAX_VALUE) break;
    }
    }


    }

      1.2错误:int类型数值计算超出范围的问题

      1.2的运行结果是:5,大家是不是感觉很奇怪啊,为什么不是1000呢?

      测试程序1如下:

    复制代码
     1 public class Question2Test {
     2     public static void main(String[] args) {
     3         int i = 24 * 60 * 60 * 1000 * 1000;
     4         long li = 24 * 60 * 60 * 1000 * 1000;
     5         long l = 24 * 60 * 60 * 1000 * 1000L;
     6         System.out.println("i=" + i);
     7         //    i=500654080
     8         System.out.println("li=" + li);
     9         //    li=500654080
    10         System.out.println("l=" + l);
    11         //    l=86400000000
    12         System.out.println(Integer.MAX_VALUE);
    13         //    2147483647
    14     }
    15 }
    复制代码

      从测试结果我们可以看出:24*60*60*1000*1000 的结果明显超出了int类型的表达范围,在运算的过程中运算结果仍然为int型,超出范围就截取后64位作为运算的结果。因此,我们看到虽然定义了long型变量li,但结果仍然是截取后的结果。

      测试程序1中仍然存在问题,我们在测试程序2中指出。

      测试程序2如下:

    复制代码
     1 public class Question2Test2 {
     2     public static void main(String[] args) {
     3         long l1 = 24*60*60*1000*1000*1000L;
     4         long l2 = 24L*60*60*1000*1000*1000;
     5         System.out.println(l1);
     6         //    500654080000
     7         System.out.println(l2);
     8         //    86400000000000
     9     }
    10 }
    复制代码

      我想大家都可以看懂我写测试程序2的用意,我就不在多说了。。。

      正确写法如下:

    复制代码
    1 public class Question2Test3 {
    2     public static void main(String[] args) {
    3         final long MICROS_PER_DAY=24L*60*60*1000*1000;
    4         final long MILLIS_PER_DAY=24L*60*60*1000;
    5         System.out.println(MICROS_PER_DAY/MILLIS_PER_DAY);
    6         //    1000
    7     }
    8 }
    复制代码

      1.3正确     (本人这么觉得)

      测试程序1:

    复制代码
     1 public class Question3Test1 {
     2     public static void main(String[] args) {
     3         System.out.println("byte类型的最大值:"+Byte.MAX_VALUE);
     4         //    byte类型的最大值:127
     5         for(byte b=(byte)(Byte.MAX_VALUE-5);b<Byte.MAX_VALUE;b++){
     6             System.out.println("b="+b);
     7             if(b==0x90)
     8                 System.out.println("Joy!");
     9         }
    10     }
    11 }
    复制代码

      运行结果:

    1 b=122
    2 b=123
    3 b=124
    4 b=125
    5 b=126

      由于1.3程序的for循环中 b<Byte.MAX_VALUE  而并不是 b<=Byte.MAX_VALUE ,所以没有出现1.1中的错误。

      有人觉得0x90超出了byte类型的表示范围[-128,127],但是我并不觉得在这里是错误,因为题目也没有特殊要求。

  • 相关阅读:
    BZOJ1146:[CTSC2008]网络管理
    AtCoder Grand Contest 004 C:AND Grid
    BZOJ3295:[CQOI2011]动态逆序对
    AtCoder Regular Contest 070F:Honest Or Unkind
    BZOJ3110:[ZJOI2013]K大数查询
    BZOJ3196:二逼平衡树
    浅谈splay
    BZOJ3938:Robot
    浅谈标记永久化
    AtCoder Regular Contest 068E:Snuke Line
  • 原文地址:https://www.cnblogs.com/java2016/p/5371746.html
Copyright © 2011-2022 走看看