blog banner

1. What is the importance of super keyword in JAVA ?

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

    • It is used inside a sub-class method definition to call a method defined in the super class. Private methods of the super-class cannot be called. Only public and protected methods can be called by the super keyword.
    • It is also used by class constructors to invoke constructors of its parent class.
    • Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable.



2. What are the usages of super keyword?

1) super.<variable_name> refers to the variable of variable of parent class.

2) super() invokes the constructor of immediate parent class.

3) Super(...) invokes the parameterized constructor of immediate parent class.

4) super.<method_name> refers to the method of immediate parent class.

5) we use super to invoke when there is overriding and we need to call parent version from subclass. for example if there is a method overriding is present between parent class and subclass then we will use super keyword in subclass to invoke parent class's overriden method. And if there is any public or protected method  in parent class which is not present(means not overriden) in subclass then we will not need to use super keyword to access it as that method will already be present in subclass due to inheritance.

3. Write a program to display how can we use parent class members and constructors using super keyword ?

//Parent class or Superclass

class Parentclass

int num;




parentclass(int n){

num = n


void printNumber(){

System.out.println("printing parent class from parent class : " + num);



//Child class or subclass
class Subclass extends Parentclass

int num=110;


/* super() must be added to the first line of constructor otherwise it would throw

* compilation error:  " Constructor call must be the first statement  in a constructor". */

super(); //calling parent class default constructor explicitly


subclass() {

super(n); //calling parent class paremetrized constructor


void printNumber1(){

//Super.variable_name to access parent class variables
System.out.println("printing parent class from sub class : " + super.num); 


void printNumber2(){

System.out.println("printing sub class from sub class : " +  num);


void printNumber3(){

//Super.method_name to access parent class methods


public static void main(String args[]){

Subclass obj= new Subclass();



Subclass obj2= new Subclass(150);




printing parent class from sub class : 100

printing sub class from sub class : 110

printing parent class from parent class : 150


4. Do we always need to call parent class default constructor explicitly ?

No, Parent class default constructor is called implicitly and automatically. Even if we do not code to call parent class default constructor by using super() then also it will call parent class default constructor implicitly.

Super(..) is very useful because it can call parent class parameterized constructor. To call parameterized constructor of parent class we will have to use super(...) because it is not done implicitly/automatically.

There is a condition while using super() to call parent class default constructor, which is  :  

super() must be added to the first line of constructor otherwise it would throw  compilation error: " Constructor call must be the first statement in a constructor". 

class parentclass {

int num;






class Subclass extends Parentclass{

int num; 


num=110; // super() will be called implicitly as first line of this block


void printNumber(){

//Super.variable_name to access parent class variables
System.out.println("printing parent class from sub class : " + super.num);


public static void main(String args[]){

Subclass obj= new Subclass();



Output :

printing parent class from sub class : 100


5. What if a class don't extends another class, still uses 'super' in his constructor ?

If a class dont extends another class and still uses a super() call, this is perfectly ok. No error will be shown because every independent class extends 'Object' class by default. And in this case a dafult constructor of 'Object' class is being called.

public class Apple {

Apple() {


System.out.println("Sub class default constructor !");




6. Can super keyword be used to invoke parent class private method ? Or how can we access parent class's private members using super keyword.

No, A private class member is accessible only within the body of the top level class, that encloses the declaration of the member or constructor. It is not inherited by subclasses. It can not be accessed using super key from subclass.

Protected,public and default members can used using super keyword from subclasses.

7. Describe memory pool in JVM? How memory is distributed in JVM?

Memory pools are created by JVM memory managers during runtime. Memory pool may belong to either heap or non-heap memory.A run time constant pool is a per-class or per-interface run time representation of the constant_pool table in a class file. Each runtime constant pool is allocated from the Java virtual machine’s method area and Static Variables are stored in this Method Area. Also this non-heap is nothing but perm gen area.Actually Method area is part of perm gen.


8. what is the usage of stack memory area in JAVA ?

Java Stack memory is used for execution of a thread. They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method. Stack memory is always referenced in LIFO (Last-In-First-Out) order. Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other objects in the method. 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.

Properties :

  • a memory space reserved for your process by the OS;
  • the stack size is fixed and it is determined in the compiler phase based on variables declaration and other compiler options;
  • it is important to establish that the stack is limited and its size is fixed (one the process has started, it can’t change the stack size);
  • most of the time, the stack it is used to store functions/methods variables (input arguments and local variables).
  • each method has its own stack (a zone in the process stack), including main, which is also a function.
  • a method stack exists only during the lifetime of that method: from the calling moment until the return moment.


9. What is the usage of heap memory area in JAVA ?

Class instances and arrays are stored in heap memory. Heap memory is also called as shared memory. As this is the place where multiple threads will share. Heap is the runtime data area from which memory for all class instances and arrays are allocated. It is created at the JVM start-up.

The JVM allocates Java heap memory from the OS and then manages the heap for the Java application. When an application creates a new object, the JVM sub-allocates a contiguous area of heap memory to store it. An object in the heap that is referenced by any other object is "live," and remains in the heap as long as it continues to be referenced. Objects that are no longer referenced are garbage and can be cleared out of the heap to reclaim the space they occupy. The JVM performs a garbage collection (GC) to remove these objects, reorganizing the objects remaining in the heap.

Properties :

    • a memory space managed by the OS and used by processes to get additional space at run-time;
    • this area it is a global, meaning that any process can use it (of course, processes can’t read or write in another process Heap reserved area);
    • the role of this memory is to provide additional memory resources to processes that need that supplementary space at run-time (for example, you have a simple Java application that is constructing an array with values from console);
    • the space needed at run-time by a process is determined by functions like new (remember, it the same function used to create objects in Java) which are used to get additional space in Heap.
    • By default, the maximum heap size is 64 Mb. But can be configured using JVM options like
  1. -Xmx - to set the maximum Java heap size
  2. -Xms - to set the initial Java heap size


10. Difference Between Stack memory and heap memory ?

1) The main difference between heap and stack is that stack memory is used to store local variables and function call while heap memory is used to store objects in Java. No matter, where the object is created in code e.g. as a member variable, local variable or class variable, they are always created inside heap space in Java.

2) If there is no memory left in the stack for storing function call or local variable, JVM will throw java.lang.StackOverFlowError, while if there is no more heap space for creating an object, JVM will throw java.lang.OutOfMemoryError: Java Heap Space.

3) Another difference between stack and heap is that size of stack memory is a lot lesser than the size of heap memory in Java.

4) Variables stored in stacks are only visible to the owner Thread while objects created in the heap are visible to all thread. In other words, stack memory is kind of private memory of Java Threads while heap memory is shared among all threads.

11. what is method are in Java/JVM ?

Method area has per class structures and fields. Nothing but static fields and structures. It also includes the method data, method and constructor code, run-time constant pool. Method area is created at JVM startup and shared among all the threads. JVM will throw OutOfMemoryError if the allocated memory area is not sufficient during the run-time.In addition to the above said , static variable are stored in non heap area which is Method Area.

The Method area is a memory shared among all Java Virtual Machine Threads. It is created on virtual machine start-up and is loaded by classloaders from bytecode. The data in the Method Area stay in memory as long as the classloader which loaded them is alive.

The method area stores:

  • class information (number of fields/methods, super class name, interfaces names, version, …)
  • the bytecode of methods and constructors.
  • a runtime constant pool per class loaded.


12. What is PermGen in JAVA?

PermGen is short for Permanent Generation. If an object is supposed to live until over process exist then object is moved to Perm Generation. Java classes are stored in the permanent generation.

Almost every JVM nowadays uses a separate region of memory, called the Permanent Generation (or PermGen for short), to hold internal representations of java classes.


13. What is memory leak in JAVA?

A memory leak in Java is a situation where some objects are no longer used by an application, but the Garbage Collector fails to recognize them as unused. This leads to the OutOfMemoryError if those unused objects contribute to the heap usage significantly enough that the next memory allocation request by the application cannot be fulfilled.

14. Why does memory leak occurs in JAVA?

From the diagram, there are referenced objects and unreferenced objects. Unreferenced objects will be garbage collected, while referenced objects will not be garbage collected. Unreferenced objects are surely unused, because no other objects refer to it. However, unused objects are not all unreferenced. Some of them are being referenced! That's where the memory leaks come from.


Now look at the next image,

Let's take a look at the above image and see why memory leaks happen. In the example below, object A refers to object B. A's lifetime (t1 - t4) is much longer than B's (t2 - t3). When B is no longer being used in the application, A still holds a reference to it. In this way, Garbage Collector can not remove B from memory. This would possibly cause out of memory problem, because if A does the same thing for more objects, then there would be a lot of objects that are uncollected and consume memory space.

It is also possible that B hold a bunch of references of other objects. Those objects referenced by B will not get collected either. All those unused objects will consume precious memory space.

15. How to prevent memory leak ?

The following are some quick hands-on tips for preventing memory leaks.

  • Pay attention to Collection classes, such as HashMap, ArrayList, etc., as they are common places to find memory leaks. When they are declared static, their life time is the same as the life time of the application.
  • Pay attention to event listeners and callbacks. A memory leak may occur if a listener is registered but not unregistered when the class is not being used any longer.
  • "If a class manages its own memory, the programer should be alert for memory leaks." Often times member variables of an object that point to other objects need to be null out.


16. What is Permanent Generation Leak? Or, What is PermGen space in JAVA?

The root cause of java.lang.OutOfMemoryError: PermGen space is exactly the same: the JVM needs to load the definition of a new class but there is not enough space in PermGen to do it – there are already too many classes stored there. A possible reason for this could be your application or server using too many classes for the current size of PermGen to be able to accommodate them. Another common reason could be a memory leak.

17. The importance of static keyword in JAVA?

The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.

The static keyword denotes that a member variable, or method, can be accessed without requiring an instantiation of the class to which it belongs.

In simple terms, it means that you can call a method, even if you've never created the object to which it belongs! Every time you run a stand-alone application (which requires a static main method), the virtual machine can call the main method without creating a new application object. Of course, unless the application's methods are all static, you will need to create an instance of it at some point.

With regard to member variables, it means that they can be read from, and written to, without creating an object. You may have noticed that many classes create constants that can be read, without creating an object.

static final int VERSION = 2;

Static member variables are shared by all instances of the class to which they belong. When writing classes, this can be a handy feature.

18. When can i use Static keyword? Or, Where can we apply Static variable ?

The Static keyword can be applied to

  1. variable (also known as class variable)
  2. method (also known as class method)
  3. block
  4. nested class


19. Can we use static keyword wherever we want ? Or, Where we can not apply static keywords ?

The Static keyword can not be applied to

    1. Class (Not Nested)
    2. Constructor
    3. Interfaces
    4. Method Local Inner Class(Different than nested class)
    5. Inner Class methods
    6. Instance Variables
    7. Local Variables


20. What is static variable in java? What is the usage of Static variable in java?

If you declare any variable as static, it is known static variable. The static variable can be 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. The static variable gets memory only once in class area at the time of class loading.


  • Static variables are stored in method area of JVM.
  • Static variables are also known as Class Variables.
  • Such variables get default values based on the data type.
  • Data stored in static variables is common for all the objects( or instances ) of that Class.
  • Memory allocation for such variables only happens once when the class is loaded in the memory.
  • These variables can be accessed in any other class using class name.
  • Unlike non-static variables, such variables can be accessed directly in static and non-static methods.


  • It makes your program memory efficient (i.e it saves memory).


21. Write a code to display the impact of Static variable in java?

Program of counter without & with using static variable
Without Static With Static

class Counter{

int count=0;//will get memory when instance is created




public static void main(String args[]){

Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();


class Counter2{

static int count=0;//will get memory only once and retain its value




public static void main(String args[]){

Counter2 c1=new Counter2();
Counter2 c2=new Counter2();
Counter2 c3=new Counter2();







22. What is the role of Static method in JAVA? Or, Describe Static mathods in JAVA.

If you apply static keyword with any method, it is known as static method. Static Methods can access class variables without using object of the class. It can access non-static methods and non-static variables by using objects. Static methods can be accessed directly in static and non-static methods.

properties : 

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

Restrictions for static method

    • The static method can not use non static data member or call non-static method directly.
    • this and super cannot be used in static context.


23. Write a code in JAVA to explain static method ?

class Calculate{

static int cube(int x){

return x*x*x;


public static void main(String args[]){

int result=Calculate.cube(5);





24. What is the use of Static block in JAVA?

Static block is mostly used for changing the default values of static variables.This block gets executed when the class is loaded in the memory.
A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.


  • Is used to initialize the static data member.
  • It is executed before main method at the time of classloading.


25. Write a code in Java to explain the use of Static Block

class A2{

static{System.out.println("static block is invoked");}
public static void main(String args[]){

System.out.println("Hello main");




static block is invoked
Hello main

26. What is the use of Static Class in Java? Or, What is Static Class in Java?

A Class can be made static only if it is a nested Class. The nested static class can be accessed without having an object of outer class.

Properties :

    • It can access static data members of outer class including private.
    • Static nested class cannot access non-static (instance) data member or method.


27. Write an code in java to explain the use of Static class or static nested class .

class TestOuter1{

static int data=30;
static class Inner{


System.out.println("Inner Class object created!");


void msg(){

System.out.println("data is "+data);




System.out.println("Outer Class object created!");

public static void main(String args[]){

TestOuter1 ot = new TestOuter1():

TestOuter1.Inner in=new TestOuter1.Inner();



Output :

Outer Class object created!

Inner Class object created!

data is 30

NOTE :  In the above programme if the outer class's instance variable/ data member "data" was not declared as static and then static inner class tried to print it then it would throw  a Compile time error. Static inner class cannot access instance data of outer class.


28. What are the differences between static and non-static nested classes?

Following are major differences between static nested class and non-static nested class. Non-static nested class is also called Inner Class.

1) Nested static class doesn’t need reference of Outer class, but Non-static nested class or Inner class requires Outer class reference.

2) Inner class(or non-static nested class) can access both static and non-static members of Outer class. A static class cannot access non-static members of the Outer class. It can access only static members of Outer class.

3) An instance of Inner class cannot be created without an instance of outer class and an Inner class can reference data and methods defined in Outer class in which it nests, so we don’t need to pass reference of an object to the constructor of the Inner class. For this reason Inner classes can make program simple and concise.


29. What is the effect/importance of Final keyword in JAVA ?

Final keyword is also known as a final modifier in Java. You can use a final modifier with class, method, and variables in Java. Once created and initialized, you cannot reassign a final variable. This may look easy but it's a subtle concept to understand.

It is used to make a variable as a constant, Restrict method overriding, Restrict inheritance. It is used at variable level, method level and class level. In java language final keyword can be used in following way.

  1. Final variable
  2. Final method
  3. Final class


30. What is Final variable in jAVA? Or, How to use final to define constants ?

final variables are nothing but constants. We cannot change the value of a final variable once it is initialized. This is similar to const in other language. A variable declared with the final keyword cannot be modified by the program after initialization. This is useful to universal constants, such as "PI( )".

If you want to make a local variable, class variable (static field), or instance variable (non-static filed) constant, declare it final. A final variable may only be assigned to once and its value will not change and can help avoid programming errors.

Once a final variable has been assigned, it always contains the same value. If a final variable holds a reference to an object, then the state of the object may be changed by operations on the object, but the variable will always refer to the same object.

This also applies to arrays, because arrays are objects; if a final variable holds a reference to an array, then the components of the array may be changed by operations on the array, but the variable will always refer to the same array.

class Bike9{

final int speedlimit=90;//final variable
void run(){

speedlimit=400; // Error





The above code will throw error because final variables can not be re assigned again. This will be a Compile Time Error.

public class Circle

final double PI=3.14159;

public static void main(String[] args)

Circle c = new Circle();



This above code is fine and will run.

31. What is blank final variable in JAVA ?

A final variable that is not initialized at the time of declaration is known as blank final variable.A final variable can only be initialized once, either via an initializer or an assignment statement. It does not need to be initialized at the point of declaration: this is called a "blank final" variable. A blank final instance variable of a class must be definitely assigned at the end of every constructor of the class in which it is declared; similarly, a blank final static variable must be definitely assigned in a static initializer of the class in which it is declared: otherwise, a compile-time error occurs in both cases. (Error: variable MAX_VALUE might not have been initialized).

Note : A blank final instance variable must be definitely assigned at the end of every constructor of the class in which it is declared; otherwise a compile-time error occurs.

32. What is the use of Blank Final Variable in JAVA?

Lets say we have a Student class which is having a field called Roll No. Since Roll No should not be changed once the student is registered, we can declare it as a final variable in a class but we cannot initialize roll no in advance for all the students(otherwise all students would be having same roll no). In such case we can declare roll no variable as blank final and we initialize this value during object creation like this:

class StudentData{

//Blank final variable
final int ROLL_NO;

StudentData(int rnum){

//It must be initialized in constructor

void myMethod(){

System.out.println("Roll no is:"+ROLL_NO);

public static void main(String args[]){

StudentData obj=new StudentData(1234);




Roll no is:1234

33. How can we initialize Uninitialized static final variable ?

A static final variable that is not initialized during declaration can only be initialized in static block. Example:

class Example{

//static blank final variable
static final int ROLL_NO;


public static void main(String args[]){






34. What is Final method in JAVA? How can we stop a method to be overriden ?

A final method cannot be overridden. Which means even though a sub class can call the final method of parent class without any issues but it cannot override it. It makes a method final, meaning that sub classes can not override this method. The compiler checks and gives an error if you try to override the method.

When a class is extended by other classes, its methods can be overridden for reuse. There may be circumstances when you want to prevent a particular method from being overridden, in that case, declare that method final. Methods declared as final cannot be overridden.

class Bike{

final void run(){




class Honda extends Bike{

void run(){

System.out.println("running safely with 100kmph"); // Error





This above code will throw a compile time error because we can not override a final method in java.

class XYZ{

final void demo(){

System.out.println("XYZ Class Method");



class ABC extends XYZ{

public static void main(String args[]){

ABC obj= new ABC();




XYZ Class Method

This above code will run.

35. What is Final Class in JAVA? How to prevent inheritance in JAVA?

If you make any class as final, you cannot extend it. It makes a class final, meaning that the class can not be inheriting by other classes. When we want to restrict inheritance then make class as a final.

If you find a class's definition is complete and you don't want it to be sub-classed, declare it final. A final class cannot be inherited, therefore, it will be a compile-time error if the name of a final class appears in the extends clause of another class declaration; this implies that a final class cannot have any subclasses.

It is a compile-time error if a class is declared both final and abstract, because the implementation of such a class could never be completed.

Because a final class never has any subclasses, the methods of a final class are never overridden

public final class A

public class B extends A

// it gives an error, because we can not inherit final class



36. What is final parameter?

If you declare any parameter as final, you cannot change the value of it.

class Bike{

int cube(final int n){

n=n+2;//can't be changed as n is final

public static void main(String args[]){

Bike b=new Bike();



Output: Compile Time Error


37. Can we declare a constructor final?

No, because constructor is never inherited.

38. Few important points on final keyword

1) A constructor cannot be declared as final.
2) Local final variable must be initializing during declaration.
3) All variables declared in an interface are by default final.
4) We cannot change the value of a final variable.
5) A final method cannot be overridden.
6) A final class not be inherited.
7) If method parameters are declared final then the value of these parameters cannot be changed.

39. What is finally keyword in JAVA? what is the importance of finally in JAVA ?

Finally is used in java for exception handling , it is used with try catch block. The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

Properties :

1. A finally statement must be associated with a try statement. It identifies a block of statements that needs to be executed regardless of whether or not an exception occurs within the try block.

2. After all other try-catch processing is complete, the code inside the finally block executes. It is not mandatory to include a finally block at all, but if you do, it will run regardless of whether an exception was thrown and handled by the try and catch parts of the block.

3. In normal execution the finally block is executed after try block. When any exception occurs first the catch block is executed and then finally block is executed.

4. An exception in the finally block, exactly behaves like any other exception.

5. The code present in the finally block executes even if the try or catch block contains control transfer statements like return, break or continue.

Example :

class JavaFinally

public static void main(String args[])


public static int myMethod()

try {

return 112;

finally {

System.out.println("This is Finally block");
System.out.println("Finally block ran even after return statement");




Output of above program:

This is Finally block
Finally block ran even after return statement

40. When does a finnally block doesn't execute?

The circumstances that prevent execution of the code in a finally block are:

– The death of a Thread
– Using of the System. exit() method.
– Due to an exception arising in the finally block.

- If JVM stops performing after try block.

Admin Team

0 Respond

Exam Questions
BLOG Posts
For study materials
Job Walk-In
Updates across India
Interview round
Interview or procedure
For study materials
Please LIKE our page to Get regular JOB WALK-IN UPDATES across India and STUDY MATERIALS on facebook news feed.