java面试题整理

119 阅读5分钟

这是我参与 8 月更文挑战的第 26 天,活动详情查看: 8月更文挑战

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

  1. public class Test {
  2. public static void main(String[] args) {
  3. int k = 0;
  4. int ret = ++k + k++ + ++k + k;
  5. // ret的值为多少
  6. System.err.println(ret);
  7. }
  8. }

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

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

  1. public class Test {
  2. public static void main(String[] args) {
  3. int i1 = 10, i2 = 10;
  4. System.err.println("i1 + i2 = " + i1 + i2);
  5. System.err.println("i1 - i2 = " + i1 - i2);
  6. System.err.println("i1 * i2 = " + i1 * i2);
  7. System.err.println("i1 / i2 = " + i1 / i2);
  8. }
  9. }

解答:主要考察两点,运算符的优先级、字符串与数字中的+为连接符号。第一条中,都是相加,则从前到后的顺序运算,字符串与数字相加,连接为一个字符串,再与后面的数字相加,再次连接为字符串,因此结果为“i1 + i2 = 1010”。第二条是错误的,字符串无法与数字用减号连接。第三条、第四条中乘除的优先级高,会先运算,而后再与字符串连接,因此结果分别为:“i1 * i2 = 100”、“i1 * i2 = 1”。

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

  1. public class Test {
  2. public void myMethod(String str) {
  3. System.err.println("string");
  4. }
  5. public void myMethod(Object obj) {
  6. System.err.println("object");
  7. }
  8. public static void main(String[] args) {
  9. Test t = new Test();
  10. t.myMethod(null);
  11. }
  12. }

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

  1. class A {
  2. }
  3. class B extends A {
  4. }
  5. public class Test {
  6. public static void main(String[] args) {
  7. A b1 = new B();
  8. B b2 = new B();
  9. get(b1);// A
  10. get(b2);// B
  11. }
  12. public static void get(A a) {
  13. System.out.println("A");
  14. }
  15. public static void get(B a) {
  16. System.out.println("B");
  17. }
  18. }

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

  1. public void myMethod(Integer obj) {
  2. System.err.println("Integer");
  3. }

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

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

  1. public class Test {
  2. public static void main(String[] args) {
  3. Date date = new Date();
  4. System.err.println(date.getMonth() + " " + date.getDate());
  5. }
  6. }

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

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

  1. public class Test {
  2. public static void main(String[] args) {
  3. double val = 11.5;
  4. System.err.println(Math.round(val));
  5. System.err.println(Math.floor(val));
  6. System.err.println(Math.ceil(val));
  7. }
  8. }

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

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

  1. public class Test {
  2. public static void main(String[] args) {
  3. new Test().read("D:/test", "");
  4. }
  5. public void read(String path, String tab) {
  6. File file = new File(path);
  7. File[] childFiles = file.listFiles();
  8. for (int i = 0; childFiles != null && i < childFiles.length; i++) {
  9. System.err.println(tab + childFiles[i].getName());
  10. if (childFiles[i].isDirectory()) {
  11. read(childFiles[i].getPath(), tab + "\t");
  12. }
  13. }
  14. }
  15. }

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

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

  1. public class Test {
  2. public static void main(String[] args) {
  3. Scanner in = new Scanner(System.in);
  4. // 注意这里的数组,不是int的
  5. Integer[] arr = new Integer[10];
  6. for (int i = 0; i < 10; i++) {
  7. arr[i] = in.nextInt();
  8. }
  9. Arrays.sort(arr, new Comparator<Integer>() {
  10. @Override
  11. public int compare(Integer o1, Integer o2) {
  12. if (o1 > o2) return -1;
  13. if (o1 < o2) return 1;
  14. return 0;
  15. }
  16. });
  17. System.err.println(Arrays.toString(arr));
  18. }
  19. }


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

  1. public class Test extends Base {
  2. public static void main(String[] args) {
  3. Base b = new Test();
  4. b.method();
  5. Test t = new Test();
  6. t.method();
  7. }
  8. @Override
  9. public void method() {
  10. System.err.println("test");
  11. }
  12. }
  13. class Base {
  14. public void method() throws InterruptedException {
  15. System.err.println("base");
  16. }
  17. }

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

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

  1. package test;
  2. public class Test extends Base {
  3. public static void main(String[] args) {
  4. new Test().method();
  5. }
  6. public void method() {
  7. System.err.println(super.getClass().getName());
  8. System.err.println(this.getClass().getSuperclass().getName());
  9. }
  10. }
  11. class Base {
  12. }

解答:第一个输出test.Test、第二个输出test.Base。super很容易让人以为也是调用了父类,实际上还是本类。具体原因也没太搞懂,有懂的同学欢迎留言。

