1. 程式人生 > >NIO-DO Java 線上筆試(程式設計)題,蔚來汽車

NIO-DO Java 線上筆試(程式設計)題,蔚來汽車

NIO-DO Java 線上筆試(程式設計)題

1.使用二分查詢的方式來定位某一元素

2.請用你熟悉的開發語言,完成如下題目:
輸入:若干個集合,各集合中的元素不會重複
輸出:求這些集合的笛卡爾積例如:
輸入:N個集合(這裡N=3) :(a,b)(x,y)(1,2,3)
輸出: ((a,x,1), (a,x,2)…(b,y,3))
在保證正確性的情況下儘可能優化效率,同時注意程式碼風格

3.利用迴圈的方式實現我輸入n 得到n對應的裴波拉契數字,裴波拉契舉例:1 1 2 3 5 8 13 21 。。。。。。。

4.用Java編寫一個會導致死鎖的程式

5.Java寫程式碼來解決生產者——消費者問題

6.在Java中Lock介面比synchronized塊的優勢是什麼?你需要實現一個高效的快取,它允許多個使用者讀,但只允許一個使用者寫,以此來保持它的完整性,你會怎樣去實現它?

1.使用二分查詢的方式來定位某一元素
public int search(List listA, int keyword) {
int startNo=0;
int endNo=listA.size()-1;
while (startNo< endNo) {
int middleNo = (startNo + endNo)>>>1;
if (keyword == listA.get(middle)) {
return middle;
} else if (keyword< listA.get(middle)) {
endNo = middle -1;
} else {
startNo = middle +1;
}
}
return -1;
}

4.用Java編寫一個會導致死鎖的程式
public class Entrance {
public static void main(String[] args) {
DeadLockThread deadlock1 = new DeadLockThread();
DeadLockThread deadlock2 = new DeadLockThread();
deadlock1.flag = true;
deadlock2.flag = false;
new Thread(deadlock1).start();
new Thread(deadlock2).start();
}
}

public class DeadLockThread implements Runnable {
private static final Object objectA = new Object();
private static final Object objectB = new Object();
public boolean flag;

@Override
public void run() {
    if (flag) {
        synchronized (objectA) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("flag=true && lock objectA");
            synchronized (objectB) {
                System.out.println("flag=true && lock objectB");
            }
        }
    } else {
        synchronized (objectB) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("flag=false && lock objectB");
            synchronized (objectA) {
                System.out.println("flag=false && lock objectA");
            }
        }
    }
}

}

5.Java寫程式碼來解決生產者——消費者問題
public class ProducerConsumer {
private LinkedList storeHouse = new LinkedList();
private int MAX = 5;

public ProducerConsumer() {
}

public void start() {
    new Producer().start();
    new Comsumer().start();
}

class Producer extends Thread {
    public void run() {
        while (true) {
            synchronized (storeHouse) {
                try {
                    while (storeHouse.size() == MAX) {
                        System.out
                                .println("storeHouse is full , please wait");
                    storeHouse.wait();
                }
                Object newOb = new Object();
                if (storeHouse.add(newOb)) {
                    System.out
                            .println("Producer put a Object to storeHouse");
                    Thread.sleep((long) (Math.random() * 3000));
                    storeHouse.notify();
                }
            } catch (InterruptedException ie) {
                System.out.println("producer is interrupted!");
            }

        }
    }
}

}

class Comsumer extends Thread {
public void run() {
while (true) {
synchronized (storeHouse) {
try {
while (storeHouse.size() == 0) {
System.out
.println(“storeHouse is empty , please wait”);
storeHouse.wait();
}
storeHouse.removeLast();
System.out
.println(“Comsumer get a Object from storeHouse”);
Thread.sleep((long) (Math.random() * 3000));
storeHouse.notify();
} catch (InterruptedException ie) {
System.out.println(“Consumer is interrupted”);
}
}
}
}
}

public static void main(String[] args) throws Exception {
    ProducerConsumer pc = new ProducerConsumer();
    pc.start();
}

}