精选20道Java代码笔试题

2023-06-26,

1、运算符优先级问题,下面代码的结果是多少?

public class Test {  

    public static void main(String[] args) {  

        int k = 0;  

        int ret = ++k + k++ + ++k + k;  

        // ret的值为多少  

        System.err.println(ret);  

    }  

}

  

 

解答:主要考察++i和i++的区别。++在前则先自增再赋值运算,++在后则先赋值再自增运算。因此,结果为8。

2、运算符问题,下面代码分别输出什么?

public class Test {  

    public static void main(String[] args) {  

        int i1 = 10, i2 = 10;  

        System.err.println("i1 + i2 = " + i1 + i2);  

        System.err.println("i1 - i2 = " + i1 - i2);  

        System.err.println("i1 * i2 = " + i1 * i2);  

        System.err.println("i1 / i2 = " + i1 / i2);  

    }  

}

  

 

解答:主要考察两点,运算符的优先级、字符串与数字中的+为连接符号。

第一条中,都是相加,则从前到后的顺序运算,字符串与数字相加,连接为一个字符串,再与后面的数字相加,再次连接为字符串,因此结果为“i1 + i2 = 1010”。

第二条编译错误,字符串无法与数字用减号连接。

第三条、第四条中乘除的优先级高,会先运算,而后再与字符串连接,因此结果分别为:“i1 * i2 = 100”、“i1 * i2 = 1”。

3、下面代码的结果是什么?

public class Test {  

    public void myMethod(String str) {
System.err.println("string");
} public void myMethod(Object obj) {
System.err.println("object");
} public static void main(String[] args) {
Test t = new Test();
t.myMethod(null);
}
}

  

 

解答:这道题考察重载方法参数具有继承关系时的调用问题,还有对null的认识。如果是一般具有继承关系的对象分别作为参数,看对象的引用,如:

class A {
} class B extends A {
} public class Test { public static void main(String[] args) {
A b1 = new B();
B b2 = new B();
get(b1);// A
get(b2);// B
} public static void get(A a) {
System.out.println("A");
} public static void get(B a) {
System.out.println("B");
}
}

  

 

这道题中,Object是一切类的父类,具有继承关系,那null是指向什么呢?null是任何引用类型的初始值,String和Object的初始值都是null,但是null会优先匹配引用类型参数为String的方法,因此这道题答案是string。假设这道题中还有其他同是引用类型的重载方法呢?如:

public void myMethod(Integer obj) {  

    System.err.println("Integer");  

}  

如果是这样的话,调用这个方法传入参数null时会报错,他不知道选哪个方法进行匹配调用了。

4、假设今天是9月8日,下面代码输出什么?

public class Test {  

    public static void main(String[] args) {  

        Date date = new Date();  

        System.err.println(date.getMonth() + " " + date.getDate());  

    }  

}

  

 

解答:这道题考察的是日期中获取的月份是从0开始的,因此会比我们日常的月份少1,这个题答案是8 8。

5、下面代码的输出结果是什么?

public class Test {  

    public static void main(String[] args) {  

        double val = 11.5;  

        System.err.println(Math.round(val));  

        System.err.println(Math.floor(val));  

        System.err.println(Math.ceil(val));  

    }  

}

  

 

解答:这个是在考Math取整数的三种方法。round()是四舍五入取证,floor()是舍去小数位,ceil()是向上进一位。floor是地板ceil是天花板,一个在下,则舍去,一个在上,则向上进1。那是不是结果应该为12、11、12呢?还要考虑返回值类型,round()返回值类型为long长整型,floor()和ceil()返回值的是double类型,因此正确的答案应该是12、11.0、12.0。

6、编程输出一个目录下的所有目录及文件名称,目录之间用tab。

public class Test {  

    public static void main(String[] args) {  

        new Test().read("D:/test", "");  

    }  

    public void read(String path, String tab) {  

        File file = new File(path);  

        File[] childFiles = file.listFiles();  

        for (int i = 0; childFiles != null && i < childFiles.length; i++) {  

            System.err.println(tab + childFiles[i].getName());  

            if (childFiles[i].isDirectory()) {  

                read(childFiles[i].getPath(), tab + "\t");  

            }  

        }  

    }  

}  

  

这个主要是考察IO部分知识点了。

7、从键盘读入10个整数,然后从大到小输出。

public class Test {  
    public static void main(String[] args) {
Scanner in = new Scanner(System.in); // 注意这里的数组,不是int的
Integer[] arr = new Integer[10]; for (int i = 0; i < 10; i++) {
arr[i] = in.nextInt();
} Arrays.sort(arr, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (o1 > o2) return -1;
if (o1 < o2) return 1;
return 0;
}
});
System.err.println(Arrays.toString(arr));
}
}

  

 

8、下面代码的结果是什么?

public class Test extends Base {  

    public static void main(String[] args) {
Base b = new Test();
b.method();
Test t = new Test();
t.method();
} @Override
public void method() {
System.err.println("test");
}
} class Base {
public void method() throws InterruptedException {
System.err.println("base");
}
}

  

 

解答:两次调用输出都是test。多态的情况下,尽管是父类的引用,调用方法时,还是调用子类的方法。

9、以下代码的结果是什么?

package test;
public class Test extends Base {
public static void main(String[] args) {
new Test().method();
} public void method() {
System.err.println(super.getClass().getName());
System.err.println(this.getClass().getSuperclass().getName());
}
} class Base { }

  

  

解答:第一个输出test.Test、第二个输出test.Base。super很容易让人以为也是调用了父类,实际上还是本类。

10、true or false?

public class Test {  