10、true or false?

  1. public class Test {
  2. public static void main(String[] args) {
  3. String str1 = new String("abc");
  4. String str2 = new String("abc");
  5. System.err.println(str1.equals(str2));
  6. StringBuffer sb1 = new StringBuffer("abc");
  7. StringBuffer sb2 = new StringBuffer("abc");
  8. System.err.println(sb1.equals(sb2));
  9. }
  10. }

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

  1. public boolean equals(Object anObject) {
  2. if (this == anObject) {
  3. return true;
  4. }
  5. if (anObject instanceof String) {
  6. String anotherString = (String)anObject;
  7. int n = value.length;
  8. if (n == anotherString.value.length) {
  9. char v1[] = value;
  10. char v2[] = anotherString.value;
  11. int i = 0;
  12. while (n-- != 0) {
  13. if (v1[i] != v2[i])
  14. return false;
  15. i++;
  16. }
  17. return true;
  18. }
  19. }
  20. return false;
  21. }

Object中的equests方法如下:

  1. public boolean equals(Object obj) {
  2. return (this == obj);
  3. }

11、输出的结果是什么?

  1. public class Test {
  2. public static void main(String[] args) {
  3. System.err.println(new Test().method1());
  4. System.err.println(new Test().method2());
  5. }
  6. public int method1() {
  7. int x = 1;
  8. try {
  9. return x;
  10. } finally {
  11. ++x;
  12. }
  13. }
  14. public int method2() {
  15. int x = 1;
  16. try {
  17. return x;
  18. } finally {
  19. return ++x;
  20. }
  21. }
  22. }

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

如果这样呢?

  1. public class Test {
  2. public static void main(String[] args) {
  3. System.err.println(method());
  4. }
  5. public static boolean method() {
  6. try {
  7. return true;
  8. } finally {
  9. return false;
  10. }
  11. }
  12. }

很明显返回值应该为false。

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

  1. public class Test {
  2. public static void main(String[] args) {
  3. }
  4. public synchronized void m1() {
  5. }
  6. public static synchronized void m2() {
  7. }
  8. }

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

13、true or false?

  1. public class Test {
  2. public static void main(String[] args) {
  3. Integer i1 = 127;
  4. Integer i2 = 127;
  5. System.err.println(i1 == i2);
  6. i1 = 128;
  7. i2 = 128;
  8. System.err.println(i1 == i2);
  9. }
  10. }

解答:这个是对Integer包装类型中应用的享元模式的考察。具体可以看一下blog.csdn.net/m0_37240709…

14、true or false?

  1. public class Test {
  2. public static void main(String[] args) {
  3. String str1 = "a";
  4. String str2 = "a";
  5. String str3 = new String("a");
  6. System.err.println(str1 == str2);
  7. System.err.println(str1 == str3);
  8. str3 = str3.intern();
  9. System.err.println(str1 == str3);
  10. }
  11. }

解答:这个是对String Pool的考察。参看blog.csdn.net/m0_37240709…

15、true or false?

  1. public class Test {
  2. public static void main(String[] args) {
  3. System.err.println(12 - 11.9 == 0.1);
  4. }
  5. }

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

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

  1. public class Test {
  2. public static void main(String[] args) {
  3. BigInteger one = new BigInteger("1");
  4. BigInteger two = new BigInteger("2");
  5. BigInteger three = new BigInteger("3");
  6. BigInteger sum = new BigInteger("0");
  7. sum.add(one);
  8. sum.add(two);
  9. sum.add(three);
  10. System.out.println(sum.toString());
  11. }
  12. }

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

17、输出的结果是什么?

  1. public class Test {
  2. public static void main(String[] args) {
  3. Set<String> set = new HashSet<String>();
  4. set.add("one");
  5. set.add("two");
  6. set.add("three");
  7. set.add("four");
  8. set.add("five");
  9. for (Iterator<String> it = set.iterator(); it.hasNext();) {
  10. System.err.println(it.next());
  11. }
  12. }
  13. }

解答:结果顺序是four  one  two  three  five。有懂的同学欢迎留言评论。

18、如何迭代Map容器?

  1. Map<String, String> map = new HashMap<>();
  2. Set<Map.Entry<String, String>> entrySet = map.entrySet();
  3. for(Map.Entry<String, String> entry : entrySet){
  4. String key = entry.getKey();
  5. String value = entry.getValue();
  6. }
  7. Set<String> keySet = map.keySet();
  8. Iterator<String> it1 = keySet.iterator();
  9. if(it1.hasNext())
  10. System.out.println(it1.next());
  11. Collection<String> values = map.values();
  12. Iterator<String> it2 = values.iterator();
  13. if(it2.hasNext())
  14. System.out.println(it2.next());


19、以下代码输出的结果

  1. public class Test {
  2. public static void main(String[] args) {
  3. System.err.println(args.length);
  4. }
  5. }
  6. /*
  7. A. null B. 0 C. Test
  8. D. Exception in thread "main" java.lang.NullPointerException
  9. */

解答:0.

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

  1. public class Test {
  2. public Test instance = null;
  3. public static Test getInstance() {
  4. if (instance == null) {
  5. instance = new Test();
  6. return instance;
  7. }
  8. }
  9. }

解答:单例模式要满足三点:1、私有化构造方法;2、创建私有静态对象;3、提供公有静态方法获取唯一实例。因此错误包含,1构造函数没有私有化,2对象非私有静态,3获取实例的方法中return不应包含在条件中。单例模式更详细的讲解,参看blog.csdn.net/m0_37240709…

题目参考了blog.csdn.net/smcwwh/arti…