IQ-Java_Basics





  • JVM (Java Virtual Machine):
                     It is an abstract machine. It is a specification that provides run-time environment in which java bytecode can be executed.It follows three notations:

- Specification: It is a document that describes the implementation of the Java virtual machine. It is provided by Sun and other companies.
- Implementation: It is a program that meets the requirements of JVM specification.
- Runtime Instance: An instance of JVM is created whenever you write a java command on the command prompt and run the class.
---------------------------

  • JRE (Java Runtime Environment)

                   JRE refers to a runtime environment in which java bytecode can be executed. It implements the JVM (Java Virtual Machine) and provides all the class libraries and other support files that JVM uses at runtime.
So JRE is a software package that contains what is required to run a Java program. Basically, it’s an implementation of the JVM which physically exists.
---------------------------


  • JDK(Java Development Kit) :

                 It is the tool necessary to compile, document and package Java programs.
The JDK completely includes JRE which contains tools for Java programmers. The Java Development Kit is provided free of charge. Along with JRE, it includes an interpreter/loader, a compiler (javac), an archiver (jar), a documentation generator (javadoc) and other tools needed in Java development.
                  In short, 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 difference between 32-bit and 64-bit JVM?

                 The main differences between 32-bit and 64-bit JVM are that later is designed for 64-bit operating system e.g. Windows 8 or later versions of Linux.

                 From the perspective of Java developer's, the main difference between them is heap size. A 64-bit JVM virtually has unlimited heap memory as compared to 4GB of the theoretical limit of 32-bit JVM.
                If your program needs more memory, better run it on 64-bit JVM with large heap space.
---------------------------

  • Why Java is platform independent?


                 Platform independent practically means write once run anywhere.
       Java is called so because of its byte codes which can run on any system irrespective of its underlying operating system.
---------------------------


  • Explain public static void main(String args[]).


public : Public is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class.

static : It is a keyword in java which identifies it is class based i.e it can be accessed without creating the instance of a Class.

void : It is the return type of the method. Void defines the method which will not return any value.

main: It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.

String args[] : It is the parameter passed to the main method.
---------------------------


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


  • What is JIT compiler?

Just-In-Time(JIT) compiler:It is used to improve the performance.
JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation.Here the term compiler refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.
---------------------------


  • 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 are constructors in Java?

              In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.

There are two types of constructors:
Default constructor
Parameterized constructor
---------------------------


  • Can you make a constructor final?

               No, constructor can't be final.
---------------------------


  • 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 is singleton class and how can we make a class singleton?

                 Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.
---------------------------


  • What are the differences between Heap and Stack Memory?

              Memory: Stack memory is used only by one thread of execution while Heap memory is used by all the parts of the application.
              Access: Stack memory can't be accessed by other thread while Objects stored in the heap are globally accessible.
             Lifetime:  Stack exists until the end of execution of the thread while Heap memory lives from the start till the end of application execution.
            Memory Management:  Stack follows LIFO manner to free memory while in Heap Memory management is based on generation associated to each object.
            Usage: Stack memory only contains local primitive and reference variables to objects in heap space while Whenever an object is created, it's always stored in the Heap space.
---------------------------


  • What are the OOPs Concepts in Java ?

              OOPs, concepts in Java are the main ideas behind Java’s Object Oriented Programming.
These are an abstraction, encapsulation, inheritance, and polymorphism. These are the key to understanding how Java works.
---------------------------


  • What is Encapsulation ?

             Encapsulation is the technique used to implement abstraction in object oriented programming. Encapsulation is used for access restriction to a class members and methods.

           Access modifier keywords are used for encapsulation in object oriented programming. For example, encapsulation in java is achieved using private, protected and public keywords.
---------------------------


  • What is an Abstraction?

             Abstraction is the concept of hiding the internal details and describing things in simple terms.
For example, a method that adds two integers. The method internal processing is hidden from outer world.
---------------------------


  • What is Polymorphism?

           Polymorphism is briefly described as one interface, many implementations. Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts specifically, to allow an entity such as a variable, a function, or an object to have more than one form.
There are two types of polymorphism:

             Compile time polymorphism
             Run time polymorphism

Compile time polymorphism is method overloading whereas Runtime time polymorphism is done using inheritance and interface i.e Method Overriding.
---------------------------


  • What is runtime polymorphism or dynamic method dispatch?

                  In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass.
---------------------------


  • What is method overloading ?

                   In Method Overloading, Methods of the same class shares the same name but each method must have different number of parameters or parameters having different types and order.
Method Overloading is to "add" or "extend" more to method’s behavior.
It is a compile time polymorphism.
The methods must have different signature.
It may or may not need inheritance in Method Overloading.
---------------------------


  • What is method Overriding ?

             In Method Overriding, sub class have the same method with same name and exactly the same number and type of parameters and same return type as a super class.
