JAVA基礎(chǔ)知識榴徐,面試必備

JAVA Interview Question

1. Does JAVA has destructor? How to force it?

No. Because Java is a garbage collected language. You can not predict when an object will be destroyed.

2. How JAVA garbage collection works? Is it guarateed to work?

Java garbage collection is the process by which Java programs perform automatic memory management. When Java programs run on the JVM, objects are created on the heap, once an object is no longer referenced and therefore is not reachable by the code, the garbage collector finds these unused objects and deletes them to free up memory.

No, it is not guaranteed to work. If there is insufficient memory remaining to satisfy the amount needed for a new object, the garbage collector will attempt to reclaim as much memory as possible by releasing memory. However, it is possible for a developer to mistakenly create objects which never go out of scope, thus consuming more and more memory until all heap is exhausted.

3. "==" VS "equals()"

  • The two operators are used to compare objects to check equality.

  • "==" for address comparison; "equals()" for content comparison. == checks if both objects point to the same memory location whereas .equals() evaluates to the comparison of values in the objects.

4. HashTable VS HashMap

  • HashTable is synchronized and thread-safe(can be shared with many threads), whereas HashMap is not and not-thread safe(can not be shared). Thus, HashMap is better for non-threaded applications.(HashMap can be synchronized by Map m = Collections.synchronizeMap(hashMap))

  • HashTable does not allow null keys or values, HashMap allows one null key and any number of null values.

5. Does Java support multiple inheritance?

No, you can only extend a single class but you can implement multiple interfaces.

6. What is difference between JDK,JRE and JVM?

JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the runtime environment in which java bytecode can be executed. It is a specification. JVMs are available for many hardware and software platforms (so JVM is platform dependent).

JRE stands for Java Runtime Environment. It is the implementation of JVM.

JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.

7. Explain the Java Exception Hierarchy.

Java Exceptions are hierarchical and inheritance is used for categorizing the different types of exceptions. Throwable is the parent class of Java Exceptions Hierarchy and it has two child objects

Throwable: Error & Exceptions

Exceptions: checked & runtime exception

Errors are exceptional scenarios which are out of the scope of applications and it’s not possible to anticipate and recover from them, for example, hardware failure, JVM crash or out of memory error

Checked exceptions are exceptional scenarios that we can anticipate in a program and try to recover from it, for example, FileNotFoundException. We should catch this exception and provide a useful message to the user and log it properly for debugging purpose.

Runtime exceptions are caused by bad programming, for example, trying to retrieve an element from the Array. At first, we should check the length of the array before trying to retrieve the element otherwise it might throw ArrayIndexOutOfBoundException at runtime.

8. What is data encapsulation and what’s its significance?

Encapsulation is a concept in Object Oriented Programming for combining properties and methods in a single unit. Encapsulation helps programmers to follow a modular approach for software development as each object has its own set of methods and variables and serves its functions independent of other objects. Encapsulation also serves data hiding purpose.

9. Explain Mutex

We use mutex to protect critical section and thus prevent race condition. Each time only one process will be able to have the key and proceed with their work. Mutex has a boolean variable and indicates if the lock is available. A process that attempts to acquire an unavailable lock is blocked until the lock is released. It is locking mechanism.

10. Explain Semaphore

A semaphore S is a variable. It is signaling mechanism. wait() & signal()

11. What happened when compiling and running JAVA code?

Programs are not compiled into executable file. They are compiled into bytecode, which the JVM executes at runtime.

Java source code is compiled into bytecode when we use javac compiler.

The bytecode gets saved on the disk with the file extension.class when the program is to be run.

The bytecode is converted using the Just-in-time(JIT) compiler.

The result is machine code which is fed to the memory and is executed.

12. String VS StringBuffer VS StringBuiler

  • String is immutable, whereas StringBuffer and StringBuider are mutable classes.

    whenever we do String manipulation like concatenation, substring etc, it generates a new String and discards the older String for garbage collection.

  • StringBuffer is thread safe and synchronized whereas StringBuilder is not.

    StringBuilder is more faster than StringBuffer. StringBuffer provides Thread safety but on a performance cost. StringBuffer: all of its public methods are synchronized

13. Stack memory VS Heap memory

  • Stack memory is used to store local variables and function call while heap memory is used to store objects in Java

  • Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM.

  • Stack frame access is easier than the heap frame as the stack have small region of memory and is cache friendly, but in case of heap frames which are dispersed throughout the memory so it cause more cache misses.

14. final VS finalize VS finally

  • Final is a keyword. Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final method can't be overridden and final variable value can't be changed.

  • Finally is a block. Finally is used to place important code, it will be executed whether exception is handled or not.

  • Finalize is a method. Finalize is used to perform clean up processing just before object is garbage collected.

15. Boxing and Unboxing

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

|Primitive type|Wrapper class|

|--|--|

|boolean|Boolean|

|byte | Byte|

|char |Character|

|float |Float|

|int |Integer|

|long |Long|

|short |Short|

|double |Double|

16. Overriding VS Overloading

Overloading occurs when two or more methods in one class have the same method name but different parameters.

Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class.

17. List VS Set VS Map

Set: HashSet, LinkedHashSet, TreeSet

List: ArrayList, LinkedList, Vector

Map: HashMap, LinkedHashMap, HashTable, TreeMap

  • List allows duplicates while Set doesn't allow duplicates.

  • Map holds two objects per Entry e.g. a key and a value and It may contain duplicate values but keys are always unique.

  • List is an ordered collection, List's contract maintains insertion order or element. Set is an unordered collection, you get no guarantee on which order element will be stored.

  • The list allows null elements and you can have many null objects in a List because it also allowed duplicates. Set just allow one null element as there is no duplicate permitted while in Map you can have null values and at most one null key.

18. Serialization VS Deserialization

Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory.

19. What are the object-oriented features of java?

abstraction, encapsulation, inheritance, and polymorphism.

Abstraction: simple things like objects, classes, and variables represent more complex underlying code and data.

Encapsulation: It’s a protective barrier that keeps the data and code safe within the class itself. This way, we can re-use objects like code components or variables without allowing open access to the data system-wide.

Inheritance: It lets programmers create new classes that share some of the attributes of existing classes.

Polymorphism: use the same word to mean different things in different contexts. method overloading & method overriding.

10. what is contract between hashcode and equals method in java?

public boolean equals(Object obj)

public int hashCode()

  • If two objects are equal, then they must have the same hash code.

  • If two objects have the same hash code, they may or may not be equal.

21. ArrayList VS Vector VS LinkedList

  • ArrayList is non-synchronized, while Vector is synchronized which means multiple threads can work on ArrayList at the same time.

  • ArrayList grow by half of its size when resized while Vector doubles the size of itself by default when grows.

  • ArrayList gives better performance as it is non-synchronized. Vector operations gives poor performance as they are thread-safe.

  • LinkedList is faster in add and remove, but slower in get.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末窑业,一起剝皮案震驚了整個濱河市枕屉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌西潘,老刑警劉巖喷市,帶你破解...
    沈念sama閱讀 211,376評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件品姓,死亡現(xiàn)場離奇詭異,居然都是意外死亡衬潦,警方通過查閱死者的電腦和手機(jī)植酥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評論 2 385
  • 文/潘曉璐 我一進(jìn)店門惧互,熙熙樓的掌柜王于貴愁眉苦臉地迎上來喊儡,“玉大人,你說我怎么就攤上這事买喧〈以撸” “怎么了算柳?”我有些...
    開封第一講書人閱讀 156,966評論 0 347
  • 文/不壞的土叔 我叫張陵瞬项,是天一觀的道長。 經(jīng)常有香客問我猪杭,道長妥衣,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,432評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮芦倒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘闺属。我一直安慰自己周霉,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,519評論 6 385
  • 文/花漫 我一把揭開白布国瓮。 她就那樣靜靜地躺著乃摹,像睡著了一般跟衅。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上掰读,一...
    開封第一講書人閱讀 49,792評論 1 290
  • 那天蹈集,我揣著相機(jī)與錄音雇初,去河邊找鬼。 笑死靖诗,一個胖子當(dāng)著我的面吹牛呻畸,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播咒循,決...
    沈念sama閱讀 38,933評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼绞愚,長吁一口氣:“原來是場噩夢啊……” “哼位衩!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起僚祷,我...
    開封第一講書人閱讀 37,701評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎俺榆,沒想到半個月后装哆,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,143評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡萍桌,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,488評論 2 327
  • 正文 我和宋清朗相戀三年上炎,在試婚紗的時候發(fā)現(xiàn)自己被綠了反症。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片畔派。...
    茶點(diǎn)故事閱讀 38,626評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡线椰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出烦绳,到底是詐尸還是另有隱情配紫,我是刑警寧澤,帶...
    沈念sama閱讀 34,292評論 4 329
  • 正文 年R本政府宣布享扔,位于F島的核電站惧眠,受9級特大地震影響于个,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜秀存,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,896評論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望宙项。 院中可真熱鬧,春花似錦汇荐、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽倾贰。三九已至匆浙,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間首尼,已是汗流浹背言秸。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評論 1 265
  • 我被黑心中介騙來泰國打工查排, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人跋核。 一個月前我還...
    沈念sama閱讀 46,324評論 2 360
  • 正文 我出身青樓了罪,卻偏偏與公主長得像泊藕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子娃圆,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,494評論 2 348

推薦閱讀更多精彩內(nèi)容