Java Interview Questions

Top 100+ Java Interview Questions You Must Know in 2023

1. What is the use of wrapper classes in java?

These classes are used to convert primitive datatype values into object

2. Which one is not a wrapper class?

  • Integer
  • Float
  • double
  • Boolean

double

3. What is autoboxing in java?

Automatic conversion of primitive datatype values into object is called autoboxing. This feature was introduced in Java 5

4. In which version of java collection framework was introduced?

Java version 2

5. What is collection in collection framework and what is use of collection framework?

Group of objects is known as collection. Collection framework is used to manage java objects in memory

6. How many types of collections are given in java

  • There are three types of collection in java
  • List collection
  • Set collection
  • Queue collection

7. What is difference between list collection and set collection?

  • List collection is ordered collection and set collection is unordered
  • List collection allows duplicacy of element and set collection does not allow it
  • List collection allows multiple null and set collection does not allow

8. Which classes are implementation of List interface?

  • ArrayList class
  • LinkedList class
  • Vector class

9. What is similarity and what is difference between ArrayList class and LinkedList class?

Both creates and uses resizable array of size 10 to store elements. ArrayList is an unsynchronized class while Vector class is a synchronized class. ArrayList class was introduced in JDK1.2 while Vector class was introduced in JDK1.0

10. Which one is not a method of Collection interface

  • public void set(int index, E element)
  • public boolean remove(E element)
  • public boolean add(E element)
  • public Boolean isEmpty()

public void set(int index, E element)

11. LinkedList list1=new ArrayList();
List1.add(“AAA”);
List1.add(“BBB”);
List1.add(“CCC”);
LinkedList list2=new ArrayList();
List2.add(“ABC”);
List2.add(“XYZ”);
Create a new ArrayList collection and copy all elements of both ListList into new collection

LinkedList list3=new ArrayList();
List3.addAll(list1);
List3.addAll(list2);

12. ArrayList list=new ArrayList();Display each element from the collection using Iterator interface

Iterator itr=list.iterator();
While(itr.hasNext())
{
String str=list.next();
System.out.println(str);
}

13. ArrayList list=new ArrayList();Display each element from the collection using ListIterator in reverse order

ListIterator itr=list.listIterator(list.size());
While(itr.hasPrevious())
{
String str=list.previous();
System.out.println(str);
}

14. LinkedList list=new LinkedList();
list.add(“Rahul”);
list.add(“Kapil”);
list.add(“Hemant”);
list.add(“Kuldeep”);
list.add(“Amit”);
Write code to sort element of above collection in ascending order

Collections.sort(list);

15. public class Employee
{
private int eid;
private String name;
private Sint salary;
public Employee(){}
public Employee(int eid,String name,int salary)
{
this.eid=eid;
this.name=name;
this.salary=salary;
}
//getters and setters
}
Vector list=new Vector();
list.add(new Employee(103,”Amit”,90000));
list.add(new Employee(101,”David”,67000));
list.add(new Employee(104,”Imran”,46000));
list.add(new Employee(102,”Neha”,78000));
Write code to sort element of above collection in the ascending order of salary

Collections.sort(list,(obj1,obj2)->obj1.getSalary()-obj2.getSalary());

16. public class Employee
{
private int eid;
private String name;
private Sint salary;
public Employee(){}
public Employee(int eid,String name,int salary)
{
this.eid=eid;
this.name=name;
this.salary=salary;
}
//getters and setters
//toString() method
}
Vector list=new Vector();
list.add(new Employee(103,”Amit”,90000));
list.add(new Employee(101,”David”,67000));
list.add(new Employee(104,”Imran”,46000));
list.add(new Employee(102,”Neha”,78000));
Write code to get and display those employees from the collection whose salary is more than 50000. Do it by using Stream API

List.stream().filter(emp->emp.getSalary()>50000).forEach(emp->System.out.println(emp));

17. public class Employee
{
private int eid;
private String name;
private Sint salary;
public Employee(){}
public Employee(int eid,String name,int salary)
{
this.eid=eid;
this.name=name;
this.salary=salary;
}
//getters and setters
//toString() method
}
Vector list=new Vector();
list.add(new Employee(103,”Amit”,90000));
list.add(new Employee(101,”David”,67000));
list.add(new Employee(104,”Imran”,46000));
list.add(new Employee(102,”Neha”,78000));
Write code to get those employees from the collection whose salary is more than 50000 and add them into a new list collection. Do it by using Stream API

List newlist=List.stream().filter(emp->emp.getSalary()>50000).collect(Collectors.toList());

18. Which package contains all collection classes and interface

java.util

19. Which package contains Interable interface

java.lang

20. Which two methods are called to avoid to duplicacy of elements in set collection

  • public int hashCode()
  • public Boolean equals(Object obj)

21. Which set collection should be used to keep element in ascending or descending order

TreeSet class

22. Which method is called by TreeSet collection to arrange elements in ascending or descending order

public int compareTo(Object obj) method of Comparable interface.

23. What is difference between Comparable and Comparator interface

Comparable interface has compareTo() method and this method is used to compare object of implementation of Comparable interface with another object of this interface implementation. Method is invoke on one object and another object is passed as argument to this method Comparator interface has compare() method and this method is used to compare two object of any class. Both objects are passed as arguments to this method Comparable interface provides single sorting sequence, while Comparator provides multiple sorting sequence

24. public class Employee
{
private int eid;
private String name;
private Sint salary;
public Employee(){}
public Employee(int eid,String name,int salary)
{
this.eid=eid;
this.name=name;
this.salary=salary;
}
//getters and setters
//toString() method
}
TreeSet set=new TreeSet();
set.add(new Employee(103,”Amit”,90000));
set.add(new Employee(101,”David”,67000));
set.add(new Employee(104,”Imran”,46000));
On execution above code creates and throw ClassCastException. How to avoid this exception?

