JAVA interview questions

Cards (89)

  • How to delete a character in a String
    Because Strings in Java are immutable. You'll have to create a new string removing the character you don't want.

    String newstr = str.substring(0, idx) + str.substring(idx + 1);

    public String removeChar(String str, Integer n) {
    String front = str.substring(0, n);
    String back = str.substring(n+1, str.length());
    return front + back;
    }
  • What is Object Oriented Programming
    Object-oriented programming (OOP) is a programming language model organized around objects rather than "actions" and data rather than logic.

    The first step in OOP is to identify all the objects the programmer wants to manipulate and how they relate to each other, an exercise often known as data modeling.
  • What are be benefits of Object-Oriented Programming
    Improved code reuse.
    Improved software maintainability.
    Faster development.
  • 4 major principles that make a
    language object-oriented.
    Encapsulation, Data Abstraction, Polymorphism and Inheritence.
  • What is multithreading?
    Multithreading is a process of executing multiple threads simultaneously. Its main advantage is:

    Threads share the same address space.
    Thread is lightweight.
    Cost of communication between process is low.
  • What is thread?
    A thread is a lightweight subprocess.It is a separate path of execution.It is called separate path of execution because each thread runs in a separate stack frame.
  • What is 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.
    JRE stands for Java Runtime Environment. It is the implementation of JVM.
    JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.
  • How many types of memory areas are allocated by JVM?
    Class(Method) Area
    Heap
    Stack
    Program Counter Register
    Native Method Stack
  • What is the main difference between Java platform and other platforms?
    The Java platform differs from most other platforms in the sense that it's a software-based platform that runs on top of other hardware-based platforms.It has two components:

    Runtime Environment
    API(Application Programming Interface)
  • What gives Java its 'write once and run anywhere' nature?
    The bytecode. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific and hence can be fed to any platform.
  • What is classloader?
    The classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.
  • Is Empty .java file name a valid source file name?

    Yes, save your java file by .java only, compile it by javac .java and run by java yourclassname
  • If I don't provide any arguments on the command line, then the String array of Main method will be empty or null?
    It is empty. But not null.
  • What if I write static public void instead of public static void?

    Program compiles and runs properly.
  • What is the default value of the local variables?

    The local variables are not initialized to any default value, neither primitives nor object references.
  • What is difference between object oriented programming language and object based programming language?
    Object based programming languages follow all the features of OOPs except Inheritance. Examples of object based programming languages are JavaScript, VBScript etc.
  • What will be the initial value of an object reference which is defined as an instance variable?
    The object references are all initialized to null in Java.
  • What is constructor?
    Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation
  • What is the purpose of default constructor?
    The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.
  • Does constructor return any value?
    yes, that is current instance
  • Is constructor inherited?
    No, constructor is not inherited.
  • Can you make a constructor final?
    No, constructor can't be final.
  • What is static variable?
    static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
    static variable gets memory only once in class area at the time of class loading.
  • What is static method?
    A static method belongs to the class rather than object of a class.
    A static method can be invoked without the need for creating an instance of a class.
    static method can access static data member and can change the value of it.
  • Why main method is static?
    because object is not required to call static method if It were non-static method,jvm creats object first then call main() method that will lead to the problem of extra memory allocation
  • What is static block?
    Is used to initialize the static data member.
    It is excuted before main method at the time of classloading.
  • What if the static modifier is removed from the signature of the main method?
    Program compiles. But at runtime throws an error "NoSuchMethodError".
  • What is "this" in java?
    It is a keyword that that refers to the current object.
  • What is Inheritance?
    Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding.
  • Which class is the superclass for every class.
    Object class.
  • What is super in java?
    It is a keyword that refers to the immediate parent class object
  • Can you use this() and super() both in a constructor?
    No. Because super() or this() must be the first statement.
  • What is method overloading?
    If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program.
  • What is method overriding?
    If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to provide the specific implementation of the method.
  • Can we override static method?
    No, you can't override the static method because they are the part of class not object.
  • Why we cannot override static method?
    It is because the static method is the part of class and it is bound with class whereas instance method is bound with object and static gets memory in class area and instance gets memory in heap.
  • Difference between method Overloading and Overriding.
    Method overloading increases the readability of the program. method overlaoding is occurs within the class. parameter must be different.

    Method overriding provides the specific implementation of the method that is already provided by its super class. Method overriding occurs in two classes that have IS-A relationship. In this case, parameter must be same.
  • What is abstraction?
    Abstraction is a process of hiding the implementation details and showing only functionality to the user. Abstraction lets you focus on what the object does instead of how it does it.
  • What is the difference between abstraction and encapsulation?
    Abstraction hides the implementation details whereas encapsulation wraps code and data into a single unit.
  • What is abstract class?
    A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.