收集非常经典的Java面试题

来源:互联网 发布:湖人拉塞尔大学数据 编辑:程序博客网 时间:2024/05/16 09:22

15道非常经典的Java面试题

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

(1)下面程序的运行结果是:()

    String str1 = "hello";    String str2 = "he" + new String("llo");    System.out.println(str1 == str2);

A. true B. false C. I dont know

(2) 存在使 i+1

(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,将会输出什么结果()

public void getCustomerInfo(){    try{        //do something that may cause an Exception       } catch(java.io.FileNotFoundException e){        System.out.print("FileNotFoundException");    } catch(java.io.IOException e){        System.out.print("IOException");    } catch(java.lang.Exception e){        System.out.print("Exception");    }}

A. IOException

B. IOExceptionException

C. FileNotFoundExceptionIOException

D. FileNotFoundExceptionIOExceptionException

(8) 下面代码的运行结果为()

import java.io.*;import java.util.*;public class foo{    public static void main(String[] args){        String s;        System.out.println("s=" + s);    }}

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的时候返回值是多少()

public static int getValue(int i){    int result = 0;    switch(i){        case 1:            result = result + i;        case 2:            result = result + i*2;        case 3:            result = result + i*3;    }    return result;}

A.0 B.2 C.4 D.10

(二) 简答题

(1) 下面程序能正常运行结果为?

    public static void hello(){        System.out.println("hello");    }    public static void main(String[] args){        ((NULL)null).hello();    }

答案:

(2) 指出下列程序运行结果

public class Example{    String str = new String("good");    char[] ch = {'a','b','c'};    public static void main(String[] args){        Example ex = new Example();        ex.change(ex.str,ex.ch);        System.out.println(ex.str + " and ");        System.out.println(ex.ch);    }    public void change(String str ,char ch[]){        str = "test ok";        ch[0] = 'g';    }}

答案

(3) 下面程序的运行结果

public class Test1{    public static void changeStr(String str){        str = "welcome";    }    public static void main(String[] args){        String str = "1234";        changeStr(str);        System.out.println(str);    }}

答案

(4) 下面程序的运行结果

public class Test2{    static boolean foo(char c){        System.out.println(c);        return true;    }    public static void main(String[] args){        int i = 0;        for(foo('A'); foo('B') && (i<2); foo('C')){            i++;            foo('D');        }    }}

答案

(5) 下面程序的运行结果

class HelloA{    public HelloA(){        System.out.println("HelloA");    }    {        System.out.println("I am A class");    }    static {        System.out.println("static A");    }}public calss HelloB extends HelloA{    public HelloB(){        System.out.println("HelloB");    }    {        System.out.println("I am B class");    }    static {        System.out.println("static B");    }    public static void main(String[] args){        System.out.println("---- main start ----);        new HelloB();        new HelloB();        System.out.println("---- main end ----);    }}

答案


解答

/** * socket:test.Interview.java * 日期:2017年3月22日 */package test;import java.io.FileNotFoundException;import java.io.IOException;import java.util.ArrayList;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * 15道非常经典的java面试题 Interview <br> *  * @author 王俊伟 wjw.happy.love@163.com * @blog https://www.github.com/junehappylove * @date 2017年3月22日 上午11:02:54 * @version 1.0.0 */public class Interview {    /**     * 字符串不是基本的类型数据     *      */    private static void one() {        String str1 = "hello";        String str2 = "he" + new String("llo");        System.err.println(str1 == str2);        System.out.println("1. false");    }    /**     * 考察数的范围问题     *      */    private static void two() {        int i = Integer.MAX_VALUE;        System.err.println((i + 1) < i);        System.out.println("2. 存在一个i, 使得(i+1)<i");    }    /**     * 这个得jdk8才能支持     *      */    private static void three() {        System.err.println("gc is not a Java Thread, it is a native thread");        Thread.getAllStackTraces().keySet().forEach(                thread -> System.out.println(thread.getName() + "->" + thread.isDaemon() + " " + thread.getPriority()));        System.out.println("3. gc线程是daemon线程");    }    private static volatile int count = 0;    private static void four() {        ExecutorService executorService = Executors.newCachedThreadPool();        for (int j = 0; j < 10; j++) {            executorService.submit(() -> {                for (int i = 0; i < 1000000; i++) {                    count++;                }            });        }        System.out.println("count should be: " + 10000000 + ", actual be: " + count);        System.out.println("4. volatile不能保证线程安全");    }    private static void five() {        ArrayList<Integer> list = new ArrayList<>(20);        list.add(1);        System.out.println("debug code, not execute grow method");        System.out.println("5. list grow 0 times");    }    private static void six() {        System.out.println("BufferedReader's constructor only accepts a Reader instance");        System.out.println("6. new BufferedReader(new FileInputStream(\"a.dat\")); is wrong");    }    private static void seven() {        try {            if (true) {                throw new IOException();            }        } catch (FileNotFoundException e) {            System.out.print("FileNotFoundException!");        } catch (IOException e) {            System.out.print("IOException!");        } catch (Exception e) {            System.out.print("Exception!");        }        System.out.println("\n7. IOException!");    }    private static void eight() {        System.out.println(                "String s;System.out.println(s); error: variable s might not have been initialized\nRecompile with -Xlint:unchecked for details.");        System.out.println("8. 由于String s没有初始化, 代码不能编译通过");    }    private static void nine() {        System.out.println("5" + 2);        System.out.println("9. 52");    }    private static void ten() {        int i = 2;        int result = 0;        switch (i) {        case 1:            result = result + i;        case 2:            result = result + i * 2;        case 3:            result = result + i * 3;        }        System.out.println("result=" + result);        System.out.println("10. 10");    }    private static class Null {        public static void hello() {            System.out.println("hello");        }        public static void main(String[] args) {            ((Null) null).hello();//不推荐这种写法,null是万物之始            Null _null = (Null) null;            _null.hello();            System.out.println("11. hello");        }    }    /**     * 形参实参     * StringExample1 <br>     *      * @author 王俊伟 wjw.happy.love@163.com     * @blog https://www.github.com/junehappylove     * @date 2017年3月22日 上午11:08:17     * @version 1.0.0     */    private static class StringExample1 {        String str = new String("good");        char[] ch = { 'a', 'b', 'c' };        public void change(String str, char[] ch) {            str = "test ok";            ch[0] = 'g';        }        public static void main(String[] args) {            StringExample1 ex = new StringExample1();            ex.change(ex.str, ex.ch);            System.out.print(ex.str + " and ");            System.out.print(ex.ch);            System.out.println();            System.out.println("12. good and gbc");        }    }    private static class StringExample2 {        public static void change(String str) {            str = "welcome";        }        public static void main(String[] args) {            String str = "1234";            change(str);            System.out.println(str);            System.out.println("13. 1234");        }    }    private static class ForLoop {        static boolean foo(char c) {            System.out.print(c);            return true;        }        public static void main(String[] args) {            int i = 0;            for (foo('A'); foo('B') && (i < 2); foo('C')) {                i++;                foo('D');            }            System.out.println();            System.out.println("14. ABDCBDCB");        }    }    private static class HelloA {        public HelloA() {            System.out.println("HelloA");        }        {            System.out.println("I'm A class");        }        static {            System.out.println("static A");        }    }    private static class HelloB extends HelloA {        public HelloB() {            System.out.println("HelloB");        }        {            System.out.println("I'm B class");        }        static {            System.out.println("static B");        }        public static void main(String[] args) {            System.out.println("main start");            new HelloB();            new HelloB();            System.out.println("main end");        }    }    /**     * 运行此方法测试     * @param args     */    public static void main(String[] args) {        one();        two();        three();        four();        five();        six();        seven();        eight();        nine();        ten();        Null.main(null);        StringExample1.main(null);        StringExample2.main(null);        ForLoop.main(null);        HelloB.main(null);    }}

日积月累

在泛型中,和T的区别是什么呢?

很多人没有回答出来。正确的答案应该是这样的:是定义,T是使用,在Java中遵守先定义后使用的原则。

在泛型中,有几种分类?

估计也有很多人答不出来。正确的答案是:
泛型类泛型方法泛型接口

0 0
原创粉丝点击