zoukankan      html  css  js  c++  java
  • 本文收录各种猥琐的Java笔试/面试题

    本文收录各种猥琐的Java笔试/面试题,一些比较容易忘记的,不定期更新。也希望大家在底下留言,贴出自己碰到或看到的各种猥琐笔试、面试题目。

    J2EE基础部分

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

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

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

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

    3、下面代码的结果是什么?还是抛出异常?(笔试)

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

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

    [java] view plaincopy
     
    1. package test;  
    2.   
    3. import java.util.Date;  
    4.   
    5. public class Test {  
    6.   
    7.     public static void main(String[] args) {  
    8.         Date date = new Date();  
    9.         System.err.println(date.getMonth() + " " + date.getDate());  
    10.     }  
    11. }  

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

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

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

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

    不要觉得很简单,最起码你要记得返回当前文件夹下的所有文件的方法是listFiles(),isDirectory别拼错了。

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

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

    自己手写排序算法的可以无视此题,如果是Arrays.sort()的,请注意Comparator与Comparable接口的区别,别搞混了。

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

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

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

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

    10、true or false?

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

    11、输出的结果是什么?

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

    这样呢?输出什么

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

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

    [java] view plaincopy
     
    1. package test;  
    2.   
    3. public class Test {  
    4.   
    5.     public static void main(String[] args) {  
    6.     }  
    7.   
    8.     public synchronized void m1() {  
    9.     }  
    10.   
    11.     public static synchronized void m2() {  
    12.     }  
    13. }  

    13、true or false?理由

    [java] view plaincopy
     
    1. package test;  
    2.   
    3. public class Test {  
    4.   
    5.     public static void main(String[] args) {  
    6.         Integer i1 = 127;  
    7.         Integer i2 = 127;  
    8.         System.err.println(i1 == i2);  
    9.           
    10.         i1 = 128;  
    11.         i2 = 128;  
    12.         System.err.println(i1 == i2);  
    13.     }  
    14. }  

    14、true or false?理由

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

    15、true or false?理由

    [java] view plaincopy
     
    1. package test;  
    2.   
    3. public class Test {  
    4.   
    5.     public static void main(String[] args) {  
    6.         System.err.println(12 - 11.9 == 0.1);  
    7.     }  
    8. }  

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

    [java] view plaincopy
     
    1. package test;  
    2.   
    3. import java.math.BigInteger;  
    4.   
    5. public class Test {  
    6.   
    7.     public static void main(String[] args) {  
    8.         BigInteger one = new BigInteger("1");  
    9.         BigInteger two = new BigInteger("2");  
    10.         BigInteger three = new BigInteger("3");  
    11.         BigInteger sum = new BigInteger("0");  
    12.         sum.add(one);  
    13.         sum.add(two);  
    14.         sum.add(three);  
    15.         System.out.println(sum.toString());  
    16.     }  
    17. }  

    17、输出的结果是什么?12345?根据单词排序?还是?

    [java] view plaincopy
     
    1. package test;  
    2.   
    3. import java.util.HashSet;  
    4. import java.util.Iterator;  
    5. import java.util.Set;  
    6.   
    7. public class Test {  
    8.   
    9.     public static void main(String[] args) {  
    10.         Set<String> set = new HashSet<String>();  
    11.         set.add("one");  
    12.         set.add("two");  
    13.         set.add("three");  
    14.         set.add("four");  
    15.         set.add("five");  
    16.         for (Iterator<String> it = set.iterator(); it.hasNext();) {  
    17.             System.err.println(it.next());  
    18.         }  
    19.     }  
    20. }  

    18、如何迭代Map容器,别所大概是......,手写个试试?

    19、以下代码输出的结果(笔试选择题)

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

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

    [java] view plaincopy
     
      1. public class Test {  
      2.       
      3.     public Test instance = null;  
      4.       
      5.     public static Test getInstance() {  
      6.         if (instance == null) {  
      7.             instance = new Test();  
      8.             return instance;  
      9.         }  
      10.     }  
      11. }  
  • 相关阅读:
    keras_12_keras自带的Applications
    keras_11_keras中示例数据集
    keras_10_回调函数 Callbacks
    Runloop
    SDWebImage
    NSOperation
    单例模式
    GCD
    一文读懂汉明码
    聊聊SPOOLing技术
  • 原文地址:https://www.cnblogs.com/prctice/p/4924354.html
Copyright © 2011-2022 走看看