JAVA基础知识,面试必备

2020-01-30  本文已影响0人  奔跑的程序媛A

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()"

4. HashTable VS HashMap

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

13. Stack memory VS Heap memory

14. final VS finalize VS finally

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

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()

21. ArrayList VS Vector VS LinkedList

上一篇下一篇

猜你喜欢

热点阅读