[go: up one dir, main page]

0% found this document useful (0 votes)
34 views4 pages

Java Interview Questions

The document discusses object oriented programming principles like inheritance, encapsulation, abstraction and polymorphism. It also discusses method overloading, static vs non-static methods, final keyword, cloning, interfaces and access modifiers in Java.

Uploaded by

Amal Balu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

Java Interview Questions

The document discusses object oriented programming principles like inheritance, encapsulation, abstraction and polymorphism. It also discusses method overloading, static vs non-static methods, final keyword, cloning, interfaces and access modifiers in Java.

Uploaded by

Amal Balu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

JAVA INTERVIEW QUESTIONS

1) What are the fundamental principles of object oriented programming?

1. Inheritance: Inheritance is a process where one class acquires the properties


of another.
2. Encapsulation: Encapsulation in Java is a mechanism of wrapping up the
data and code together as a single unit.
3. Abstraction: Abstraction is the methodology of hiding the implementation
details from the user and only providing the functionality to the users. 
4. Polymorphism: Polymorphism is the ability of a variable, function or object to
take multiple forms.

2) What is the method overloading in java?

If a class has more than one method with same name but with different list of
arguments, then it is called method overloading.

3) Does java supports multiple inheritance?

Java supports multiple inheritance but only through interfaces. That means a class
can implement more than one interfaces but can not extend more than one class.

4) What are the differences between static and non-static methods?

Static method is common to all instances of a class. Static methods are stored in the
class memory. Where as non-static methods are stored in the object memory. Each
instance of a class will have their own copy of non-static methods.

5) What is the use of final keyword in java?

final keyword in java is used to restrict the modification of a class or a method or a


variable. A final class can not be extended, a final method can not be overridden and
we can not change the value of a final variable.

6) What is garbage collection in java?

Removing unwanted objects or abandoned objects from the memory is called


garbage collection. Garbage collection is done automatically in java. You need not to
remove the unwanted objects explicitly. Garbage collector thread does this for you
7) What is cloning in java?

Cloning is a process of creating an exact copy of an existing object in the memory.


Cloning may be shallow or deep. In java, clone() method is used to create a clone of
an object.

8) Can we declare local inner class as abstract?

Yes. Local inner class can be abstract.

9). What is encapsulation in Java?

Encapsulation is a mechanism where you bind your data(variables) and


code(methods) together as a single unit. Here, the data is hidden from the outer
world and can be accessed only via current class methods. This helps in protecting
the data from any unnecessary modification. We can achieve encapsulation in Java
by:

10)What is the difference between this() and super() in Java?

this() represents the current instance of a super() represents the current instance
class of a parent/base class

11). Why Java is not 100% Object-oriented?

Java is not 100% Object-oriented because it makes use of eight primitive data types
such as boolean, byte, char, int, float, double, long, short which are not objects.

12) How many types of constructors are used in Java?


1. Based on the parameters passed in the constructors, there are two types of
constructors in Java.
Default Constructor: In Java, a default constructor is the one which does not
take any inputs. In other words, default constructors are the no argument
constructors which will be created by default in case you no other constructor
is defined by the user. Its main purpose is to initialize the instance variables
with the default values. Also, it is majorly used for object creation. 
2. Parameterized Constructor: The parameterized constructor in Java, is the
constructor which is capable of initializing the instance variables with the
provided values. In other words, the constructors which take the arguments
are called parameterized constructors.

13). What are access modifiers in Java?

In Java, access modifiers are special keywords which are used to restrict the access
of a class, constructor, data member and method in another class. Java supports
four types of access modifiers:

1. Default
2. Private
3. Protected
4. Public

14. What do you mean by an interface in Java?

An interface in Java is a blueprint of a class or you can say it is a collection of


abstract methods and static constants. In an interface, each method is public and
abstract but it does not contain any constructor. Thus, interface basically is a group
of related methods with empty bodies. Example:

public interface Animal {


  public void eat();
  public void sleep();
  public void run();
}
Factorial Program using loop in java
class FactorialExample{  
 public static void main(String args[]){  
  int i,fact=1;  
  int number=5;//It is the number to calculate factorial    
  for(i=1;i<=number;i++){    
      fact=fact*i;    
  }    
  System.out.println("Factorial of "+number+" is: "+fact);    
 }  
}  

You might also like