Request a Call back


captcha

Java Interview Questions

Frequently Asked Interview Questions And Answers For Java Freshers

What Is The Difference Between an Inner Class and a Sub-Class In Java?

A Nested Class is a member of its enclosing class. An inner class can access the variables and functions of the outer class. The class derived from the superclass is called subclass. Sub-class can use all the protected and public methods of the superclass.

What Are The Various Access Specifiers for Java Classes In Java?

These Specifiersdetermine whether a field or method in a class, can be used or invoked by another method in another class or sub-class The types of access specifiers for classes are:

1.) Public: We can access all the methods and variables from anywhere.
2.) Protected: We can access the methods and variables of the related subclass that should be in the same package.
3.) Default: All the methods, variables can be accessed from the classes that are in the same package only.
4.) Private: All the methods and variables can be accessed from the only one class with which it is connected.

What's The Purpose of Static Methods and Static Variables In Java?

When we want to share the method or variable to all the objects of the class then we declare it as static.

What Is Data Encapsulation In Java and What's Its Significance?

Encapsulation is the process of combining the data members and methods together in a unit.

Encapsulation allows the developer to make its variables and functions independent from other objects for Hiding purpose. Encapsulation also serves data hiding purpose.

What Is a Singleton Class In Java? Give a Practical Example of its Usage.

In singleton class, all the methods and variables can belong to only one instance because of the need for limiting the number of objects of the class.
The best example of the singleton class is when we want the only one connection to the database for security reason.

What Are Loops in Java? What Are Three Types Of Loops?

Loops are used to repeat the execution of the same statement in a program There are three types of loops in Java:

1) For Loops
When a number of iteration is fixed then we can use for loop in a program.

2) While Loops
We can use while loop when we want to check the condition first and after that, the statements are executed.

3) Do While Loops
I do while loop first the statement block is executed and after that condition is checked.

What Is An Infinite Loop? How Infinite Loop Is Declared?

An infinite loop is a loop where there is no break condition so it runs infinitely.
An infinite loop is declared as follows:
for (;;)
{
// Statements to execute

// Add any loop breaking logic
}

What Is The Difference Between Continue and Break Statement?

In loops, the break keyword is used to stop the execution in the loop and continue keyword is used to continue the broken iteration in the loop.
In below example, Loop is broken when counter reaches 4.
for (counter = 0; counter & lteration; 10; counter++)
system.out.println(counter);

if (counter == 4) {
system.out.println(“here the break keyword is applied”)
break;
}

}
In the below example when the counter reaches 4, loop jumps to the next iteration and any statements after the continue keyword is skipped for the current iteration.
for (counter = 0; counter < 10; counter++) system.out.println(counter); if (counter == 4) { continue; } system.out.println("This will not get printed when counter is 4,because we have used continue keyword here "); }

What Is The Difference Between Double And Float Variables In Java?

In Java, float uses 4 bytes in memory and double takes 8 bytes. Float is single precision floating point decimal number while Double is a double precision decimal number.

What Is Final Keyword in Java? Give an Example.

When a variable is declared using the final keyword its value cannot be changed.
In below example, a variable with the name const_value is declared and assigned avalue:
Private Final int const_value=100
Subclasses cannot override the final methods because they are found at compile time
We can not make a subclass of the Final class.

What Is Ternary Operator? Give an Example.

It is like a simplified if statement, the ternary operator consists of a condition that evaluates to either true or false, plus the value that is returned if the condition is true and another value is returned if the condition is false.

In the below example, if rank is 1, status is assigned a value of “Done” else “Pending”.
public class condition Test {
public static void main(String args[]) {
String status;
int rank = 3;
status = (rank == 1) ? “Done” : “Pending”;
System.out.println(“value:”+status);
}
}

How Can You Generate Random Numbers In Java?

We can generate random number using Math.random() in between 0.1 &1.0. The random class is in java. util package.

What Is Default Switch Case? Give an Example.

The Switch Statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. The default case is optional, it is executed after the break statement or if any condition is not matched. In the below example, when the score is not 1 or 2, the default case is used.
public class switch example {
int score = ;
public static void main(String args[]) {
switch (score) {
case 1:
system.out.println(“I have 1 kite runs”);
break;
case 2:
system.out.println(“I have 2 kites “);
break;
default:
system.out.println(“I have no kite”);
}
}
}

What's The Base Class In Java From Which All Classes Are Derived?

Java.lang.object is the class where all the classes are driven.

Can Main() Method In Java Can Return Any Data?