    public static void main(String[] args) {  

        String str1 = new String("abc");  

        String str2 = new String("abc");  

        System.err.println(str1.equals(str2));  

        StringBuffer sb1 = new StringBuffer("abc");  

        StringBuffer sb2 = new StringBuffer("abc");  

        System.err.println(sb1.equals(sb2));  

    }  

}

  

 

解答:第一个true,第二个false。String重写了Object中的equals方法,会将string拆分为字符数组,逐个比较各个字符,代码如下:

public boolean equals(Object anObject) {

        if (this == anObject) {

            return true;

        }

        if (anObject instanceof String) {

            String anotherString = (String)anObject;

            int n = value.length;

            if (n == anotherString.value.length) {

                char v1[] = value;

                char v2[] = anotherString.value;

                int i = 0;

                while (n-- != 0) {

                    if (v1[i] != v2[i])

                        return false;

                    i++;

                }

                return true;

            }

        }

        return false;

    }

  


Object中的equests方法如下:     public boolean equals(Object obj) {         return (this == obj);     }  

11、输出的结果是什么?

public class Test {  

    public static void main(String[] args) {
System.err.println(new Test().method1());
System.err.println(new Test().method2());
} public int method1() {
int x = 1;
try {
return x;
} finally {
++x;
}
} public int method2() {
int x = 1;
try {
return x;
} finally {
return ++x;
}
}
}

  

 

解答:第一个返回1,第二个返回2。finally中的代码是一定会被执行的且在try中的代码执行完之后,因此若在其中return返回,会覆盖掉try中的返回值。

如果这样呢?

public class Test {  

    public static void main(String[] args) {
System.err.println(method());
} public static boolean method() {
try {
return true;
} finally {
return false;
}
}
}

  

 

很明显返回值应该为false。

12、方法m1和m2有区别吗?

public class Test {  

    public static void main(String[] args) {
} public synchronized void m1() {
} public static synchronized void m2() {
}
}

  

 

解答:这里考察的是同步方法的问题。synchronized修饰方法时锁定的是调用该方法的对象。它并不能使调用该方法的多个对象在执行顺序上互斥,静态修饰符很有必要。因此当不适用静态时,创建多个对象执行该方法,锁都不一样,还同步什么呢,因此用static修饰后才能实现想要的效果。

13、true or false?

public class Test {  

    public static void main(String[] args) {  

        Integer i1 = 127;  

        Integer i2 = 127;  

        System.err.println(i1 == i2);  

        i1 = 128;  

        i2 = 128;  

        System.err.println(i1 == i2);  

    }  

}

  

 

解答:第一个为true,第二个为false。这个是对Integer包装类型中应用的享元模式的考察。

14、true or false?

public class Test {  

    public static void main(String[] args) {  

        String str1 = "a";  

        String str2 = "a";  

        String str3 = new String("a");  

        System.err.println(str1 == str2);  

        System.err.println(str1 == str3);  

        str3 = str3.intern();  

        System.err.println(str1 == str3);  

    }  

}

  

 

解答:这个是对String Pool的考察。答案为true、false、true

15、true or false?

public class Test {  

    public static void main(String[] args) {  

        System.err.println(12 - 11.9 == 0.1);  

    }  

}

  

 

解答:结果为false。这个题我只说下我的想法,12-11.9进行运算后会转换成对象,不在是基本数据类型,因此在进行恒等判断时,就会是false。

16、以下代码输出是什么?

public class Test {  

    public static void main(String[] args) {  

        BigInteger one = new BigInteger("1");  

        BigInteger two = new BigInteger("2");  

        BigInteger three = new BigInteger("3");  

        BigInteger sum = new BigInteger("0");  

        sum.add(one);  

        sum.add(two);  

        sum.add(three);  

        System.out.println(sum.toString());  

    }  

}

  

 

解答:这个是对大整数的考察。结果是不是6呢?看起来好像没毛病,其实不然。sum.add(one)与我们基本类型的sum+=one可不同,前者不会讲结果赋值给sum对象,结果被赋值了这条语句的返回值。因此不管怎么add,sum对象的值是没有变化的,因此结果为0。

18、如何迭代Map容器?

---------------------------法1------------------------------
Map<String, String> map = new HashMap<>(); Set<Map.Entry<String, String>> entrySet = map.entrySet(); for(Map.Entry<String, String> entry : entrySet){ String key = entry.getKey(); String value = entry.getValue(); }
-----------------------法2----------------------------------- Set<String> keySet = map.keySet(); Iterator<String> it1 = keySet.iterator(); if(it1.hasNext()) System.out.println(it1.next()); -----------------------法3---------------------------------------
Collection<String> values = map.values(); Iterator<String> it2 = values.iterator(); if(it2.hasNext()){
System.out.println(it2.next());

  

19、以下代码输出的结果

 
 public class Test {  

        public static void main(String[] args) {  

            System.err.println(args.length);  

        }  

    }

  

 

/*

A. null     B. 0        C. Test

D. Exception in thread "main" java.lang.NullPointerException

*/

解答:0.

20、下面为一个单例的实现代码,请指出代码中有几个错误或不合理之处,并改正。

  
  public class Test {  

        public Test instance = null;  

        public static Test getInstance() {
if (instance == null) {
instance = new Test();
return instance; } } }

  

解答:单例模式要满足三点:1、私有化构造方法;2、创建私有静态对象;3、提供公有静态方法获取唯一实例。

因此错误包含,1构造函数没有私有化,2对象非私有静态,3获取实例的方法中return不应包含在条件中。

精选20道Java代码笔试题的相关教程结束。

《精选20道Java代码笔试题.doc》

下载本文的Word格式文档,以方便收藏与打印。