Top 50 Mostly Asked Core Java Interview Questions

Top 50 core java interview questions, core java interview questions

Java is a widely-used programming language that is known for its robustness, scalability, and platform independence. As an Java developer, it is very important to have a deep understanding of the language and its advanced concepts to excel in interviews. In this article, I am going to share some common and challenging interview questions related to Core Java topics. These questions will help you prepare well for your next Java interview.


1. What is Java?

Java is a high-level, widely used, object-oriented and secure programming language developed by Sun Microsystems.


2. Why is Java not a pure object oriented language?

Java is not a pure object-oriented programming language because it makes use of primitive data types such as byte, boolean, char, short, int, long, float, and double.


3. What are the primitive data types in Java?

Java has eight primitive data types, including int, byte, short, long, float, double, char, and boolean.


4: What is the difference between an Inner Class and a Sub-Class?

 An Inner Class is a class which is declared within another class. A Sub-Class is a class which inherits from another class.


5. Explain the concept of method overloading in Java.

Method overloading in java means within a class creating multiple methods with the same name but different parameters. The compiler resolves which method to call based on the number, type, and order of arguments passed. Method Overloading can provide more flexibility and readability in code.


6. What is the difference between method overloading and method overriding?

- Method overloading occurs in the same class, while method overriding occurs in the parent-child class relationship.

- In method overloading, the number and type of arguments need not be the same. However, in method overriding, the number of arguments and their types should be the same as they are present in the parent class.


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

equals() method is used to compare the content of objects, while '==' is used to compare object references.


8. What is the difference between a JDK and a JRE?

A JDK contains the development tools necessary to compile and debug programs written in the Java programming language. A JRE contains only the runtime environment needed to execute a Java program.


9. What is the purpose of garbage collection in Java? 

The purpose of garbage collection in Java is to reclaim memory that is no longer being used by the program. This helps to keep the program running smoothly and prevents it from running out of memory.


10. What is a static block?

A static block is a block of code that is executed when a class is first loaded into memory.  It is used to initialize static variables or perform any other static initialization tasks.


11. What is the use of 'static' keyword in Java?

 The 'static' keyword is used to create class members that belong to the class rather than instances of the class.


12. What are the access modifiers in Java?

The four access modifiers are: public, protected, private, and default (no modifier).


13. What is a Java package?

A package is a way of organizing related classes and interfaces into a single space.


14. What are the differences between the equals() and hashCode() methods in Java?

The equals() method in Java is used to compare the equality of two objects based on their content. The hashCode() method is used to generate hash code for an object. Hash codes are used in data structures like 'HashMap' to locate objects quickly in hash-base collections.


15. Why is Java a platform independent programming language?

Java is considered as a platform-independent programming language because of the JVM (Java Virtual Machine). When we compile our source code, it gets converted into bytecode, and that bytecode can run on any operating system, but the JVM must be present on that machine. This is why Java is considered as a platform-independent programming language. 


16. What is the bootstrap classloader?

The bootstrap classloader is responsible for loading the core java classes from rt.jar and other jar files in the jre/lib directory.


 17. Difference between Heap and Stack Memory in java.

Stack memory is used to store all variables and methods in our program. 

And when we create objects in our program, they get created in the heap memory and are referenced from the stack memory.


18. In your own words, please clarify the concepts of an instance variable and a local variable.

Instance varibles:  are the variables that are declared inside the class but outside any other blocks like methods, constructors, etc. These variables are accessible by all the methods in the class. and these variables describe the properties of an object.

Example: 

class Student {
	String name;
	int rollNo;
	String address;
}


Local variables: are the variables that are declared inside the block of code like a method and constructors. These variables are accessible only inside that block, Which means other class methods don't have any knowledge about the local variables.

Example:

class Student{
	public void s1(){
	  String name;
	  int rollNo;
	  String address;
    }
}



19.What are the pillars of OOP?

The four pillars of OOPs are:

Inheritance

Encapsulation

Polymorphism

Abstraction

For more OOPs interview questions click here.


20. What is an abstract class? 

 An abstract class is a class that contains one or more abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.


21. What is the difference between an Interface and an Abstract Class?

An Interface is a completely abstract class which can contain only static final constants and method signatures. An Abstract Class can contain instance variables, concrete methods, and abstract methods.



22. What is Interface in Java?

Interface in Java is the same as a class, but the difference is that it contains only method declarations and not method implementations. In simple terms, it includes methods with empty bodies. The method body or implementation is provided by the class that implements the interface.


23. What is the difference between Abstract class and Interface?

Abstract Class:

- An abstract class can contain both abstract and non-abstract methods.

- The class that extends the abstract class does not require providing the implementation of all of its methods.

- An abstract class can have instance variables.

- It can have a constructor.


Interface:

- An interface contains only abstract methods, and from Java 8, it can also contain default and static methods.

- The classes that implement the interface should provide the implementation of all of its methods.

- An interface cannot have instance variables.

- It cannot have constructors because it cannot be instantiated directly.



24. What is the Constructor?

A constructor is a special type of method which is used to initialize an object. To declare a constructor, we should write its name the same as the class name, and a constructor does not have an explicit return type. The constructor is invoked when an object of the class is created.


25. What are the types of the constructor in Java?

There are two types of constructor in Java:

