Common Java Interview Questions and Answers

7 kind of common questions:
Basic Java Interview Questions
Java OOPs Interview Questions
JDBC Interview Questions
Spring Interview Questions
Hibernate Interview Questions
JSP Interview Questions
Java Exception and Thread Interview Questions

let conquer them one by one:
Basic Java Interview Questions
Q1: what is JDK/JRE/JVM?
I know what they are stand for.
JDK is the tool to help compile and package Java programs
JRE(Java Runtime environment): makes Java bytecode can be executed
JVM: makes Java bytecode can be executed. JVM + Library = JRE
We use JDK(which is Java API)to program our own code, and compile it into Java bytecode. JRE executed those bytecode. JVM interprets those bytecodes.

Q2: explain public static void main(String args[]) in Java
main() us the entry point of any java program(Don’t need anything! Simple!)
Public: this is a access modifier.(public, private, protected)
Static: it’s the key word of Java. it can use in everywhere: variable, method and classes. and it indicates that this member belongs to a type itself, rather than to an instance of that type.
Void: return type
main: one of the key word of Java, it will searched by JVM as a starting point.

Q3: why do we call Java “platform independent”?
Because it’s bytecode can run on any system irrespective of its underlying operating system.

Q4: what make Java not 100% objected oriented?
because of those 8 primitive type data.

Q5: what are wrapper classes in Java?
it convert the java primitives into the reference types. like int -> Integer

Q6: what are constructors?
It’s a block of code which is used to initialize an object.
two type of constructors: Default and Parameterized.

Q7: what is singleton class? how to develop a singleton class?
this class is a class whose only one instance can be created in any time.
we only need to set the constructor with private keyword. (see? private keyword can be used in anywhere: class, variable, constructor, method…)

8 difference between equals() and ==
equals() used to checking the value equality of two objects
= =use to check the address for two primitive data or reference object.

9 difference between heap and stack memory
Common Java Interview Questions and Answers

10 what is package in Java?
Package is a collection of related classes and interfaces which are bundled together.

11 what is OOP?
it is a programming model where programs are organized around objects rather than logic and functions.

12 the main concept of OOP and OOD:
OOP: IEAP
OOD: SOLID

13 the difference between this() and super()?
they are special keywords used to call the constructor.
this() represents the current instance of a class. and it used to access method of the current class.
super() represents the current instance of a parent/base class, and it used to access methods of the base class.

14 what is Java string pool?
it’s a collection of strings which are stored in heap memory. whenever a new object is created, string pool first checks whether the object is already present in the pool or not, if it is, then the same reference is returned to the variable else new object will be created in the string pool.

15 what’s the difference between static and non-static method?
1 static method need to be specified with the key word “static” but non static don’t
2 static method needs to be called in className.methodName, but non-static method can be called instanceName.methodName()
3 static method can’t use non-static method. and non static method can access any static method and any static variable without creating an instance of the class.

16 what is constructor chain in Java?
constructor chaining is the process of calling one constructor from another with respect to the current object.
it has related to this() and super()

17 stringBuilder and stringBuffer
remember the reason why we don’t use stringbuffer more, not because it’s thread not safe, but the opposite. but in the same time it’s not efficient.

18 what is collection class in Java?
the collection is a framework that acts as an architecture for storing and manipulating a group of objects.(like searching. sorting, inserting, deletion etc)
collection framework includes: interfaces and classes.
List Queue Set is the three main interfaces of this framework.

19 method overloading and overriding?
overloading exist in the same method but overriding is the exactly same method(including name, parameters, return type etc)

20 can you override a private or static method in java?
let’s think about private: private means you can’t access outside of the class. and override means override method in anothe r class, so definitely a no-no.
static means it will be loaded even before we create any class instances, if we try to override it, then what’s the point? so static can’t be override too.

21 how can we achieve encapsulation in Java?
1 declaring the variable of a class as private.
2 provide public setter and getter methods to modify and view he values of the variables.

22 what is object cloning in Java?
Object cloning in java is the process of create an exact copy of an object. (it usually means deep clone)
To object clone(), the marker interface java.lang.Cloneable must be implemented to avoid any runtime exceptions. One thing you must note is Object clone() is a protected method, thus you need to override it.

上一篇:DevSecOps 三问:Why?What?How?


下一篇:剑桥雅思写作高分范文ESSAY64