Java Interview Questions for Beginners — 5
Common Interview Questions
1. Whether the program get compile, if public static void main changed to static public void main?
Compilation will get successful.
2. What are all methods in Object class ?
clone(), equals(), finalize(), hashCode(), wait(), notify(), notifyAll(), toString()
3. Why you need to override hashCode, when you override equals in Java?
Before discussing the relationship between both Equals and hashcode, we first what those methods actually do.
First they are the methods from Object class.
equals:
public boolean equals(Object obj)
This method accepts an Object as argument. The default implementation of this method in Object class simply checks if two object references x and y refer to the same object. i.e. It checks if x == y. This comparison is called Shallow Comparison
hashCode:
public int hashCode()
This method returns hash code value for the object on which this method is invoked. This method is widely used by Hashtable, HashMap, HashSet
There is a contract between two methods, Equal objects must produce the same hash code as long as they are equal. So when you override equals method, you should override hashcode method as well.
4. What is Auto boxing and Unboxing
Autoboxing — Conversion of primitive to corresponding wrapper class automatically by Java compiler
int a = 10;Integer a2 = new Integer(a); // Without AutoboxingInteger a3=a; //With Autoboxing
Unboxing — Conversion of wrapper to corresponding wrapper class automatically by Java compiler
Integer a = new Integer(10);int a2 = a; // Integer will be converted to int automatically
5. Can we overload main method?
Yes we can overload main method. Still the method public static void main(String arg[]) only will get called at Java execution startup.
6. What is static block?
static {// code goes here}
Like String, static keyword has a number of uses in Java. Hence we expect all of you to go through complete detail about static keyword which will help you in any interview.
Please read the following article completely & carefully. You can get back to us anytime on any issue in understanding.
http://www.journaldev.com/1365/java-static-keyword-class-method-variable-block-import
http://www.jusfortechies.com/java/core-java/static-blocks.php
7. What is the use of the final keyword in Java ?
Final have different meaning depends on the context its been used.
In class : Final class cannot be inherited further..simply there can’t be any subclass for a final class. String is the example for final class in Java.
In Property: If a class property is declared as final, it will be treated as constant.. so value can’t be changes further (so needs a value while creating itself).
In Method: if method declared as final, it can’t be overridden.
8. What is ternary operator in Java ?
Conditional operator (?:) is also called the ternary operator in Java. Since its accepting three operands, it’s called ternary.
It’s the shorthand for if-else statement.
If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result.
String state = “Tamilnadu”;
String data = state.contains(“A”) ? “state contains ‘A’” : “state doesn’t contain ‘A’”;
In the above statement, state.contains(“A”) before ? is the condition.
If the condition is true, then String data will be stored with “state contains A” message (first value). If the condition is false, String data will contain “state doesn’t contains A message (second value).
9. What is instanceof keyword in Java ?
instanceof operator is used to check whether the object is an instance of the specified type. It will return either true or false.
class Student{}class Hostel_Student extends Student{}class Library_Member extends Student{}Student s1 = new Student();Student s2 = new Hostel_Student();Student s3 = new Library_Member();if(s1 instanceof Student) // Result is trueif(s2 instance of Hostel_Student) //Result is true
10. What is break and continue statement ?
Break — terminates the loop execution & next statement to loop will get executed.
Continue — skips the execution of current iteration of loop (Not the loop itself)
11. What is Java heap memory ?
Java objects we create will reside in the heap memory space. When Heap memory gets full, JVM runs the garbage collection operation (objects which are not referred for long time will be erased from memory, so that space can be used for other objects) .
Java memory management is a vast area & suggest all of you to go through oracle definition to get more idea about it.
https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/geninfo/diagnos/garbage_collect.html
12. What is JIT Compiler ?
The Just-In-Time (JIT) compiler is a component of the Java™ Runtime Environment that improves the performance of Java applications at run time.
The JIT compiler helps improve the performance of Java programs by compiling bytecode into native machine code at run time. The JIT compiler is enabled by default, and is activated when a Java method is called.
The JIT compiler compiles the bytecode of that method into native machine code, compiling it “just in time” to run.
13. What is Serialization in Java ?
Java object is created in memory & we can’t use it in later time once it is later erased from memory by Garbage collector.
Serialization is the way to store/persist the state of Object in binary format so we can recreate it later. A class should implement Serializable interface (Interface with no method or simply called marker interface) in order to use serialization.
14. What is a Java bean class ?
Java class which follows the following conditions are called Java bean classes
- should have a no-arg constructor,
- should implement Serializable interface,
- should provide methods to getter and setter methods for properties
15. What is marker interface ?
An interface with no methods is called Market Interface. e.g. Serializable, Clonnable are marker interfaces (no methods in those interfaces) is used to indicate something to compiler or JVM that the class implementing any of these would have some special behavior.
16. What is package in Java?
A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer.
Default imported package in all Java class is java.lang.* To use a package, we use the keyword import.
17. What will happen if you call return statement or System.exit on try or catch block ? will finally block execute?
If we use return statement in try block, finally will surely works.
If we use System.exit(0) in try block, finally won’t work