美文网首页
经典面试题总结

经典面试题总结

作者: Ace_b90f | 来源:发表于2019-12-11 17:31 被阅读0次

整理一些经典的面试题,这些题大部分出自牛客网,还有平时面试碰到的一些。这些题并不是很难,只是自认为比较有代表性,在工作中可能也会碰到。

递归问题

思考下面代码,x函数执行了多少次,i=?

    public static void main(String[] args) {
        int i;
        i = x(x(8));
    }

    static int x(int n) {
        if (n <= 3)
            return 1;
        else
            return x(n - 2) + x(n - 4) + 1;
    }

思路

    // x(8) => x(6)+x(4)+1 = 5+3+1 = 9
    // x(6) => x(4)+x(2)+1 = 5
    // x(4) => x(2)+x(0)+1 = 3
    // x(8) x(6) x(4) x(2) x(0) x(2) x(4) x(2) x(0)

    // x(9) => x(7)+x(5)+1
    // x(7) => x(5)+x(3)+1
    // x(5) => x(3)+x(1)+1
    // x(9) x(7) x(5) x(3) x(1) x(1) x(5) x(3) x(1)

LRU算法

题目来源

一进程刚获得3个主存块的使用权,若该进程访问页面的次序是1,2,3,4,1,2,5,1,2,3,4,5.当采用LRU算法时,发生的缺页此次数是(10)

访问页号顺序 1 2 3 4 1 2 5 1 2 3 4 5
访问页号顺序 1 2 3 4 1 2 5 1 2 3 4 5
第三个内存页 3 4 1 2 5 1 2 3 4 5
第二个内存页 2 2 3 4 1 2 5 1 2 3 4
第一个内存页 1 1 1 2 3 4 1 2 5 1 2 3
是否缺页中断 × × × × × × × × × ×

进制转换

题目来源

大整数845678992357836701转化成16进制表示,最后两位字符是? D<br /><br />
A. AB<br />
B. EF<br />
C. 8B<br />
D. 9D<br />

利用同余关系做。845678992357836701除以4余数为1( 845678992357836700能被4整除 ),在16进制数中,决定除以4后余数的只有最后一位(前面位都是16的倍数,自然被4整除),算算也只有D(13)除以4余1,所以选D.

二进制最后两位是01.只有D符合

十进制转十六进制的方法:用16整除十进制整数,可到一个商和余,再用16去除商,又得到一个商和余...直到商为0,然后将余数逆向排序,即为所求。

例如

求1000的十六进制数。

1000/16 商为62 余数为8
62/16   商为3  余数为14
3/16    商为0  余数为3

则1000的十六进制数为3E8

同理,求二进制时依次用2整除。

例如

求29的二进制数。

29/2  商为14  余数为1
14/2  商为7   余数为0
7/2   商为3   余数为1
3/2   商为1   余数为1
1/2   商为0   余数为1

则29的二进制数为11101

基于这个我们可以手写个简单的进制转换

    static String convert(int input, int p) {
        StringBuilder sb = new StringBuilder();
        while(input/p != 0){
            int y = input % p;
            if(p == 16){
                sb.append(to16(y));
            }else {
                sb.append(y);
            }
            input = input/p;
        }
        sb.append(input%p);
        return sb.reverse().toString();
    }

    public static void main(String[] args) {
        int x = 1000;

        String r = convert(x,8);

        System.out.println(r);
    }

    static char to16(int x){
        String match = "123456789abcdef";
        return match.charAt(x-1);
    }

数据类型

Integer a = 1;
Integer b = 1;
Integer c = 500;
Integer d = 500;
System.out.print(a == b);
System.out.print(c == d);

上述代码返回结果为: true false

Integer a = 1 是自动装箱会调用Interger.valueOf(int)方法,查看该方法如下所示:

    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
    /**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */

    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }

        private IntegerCache() {}
    }

也就是说IntegerCache类缓存了-128到127的Integer实例,在这个区间内调用valueOf不会创建新的实例。

相关文章

  • iOS经典面试题总结--内存管理

    iOS经典面试题总结--内存管理 iOS经典面试题总结--内存管理

  • Paper Collection - InInterview

    ios 面试题 经典(比较全) 根据重点总结(总结的题还是比较多的,有答案) sunnyxx的面试题 很全的面试题目

  • JavaScript面试题整理(1)

    做面试题就是要做经典的面试题,通过这些具有代表性的面试题来学习与总结,从而掌握相同类型的题目。 对于面试题的总结与...

  • iOS 面试题收集自测

    目录 1.相关经典面试题2.相关优秀文章 1.相关经典面试题 2.相关优秀文章 iOS 面试知识总结之文章收录:h...

  • Java面试题汇总

    Java面试题总结 Java面试题总结一Java面试题总结二

  • 原生JS---经典面试题总结---doing

    总结的很全面:前端知识图谱 小昭聊前端--经典面试题总结 vue双向绑定原理分析 vue 3.0新特性 2019 ...

  • 98道经典Vue面试题总结

    98道经典Vue面试题总结 本文档基于vue-cli技术栈总结了 vue-cli工程 vue.js核心知识 vue...

  • 经典面试题总结

    整理一些经典的面试题,这些题大部分出自牛客网,还有平时面试碰到的一些。这些题并不是很难,只是自认为比较有代表性,在...

  • 面试题01-软件测试常见

    最近看到测试学习公众号「简尚」中有一个关于面试题的总结分析,这里整理其中几类个人认为软件测试中比较经典的面试题,作...

  • 经典面试100题 - 持续更新中

    打算整理100道经典面试题,整理出来的链接都会附录在下面。 经典面试题1:图片占多少内存经典面试题2:时针和分针经...

网友评论

      本文标题:经典面试题总结

      本文链接:https://www.haomeiwen.com/subject/fwhagctx.html