2013金山校園招聘Java筆試題
作者:xcbeyond
瘋狂源自夢(mèng)想,技術(shù)成就輝煌!微信公眾號(hào):《程序猿技術(shù)大咖》號(hào)主,專注后端開發(fā)多年,擁有豐富的研發(fā)經(jīng)驗(yàn),樂于技術(shù)輸出、分享,現(xiàn)階段從事微服務(wù)架構(gòu)項(xiàng)目的研發(fā)工作,涉及架構(gòu)設(shè)計(jì)、技術(shù)選型、業(yè)務(wù)研發(fā)等工作。對(duì)于Java、微服務(wù)、數(shù)據(jù)庫、Docker有深入了解,并有大量的調(diào)優(yōu)經(jīng)驗(yàn)。
第一題 :棧內(nèi)存與堆內(nèi)存的特點(diǎn)與區(qū)別,java中是怎樣分配的?
棧內(nèi)存中用來存放基本數(shù)據(jù)類型(8種基本類型)和對(duì)象的引用變量,存取速度比堆快,棧中的數(shù)據(jù)可以被共享使用,堆內(nèi)存中用來存放new創(chuàng)建的對(duì)象和數(shù)組對(duì)象。
第二題:對(duì)象序列化,作用,那些不能序列化?
對(duì)象序列化是為了能夠讓對(duì)象像其他變量數(shù)據(jù)一樣能夠長(zhǎng)久的保存下來,其實(shí)質(zhì)是把對(duì)象在內(nèi)存中的數(shù)據(jù)按照一定的規(guī)則,變成一系列的字節(jié)數(shù)據(jù),然后寫入到流中。沒有實(shí)現(xiàn)java.io.Seralizabled接口的類不能實(shí)例化。關(guān)于序列化更加詳細(xì)的介紹:Java序列化的那些事。
第三題 線程的p、v操作
線程對(duì)于程序員而言,是比較重要的一塊知識(shí),不會(huì)線程編程,就算不上一個(gè)合格的程序員。因此,線程也是各個(gè)公司筆試面試必考的內(nèi)容之一。PV操作本是操作系統(tǒng)中相關(guān)的內(nèi)容,簡(jiǎn)單來說,P操作是申請(qǐng)資源,V操作是釋放資源。本題最好可以用生產(chǎn)者/消費(fèi)者來實(shí)現(xiàn)PV操作最為合適,同時(shí)也考慮到了多線程同步的問題。舉例說明:
package common;
import org.junit.Test;
/**
* PV操作示例
* @author xcbeyond
*
* 2012-10-2下午08:05:09
*/
public class PVOperator {
public static void main(String [] args){
Store s = new Store(5);
Produce pro1 = new Produce(s);
Produce pro2 = new Produce(s);
Consumer con1 = new Consumer(s);
Consumer con2 = new Consumer(s);
pro1.start();
con1.start();
pro2.start();
con2.start();
}
}
/**
* 倉庫類:臨界資源
*
*/
class Store{
private final int maxSize; //最大容量
private int count;
public Store(int size){
maxSize = size;
count = 0;
}
/**
* 添加資源
*/
public synchronized void add(){
while(count >=maxSize){
System.out.println("----倉庫滿了!----");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count++;
System.out.println(Thread.currentThread().toString()+ "put" +count);
notifyAll();
}
public synchronized void remove() {
while(count <= 0) {
System.out.println("----倉庫空了!----");
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().toString()+ "get"+count);
count--;
notify();
}
}
/**
* 生產(chǎn)者:P操作
*/
class Produce extends Thread {
private Store s;
public Produce(Store s) {
this.s = s;
}
@Override
public void run() {
while(true){
s.add();
try {
Thread.sleep(1000);//只是為了利于查看線程間的同步,所以延遲1s
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 消費(fèi)者:V操作
*/
class Consumer extends Thread {
private Store s;
public Consumer(Store s) {
this.s = s;
}
@Override
public void run() {
while(true) {
s.remove();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
第四題 設(shè)計(jì)一個(gè)文本編輯器,類似于wps,寫出思想和架構(gòu)。
第五題:
共五道題
現(xiàn)不給出具體答案,望大家給出合理的答案,相互參考!