JavaRush /Java Blog /Random-TW /面試時的邏輯問題
Юрий Кузнецов
等級 35
Москва

面試時的邏輯問題

在 Random-TW 群組發布
你要分析我的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