Core Java Interview Questions and Answers 2024


Java is high-level object-oriented, robust, secure platform independent, high performance and portable programming language. James Gosling created it in 1991. It is referred recognized as a platform since it has its own Java Runtime Environment (JRE) and Application Programming Interface (API).

1. List the features of Java programming ?

  • simple : Java is easy to learn. The symtax of Java is based on C++ which makes it easier to write program in it.
  • Object-oriented : Java follows an object-oriented paradigm which allows us to maintain our code as a combination of different types of objects.
  • Portable : Java is write once and run anywhere. We can execute Java programs on every machine.Java program ( .java )converted to bytecode ( .class ) which can easily run on every machine.
  • platform Independent : Java is compiled to bytecode, which means it can run on any device that has a Java Virtual Machine (JVM).This makes Java platform-independent.
  • Secured : Java is protected because it doesn't utilize explicit pointers. Java also provides concept of Bytecode and Exception Handling which makes it more secured.
  • Robust : Java is strong strong programming language. its uses strong memory management. The concept like automatic garbage collection, exception handling make it more robust.
  • Interpreted : Java uses JTT Interpreter along with the compiler for program execution. Java uses JTT Interpreter along with the compiler for program execution.
  • High Performance : Java is faster than other traditional languages because Java bytecode is "close "to native code. Java is faster than other traditional languages because Java bytecode is 'close'to native code.
  • Multi-threaded : The main advantages of multi-threading is that it doesn't occupy memory for each thread. It shares common memory area.
  • Distributed: create distributed applications.
  • Dynamic: Java is dynamic language. It support dynamic loading of classes. It means classes are loaded on demand.

2. What is platform ?

Platform is a set of hardware and software environment in which piece of s/w is executed. Java is software based platform.

3. What do you understand by JVM in JAVA?

JVM is a Virtual Machine that enables the computer to run java program. JVM acts like a run-time engine which calls the main method present In Java code. The Java code is Compile by JVM to be a bytecode which is closed to native code.

4. How many types of Memory allocated by JVM ?

  • Heap -> Whenever object is created.
  • Class Area.
  • Staric.
  • Native Method stack.
  • Program counter register.

5. What gives Java its 'Write once Run Anywhere' nature?

The bytecode Java Compiler converts java source code file (.class) into byre code which is intermidiate language b/w source code machine code. The bytecode is not platform-specific and can run on any machine.

6. What are JDK , JRE, and JVM?

JDK
  • Java Development kit.
  • Platform dependent.
  • It is superset of JRE.
  • JDK comes with Installer.
  • It contains dev tools, debug tools and other tools.
JRE
  • Java Runtime Environment.
  • Platform dependent.
  • It is a subset of JDK.
  • JRE only contain environment to execute source code.
  • It contains set of libraries and other Supporting JAR TO execure Java code.
JVM
  • Java Virtual Machine.
  • Highly platform dependent.
  • It is subset of JRE.
  • JVM bundled in both JDK and JRE.
  • It does not contain any tools.

7. What is the default value assigned to local variables in Java ?

Local variables are not initialized to any value, not primitive not object references.

8. What are different access specifiers In Java?

Public, Privare, Prorected, default.

  • Public -> Within class , package, Outside package, outside Package by subclass.
  • Privare -> within class only.
  • Protected -> Within class, package, Outside package by Subclass only.
  • default -> Within class & Package.

9. What is the purpose of static methods and variables in Java?

The methods or variables that are defined as static are shared among all the objects of the class. The static is a part of class and not the objects. The static variables are defined in class area and we don't need to create object of class to access such variables.

10. What are the advantages of Packages in Java?

  • Packages provides easier access control.
  • Packages avoid name clashes.
  • we can have classes access only within the package.

11. What is the initial value of Object references which is defined as an instance Variable ?

All Object references are initialized to null.

12. What is constructor?

Constructor is a special type of method that has same name as of class name and is used to initialize state of object. Every time object is created using new keyword, default constructor is called. It must not return any explicit Value.

13. How many types of constructor are used in Java?

4 types

  • Default Constructor.
  • Parameterized Constructor.
  • copy constructor.
  • No-Argument Constructor.

14. Does constructor return any value?

Instance of a class.

15. Can constructor Inherited?

No

16. can you make constructor final?

No

17. Difference b/w constructor & Method ?

constructor
  • Constructor must have same name As of class name.
  • It must not have any explicit return type.
  • Constructor cannot be overrided.
  • Constructor is used to Iniffatize stare of object.
  • Constructor Invoked automatically When the objects created using new keyword.
  • Java compiler provide default constructor if you don't have any constructor in class.
Method
  • It is possible that the method name may be the same or different.
  • It should must have a return type.
  • Method can be overrided
  • Method exposes the behaviour of the object.
  • Method needs to be exposed explicity.
  • compiler does not provide any default method.

18. Static variable and static Method ?

Static variable used to refer to common property of all objects.

  • Static method belongs to class rather then object.
  • It can be called using class name.
  • A static method can access and change value of static Variable.

19. what are the restrictions applied to Java staric methods ?

  • Static method cannot use not static variable or call non-static method directly.
  • this and super keyword cannot used in staric context as they are non-static.

20. Why the main method is static ?

Because Objects are not needed to call the static method. If we make java main method non-static, JVM will have to create object first and then call main() method which will lead to extra memory allocation.

21. Can we override static method ?

No

22. can we execute java program without main() method ?

It is acceptable before JDK 1.7 Since JDK 1.7 it is not acceptable.

23. can we make constructor static ?

Constructor is invoked when object is created. If we try to make constructor static, it will throw complie-time error.

24. Can we make abstract method static ?

In java, if we declare abstract methods as static then it will become part of class and we can directly call it which is unnecessary. Calling undefined method completely useless therefore it is not allowed.

25. Can we declare staric methods and variables inside abstract class?

Yes

26. What is the keyword in JAVA?

'this' keyword is a reference variable that refers to current object.

  • this can be used to invoke to invoke current class method.
  • this can be used to refer current class instance variable.
  • this can be used to Invoke current class constructor.
  • this can be used to return current class instance from the method.
  • It can be used as argument in method call.
  • It can passed as argument in constructor in constructor call.

27. can 'this' is used to refer static members ?

Yes, but it not good practice.