找回密码
 立即注册

QQ登录

只需一步,快速开始

断天涯大虾
社区贡献组   /  发表于:2017-3-23 09:51  /   查看:4969  /  回复:0
本帖最后由 断天涯大虾 于 2017-3-23 10:03 编辑

(一)选择题(每题5分)

(1)下面程序的运行结果是: ( )   
  1. String str1 = "hello";
  2. String str2 = "he" + new String("llo");
  3. System.out.println(str1 == str2);
复制代码
A. true B. false C. I dont know


(2) 存在使 i+1<i的数吗?()
A. yes B. no C. I dont know


(3) GC线程是否为守护线程?()
A. yes B. no C. I dont know


(4) volatile关键字是否能保证线程安全?()
A. yes B. no C. I dont know


(5) ArrayList list = new ArrayList(20);中的list扩充几次?()
A. 0 B. 1 C. 2 D. 3 E. I dont know


(6) 新建一个流对象,下面哪个选项的代码是错误的?()
A. new BufferedWriter(new FileWriter("a.txt"));
B. new BufferedReader(new FileInputStream("a.dat");
C. new GZIPOutputStream(new FileOutputStream("a.zip"));
D. new ObjectInputStream(new FileInputStream("a.dat"));


(7) getCustomerInfo()方法如下,try中可以捕获三种类型的异常,如果在该方法运行中产生了一个IOException,将会输出什么结果()
  1. public void getCustomerInfo(){
  2.   try{
  3.         //do something that may cause an Exception   
  4.     } catch(java.io.FileNotFoundException e){
  5.         System.out.print("FileNotFoundException");
  6.     } catch(java.io.IOException e){
  7.         System.out.print("IOException");
  8.     } catch(java.lang.Exception e){
  9.         System.out.print("Exception");
  10.     }
  11. }
复制代码
A. IOException
B. IOExceptionException
C. FileNotFoundExceptionIOException
D. FileNotFoundExceptionIOExceptionException


(8) 下面代码的运行结果为()
  1. import java.io.*;
  2. import java.util.*;

  3. public class foo{
  4.     public static void main(String[] args){
  5.         String s;
  6.         System.out.println("s=" + s);
  7.     }
  8. }
复制代码
A. 代码得到编译,并输出 "s="
B. 代码得到编译,并输出 "s=null"
C. 由于String s没有初始化,代码不能编译通过
D. 代码得到编译,但捕获到NullPointException异常


(9) System.out.println("5"+2);的输出结果应该是()
A. 52 B. 7 C. 2 D. 5


(10) 下面的方法,当输入i为2的时候返回值是多少()
  1. public static int getValue(int i){
  2.     int result = 0;
  3.     switch(i){
  4.         case 1:
  5.             result = result + i;
  6.         case 2:
  7.             result = result + i*2;
  8.         case 3:
  9.             result = result + i*3;
  10.     }
  11.     return result;
  12. }
复制代码
A.0 B.2 C.4 D.10


(二) 简答题


(1) 下面程序能正常运行结果为?
  1. public static void hello(){
  2.         System.out.println("hello");
  3.     }
  4.     public static void main(String[] args){
  5.         ((NULL)null).hello();
  6.     }
复制代码


(2) 指出下列程序运行结果
  1. public class Example{
  2.     String str = new String("good");
  3.     char[] ch = {'a','b','c'};
  4.     public static void main(String[] args){
  5.         Example ex = new Example();
  6.         ex.change(ex.str,ex.ch);
  7.         System.out.println(ex.str + " and ");
  8.         System.out.println(ex.ch);
  9.     }

  10.     public void change(String str ,char ch[]){
  11.         str = "test ok";
  12.         ch[0] = 'g';
  13.     }
  14. }
复制代码


(3) 下面程序的运行结果
  1. public class Test1{
  2.     public static void changeStr(String str){
  3.         str = "welcome";
  4.     }
  5.     public static void main(String[] args){
  6.         String str = "1234";
  7.         changeStr(str);
  8.         System.out.println(str);
  9.     }
  10. }
复制代码


(4) 下面程序的运行结果
  1. public class Test2{
  2.     static boolean foo(char c){
  3.         System.out.println(c);
  4.         return true;
  5.     }
  6.     public static void main(String[] args){
  7.         int i = 0;
  8.         for(foo('A'); foo('B') && (i<2); foo('C')){
  9.             i++;
  10.             foo('D');
  11.         }
  12.     }
  13. }
复制代码


(5) 下面程序的运行结果
  1. class HelloA{
  2.     public HelloA(){
  3.         System.out.println("HelloA");
  4.     }
  5.     {
  6.         System.out.println("I am A class");
  7.     }
  8.     static {
  9.         System.out.println("static A");
  10.     }
  11. }

  12. public calss HelloB extends HelloA{
  13.     public HelloB(){
  14.         System.out.println("HelloB");
  15.     }
  16.     {
  17.         System.out.println("I am B class");
  18.     }
  19.     static {
  20.         System.out.println("static B");
  21.     }

  22.     public static void main(String[] args){
  23.         System.out.println("---- main start ----);
  24.         new HelloB();
  25.         new HelloB();
  26.         System.out.println("---- main end ----);
  27.     }
  28. }
复制代码


解答
  1. /**
  2. * socket:test.Interview.java
  3. * 日期:2017年3月22日
  4. */
  5. package test;

  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.concurrent.ExecutorService;
  10. import java.util.concurrent.Executors;

  11. /**
  12. * 15道非常经典的java面试题 Interview <br>
  13. *
  14. * @author 王俊伟 wjw.happy.love@163.com
  15. * @blog https://www.github.com/junehappylove
  16. * @date 2017年3月22日 上午11:02:54
  17. * @version 1.0.0
  18. */
  19. public class Interview {

  20.     /**
  21.      * 字符串不是基本的类型数据
  22.      *
  23.      */
  24.     private static void one() {
  25.         String str1 = "hello";
  26.         String str2 = "he" + new String("llo");
  27.         System.err.println(str1 == str2);
  28.         System.out.println("1. false");
  29.     }

  30.     /**
  31.      * 考察数的范围问题
  32.      *
  33.      */
  34.     private static void two() {
  35.         int i = Integer.MAX_VALUE;
  36.         System.err.println((i + 1) < i);
  37.         System.out.println("2. 存在一个i, 使得(i+1)<i");
  38.     }

  39.     /**
  40.      * 这个得jdk8才能支持
  41.      *
  42.      */
  43.     private static void three() {
  44.         System.err.println("gc is not a Java Thread, it is a native thread");
  45.         Thread.getAllStackTraces().keySet().forEach(
  46.                 thread -> System.out.println(thread.getName() + "->" + thread.isDaemon() + " " + thread.getPriority()));
  47.         System.out.println("3. gc线程是daemon线程");
  48.     }

  49.     private static volatile int count = 0;

  50.     private static void four() {
  51.         ExecutorService executorService = Executors.newCachedThreadPool();
  52.         for (int j = 0; j < 10; j++) {
  53.             executorService.submit(() -> {
  54.                 for (int i = 0; i < 1000000; i++) {
  55.                     count++;
  56.                 }
  57.             });
  58.         }
  59.         System.out.println("count should be: " + 10000000 + ", actual be: " + count);
  60.         System.out.println("4. volatile不能保证线程安全");
  61.     }

  62.     private static void five() {
  63.         ArrayList<Integer> list = new ArrayList<>(20);
  64.         list.add(1);
  65.         System.out.println("debug code, not execute grow method");
  66.         System.out.println("5. list grow 0 times");
  67.     }

  68.     private static void six() {
  69.         System.out.println("BufferedReader's constructor only accepts a Reader instance");
  70.         System.out.println("6. new BufferedReader(new FileInputStream("a.dat")); is wrong");
  71.     }

  72.     private static void seven() {
  73.         try {
  74.             if (true) {
  75.                 throw new IOException();
  76.             }
  77.         } catch (FileNotFoundException e) {
  78.             System.out.print("FileNotFoundException!");
  79.         } catch (IOException e) {
  80.             System.out.print("IOException!");
  81.         } catch (Exception e) {
  82.             System.out.print("Exception!");
  83.         }
  84.         System.out.println("\n7. IOException!");
  85.     }

  86.     private static void eight() {
  87.         System.out.println(
  88.                 "String s;System.out.println(s); error: variable s might not have been initialized\nRecompile with -Xlint:unchecked for details.");
  89.         System.out.println("8. 由于String s没有初始化, 代码不能编译通过");
  90.     }

  91.     private static void nine() {
  92.         System.out.println("5" + 2);
  93.         System.out.println("9. 52");
  94.     }

  95.     private static void ten() {
  96.         int i = 2;
  97.         int result = 0;
  98.         switch (i) {
  99.         case 1:
  100.             result = result + i;
  101.         case 2:
  102.             result = result + i * 2;
  103.         case 3:
  104.             result = result + i * 3;
  105.         }
  106.         System.out.println("result=" + result);
  107.         System.out.println("10. 10");
  108.     }

  109.     private static class Null {
  110.         public static void hello() {
  111.             System.out.println("hello");
  112.         }

  113.         public static void main(String[] args) {
  114.             ((Null) null).hello();//不推荐这种写法,null是万物之始
  115.             Null _null = (Null) null;
  116.             _null.hello();
  117.             System.out.println("11. hello");
  118.         }
  119.     }

  120.     /**
  121.      * 形参实参
  122.      * StringExample1 <br>
  123.      *
  124.      * @author 王俊伟 wjw.happy.love@163.com
  125.      * @blog https://www.github.com/junehappylove
  126.      * @date 2017年3月22日 上午11:08:17
  127.      * @version 1.0.0
  128.      */
  129.     private static class StringExample1 {
  130.         String str = new String("good");
  131.         char[] ch = { 'a', 'b', 'c' };

  132.         public void change(String str, char[] ch) {
  133.             str = "test ok";
  134.             ch[0] = 'g';
  135.         }

  136.         public static void main(String[] args) {
  137.             StringExample1 ex = new StringExample1();
  138.             ex.change(ex.str, ex.ch);
  139.             System.out.print(ex.str + " and ");
  140.             System.out.print(ex.ch);
  141.             System.out.println();

  142.             System.out.println("12. good and gbc");
  143.         }
  144.     }

  145.     private static class StringExample2 {
  146.         public static void change(String str) {
  147.             str = "welcome";
  148.         }

  149.         public static void main(String[] args) {
  150.             String str = "1234";
  151.             change(str);
  152.             System.out.println(str);
  153.             System.out.println("13. 1234");
  154.         }
  155.     }

  156.     private static class ForLoop {
  157.         static boolean foo(char c) {
  158.             System.out.print(c);
  159.             return true;
  160.         }

  161.         public static void main(String[] args) {
  162.             int i = 0;
  163.             for (foo('A'); foo('B') && (i < 2); foo('C')) {
  164.                 i++;
  165.                 foo('D');
  166.             }
  167.             System.out.println();
  168.             System.out.println("14. ABDCBDCB");
  169.         }
  170.     }

  171.     private static class HelloA {
  172.         public HelloA() {
  173.             System.out.println("HelloA");
  174.         }

  175.         {
  176.             System.out.println("I'm A class");
  177.         }

  178.         static {
  179.             System.out.println("static A");
  180.         }
  181.     }

  182.     private static class HelloB extends HelloA {
  183.         public HelloB() {
  184.             System.out.println("HelloB");
  185.         }

  186.         {
  187.             System.out.println("I'm B class");
  188.         }

  189.         static {
  190.             System.out.println("static B");
  191.         }

  192.         public static void main(String[] args) {
  193.             System.out.println("main start");
  194.             new HelloB();
  195.             new HelloB();
  196.             System.out.println("main end");
  197.         }
  198.     }

  199.     /**
  200.      * 运行此方法测试
  201.      * @param args
  202.      */
  203.     public static void main(String[] args) {
  204.         one();
  205.         two();
  206.         three();
  207.         four();
  208.         five();
  209.         six();
  210.         seven();
  211.         eight();
  212.         nine();
  213.         ten();
  214.         Null.main(null);
  215.         StringExample1.main(null);
  216.         StringExample2.main(null);
  217.         ForLoop.main(null);
  218.         HelloB.main(null);
  219.     }
  220. }
复制代码




日积月累


在泛型中,<T>和T的区别是什么呢?
很多人没有回答出来。正确的答案应该是这样的:<T>是定义,T是使用,在Java中遵守先定义后使用的原则。


在泛型中,有几种分类?
估计也有很多人答不出来。正确的答案是: 泛型类,泛型方法,泛型接口。


   
关于葡萄城:全球最大的控件提供商,世界领先的企业应用定制工具、企业报表和商业智能解决方案提供商,为超过75%的全球财富500强企业提供服务。

1 个回复

倒序浏览
您需要登录后才可以回帖 登录 | 立即注册
返回顶部