15道非常经典的Java面试题

可莉
• 阅读 651

试题如下:

15道非常经典的Java面试题 15道非常经典的Java面试题 15道非常经典的Java面试题 15道非常经典的Java面试题 15道非常经典的Java面试题

参考答案:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by ysc on 7/26/16.
 */
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");
    }
    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.hello();
        }
    }
    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();
        }
    }
    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);
        }
    }
    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();
        }
    }
    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");
        }
    }
    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);
    }
}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
待兔 待兔
3个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
3年前
java——20171121
!(http://a.51jsoft.com/uploads/default/original/1X/c542896b094a42a5653fb75adf6cdacd6e35d12e.png)!(https://static.oschina.net/uploads/space/2017/1121/210719_G80Z_3715033.png)
Stella981 Stella981
3年前
H5接入app准入报告
功能标准!(http://static.oschina.net/uploads/space/2016/0725/160658_qiJY_1032182.png)!(http://static.oschina.net/uploads/space/2016/0725/160850_gQgJ_1032182.png)iPhone性能标准!
Stella981 Stella981
3年前
AngularJS 学习笔记 (六) 基本概念和用法 之 Service与Provider
1、使用$http服务!(http://static.oschina.net/uploads/space/2016/0623/092916_7mPM_199513.gif)HTML代码:!(http://static.oschina.net/uploads/space/2016/0623/093044_Y
Wesley13 Wesley13
3年前
POI
!(http://static.oschina.net/uploads/space/2016/0120/131416_Dswb_1444646.png)!(http://static.oschina.net/uploads/space/2016/0120/131416_cIpL_1444646.jpg)ApachePOI是Apache(
Stella981 Stella981
3年前
PHP+jQuery寥寥几行代码轻松实现百度搜索那样的无刷新PJAX的分页列表和导航链接
!(https://static.oschina.net/uploads/space/2016/1208/171419_U00R_561214.png)PHP寥寥几行代码轻松实现百度搜索那样的分页列表和导航链接,某些语言的拥趸哭晕在厕所.<?php$apparray('db_prefix''
Stella981 Stella981
3年前
Android改变图片颜色的自定义控件(十)
效果如下:!(http://static.oschina.net/uploads/space/2016/0218/170430_QStH_159896.png)理解:Xfermode的16总模式如图!(http://static.oschina.net/uploads/space/2016/0218/170632_Jmbz_15989
Stella981 Stella981
3年前
Ngrok搭建环境映射外网访问本地
!(http://static.oschina.net/uploads/space/2016/0105/151215_OnNg_1444646.png)!(http://static.oschina.net/uploads/space/2016/0112/144706_tH0X_1444646.jpg)在开发微信公众号、企业号等需要外
Stella981 Stella981
3年前
Python入门教程之安装MyEclipse插件和安装Python环境
!(http://static.oschina.net/uploads/space/2015/0303/121617_wJt9_1444646.jpg)!(http://static.oschina.net/uploads/space/2016/0120/130908_6m1a_1444646.jpg)http://dlwt.c