Java Interview Questions & Answers – Part2

Java Interview Questions & Answers – Part2

1)What is the different between inheritance and interface?

Inheritance:
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.

Interface:
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?

java.lang.Object

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.

Need
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?

HTTP PUT:

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.

HTTP POST:
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++){
System.out.println(r.nextInt(10));
}

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.

Q21) What are the Features of Java?

These are the features of Java programming,

  • Oops concepts
  • Platform independent
  • High Performance
  • Multi-threaded
  • platform independent language.

Q22) What is Class?

All Java code are defined in a class. A Class have variables and methods.

Q23) Example program for Class?

Example Program for Class:

public class Dog {
   String breed;
   int age;
   String color; 
   void barking() {
   } 
   void hungry() {
   } 
   void sleeping() {
   }
}

Q24) Write Some Oops Concepts of Java?

Oops Concept of java:

  • Object Oriented
  • Encapsulation
  • Abstraction
  • Polymorpism
  • Inheritance
  • Association
  • Composition

Q25) What is Inheritance? Give Example of Inheritance?

In java Inheritance means one class can be extended to another class. So that the codes  reused from one class to another class.

Existing class is known as Super class, where the derived class is known as a sub class.

Q26) What are the purpose of Encapsulation?

Purpose of Encapsulation:

  • Protects the code from others.
  • Code maintainability
  • List: List is a child

Q27) What is java Collections Framework?

Collections is a framework. It like containers that group multiple items in a single unit. A group of objects is Called as collections.

Framework: Framework is a Architecture to represent and Manipulate collections in java as a standard way.

java Collections Framework Contains some following Parts:

  • Interface
  • Implementation Classes
  • Algorithms

Q28) What are the Benefits of Java Collections Framework.

The Benefits of Java Collections Framework:

  • Reduced Development Effort
  • Reusability and Interoperability
  • Increased Quality

Q29) List out the Interfaces in Java Collections?

Interfaces in Java Collections are:

  • Collection Interface
  • Iterator Interface
  • Set Interface
  • List Interface
  • Queue Interface
  • Dequeue Interface
  • Map Interface
  • ListIterator Interface
  • SortedSet Interface
  • SortedMap Interface

Q30) List out the Classes in Java Collections?

Classes in Java Collections  are:

  • HashSet Class
  • TreeSet Class
  • ArrayList Class
  • LinkedList Class
  • HashMap Class
  • TreeMap Class
  • PriorityQueue Class
  • Collections class

Q31) What is meant by ordered and sorted in java Collections?

Ordered:

Ordered means the valuesinterface of Collection. Values can be added to the list is based on the index position and it is ordered by index position. Duplicates can be allowed.

Types of List:

  • Array list
  • Linked list
  • Vector
  • Stack classes

Q32) Types of Exceptions?

There are two types of exceptions:

  • Checked Exceptions
  • UnChecked Exceptions

Unchecked Exceptions Includes

  • Arithmetic Exception
  • ArrayIndexOutOfBounds Exception

Q33) What is Thread?

The flow of a execution is called Thread. Every java program have at least one thread called as main thread, JVM created that Main Thread. The user can defined their own thread by extending Thread class or by implementing Runnable interface. Threads are executed concurrently.

Example:

public static void main(String[] args)
{
//main thread starts here
}

Q34) Expand form of JDK, JRE, and JVM?

The JVM,JRE and JDK Stands for

  • JVM stands for Java Virtual Machine
  • JRE stands for Java Runtime Environment
  • JDK stands for Java Development Kit

Q35) How Do Connect to Database in Java?

These are the steps to connect to database in Java:

  • Registering the driver class
  • Creating connection
  • Creating statement
  • Executing queries
  • Closing connection

Q36) What is JSON?

JSON stands for Java Script Object Notation. This format is text only.  it uses JavaScript syntax.

Q37) What are the different type of constructors?

There are two different types of Constructors,

  • Default Constructor
  • Parameterized Constructor

Q38) What are the Difference Between Stack and Queue?

The difference between a stack and a queue are:

  • The stack is based on LIFO, It Stands for Last in First out principle,
  • The queue is based on FIFO, It Stands for First in First out principle.

Q39) What is JDBC?

JDBC is a software component. JDBC is used to interact with the database.

There are Four types of JDBC drivers:

  • JDBC-ODBC bridge driver
  • Native-API driver (partially java driver)
  • Network Protocol driver (fully java driver)
  • Thin driver (fully java driver)

Q40) What are the Various steps in Java to connect to a database?

These are the step to connect Database in Java,

  • Registering the driver class
  • Creating connection
  • Creating statement
  • Executing queries
  • Closing connection