Provide implementation of compareTo() method of Comparable interface inside Employee class

25. Which set collection should be used to maintain insertion order of elements

LinkedHashSet class, It is child of HashSet class

26. Map interface is a collection or not?

It is not child of Collection interface, so it is not a collection. It keeps elements in key-value pair. Each key maps to only one value

27. Which class is implementation of Map interface?

HashMap class

28. Write code to keep phone number of 5 persons in map. Name of person should be key and phone number should be value

HashMap<String,String> map=new HashMap<String,String>();
map.put(“amit”,”9988776655”);
map.put(“manoj”,”6688776655”);
map.put(“imran”,”2288776688”);
map.put(“neha”,”9188786633”);
map.put(“david”,”3488776655”);

29. Which method of Map interface should be called to get Set collection of keys

public Set keyset()

30. What is used Map.Entry interface in Map

An object of Map.Entry interface is created to store key-value pair. This object is inserted as an entry in the map. So object of Map.Entry interface is element of map

31. What is JDBC in java?

JDBC stands for java database connectivity. JDBC is a Java API. It is called by java program to connect database and execute SQL queries Specification of this API is given by Java vendor and implementation is given by Database vendors

32. What is JDBC drivers?

These are client-side programs (Running on the client machine, not server) that convert requests from java programs to a protocol that DBMS/database can understand

33. How many types of JDBC drivers are given?

  • There are 4 types are JDBC drivers are given. These are as follows
  • Type-1 driver or JDBC-ODBC bridge driver
  • Type-2 driver or Native-API driver
  • Type-3 driver or Network Protocol driver
  • Type-4 driver or Thin driver

34. Which JDBC driver is fastest and used more commonly?

Thin driver (Type-4 driver) is fastest and most commonly used driver. It interacts directly to the database

35. Which datatypes are used for storing the images and files in database?

BLOB (binary large object) data type is used to store image in the database. We can also store videos and audio by using the BLOB data type. It stores the binary type of data CLOB (character large object) datatype is used to store the file in the database. It stores the character type of data

36. What is Driver interface in JDBC?

connect() method of this interface is called by java program to connect to the database.This method is declared inside Driver interface and implementation of this method is given by database vendors

37. Can we write code to call connect() method

Yes. But code to call this method is already written inside getConnection() method of java.sqlDriverManager class.

38. What is driver class in JDBC and what is specialty of this class

This class is implementation of java.sql.Driver interface When this class is loaded, it creates an object of itself and registers it with the DriverManager class. This is the specialty of this class

39. What are the steps of JDBC code?

  • There are the steps of JDBC code
  • Load class class
  • Create connection object
  • Create statement object
  • Execute query
  • Close connection

40. What is the purpose of loading driver class and what is the code to load driver class

When driver class loads, it creates an object of itself and registers it with the DriverManager class. As we known getConnection() method of DriverManager class is responsible to call connect() method of driver class Class.forName(“fully qualified driver class”);

41. What is code to create connection object?

Following code should be written to create connection object. Connection cn=DriverManager .getConnection (“url”,”username”,”password”);

42. What is statement object and how many types of statement can be created?

Statement object is required to execute SQL queries. We can create following three types of statement object

Object of Statement interface
Object of PreparedStatement interface
Object of CallableStatement interface

  • Object of Statement interface
  • Object of PreparedStatement interface
  • Object of CallableStatement interface

43. What is difference between object of statement interface and object of PreparedStatement interface?

Object of Statement interface should be created when you want to execute static SQL queries(Those queries that are not using parameters) Object of PreparedStatement interface should be created when you want to execute dysnamic SQL queries(Those queries that are using parameters). Object of Statement interface does not hold query while object of PreparedStatement holds query. So you cant execute multiple SQL queries using single object of Statement interface while this is not possible using object of PreparedStatement interface

44. When to create and use object of CallableStatement interface

This object should be created and used when you want to execute SQL stored procedures and SQL functions

45. Which method should be executed on statement object to execute SQL DML query?

  • Following method should be executed
  • public int executeUpdate(String query):It is method of Statement interface
  • public int executeUpdate():It is method of PreparedStatement interface

46. Which method should be executed on statement object to execute SQL DQL query?

  • Following method should be executed
  • public ResultSet executeQuery(String query):It is method of Statement interface
  • public ResultSet executeQuery():It is method of PreparedStatement interface

47. What is ResultSet interface?

An object of this interface is created by executeQuery() method.In this object executeQuery() method keeps resultset(records retrieved from database as a response of select query)

48. What is resultset cursor?

Every resultset object(Object of ResultSet interface) maintains a pointer.This pointer is called resultset cursor. This cursor is initially positioned just before the first record(At index -1)

49. Which methods are given in Result Set interface to change cursor position?

  • There are several methods. Few of them are as follows
  • public boolean next()
  • public boolean previous()
  • public boolean last()
  • public boolean first()
  • public boolean absolute(int n)
  • public boolean relative(int n)

50. Write JDBC code to create a table?

Try
{
Class.forName(“fully driver class name”);
Connection cn= DriverManager. getConnection (“url”,”username”,”password”);
Statement st=cn.createStatement();
st.execute(“create table command”);
cn.close();
}

Leave A Reply

Your email address will not be published. Required fields are marked *

9212172602

 
  9212172602  

    Enquire Now

    Call / WhatsApp : +919212172602

      Enquire Now

      Call / WhatsApp : +919212172602

        Enquire Now

        Call / WhatsApp : +919212172602