JavaRush /Java 博客 /Random-ZH /面试时的逻辑问题
Юрий Кузнецов
第 35 级
Москва

面试时的逻辑问题

已在 Random-ZH 群组中发布
你要分析我的5道逻辑题加上4道Java题。面试中遇到的问题和解决方案可以在网上找到。我将列出我在采访中遇到的最重要的名单。 面试中的逻辑问题 - 1
  1. 四个人戴帽子

  2. 关于桥和手电筒的问题

  3. 9 枚硬币中,一枚是假币:它更轻。如何在秤上两次称重中找到它?

  4. 和上一题几乎一样,只是尺度可以是任意的。问题“5罐药丸”

  5. 《无尽的列车》

涵盖采访期间提出的问题:

  1. 编写几种在java中实现单例的方法。

  2. 这段代码会发生什么?

    面试中的逻辑问题 - 2
    class MyExc1 extends Exception{}
    class MyExc2 extends Exception{}
    class MyExc3 extends Exception{}
    
    public class Test {
        public static void main(String[] args) throws Exception {
            try {
                System.out.print(1);
                p();
            }catch (MyExc2 e){
            }
            finally {
                throw new MyExc3();
                System.out.print(2);
            }
        }
        public static void p() throws Exception {
            try {
                throw new MyExc1();
                 } catch (MyExc1 myExc1) {
                throw new MyExc2();
            }finally {
                System.out.println(3);
            }
        }
    }
  3. 我的问题,因为我不记得有什么例子

    public class Test {
        public static void main(String[] args)  {
            String s = "Hello";
            String s1 = "Hello";
            String s2 = new String("Hello");
    
            System.out.println(s == s1);
            System.out.println(s1 == s2);
    
            Integer i = 111;
            Integer i1 = 111;
            Integer i2 = new Integer(111);
            Integer i3 = new Integer(111);
    
            Integer i4 = -129;
            Integer i5 = -129;
            Integer i6 = new Integer(-129);
    
            System.out.println(i == i1);
            System.out.println(i1 == i2);
            System.out.println(i2 == i3);
    
            System.out.println(i4 == i5);
            System.out.println(i5 == i6);
    
            System.out.println(i6 == -129);
        }
    }
  4. 运行程序会得到什么结果?如果取消最后两行的注释,我们会得到什么结果?

    class Test{
        private int id;
        private Integer id2;
    
        public Test(int id, Integer id2) {
            System.out.println("Создаем екзепляр Test");
            this.id = id;
            this.id2 = id2;
        }
    
        public Integer doIt(){
            return id + id2;
        }
    }
    class Test1{
        private Test test;
        private static int i;
    
        public Test1() {
            System.out.println("Создаем екзепляр Test1");
            this.test = new Test(i, 10);
        }
        public Test getTest() {
            return test;
        }
    }
    
    class Main {
        public static void main(String[] args)  {
            Test1 test1 = new Test1();
            System.out.println(test1.getTest().doIt());
           // Test test = new Test(null, 1);
           // test.doIt();
        }
    }
    面试中的逻辑问题 - 3
PS:逻辑题的答案都可以在网上找到。 对于那些感兴趣的人,我附上了之前关于Java 开发人员访谈的文章
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION