Top 20 Interview based question in Java

Top 20 Interview based question in Java

Java is general purpose object oriented programming language and platform independent software first introduced by Sun Microsystem in 1995. This tutorial we provide top 20 Interview based question for Java.

Best Software tutorials in porur For All students
1) What is Abstract class in Java?
Ans:-Abstract Classes are classes in Java that declare one or more abstract methods. A method without body (no implementation) is known as abstract method.
 
2) Write a Java Program, which has get the type of bike from the user based on user input it has to call related class?
Ans:-
import java.util.Scanner;
abstract class bike
{
bike()
{
System.out.println("bike is created");
}
abstract void run();
void changegear()
{
System.out.println("gear changed");
}
}
class honda extends bike
{
void run()
{
System.out.println("honda");
}
}
class unicorn extends bike
{
void run()
{
System.out.println("unicorn");
}
}
class test2
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("enter your choice");
 int choice=in.nextInt();
switch(choice)
{
case 1:
{
       bike obj=new honda();
obj.run();
obj.changegear();
break;
}
case 2:
{
bike obj=new unicorn();
obj.run();
obj.changegear();
break;
}
}
}
}          
3) Write a java program to print Good Morning, Good Afternoon, And Good Evening based on the Date Time?
Ans:-

import java.util.GregorianCalendar;
 public class DisplayDateTime
{
 public static void main(String[] args)
 {
  GregorianCalendar time = new GregorianCalendar();
  int hour = time.get(Calendar.HOUR_OF_DAY);
  int min = time.get(Calendar.MINUTE);
  int day = time.get(Calendar.DAY_OF_MONTH);
  int month = time.get(Calendar.MONTH) + 1;
  int year = time.get(Calendar.YEAR);
   System.out.println("The current time is \t" + hour + ":" + min);
  System.out.println("Today's date is \t" + month + "/" + day + "/"    + year);
   if (hour < 12)
   System.out.println("Good Morning!");
  else if (hour < 17 && !(hour == 12))
   System.out.println("Good Afternoon");
  else if (hour == 12)
   System.out.println("Good Noon");
  else
   System.out.println("Good Evening");
 }
}
4)Write a java Program to count the number of digit?
Ans:-

class noofdigit
{
public static void main(String args[])
{
int number,count=0;
Scanner sc=new Scanner(System.in);
System.out.println("Please Enter any number");
number=sc.nextInt();
while(number>0)
{
number=number/10;
count=count+1;
}
System.out.println("no of digit"+count);
}
}
5) What are the basic interfaces of Java Collections Framework?
Ans:- Set, List, Map, Queue, Iterator,SortedMap, List Iterator
6) What are the rules for declaring Java Identifier?
Ans:-

1) Identifier Should not start with the digit
2) Java identifier are case sensitive of course java language
3) There Is no length limit for identifiers but it is not Recommended to take too lengthy identifier.
4) Reserved Keyword should not used a Identifier.
7) What are the different types of Comment available in Java?
Ans:-
(i)Single line Comment
(ii)Multi line Comment
(iii)Documentation Comment
 8) What is the difference between Overloading and Overriding in JAVA?
Ans:-
Overloading:
Methods are said to be overloading when many method in a class have same name but different argument.
Overriding:
While Inherting,If method name in the base class and derived class are same, Then contents in the derived class are override by method in base class is Overloading.
10) Define Package in Java?
Ans:-Packages are nothing directory of our file system that exist our file system they contain a set of class file.
11)List 5 important things to connect Java applications with MySQL?
Ans:-
(i)Driver Class
(ii)JDBC Connection URL
(iii)Username
(iv)Password
(v)Jar File
12) What are the different OOPS concept in Java?
Ans:-Class, Inheritance, Objects, Abstraction, Encapsulation, Polymorphism.
 13) What is Inheritance?
