Java Interview Questions & Answers – Part2

Home/Interview Questions/Java Interview Questions & Answers – Part2

Java Interview Questions & Answers – Part2

1)What is the different between inheritance and interface?

In java classes can be derived from classes. Basically if you need to create a new class and here is already a class that has some of the code you require, then it is possible to derive your new class from the already existing code. This concept allows you to reuse the fields and methods of the existing class without having to rewrite the code in a new class. In this scenario the existing class is called the super class and the derived class is called the subclass.

In Java language an interface can be defined as a contract between objects on how to communicate with each other. Interfaces play a vital role when it comes to the concept of inheritance. An interface defines the methods, a deriving class (subclass) should use. But the implementation of the methods is totally up to the subclass.

2)Write a Program to search in pdf.

Click here for Sample Program.

3)What are Access Modifiers in Java?

  • Default: A variable or method declared without any access control modifier is available to any other class in the same package
  • Public: A class, method, constructor, interface etc. declared public can be accessed from any other class
  • Protected Variables methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members’ class
  • Private: Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.
    Non-access Modifiers: final, abstract.

4)What is the difference between abstraction and encapsulation?

Encapsulation: – it means binding up of data and methods it means the concept of class in which we put together the data members and methods or function members.

Abstraction: – it means that hiding the background details which are not useful to the user for it we use normally private or protected access specifier by it data is not let free to move around the system.

  • Abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented using private, package-private and protected access modifier.
  • Encapsulation is also called data hiding.
  • Design principles “programming for interface than implementation” is based on abstraction and “encapsulate whatever changes” is based upon Encapsulation.

5)What is i18n and L10n?

  • I18n stands for Internationalization (18 char between I and n).
  • L10n stands for Localization (10 char between l and n.)It is the means by which i18n applications can be used for local regions.

6)Write a program to reverse a String in Java.

Click here for Sample Program.

7)What are the difference between Java and JavaScript?

Java JavaScript
Java is an Object Oriented Programming (OOP) language JavaScript is a scripting language
Java is very much Complex JavaScript s comparatively easy
Java contains rich set of commands JavaScript contains a much smaller and simpler set of commands
Java can stand on its own that creates “standalone” applications JavaScript must (primarily) be placed inside an HTML document to function
Java must be compiled into what is known as a “machine language” before it can be run on the Web JavaScript is text-based. You write it to an HTML document and it is run through a browser

8)What is the base class of all classes?


9)Write a Java program to print Palindrome number after number n passed by user.

Click here for Sample Program.

10)What is the difference between Checked and Unchecked exceptions?

The compiler forces you to either catch checked exceptions or throw them to the calling method. For e.g. IOException.
Unchecked exceptions (Runtime exception) are due to programming bugs (for ex. index out of bound exception). The compiler expects you to handle this in the code so it does not force you to catch unchecked exceptions. Exceptions extending Runtime Exception are unchecked.

11)What is Auto boxing and unboxing.

Auto boxing-Automatic conversion of primitives into corresponding objects wrapper classes.

Unboxing – Automatic conversion of wrapper classes into primitives.

The collections like ArrayList store only the objects so primitives need to converted to object wrapper classes.
Auto boxing/Unboxing helps to do automatic conversion.

12)What is the difference between executequery and executeupdate.

  • executeQuery method is used to execute SQL which returns ResultSet.
  • executeUpdate method is used to execute update queries like insert, update, or delete.

13)What is Annotation?

An annotation indicates that the declared element should be processed in some special way by a compiler, development tool, and deployment tool or during runtime.

14)What is the Difference between put and post?


PUT puts a file or resource at a specific URI, and exactly at that URI. If there’s already a file or resource at that URI, PUT replaces that file or resource. If there is no file or resource there, PUT creates one HTTP 1.1 RFC location for PUT.

POST sends data to a specific URI and expects the resource at that URI to handle the request. The web server at this point can determine what to do with the data in the context of the specified resource.

15)What are Java Annotations?

Java 5 comes with several pre built annotations

  • Override
  • Deprecated
  • SupressWarning
  • Retention
  • Target

16)Write a program to generate 15 random integers between 0 and 10.

Random r = new Random();
for(int i=0;i<15;i++){

17)Write a program to fetch unique elements from Array.

Click here for Sample Program.

18)Write a program to transpose a Matrix.

Click here for Sample Program.

19)Difference between Throw and Throws.

  • Throws – used to advertise that the method might throw some exceptions.
  • Throw – used to throw exception from method definition.

20)Write a difference between System.exit(0) and System.exit(1).

  • System.exit(0) is used in any method to come out of the program. It terminates the program normally where as
  • System.exit(1) terminates the program because of some error encountered in the program.

For More Interview Questions Click Here – Java Interview Questions

By | 2017-07-12T15:31:45+00:00 July 12th, 2017|Categories: Interview Questions|Tags: , , |0 Comments

About the Author:

Leave A Comment