Top Advanced Java Interview Questions and Answers

    11 Votes

50) Is it possible to have multiple public classes in Java ?

As per java language specification, there can be only one public class in a file (.java) and file name should be same as public class name. If you want another class accessible in other places, you may create a separate java file.

51) Write a program that override equal() and hashcode()?

Object's hash code is a number( 32-bit signed int) that helps hash-based data structure to manage an object. Hash code is supposed to be unique number assigned to an object by JVM. But if two objects are equals, then both the objects will have same hash code. Implementation of hashcode method in a class should be in such way that if two objects are same and when compared by equal method, then those two objects must return same hash code. Both hashCode and equals method should be overridden.

class Person{
private String name; private int age;
public Person(String name, int age){ = name;
this.age = age;
public String getName() {
return name;
public void setName(String name) { = name;
public int getAge() {
return age;
public void setAge(int age) {
this.age = age;

public int hashCode(){
int hashcode = 0;
hashcode = age*12;
hashcode += name.hashCode();
return hashcode;

public boolean equals(Object personObject){
if (personObject instanceof Person) {
Person pp = (Price) Person;
return ( && pp.age == this.age);
} else {
return false;

52) Can you explain about Enum datatype in Java ?

Enum keyword is used to represent a fixed number of related values. Since enum constants are implicitly static and final,there values can't be changed once they are created.

public enum Day {
} // Uses
public void weekDay() {
switch (day) {
case MONDAY:
System.out.println("Mondays are bad.");

53) Can you explain about the recursion ?

The function called itself is called recursion. For example, a factorial program

int factorial(int number) {
if(number == 0) {
return 1;
factorial_i(number, 1);
} int factorial_i(int currentNumber, int sum) {
if(currentNumber == 1) {
return sum;
} else {
return factorial_i(currentNumber - 1, sum*currentNumber);

54) Can you explain about call by value and call by reference ?

  • Call by Value - When a method receives a value as an argument, the value received is a copy of the original value of the calling method. Even if the value is altered inside the method, it won't be reflected in the variable in the calling method.
  • Call by reference - When a method receives reference of a variable, any change made to it in the called method won't be reflected in the calling method.

55) What is responsibility of Garbage Collector ?

For efficient memory utilization, Java uses Garbage collector. The main objective of the garbage collector is free up memory by detecting unused objects and deleting them. Garbage Collector gets invoked automatically and it won't assure required memory for a program to run.

56) What is the use of finalize method ?

The Object class has a special method named finalize() that is called by garbage collector to allow an object to do the necessary cleanup that needs to occur before the memory used by the object are reclaimed. The finalize() method can be overridden by any class that uses system resources (file , db connection) and needs to release those resources or to perform other cleanup as part of implicit destruction by the garbage collector.

57) Explain JDBC ?

Java Database Connectivity is a technique of connect java front end to back end database and allowing the retrieval and manipulation of data in the database using java.

58) How do you connect to the Database ?

The process of using JDBC to connect to the database is as follows:

Register the driver: Class.forName(”driverName”); For example, sun.jdbc.odbc.JdbcOdbcDriver
Connection con = DriverManager.getConnection(”url,”myLogin”,myPassword”);
For example url may be jdbc:odbc:dsn_name.

Creating the JDBC Statement and Retreiving:

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

59) What are the drivers and when it will use ?

  • JDBC-ODBC Bridge Driver for Java and Database (Java Application)
  • JDBC-Net pure java driver for Applet and Database(Applet application)
  • Native-API partly Java driver for Native class and Database (for network application)
  • Native Protocol pure java driver for vendor specific application (EJB application)
Page 6 of 6

Popular Videos


How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

Java OOPS Interview Questions and Answers
Java Interview Questions
Java Collections Interview Questions and Answers
Java Thread Interview Questions and Answers