Jasa SEO,Jasa Pembuatan Website.
17 February, 2020
Interview questions for java

Interview questions for java that must be prepare.

Java Interview Questions

Core Java interview questions help you in preparing for java based interviews. Whether you are a fresher or highly experienced professional, core java plays a vital role in any Java/JEE interview.

Core Java is the favorite area in most of the interviews and plays a crucial role in deciding the outcome of your interview. Interview questions for java we discuss in this post are must to prepare before going to Interview.

In this Interview questions for java blog, I am going to list some of the most important Java Interview Questions and Answers which will set you apart in the interview process. Java is used by approx 10 Million developers worldwide to develop applications for 15 Billion devices supporting Java. It is also used to create applications for trending technologies like Big Data to household devices like Mobiles and DTH boxes.

This post on JAVA Interview Questions is prepared to help you understand the basic concepts of Java programming for interview purposes. All the important JAVA concepts are explained here with examples for your easy understanding.

This tutorial covers JAVA topics like basic Java definitions, Oops concepts, Access specifiers, Collections, Exceptions, Threads, Serialization etc., with examples to make you get ready perfectly to face any JAVA interview confidently.

 

 

Q1. Can we Overload or Override static methods in java ?

  • Overriding : Overriding is related to run-time polymorphism. A subclass (or derived class) provides a specific implementation of a method in superclass (or base class) at runtime.
  • Overloading: Overloading is related to compile time (or static) polymorphism. This feature allows different methods to have same name, but different signatures, especially number of input parameters and type of input paramaters.
  • Can we overload static methods?   The answer is ‘Yes’. We can have two ore more static methods with same name, but differences in input parameters
  • Can we Override static methods in java?  We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism. Hence the answer is ‘No’. Static methods cannot be overridden because method overriding only occurs in the context of dynamic (i.e. runtime) lookup of methods. Static methods (by their name) are looked up statically (i.e. at compile-time).

Q2. What do you mean by platform independence of Java?

Platform independence means that you can run the same Java Program in any Operating System. For example, you can write java program in Windows and run it in Mac OS.

Q3. What do you understand by Java virtual machine?

Java Virtual Machine is a virtual machine that enables the computer to run the Java program. JVM acts like a run-time engine which calls the main method present in the Java code. JVM is the specification which must be implemented in the computer system. The Java code is compiled by JVM to be a Bytecode which is machine independent and close to the native code.

Q4. What are the features in JAVA?

  • Oops concepts
    • Object-oriented
    • Inheritance
    • Encapsulation
    • Polymorphism
    • Abstraction
  • Platform independent:
  • Multi-threaded

Q5. Why Java is not 100% Object-oriented?

Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.

Q6. What is the difference between JDK, JRE, and JVM?

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 which specifies the working of Java Virtual Machine. Its implementation has been provided by Oracle and other companies. Its implementation is known as JRE.

JVMs are available for many hardware and software platforms (so JVM is platform dependent). It is a runtime instance which is created when we run the Java class. There are three notions of the JVM: specification, implementation, and instance.

JRE:JRE stands for Java Runtime Environment. It is the implementation of JVM. The Java Runtime Environment is a set of software tools which are used for developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.

JDK: JDK is an acronym for Java Development Kit. It is a software development environment which is used to develop Java applications and applets. It physically exists. It contains JRE + development tools. JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation:

  • Standard Edition Java Platform
  • Enterprise Edition Java Platform
  • Micro Edition Java Platform

Q7. Is it possible to make array volatile in Java?

Yes, it is possible to make an array volatile in Java, but only the reference which is pointing to an array, not the whole array. Therefore, if one thread changes the reference variable points to another array, which will provide a volatile guarantee.

However, if several threads are altering particular array elements, there won’t be any happens before assurance provided by the volatile modifier for such modification.

If the purpose is to provide memory visibility guarantee for individual indices of the array, volatile is of no practical use for you. Instead, you must rely on an alternative mechanism for thread-safety in that particular case, e.g. use of a synchronized keyword.

Q8. What do you mean by Constructor?

  • When a new object is created in a program a constructor gets invoked corresponding to the class.
  • The constructor is a method which has the same name as class name.
  • If a user doesn’t create a constructor implicitly a default constructor will be created.
  • The constructor can be overloaded.
  • If the user created a constructor with a parameter then he should create another constructor explicitly without a parameter.

Q9. What is the difference between Array list and vector in Java?

ArrayListVector
Array List is not synchronized. Vector is synchronized.
Array List is fast as it’s non-synchronized.Vector is slow as it is thread safe.
If an element is inserted into the Array List, it increases its Array size by 50%.Vector defaults to doubling size of its array.
Array List does not define the increment size.Vector defines the increment size.
Array List can only use Iterator for traversing an Array List.Vector can use both Enumeration and Iterator for traversing.

 