1.Default Constructor: Default constructor in java does not accepts any value. It is mainly used to initialize the instance variables with default values. If there is no constructor is defined in the class then default constructor will be invoked implicitly by the compiler.

2. Parameterized Constructor: In simple words, a parameterized constructor is the constructor that accepts arguments, and we can use those arguments to initialize the instance variables.


26. What are the differences between the String, StringBuffer, and StringBuilder classes in Java?

The String class in java is immutable class, that means we cannot change it once it is created. On the other hand, StringBuffer and StringBuilder classes are mutable, that means it allows us to modify it. The main difference between StringBuffer and StringBuilder is that the StringBuffer is synchronized and therefore thread-safe, while the StringBuilder is not.


27. What is the purpose of the final keyword in Java?

The final keyword is used to define constants, like if we declare variable as a final then we cannot change its value, if we make method as a final then we cannot override it and if we declare class as a final then it cannot be extended or subclassed.


28. What is the difference between fail-fast and fail-safe iterators in Java?

In Java, fail-fast iterators throws a ConcurrentModificationException if a collection is modified while iterating over it. On the other hand, fail-safe iterators don't throw an exception if collection is modified during iteration.


29. What is the this keyword used for?

The 'this' keyword refers to the current instance of the class and is used to differentiate instance variables from local variables.


30. Explain the super keyword.

The 'super' keyword is used to refer to the parent class's members.


31. What is the output of the following Java program?

class Test   
{  
    int a;   
}  
public class Main   
{  
    public static void main (String args[])   
    {  
        Test test = new Test();   
        System.out.println(test.a);  
    }  
}  
  

The output of the above code is 0 because, as we know, if there is no constructor declared in the class, the default constructor will get invoked implicitly, and it will assign value 0 to the variable 'a.'


32. How does the hashCode() method work?

The hashCode() method returns a unique integer for each object, used primarily in hash-based data structures like HashMap and HashSet.


33. Explain the 'instanceof' operator.

'instanceof' is used to test if an object is an instance of a particular class or interface.


34. How does exception handling work in Java?

Java uses try, catch, and finally blocks to handle exceptions. Exceptions are objects that represent errors. 


35. Differentiate between checked and unchecked exceptions.

Checked exceptions must be handled by the programmer using try-catch or declared in the method signature. Unchecked exceptions do not need to be explicitly handled.


36. What is the purpose of the transient keyword?

The 'transient' keyword is used to indicate that a variable should not be serialized during object serialization.


37. What is the 'try-with-resources' statement?

Introduced in Java 7, 'try-with-resources' automatically closes resources like files or sockets when they are no longer needed.


38. What is the purpose of the 'enum' keyword?

'enum' is used to define a fixed set of constants.


39. What is the Observer design pattern?

The Observer pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.


40. Explain the 'Singleton' design pattern.

'Singleton' ensures a class has only one instance and provides a global point of access to it.


41. How does Java support multiple inheritance?

Java supports multiple inheritance through interfaces.


42. What is the 'super' keyword used for in constructors?

'super()' is used to invoke the parent class's constructor.


43. What is the 'finalize()' method?

'finalize()' is called by the garbage collector before an object is reclaimed.


44. Explain the 'ClassNotFoundException' in Java.

'ClassNotFoundException' is thrown when the Java Virtual Machine tries to load a particular class but cannot find it on the classpath.


45. What is the purpose of the 'Comparator' interface?

'Comparator' is used to define custom ordering for objects.


46. How does the 'ClassLoader' work in Java?

The 'ClassLoader' is responsible for loading classes into the Java Virtual Machine dynamically.


47. Explain the concept of Java annotations.

Java annotations provide metadata about code elements such as classes, methods, and variables. They can be used to provide additional information of the code to enable compile-time checks.


48. What is a copy constructor in Java?

 A copy constructor is a special type of constructor that creates an object by copying the values of another object of the same class. But there is no copy constructor in Java

However, In Java there are other ways to copy the values of one object into another Like:

-By constructor

-By assigning the values of one object into another

-By clone() method of Object class 


49. What are the differences between constructors and methods?

 Purpose:
      - Constructors are used to initialize the state of an object when it is created.
      - Methods, on the other hand, are functions that define the behavior of an object.
 Return Type:
      - Constructors have no return type, not even `void`.
      - Methods have a return type, which can be any valid data type or `void` if the method doesn't return any value.
Invocation:
      - Constructors are automatically called when an object is created using the `new` keyword.
      - Methods are called explicitly by name, typically on an already created object.

50. Can we overload methods by making them static?

Yes, methods can be overloaded by making them static in Java. Method overloading is the ability to define multiple methods in the same class with the same name but different parameters. 

51. What is object cloning?

Object cloning in Java refers to the process of creating an exact copy of an object. This can be achieved by implementing the `Cloneable` interface and providing a `clone()` method in the class. The `clone()` method creates and returns a new object with the same state as the original object. It's important to note that shallow and deep cloning concepts come into play, where shallow cloning copies the object and its fields, but not the objects referenced by its fields, while deep cloning creates a new object and also clones the objects referenced by the original object's fields.


In this article, we have explored some of the most frequently asked Core Java interview questions. Practicing these Core Java interview questions will undoubtedly help you ace your Java-related interviews.


    Related Articles:



No comments:

Post a Comment

If you have any doubts, please discuss here...👇