JavaInterviewLearning

This is my Q/A for anything related to Java.

Java Basic Interview Questions

1.What is Java ?

  • Java is the high-level, object-oriented, robust, secure programming language, platform-independent, high performance, Multithreaded, and portable programming language. It was developed by James Gosling in June 1991. It can also be known as the platform as it provides its own JRE and API.

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

3.What is the difference between JDK, JRE, and 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 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 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

jvm vs jre vs jdk

more details

  1. Why is Java a platform independent language?
  • Java language was developed in such a way that it does not depend on any hardware or software due to the fact that the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems.
  • The only condition to run that byte code is for the machine to have a runtime environment (JRE) installed in it.
  1. Why does Java not make use of pointers?
  • Pointers are quite complicated and unsafe to use by beginner programmers.
  • Pointer utilization can also cause potential errors, security is also compromised if pointers are used because the users can directly access memory with the help of pointers.
  • The usage of pointers can make the procedure of garbage collection quite slow and erroneous.
  • Java makes use of references as these cannot be manipulated, unlike pointers.
  1. Difference between equals() method and equality operator (==) in Java?
  • equals() : This is a method defined in the Object class. This method is used for checking the equality of contents between two objects as per the specified business logic.
  • == : It is a binary operator in Java. This operator is used for comparing addresses (or references), i.e checks if both the objects are pointing to the same memory location.
  1. Can single try block and multiple catch blocks can co-exist in a Java Program?
  • Yes, multiple catch blocks can exist but specific approaches should come prior to the general approach because only the first catch block satisfying the catch condition is executed. The given code illustrates the same:

    public class MultipleCatch {
    public static void main(String args[]) {
    try {
    int n = 1000, x = 0;
    int arr[] = new int[n];
    for (int i = 0; i <= n; i++) {
    arr[i] = i / x;
    }
    }
    catch (ArrayIndexOutOfBoundsException exception) {
    System.out.println("1st block = ArrayIndexOutOfBoundsException");
    }
    catch (ArithmeticException exception) {
    System.out.println("2nd block = ArithmeticException");
    }
    catch (Exception exception) {
    System.out.println("3rd block = Exception");
    }
    }
    }
  1. Do final, finally and finalize keywords have the same function?
  • All three keywords have their own utility while programming.
  • Final: If any restriction is required for classes, variables, or methods, the final keyword comes in handy. Inheritance of a final class and overriding of a final method is restricted by the use of the final keyword. The variable value becomes fixed after incorporating the final keyword.

    final int a=100;
    a = 0;  // error
  • Finally: It is the block present in a program where all the codes written inside it get executed irrespective of handling of exceptions.

    try {
    int variable = 5;
    }
    catch (Exception exception) {
    System.out.println("Exception occurred");
    }
    finally {
    System.out.println("Execution of finally block");
    }
  • Finalize: Prior to the garbage collection of an object, the finalize method is called so that the clean-up activity is implemented.

    public static void main(String[] args) {
    String example = new String("InterviewBit");
    example = null;
    System.gc(); // Garbage collector called
    }
    public void finalize() {
    // Finalize called
    } 
  1. What is difference between Heap and Stack Memory in java?
  • Java Heap space is used by java runtime to allocate memory to Objects and JRE classes. Whenever we create any object, it’s always created in the Heap space. Garbage Collection runs on the heap memory to free the memory used by objects that doesn’t have any reference. Any object created in the heap space has global access and can be referenced from anywhere of the application.
  • Stack in java is a section of memory which contains methods, local variables and reference variables. Local variables are created in the stack. As soon as method ends, the block becomes unused and become available for next method. Stack memory size is very less compared to Heap memory.
  1. Can you have virtual functions in Java?
  • In Java, all non-static methods are by default virtual functions. Only methods marked with the keyword final, which cannot be overridden, along with private methods, which are not inherited, are non-virtual.
 Virtual function with Interface

/**
* The function applyBrakes() is virtual because
* functions in interfaces are designed to be overridden.
**/
interface Bicycle {         
    void applyBrakes();     
}                           

class ACMEBicycle implements Bicycle {
    public void applyBrakes(){               //Here we implement applyBrakes()
       System.out.println("Brakes applied"); //function
    }
}
  1. Using relevant properties highlight the differences between interfaces and abstract classes.
  • Availability of methods: Only abstract methods are available in interfaces, whereas non-abstract methods can be present along with abstract methods in abstract classes.
  • Variable types: Static and final variables can only be declared in the case of interfaces, whereas abstract classes can also have non-static and non-final variables.
  • Inheritance: Multiple inheritances are facilitated by interfaces, whereas abstract classes do not promote multiple inheritances.
  • Data member accessibility: By default, the class data members of interfaces are of the public- type. Conversely, the class members for an abstract class can be protected or private also.
  • Implementation: With the help of an abstract class, the implementation of an interface is easily possible. However, the converse is not true;
  1. Although inheritance is a popular OOPs concept, it is less advantageous than composition. Explain.
  • Inheritance lags behind composition in the following scenarios:

    • Multiple-inheritance is not possible in Java. Classes can only extend from one superclass. In cases where multiple functionalities are required, for example - to read and write information into the file, the pattern of composition is preferred. The writer, as well as reader functionalities, can be made use of by considering them as the private members.
    • Composition assists in attaining high flexibility and prevents breaking of encapsulation.
    • The loosely coupled nature of composition is preferable over the tightly coupled nature of inheritance.
    • Unit testing is possible with composition and not inheritance. When a developer wants to test a class composing a different class, then Mock Object can be created for signifying the composed class to facilitate testing. This technique is not possible with the help of inheritance as the derived class cannot be tested without the help of the superclass in inheritance.

Referances

Javatpoint
Interviewbit

©2022 Dev, by Sanjay.