Q10. Why Java is not pure Object Oriented language?

Java is not said to be pure object-oriented because it supports primitive types such as int, byte, short, long etc. I believe it brings simplicity to the language while writing our code. Obviously, java could have wrapper objects for the primitive types but just for the representation, they would not have provided any benefit.

As we know, for all the primitive types we have wrapper classes such as Integer, Long etc that provides some additional methods.

Q11. What happens if you remove static modifier from the main method?

Program compiles successfully . But at runtime throws an error “NoSuchMethodError”.

Q12. What is classloader?

Classloader is a subsystem of JVM which is used to load class files. Whenever we run the java program, it is loaded first by the classloader. There are three built-in classloaders in Java.

  1. Bootstrap ClassLoader: This is the first classloader which is the superclass of Extension classloader. It loads the rt.jar file which contains all class files of Java Standard Edition like java.lang package classes, java.net package classes, java.util package classes, java.io package classes, java.sql package classes, etc.
  2. Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader of System classloader. It loads the jar files located inside $JAVA_HOME/jre/lib/ext directory.
  3. System/Application ClassLoader: This is the child classloader of Extension classloader. It loads the class files from the classpath. By default, the classpath is set to the current directory. You can change the classpath using “-cp” or “-classpath” switch. It is also known as Application classloader.

Q13. What is overloading and overriding in java?

When we have more than one method with the 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.

Q14. What are the various access specifiers in Java?

In Java, access specifiers are the keywords which are used to define the access scope of the method, class, or a variable. In Java, there are four access specifiers given below.

  • Public The classes, methods, or variables which are defined as public, can be accessed by any class or method.
  • Protected Protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.
  • Default Default are accessible within the package only. By default, all the classes, methods, and variables are of default scope.
  • Private The private class, methods, or variables defined as private can be accessed within the class only.

Q15. What are the advantages of Packages in Java?

There are various advantages of defining packages in Java.

  • Packages avoid the name clashes.
  • The Package provides easier access control.
  • We can also have the hidden classes that are not visible outside and used by the package.
  • It is easier to locate the related classes.

read some more Interview questions for java

Q16. What is the difference between equals() and == in Java?

Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.

“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two objects.

Q17. 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[]).

Q18. What is object-oriented paradigm?

It is a programming paradigm based on objects having data and methods defined in the class to which it belongs. Object-oriented paradigm aims to incorporate the advantages of modularity and reusability. Objects are the instances of classes which interacts with one another to design applications and programs. There are the following features of the object-oriented paradigm.

  • Follows the bottom-up approach in program design.
  • Focus on data with methods to operate upon the object’s data
  • Includes the concept like Encapsulation and abstraction which hides the complexities from the user and show only functionality.
  • Implements the real-time approach like inheritance, abstraction, etc.
  • The examples of the object-oriented paradigm are C++, Simula, Smalltalk, Python, C#, etc.

Q19. Why pointers are not used in Java?

Java doesn’t use pointers because they are unsafe and increases the complexity of the program. Since, Java is known for its simplicity of code, adding the concept of pointers will be contradicting. Moreover, since JVM is responsible for implicit memory allocation, thus in order to avoid direct access to memory by the user,  pointers are discouraged in Java.

Q20. How many types of constructors are used in Java?

Based on the parameters passed in the constructors, there are two types of constructors in Java.

  • Default Constructor: default constructor is the one which does not accept any value. The default constructor is mainly used to initialize the instance variable with the default values. It can also be used for performing some useful task on object creation. A default constructor is invoked implicitly by the compiler if there is no constructor defined in the class.
  • Parameterized Constructor: The parameterized constructor is the one which can initialize the instance variables with the given values. In other words, we can say that the constructors which can accept the arguments are called parameterized constructors.

 

Candidate should be prepare these Interview questions for java before going for interview.

Q21. What are the main concepts of OOPs in Java?

Object-Oriented Programming or OOPs is a programming style that is associated with concepts like:

  1. Inheritance: Inheritance is a process where one class acquires the properties of another.
  2. Encapsulation: Encapsulation in Java is a mechanism of wrapping up the data and code together as a single unit.
  3. Abstraction: Abstraction is the methodology of hiding the implementation details from the user and only providing the functionality to the users.
  4. Polymorphism: Polymorphism is the ability of a variable, function or object to take multiple forms.

Q22. Does constructor return any value?

yes, The constructor implicitly returns the current instance of the class (You can’t use an explicit return type with the constructor).

Q23. Is constructor inherited?

No, The constructor is not inherited.

Q24. Can you make a constructor final?

No, the constructor can’t be final.

Q25. Can we overload the constructors?