Q41) What is Spring Bean and List the Various Scopes of Spring bean?

Beans is a objects that form the backbone of  Spring. Beans are managed by the Spring IoC container. Bean is a object that is instantiated, assembled, and managed by a Spring IoC container.

There are five Scopes in Spring beans.

  • Singleton
  • Prototype
  • Request
  • Session
  • Global-Session

Q42) What is the finally and finalize in java?

finally block in Java is used with try-catch to put the code that you want to get executed always, even if these any exception is thrown by the try-catch block. finally block is mostly used to release a resources created in the try block.

finalize() is a special method in Object class in java, that we can over ride in the classes.Finalize() method gets called by the garbage collector when the object is getting garbage collected. Finalize() method is usually override to release system resources when the object is garbage collected.

Q43) What is string? Write a sample String Program?

Java String is one of the most Highly used class in java. Java String class is defined in java.lang package

Examble Program of String:

package com.journaldev.strings; 
public class StringTest { 
                public static void main(String[] args) {
                                String s1 = new String("besant");
                                String s2 = new String("BESANT");
                                System.out.println(s1 = s2);
                } 
} 
OutPut: BESANT

Q44) Write a Syntax for Nested Class? what are the types of Nested Classes?

Syntax for Nested Class: 
class Outer_Demo {
   class Inner_Demo {
   }
}

There are two types of Nested Classes:

  • Non-static nested classes
  • statis nested classes

Q45) Explain extends Keyword, Give example.

In java Extends is the keyword. This is used to inherit the properties of a class.

Syntax of Extends Keyword is:

Syntax 
class Super {
   .....
}
class Sub extends Super {
   .....
}

Q46) What is constructor?Syntax of costructor

A constructor  is a special method that is used to initialize objects.  constructor is called when an object of a class is created in Java. It can be used to set initial values for object attributes.

Example:

class ClassName {
   ClassName() {
   }
}

Q47) What are the types of constructor?

In Java, two types of constructors is there

  • No argument Constructors
  • Parameterized Constructors

Q48) What is Parameterized Constructors.

Parameterized Constructors:

In java, Parameters are added to a constructor in the same way, Parameters are added to a method, just declare them inside the parentheses after the constructor’s name.

Example :

class MyClass {  
 int x;  
   // Following is the constructor
   MyClass(int i ) {
      x = i;
   }
}

Q49) What are the Types of Data Types?

There are two data types in Java

  • Primitive Data Types
  • Reference/Object Data Types

Q50) What is Primitive types? Types of Primitive datatypes.

In Data types Primitive datatypes are pre-defined by the language and named by a keyword.

There are eight primitive datatypes supported by Java.

  • Byte
  • Short
  • int
  • long
  • float
  • double
  • boolean
  • char

Q51) Examble program for Variable?

public class Test {
   public void pupAge() {
      int age = 0;
      age = age + 7;
      System.out.println("Puppy age is : " + age);
   }
   public static void main(String args[]) {
      Test test = new Test();
      test.pupAge();
   }
} 
Output: Puppy age is 7

Q52) What is Modifiers?What are the Types?

Modifiers is a  keywords that you add to those definitions to be change their meanings. Java have a variety of modifiers,

They Are

  • Java Access Modifiers
  • Non Access Modifiers

Q53) What are the types of Operators in java?

In java, they provides set of Operators to manipulate variables,they are

  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
  • Misc Operators

Q54) What is loop control?

In java a loop statement allows us to execute a single statement or group of statements on multiple times and following is the general form of a loop statement in most of the programming languages

Java Programming provides the following loops:

  • for loop
  • while loop
  • while loop

Q55) What are the types of Decision making Statements?

java provides types of decision making statements, They are:

  • if statement
  • else statement
  • nested if statement
  • switch statement

Q56) Example program for Getting current Date and time.

import java.util.Date;
public class DateDemo { 
public static void main(String args[]) {
      // Instantiate a Date object
      Date date = new Date(); 
      // display time and date using toString()
      System.out.println(date.toString());
   }
}

Output:

on FEB 04 09:51:52 CDT 2019

Q57) Define Stream

The stream , that can be defined as a sequence of data.

Two kind of stream is there,

  • Input Stream
  • Output Stream

Q58) What is Data structures in java?

In Java data structures provided by these Java utility package are very powerful and perform a wide range of functions.

The data structures contains of the following interface and classes

  • Enumeration
  • BitSet
  • Vector
  • Stack
  • Dictionary
  • Hashtable
  • Properties

 

For More Interview Questions Click Here – Java Interview Questions