Method Overriding is to Change existing behavior of method.
It is a run time polymorphism.
The methods must have same signature.
It always requires inheritance in Method Overriding.
---------------------------


  • Can you override a private or static method in Java?

                You cannot override a private or static method in Java.
If you create a similar method with same return type and same method arguments in child class then it will hide the super class method; this is known as method hiding.
Similarly, you cannot override a private method in sub class because it’s not accessible there. What you can do is create another private method with the same name in the child class.
---------------------------


  • What is the difference between abstract classes and interfaces?

                  An abstract class can provide complete, default code and/or just the details that have to be overridden. while an interface cannot provide any code at all,just the signature.
In case of abstract class, a class may extend only one abstract class. while A Class may implement several interfaces.

       An abstract class can have non-abstract methods. while all methods of an Interface are abstract.

       An abstract class can have instance variables while An Interface cannot have instance variables

       An abstract class can have any visibility: public, private, protected. while An Interface visibility must be public (or) none.

       If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly while If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method

       An abstract class can contain constructors while an Interface cannot contain constructors
Abstract classes are fast while Interfaces are slow as it requires extra indirection to find corresponding method in the actual class
---------------------------


  • What is association?

              Association is a relationship where all object have their own lifecycle and there is no owner.
Let’s take an example of Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. These relationship can be one to one, One to many, many to one and many to many.
---------------------------


  • What do you mean by aggregation?

                  Aggregation is a specialized form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let’s take an example of Department and teacher.

                 A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy.
---------------------------


  • What is composition in Java?

              Composition is again specialized form of Aggregation and we can call this as a death relationship.
             It is a strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child object will also be deleted.

            Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.
---------------------------


  • 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 is difference between static (class) method and instance method?


  1.  A method i.e. declared as static is known as static method. while a method i.e. not declared as static is known as instance method.
  2. Object is not required to call static method. while Object is required to call instance methods.
  3. Non-static (instance) members cannot be accessed in static context (static method, static block and static nested class) directly. while static and non-static variables both can be accessed in instance methods.

---------------------------


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


  • Why Java does not support pointers?

                 Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe(unsecured) and complex to understand.
---------------------------


  • What is super in java?

                The super keyword in java is a reference variable which is used to refer immediate parent class object.

              Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.
---------------------------


  • What is object cloning?

                The object cloning is a way to create exact copy of an object. For this purpose, clone() method of Object class is used to clone an object.

             The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException.

             The clone() method is defined in the Object class. Syntax of the clone() method is as follows:
protected Object clone() throws CloneNotSupportedException
---------------------------


  • Why use clone() method ?

                   The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing to be performed that is why we use object cloning.
---------------------------


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


  • What is covariant return type?

              The covariant return type specifies that the return type may vary in the same direction as the subclass.

             Before Java5, it was not possible to override any method by changing the return type. But now, since Java5, it is possible to override method by changing the return type if subclass overrides any method whose return type is Non-Primitive but it changes its return type to subclass type.
---------------------------



  • How do you take the heap dump of a Java process?


             There are many ways to take the heap dump of a Java process e.g. Tomcat, but most common is by using tools available in JDK
            e.g. jVisualVM,  jCmd,  jmap.
Here is the command you can use to take the heap dump of Java process:

            $ jmap -dump:live, file=/location/of/heap_dump.hprof  PID

            The heap dump will contain all live objects and they are stored in heap_dump.hprof file. You  need PID of Java process which you can find by using "ps" and "grep" command. You can see Java Performance Companion by Charlie Hunt to learn more about taking and analyzing heap dump in Java to find memory leak and other memory related errors.
---------------------------



  • How do you analyze a heap dump?


             There are many tools to analyze heap dump in Java e.g. you can use the jhat tool which comes along with JDK.
             You can also use Eclipse Memory Analyzer to analyze heap dump to find out any memory leak in Java while dealing with OutOfMemoryError in Java.

---------------------------


  • What is OutOfMemoryError in Java? How do you deal with it?


             The Java virtual machine throws java.lang.OutOfMemoryError when there is not enough memory to run the application e.g. no more memory to create new objects, no more memory to create new threads etc.
            The most common OutOfMemoryError is the java.lang.OutOfMemoryError: java heap space, which comes when there is no more memory left to create a new object.
---------------------------



  • What is Garbage Collector?

          The garbage collector is a memory management component of Java virtual machine which is responsible for reclaiming memory from dead objects. It's one of the key components and allows an application developer to focus on application development rather than doing memory management.
          Some of the popular garbage collectors are Concurrent Mark-Sweep garbage collector and G1 garbage collector in recent.
---------------------------

11 comments:

Home

Mastering Java Interview Questions: Your Comprehensive Guide         If you're preparing for a Java interview or just lookin...