Yes, the constructors can be overloaded by changing the number of arguments accepted by the constructor or by changing the data type of the parameters. Consider the following example.

class Test   
{  
    int i;   
    public Test(int k)  
    {  
        i=k;  
    }  
    public Test(int k, int m)  
    {  
        System.out.println("Hi I am assigning the value max(k, m) to i");  
        if(k>m)  
        {  
            i=k;   
        }  
        else   
        {  
            i=m;  
        }  
    }  
}  
public class Main   
{  
    public static void main (String args[])   
    {  
        Test test1 = new Test(10);  
        Test test2 = new Test(1215);  
        System.out.println(test1.i);  
        System.out.println(test2.i);  

    }  
}

In the above program, The constructor Test is overloaded with another constructor. In the first call to the constructor, The constructor with one argument is called, and i will be initialized with the value 10. However, In the second call to the constructor, The constructor with the 2 arguments is called, and i will be initialized with the value 15.

Q.26. What is “this” keyword in java?
Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

Q27. 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 the 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.

Java interface variables are by default final and static.

Q28. 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.

Q29. What is finally and finalize in java?

finally block is used with try-catch to put the code that you want to get executed always, even if any exception is thrown by the try-catch block. finally block is mostly used to release resources created in the try block.

finalize() is a special method in Object class that we can override in our classes. This method gets called by the garbage collector when the object is getting garbage collected. This method is usually overridden to release system resources when the object is garbage collected.

Q30. What is the static variable?

The static variable is used to refer to the common property of all objects (that is not unique for each object), e.g., The company name of employees, college name of students, etc. Static variable gets memory only once in the class area at the time of class loading. Using a static variable makes your program more memory efficient (it saves memory). Static variable belongs to the class rather than the object.

Oops Concepts Interview questions for java that must be prepare.

 

Q31 What is Inheritance?

Ans: Inheritance means one class can extend to another class. So that the codes can be reused from one class to another class.

Existing class is known as Super class whereas the derived class is known as a sub class.

Example:

Super class:
public class Manupulation(){
}
Sub class:
public class Addition extends Manipulation(){
}

Inheritance is applicable for public and protected members only. Private members can’t be inherited.

Q 32 What is Encapsulation?

Ans: Purpose of Encapsulation:

  • Protects the code from others.
  • Code maintainability.

Example:

We are declaring ‘a’ as an integer variable and it should not be negative.

public class Addition(){
int a=5;
}

If someone changes the exact variable as “a = -5” then it is bad.

Q33. 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. There are two types of polymorphism:

  1. Compile time polymorphism
  2. Run time polymorphism

Compile time polymorphism is method overloading whereas Runtime time polymorphism is done using inheritance and interface.

Q34. What is runtime polymorphism or dynamic method dispatch?

In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. Let’s take a look at the example below to understand it better.

class Car {
void run()
{
System.out.println(“car is running”);
}
}
class Audi extends Car {
void run()
{
System.out.prinltn(“Audi is running safely with 100km”);
}
public static void main(String args[])
{
Car b= new Audi();    //upcasting
b.run();
}
}

Q35. What is abstraction in Java?

Abstraction refers to the quality of dealing with ideas rather than events. It basically deals with hiding the details and showing the essential things to the user. Thus you can say that abstraction in Java is the process of hiding the implementation details from the user and revealing only the functionality to them. Abstraction can be achieved in two ways:

  1. Abstract Classes (0-100% of abstraction can be achieved)
  2. Interfaces (100% of abstraction can be achieved)

Q36. What do you mean by an interface in Java?

An interface in Java is a blueprint of a class or you can say it is a collection of abstract methods and static constants. In an interface, each method is public and abstract but it does not contain any constructor. Thus, interface basically is a group of related methods with empty bodies. Example:

public interface Animal {
  public void eat();
  public void sleep();
  public void run();
}

Q37. What is an abstract class?

Abstract classes are used in java to create a class with some default method implementation for subclasses. An abstract class can have abstract method without body and it can have methods with implementation also.

abstract keyword is used to create a abstract class. Abstract classes can’t be instantiated and mostly used to provide base for sub-classes to extend and implement the abstract methods and override or use the implemented methods in abstract class. Read important points about abstract classes at java abstract class.

Q38. What is the difference between abstract class and interface?

abstract keyword is used to create abstract class whereas interface is the keyword for interfaces.

Abstract classes can have method implementations whereas interfaces can’t.

A class can extend only one abstract class but it can implement multiple interfaces.

We can run an abstract class if it has main() method whereas we can’t run an interface.

Some more differences in detail are at Difference between Abstract Class and Interface.

Q39. 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.

From Java 8 onwards, interfaces can have default method implementations. So to handle diamond problem when a common default method is present in multiple interfaces, it’s mandatory to provide implementation of the method in the class implementing them. For more details with examples, read Java 8 interface changes.

Q40. 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.

Read more Interview questions for java

Q41. What are Wrapper classes?

Java wrapper classes are the Object representation of eight primitive types in java. All the wrapper classes in java are immutable and final. Java 5 autoboxing and unboxing allows easy conversion between primitive types and their corresponding wrapper classes.

Q42. What is encapsulation in Java?

Encapsulation is a mechanism where you bind your data(variables) and code(methods) together as a single unit. Here, the data is hidden from the outer world and can be accessed only via current class methods. This helps in protecting the data from any unnecessary modification. We can achieve encapsulation in Java by:

  • Declaring the variables of a class as private.
  • Providing public setter and getter methods to modify and view the values of the variables.

Q43. What is inner class in java?

We can define a class inside a class and they are called nested classes. Any non-static nested class is known as inner class. Inner classes are associated with the object of the class and they can access all the variables and methods of the outer class. Since inner classes are associated with the instance, we can’t have any static variables in them.

We can have local inner class or anonymous inner class inside a class. For more details read java inner class.

Q44. What is anonymous inner class?

A local inner class without name is known as anonymous inner class. An anonymous class is defined and instantiated in a single statement. Anonymous inner class always extend a class or implement an interface.

Since an anonymous class has no name, it is not possible to define a constructor for an anonymous class. Anonymous inner classes are accessible only at the point where it is defined.

Q45. What is Classloader in Java?

Java Classloader is the program that loads byte code program into memory when we want to access any class. We can create our own classloader by extending ClassLoader class and overriding loadClass(String name) method. Learn more at java classloader.

Q46. What are different types of classloaders?

There are three types of built-in Class Loaders in Java:

Bootstrap Class Loader – It loads JDK internal classes, typically loads rt.jar and other core classes.

Extensions Class Loader – It loads classes from the JDK extensions directory, usually $JAVA_HOME/lib/ext directory.

System Class Loader – It loads classes from the current classpath that can be set while invoking a program using -cp or -classpath command line options.

Here are some Other Interview questions for java

Q1. Explain JDK, JRE and JVM?
Q2. Explain public static void main(String args[]) in Java
Q3. Why Java is platform independent?
Q4. Why Java is not 100% Object-oriented?
Q5. What are wrapper classes in Java?
Q6. What are constructors in Java?
Q7. What is singleton class in Java and how can we make a class singleton?
Q8. What is the difference between Array list and vector in Java?
Q9. What is the difference between equals() and == in Java?
Q10. What are the differences between Heap and Stack Memory in Java

Must Read Interview questions for java 

  1. Name some OOPS Concepts in Java?
  2. What do you mean by platform independence of Java?
  3. What is JVM and is it platform independent?
  4. Which class is the superclass of all classes?
  5. Why Java doesn’t support multiple inheritance?
  6. Why Java is not pure Object Oriented language?
  7. What is difference between path and classpath variables?
  8. What is the importance of main method in Java?
  9. What is overloading and overriding in java?
  10. Can we overload main method?
  11. Can we have multiple public classes in a java source file?
  12. What is Java Package and which package is imported by default?
  13. What are access modifiers?
  14. What is final keyword?
  15. What is static keyword?
  16. What is finally and finalize in java?
  17. Can we declare a class as static?
  18. What is static import?
  19. What is try-with-resources in java?
  20. What is multi-catch block in java?
  21. What is static block?
  22. What is an interface?
  23. What is an abstract class?
  24. What is the difference between abstract class and interface?
  25. Can an interface implement or extend another interface?
  26. What is Marker interface?
  27. What are Wrapper classes?
  28. What is Enum in Java?
  29. What is Java Annotations?
  30. What is Java Reflection API? Why it’s so important to have?
  31. What is composition in java?
  32. What is the benefit of Composition over Inheritance?
  33. How to sort a collection of custom Objects in Java?
  34. What is inner class in java?
  35. What is anonymous inner class?
  36. What is Classloader in Java?
  37. What are different types of classloaders?
  38. What is ternary operator in java?
  39. What does super keyword do?
  40. What is break and continue statement?
  41. What is this keyword?
  42. What is default constructor?
  43. Can we have try without catch block?
  44. What is Garbage Collection?
  45. What is Serialization and Deserialization?
  46. How to run a JAR file through command prompt?
  47. What is the use of System class?
  48. What is instanceof keyword?

Here Are Some More Interview questions for java

  1. Can we use String with switch case?
  2. Java is Pass by Value or Pass by Reference?
  3. What is difference between Heap and Stack Memory?
  4. Java Compiler is stored in JDK, JRE or JVM?
  5. What will be the output of following programs?

Leave a Reply

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