Ans:- Inheritance is the mechanism of deriving a new class from an oldclass.Here the old class is called as Base class and new class is called as Derived Class.
14) What is Static Binding and Dynamic Binding?
Ans:- Static or early binding is resolved at compile time. Method overloading is an example of static binding. Dynamic or late or virtual binding is resolved at run time. Method overriding is an example of dynamic binding.
 15) Define Class and Object in Java?
Ans:-Class is a way to bind member variables and member functions together. By creating object we can access member variable and member functions in the class.
16)Write a Java program to implement emp details using class and object?
Ans:-
Emp details using Classes and Objects:
import java.util.Scanner;
class employee
{
String empname;
int empid;
double emphone;
String empaddress;
void getdata()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter empname");
empname=sc.next();
System.out.println("enter empid");
empid=sc.nextInt();
System.out.println("enter empphoneno");
emphone=sc.nextInt();
System.out.println("enter employee address");
empaddress=sc.next();
}
void putdata()
{
Scanner sc=new Scanner(System.in);
System.out.println("empname="+empname);
System.out.println("empid="+empid);
System.out.println("empname="+emphone);
System.out.println("empaddres="+empaddress);
}
}
class test
{
public static void main(String args[])
{
employee e1=new employee();
e1.getdata();
e1.putdata();
}
}
17)Can you change the above program to get n number of employee records?
Ans:-
import java.util.Scanner;
class employee
{
String empname;
int empid;
double emphone;
String empaddress;
void getdata()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter empname");
empname=sc.next();
System.out.println("enter empid");
empid=sc.nextInt();
System.out.println("enter empphoneno");
emphone=sc.nextInt();
System.out.println("enter employee address");
empaddress=sc.next();
}
void putdata()
{
Scanner sc=new Scanner(System.in);
System.out.println("empname="+empname);
System.out.println("empid="+empid);
System.out.println("empname="+emphone);
System.out.println("empaddres="+empaddress);
}
}
class test1
{
public static void main(String args[])
{
employee[] e1=new employee[10];
Scanner sc=new Scanner(System.in);
int i,n;
System.out.println("Enter how many records to store");
n=sc.nextInt();
for(i=1;i<=n;i++)
 e1[i] =  new employee();   // Allocating memory to each object
for(i=1;i<=n;i++)
{
e1[i].getdata();
}
for(i=1;i<=n;i++)
{
e1[i].putdata();
}
}
}
 18) What is Path Variable in Java?
Ans:- The PATH variable gives the location of executable like javac, java etc. It is possible to run a program without specifying the PATH but you will need to give full path of executable like C:\Program Files\Java\jdk1.8.0_131\bin\javac A.java instead of simple javac A.java.
19) Write any 3 data type declaration with examples?
Ans:-Assume that  we are using Scanner class for implementation:
Scanner sc=new Scanner(System.in);
String Data type Declaration example:
String empname;
empname=sc.next();
 
Integer Data type Declaration Example:
int empid;
empid=sc.nextInt();
 Double Data type Declaration Example:
Double fee;
fee=sc.nextDouble();
(or)
double fee=sc.nextDouble(); 
20) How to create an Array?
Ans:-
An Array is declared similar to how a variable is declared, but we need to add [] after the type.
Example:
int [] intArray;
String [] stringArray;
MyClass [] myClassArray;
Or
int[] age;
age = new int[5];
 21) Can we change the size of an array at run time?
Ans:- No we cannot change the array size. Though there are similar data types available which allow a change in size.
 22) What is the default value of Array?
Ans:-Any new Array is always initialized with a default value as follows
For byte, short, int, long – default value is zero (0).
For float, double – default value is 0.0.
For Boolean – default value is false.
For object – default value is null.
 23) Can we add or delete an element after assigning an array?
Ans:-No it is not possible.

Comments

Rajkumar

Very nice post

Comments

Nithya

Interview questions are good and helpful to me for preparing for an interview

Comments

Gomathi

The above tutorial is very useful for us.