JavaRush /Java Blog /Random EN /Logic tasks in the interview
Юрий Кузнецов
Level 35
Москва

Logic tasks in the interview

Published in the Random EN group
You have to parse 5 logical problems plus 4 Java problems from me. The tasks that were met at the interviews and the solutions to them are on the Internet. Here is a top list of those that I encountered in interviews. Logic tasks at the interview - 1
  1. Four people in hats

  2. Problem about the bridge and the flashlight

  3. Out of 9 coins, one is fake: it is lighter. How to find it for two weighings on the scales?

  4. Almost the same as the previous problem, only the scales can be any. Task "5 jars of pills" .

  5. "Endless Train"

To fill in the questions that were asked at the interviews:

  1. Write multiple ways to implement a singleton(singleton) in java.

  2. What will happen in this code?

    Logic tasks at the interview - 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. A task from me, because I don’t remember what example was there

    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. What will be the result of running the program? And what result will we get if we uncomment the last two lines?

    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();
        }
    }
    Logic tasks at the interview - 3
PS: Answers to logic problems can all be found on the Internet. For those who are interested, I am attaching a previous post about java developer interviews
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION