• New Admissions Open for Career and Placement Courses.

Java Interview Questions

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.

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.

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

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.

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.

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.

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
}

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 “);
}

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.

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.

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);
}
}

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

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”);
}
}
}

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

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

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.

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.

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.

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.

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..*

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.

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

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

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.

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);

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();
}
}

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

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 Blog

September 23, 2016

Get Certified From a Reputed Java Training Institute in Ahmedabad


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 […]

October 03, 2014

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


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. […]

January 24, 2014

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


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 […]

Hi!

Hey!

TOPS JobFest is Happening

50+ IT Cos will hire for 1,000+ Positions

Are you looking for a JOB ?

I`m looking for a job

Thank you, I`m okay

OR

Hey!

Complete the form

We are waiting for your application!