The main method is declared as void so it can’t return any data.

What Are Java Packages? What's the Significance of Packages?

A Package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another.

Can We Declare A Class as Abstract Without Having Any Abstract Method?

We can declare a class as abstract without having any abstract method in it, it is needed that the class should be declared as abstract if it has an abstract method in it else error may occur.

What's The Difference Between An Abstract Class and Interface In Java?

In The Interface, all the methods should be declared as public static without a body part, and in the abstract class, methods can be with all type of specifiers and no condition of its implementation.
All the methods from interface must be implemented in a subclass of it while abstract class doesn’t require this type of implementation.
Multiple inheritances can be solved using interface by implementing multiple interfaces, we cant inherit more than one abstract class.

What Are The Performance Implications Of Interfaces Over Abstract Classes?

An Abstract Class is faster than the interface and the problem with the interface is that all the methods should be implemented in the subclass that increases the burden on the code.

Does Importing A Package Imports Its Sub-Packages as Well In Java?

While importing a package, if you want to import its subclass you have to do that separately.

For example, if a developer imports a package vehicle.*, all classes in the package named vehicle are loaded but no classes from the sub-package are loaded. To load the classes from its sub-package ( say Two wheelers), the developer has to import it explicitly as follows:
Import Vehicle.twowheeler..*

Can We Declare The Main Method of Our Class as Private?

The Main Method is the main gate for a program to be executed then it should be declared as public static. It will give the runtime error when we declare the main method as private.

How Can We Pass Argument To a Function By Reference Instead of Pass By Value?

We cannot pass an argument by reference in java.it can be done only bypass by value.

How An Object Is Serialized In java?

To convert the object into byte stream we have to use serializable interface implemented on the object of the class to perform serialization.

When We Should Use Serialization?

When we want to transfer the data without its state changed we perform the serialization to convert the data and the data is reconverted at the destination.

Is It Compulsory For a Try Block to Be Followed By a Catch Block in Java for Exception Handling?

The Exception occurred in the try block is thrown and caught into a catch block or the final block is performed so it is compulsory that a try block must be followed by a catch block or finally block.

Is There Any Way To Skip Finally Block of Exception Even If Some Exception Occurs in the Exception Block?

The Exception Occurred in the try block must result in caught by a catch block or the final block Is executed. finally, bock is executed if there is no action available in the statement.we can stop the execution by writing the following code:
System.exit(0);

When The Constructor of a Class is Invoked?

Every time the object of a class is instantiated with the new keyword, the constructor of the class is called.
For example, in the following class, two objects are created using the new keyword and hence, the constructor is invoked two times.
public class const_example {

const_example() {

system.out.println(“It is Inside constructor”);
}
public static void main(String args[]) {

const_example c1 = new const_example();

const_example c2 = new const_example();
}
}

Can A Class Have Multiple Constructors?

There may be multiple constructors having multiple arguments or no arguments called when passed different arguments in the constructor at the object creation time.

Can We Override Static Methods of a Class?

Static methods are directly connected to the class, it is called using directly by class name and it is found at compilation time, so when we try to override the static method, it won’t give any effect on it.

Latest News

Add to your IT skills with short term and Professional courses from TOPS with Live Projects in Basic and Advance technologies.

Get Certified from a Reputed Java Training Institute in Ahmedabad

September 23, 2016

Java language – developed by Sun Microsystems on 1991 brought a great revolution to the IT Industry. It was originally called Oak by James Gosling, one of the inventors of Java language. Java has been inherited from 2 languages – C & C++. The person requiring learning Java needs to have knowledge of these programming […]

JAVA vs. PHP – Matter of Opting Between Two Open Source Languages

October 3, 2014

Obviously it’s been tough for anyone when JAVA and PHP both are standing cut to cut. It is like giving an order from Belgium chocolate cake and Belgium chocolate chip cake. You can’t find a big distinction. You can find many vast differences between open source and premium programming languages. But when both are open […]

Java Jobs – Tips for Freshers on How to Score Jobs?

January 24, 2014

Java is one of the most demanding programming languages that has been in demand everywhere. There are number of software companies that offer Java development services as the demand for websites and applications based on Java is more than any other platform whether PHP, .NET etc. The graph of Java jobs trends when found online […]

Need More Information about Jobs and Training?

Get the Answers you needs in 60 seconds or less

We will call you and answer your queries about Job Opportunities, Free Training and Internship Opportunities, References of students from your college we have placed. Just Fill the information and get the answers in 60 seconds or less.