Java Basic Interview Questions

JAVA

Java Basic Interview Questions

  1. Differentiate between JDK and JRE.

  2. Explain Java Virtual Machine (JVM)?

  3. What are the different types of memory areas allocated by JVM?

  4. What is the JIT compiler?

  5. How the Java platform is different from other platforms?

  6. Why Java is called as ‘write once and run anywhere’ language?

  7. Explain the working of ClassLoader in Java?

  8. Do you think ‘main’ used for the main method is a keyword in Java?

  9. Can you write the main method as public void static instead of the public static void?

  10. In Java, if we do not specify any value for local variables, then what will be the default value of the local variables?

  11. Let say, we run a java class without passing any arguments. What will be the value of the String array of arguments in the Main method?

  12. Differentiate between byte and char data types in Java.

  13. Explain the main principles of Object-Oriented Programming?

  14. Differentiate between Object Oriented Programming language and Object-Based Programming language.

  15. In Java what is the default value of an object reference defined as an instance variable in an Object?

  16.  Need for constructor in Java?

  17. Why do we need a default constructor in Java classes?

  18. What is the value returned by Constructor in Java?

  19. Can we inherit a Constructor?

  20. Why constructors cannot be final, static, or abstract in Java?

Answers/Hints

1- JDK contains all the tools and libraries required for the development of Java programs. It also contains compilers and debuggers required to compile a Java program. JDK is also considered a superset of JRE. JRE is included in JDK. JRE provides libraries and JVM.

2- JVM is an abstract tool that executes Java Bytecode. There are different JVMs for different hardware and software platforms. Hence, it is platform-dependent. JVM is responsible for loading, verifying, and executing the Bytecode on a specific platform. JVM is included in a JRE package. The hotSpot is one of the most popular JVMs developed by Oracle.

3-

  • ClassLoader: It is a component of JVM used to load class files.
  • Class (Method) Area: It stores per-class structures such as the runtime constant pool, field and method data, and the code for methods.
  • Heap: Heap is created on runtime and it contains the runtime data area in which objects are allocated.
  • Stack: Stack stores local variables and partial results at runtime. It also helps in method invocation and return value. Each thread creates a private JVM stack at the time of thread creation.
  • Program Counter Register: This memory area contains the address of the Java virtual machine instruction that is currently being executed.
  • Native Method Stack: This area is reserved for all the native methods used in the application.

4- Just In Time compiler known as JIT compiler, used for performance improvement. It is enabled by default. It is compilation done at execution time. JIT compiler is including in JVM.

5- Java compiler converts java code into byte code that is interpreted by JVM. Java byte code can run on any supported platform.

6- Byte code generated by Java compiler can be interpreted by any JVM. So it becomes much easier to write programs in Java and expect those to run on any platform.

7- In Java, ClassLoader is a class that is used to load files in JVM. ClassLoader loads files from their physical file locations e.g. Filesystem, Network location, etc.

There are three main types of ClassLoaders in Java.
  • Bootstrap ClassLoader: This is the first ClassLoader. It loads classes from rt.jar file.
  • Extension ClassLoader: It loads class files from jre/lib/ext location.
  • Application ClassLoader: This ClassLoader depends on CLASSPATH to find the location of class files. If you specify your jars in CLASSPATH, then this ClassLoader will load them.

8-  No, main is just a name of the method. There can be multiple methods with same name main in a class file. It is not a keyword in Java.

9- No, you cannot write it like this.  Any method has to first specify the modifiers and then the return value. The order of modifiers can change. We can write static public void main() instead of public static void main().

10-  Java does not initialize local variables with any default value. So these variables will be just null by default.

11- By default, the value of the String array of arguments is empty in Java. It is not null.

12- Both byte and char are numeric data types in Java. They are used to represent numbers in a specific range. Major difference between them is that a byte can store raw binary data whereas a char stores characters or text data.

The usage of char is for example. char ch = ‘x’;

Byte values range from -128 to 127.

A byte is made of 8 bits. But a char is made of 16 bits. So it is equivalent to 2 bytes.

13- Main principles of Object-Oriented Programming (OOPS) are:

  • Abstraction
  • Polymorphism
  • Encapsulation
  • Inheritance

14- Object Oriented Programming languages like Java and C++ follow concepts of OOPS like- Encapsulation, Abstraction, Polymorphism and Inheritance etc. It follows some features of OOPS but they do not provide support for Polymorphism and Inheritance. Egg. JavaScript, VBScript, etc. Object-Based Programming languages provide support for Objects and you can build objects from the constructor. The languages also support Encapsulation. These are also known as Prototype-oriented languages.

15- NULL

16-Java is an object-oriented language, in which we create and use objects. A constructor is a piece of code similar to a method. It is used to create an object and set the initial state of the object. A constructor is a special function that has the same name as the class name. Without a constructor, there is no other way to create an object. By default, Java provides a default constructor for every object. If we overload a constructor then we have to implement the default constructor.

17- Default constructor is the no-argument constructor that is automatically generated by Java if no other constructor is defined. Java specification says that it will provide a default constructor if there is no overloaded constructor in a class. But it does not say anything about the scenario in which we write an overloaded constructor in a class. We need at least one constructor to create an object, that’s why Java provides a default constructor. When we have an overloaded constructor, then Java assumes that we want some custom treatment in our code. Due to which it does not provide a default constructor. But it needs a default constructor as per the specification. So it gives an error.

18- When we call a constructor in Java, it returns the object created by it. 

19- Java does not support the inheritance of the constructor.

20- If we set a method as final it means we do not want any class to override it. But the constructor already cannot be overridden. So there is no sense in marking it final.

If we set a method as abstract it means that it has no body and it should be implemented in a child class. But the constructor is called implicitly when the new keyword is used. Therefore it needs a body.

If we set a method as static it means that it belongs to the class, but not a particular object. The constructor is always called to initialize an object. Therefore, there is no use of marking constructor static.

Check Interview Questions on Java

Wikipedia Java

1 thought on “Java Basic Interview Questions”

Leave a Comment

Your email address will not be published.