Core Java Interview Questions
  1. Why Java is platform independent?

    We can run the same Java Program in any Operating System. For example, We can write java program in Windows and run it in Mac OS.

  2. What is the difference between JDK and JVM?

    Java Development Kit (JDK) is for development purpose and JVM is a part of it to execute the java programs.

    JDK provides all the tools, executables and binaries required to compile, debug and execute a Java Program. The execution part is handled by JVM to provide machine independence.

  3. What is the difference between JVM and JRE?

    Java Runtime Environment (JRE) is the implementation of JVM. JRE consists of JVM and java binaries and other classes to execute any program successfully. JRE doesn’t contain any development tools like java compiler, debugger etc. If we want to execute any java program, we should have JRE installed.

  4. What are the principle concepts of OOPS?

    There are four principle concepts upon which object oriented design and programming rest. They are:

    • Abstraction
    • Polymorphism
    • Inheritance
    • Encapsulation
  5. What is Abstraction?

    Abstraction is the concept of exposing only the required essential characteristics and behavior with respect to a context.

    Real life example of Abstraction is ATM Machine, All are performing operations on the ATM machine like cash withdrawal, money transfer, retrieve mini-statement etc. but we can’t know internal details about ATM.

  6. What is Encapsulation?

    Encapsulation is a technique used for hiding the properties and behaviors of an object and allowing outside access only as appropriate. It prevents other objects from directly altering or accessing the properties or methods of the encapsulated object.

    The common example of encapsulation is capsule. In capsule all medicine are encapsulated in side capsule.

  7. What is Inheritance?

    Inheritance is the process by which objects of one class acquire the properties of objects of another class.

    The real life example of inheritance is child and parents, all the properties of father are inherited by his son.

  8. What is Polymorphism?

    Polymorphism is briefly described as “one interface, many implementations.” Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts – specifically, to allow an entity such as a variable, a function, or an object to have more than one form.

  9. What are differences between Abstract Class and Interface?

    Read our detailed article on Difference between Abstract Class and Interface

  10. What is overloading and overriding in java?

    When we have more than one method with same name in a single class but the arguments are different, then it is called as method overloading.

    Overriding concept comes in picture with inheritance when we have two methods with same signature, one in parent class and another in child class. We can use @Override annotation in the child class overridden method to make sure if parent class method is changed, so as child class.

  11. What are differences between Overloading and Overriding?

    Read our detailed article on Difference between Overloading and Overriding

  12. Why Java doesn’t support multiple inheritance?

    Java doesn’t support multiple inheritance in classes because of “Diamond Problem”.

    However multiple inheritance is supported in interfaces. An interface can extend multiple interfaces because they just declare the methods and implementation will be present in the implementing class. So there is no issue of diamond problem with interfaces.

  13. What are access modifiers?

    Java provides access control through public, private and protected access modifier keywords. When none of these are used, it’s called default access modifier.

  14. Can we overload main method?

    Yes, we can have multiple methods with name “main” in a single class. However if we run the class, java runtime environment will look for main method with syntax as public static void main(String args[]).

  15. Can we overide main method?

    No, because main is a static method. A static method can’t be overridden in Java.

  16. What is super keyword?

    super is a keyword which is used to access the method or member variables from the superclass. If a method hides one of the member variables in its superclass, the method can refer to the hidden variable through the use of the super keyword. In the same way, if a method overrides one of the methods in its superclass, the method can invoke the overridden method through the use of the super keyword.

    You can only go back one level. In the constructor, if you use super(), it must be the very first code, and you cannot access any this.xxx variables or methods to compute its parameters.

  17. What is this keyword?

    this keyword provides reference to the current object and it’s mostly used to make sure that object variables are used, not the local variables having same name.

    We can also use this keyword to invoke other constructors from a constructor.

  18. What is final keyword?

    final keyword is used with Class to make sure no other class can extend it, for example String class is final and we can’t extend it.

    We can use final keyword with methods to make sure child classes can’t override it.

    final keyword can be used with variables to make sure that it can be assigned only once. However the state of the variable can be changed, for example we can assign a final variable to an object only once but the object variables can change later on.

  19. What is static keyword?

    static keyword can be used with class level variables to make it global i.e all the objects will share the same variable.

    static keyword can be used with methods also. A static method can access only static variables of class and invoke only static methods of the class.

  20. What is static block?

    Static block which exactly executed exactly once when the class is first loaded into JVM. Before going to the main method the static block will execute.

  21. Can an interface implement or extend another interface?

    Interfaces don’t implement another interface, they extend it. Since interfaces can’t have method implementations, there is no issue of diamond problem. That’s why we have multiple inheritance in interfaces i.e an interface can extend multiple interfaces.

  22. What is Marker interface?

    A marker interface is an empty interface without any method but used to force some functionality in implementing classes by Java. Some of the well known marker interfaces are Serializable and Cloneable.

  23. What is Enum in Java?

    Enum in Java is a keyword. It is used to represent a fixed number of predefined values. Enumeration (Enum) in Java was introduced in JDK 1.5.

    Enums are lists of constants. When you need a predefined list of values which do represent some kind of numeric or textual data. Read more in details at Java Enum

  24. What is default constructor?

    No argument constructor of a class is known as default constructor. When we don’t define any constructor for the class, java compiler automatically creates the default no-args constructor for the class. If there are other constructors defined, then compiler won’t create default constructor for us.

  25. What is Garbage Collection?

    Garbage Collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. In Java, process of deallocating memory is handled automatically by the garbage collector.

    We can run the garbage collector with code Runtime.getRuntime().gc() or use utility method System.gc().

  26. What is the difference between static and non-static variables?

    A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.

  27. What is Serialization and Deserialization?

    Serialization: Serialization is a mechanism of converting the state of an object into a byte stream.

    Deserialization: Deserialization is a mechanism to convert back to object from the byte stream.

    Read more in details at Serialization and Deserialization in Java

  28. What is the use of System class?

    Java System Class is one of the core classes. One of the easiest way to log information for debugging is System.out.print() method.

    System class is final so that we can’t subclass and override it’s behavior through inheritance. System class doesn’t provide any public constructors, so we can’t instantiate this class and that’s why all of it’s methods are static.

    Some of the utility methods of System class are for array copy, get current time, reading environment variables. Read more at java.lang.System Class.

  29. What is instanceof keyword?

    We can use instanceof keyword to check if an object belongs to a class or not. We should avoid it’s usage as much as possible. Sample usage is:

    Since str is of type String at runtime, first if statement evaluates to true and second one to false.

  30. What is the difference between creating String as new() and literal?

    When we create string with new() Operator, it’s created in heap and not added into string pool while String created using literal are created in String pool itself which exists in PermGen area of heap.

    Read more in details at Facts about String in Java

  31. What is an exception?

    An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.

  32. What is error?

    An Error indicates that a non-recoverable condition has occurred that should not be caught. Error, a subclass of Throwable, is intended for drastic problems, such as OutOfMemoryError, which would be reported by the JVM itself.

  33. What are the types of Exceptions in Java

    There are two types of exceptions in Java, unchecked exceptions and checked exceptions.

    • Checked exceptions: A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses. Each method must either handle all checked exceptions by supplying a catch clause or list each unhandled checked exception as a thrown exception.
    • Unchecked exceptions: All Exceptions that extend the RuntimeException class are unchecked exceptions. Class Error and its subclasses also are unchecked.
  34. Why Runtime Exceptions are Not Checked?

    The runtime exception classes (RuntimeException and its subclasses) are exempted from compile-time checking because, in the judgment of the designers of the Java programming language, having to declare such exceptions would not aid significantly in establishing the correctness of programs. Many of the operations and constructs of the Java programming language can result in runtime exceptions. The information available to a compiler, and the level of analysis the compiler performs, are usually not sufficient to establish that such run-time exceptions cannot occur, even though this may be obvious to the programmer. Requiring such exception classes to be declared would simply be an irritation to programmers.

  35. What is the use of finally block?

    The finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not. This is right place to close files, release your network sockets, connections, and perform any other cleanup your code requires.

    Note: If the try block executes with no exceptions, the finally block is executed immediately after the try block completes. It there was an exception thrown, the finally block executes immediately after the proper catch block completes.

  36. What if there is a break or return statement in try block followed by finally block?

    If there is a return statement in the try block, the finally block executes right after the return statement encountered, and before the return executes.

  37. Can we have the try block without catch block?

    Yes, we can have the try block without catch block, but finally block should follow the try block.

    Note: It is not valid to use a try clause without either a catch clause or a finally clause.

  38. How to create custom exceptions?

    By extending the Exception class or one of its subclasses.

  39. What are the different ways to handle exceptions?

    There are two ways to handle exceptions:

    • Wrapping the desired code in a try block followed by a catch block to catch the exceptions.
    • List the desired exceptions in the throws clause of the method and let the caller of the method handle those exceptions.
  40. What are differences between Throw and Throws?

    Read our detailed article on Difference between Throw and Throws

  41. What is an immutable object?

    The state of an immutable object cannot be changed after construction. Immutable objects can play an important roll in threading.

    A good example of an immutable objeect is String. Any modification to an immutable object will result in the creation of a new object.

  42. What is pass by reference and pass by value?

    In pass by reference, a reference to the object is passed around, not the actual object.

    In pass by value, a copy of the actual object is passed around. This means it cannot be modified by the method it is passed to.

  43. Is Java Pass by reference or pass by value?

    Java is always passed by value. However, when passing an object to a method, the reference is passed by value. This means the same object is referenced by both the method and the caller despite being passed by value.

  44. What is Autoboxing?

    Autoboxing is the process of the JVM automatic converting between a primitive type and its companion Object equivalent, e.g. int and Integer, boolean and Boolean.

    We should be careful when relying on this as it can have huge performance impacts if not used correctly as well as causing some very subtle bugs.

  45. How is Garbage Collection managed?

    The JVM controls the Garbage Collector; it decides when to run the Garbage Collector. JVM runs the Garbage Collector when it realizes that the memory is running low. The behavior of GC can be tuned by passing parameters to JVM.

    One can request the Garbage Collection to happen from within the Java program, but there is no guarantee that this request will be taken care of by JVM.

  46. When do you override hashCode() and equals()?

    hashCode() and equals() methods have been defined in Object class which is the parent class for Java objects. For this reason, all Java objects inherit a default implementation of these methods.

    hashCode () method is used to get a unique integer for giving an object. This integer is used for determining the bucket location, when this object needs to be stored in some HashTable like data structure. By default, Object’s hashCode() method returns and integer representation of memory address where the object is stored. Read more in details at hashCode() Method in Java

    equals() method, as the name suggests, is used to simply verify the equality of two objects. Default implementations simply checks the object references of two objects to verify their equality. Read more in details at equals() Method in Java

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *