Java Interview Questions and Answers

1. What are the different access specifiers for Java classes?

Access classifiers are said to be the keywords that are used before a class name which refers the access scope in Java.

  • public: field, method, class is accessible from anywhere.
  • protected: field and method can be accessed from the same class to where they belong or from subclasses, and from same package class, but not from outside.
  • default: field, method, the class can be accessed from the same package only and not from outside of the native package.
  • private: field, the method can be accessed from the same class to where they belong.
2. What is known as data encapsulation and explain it’s significance?

Encapsulation is the concept which is asked frequently in the most Java Interview Questions for Freshers.

Encapsulation is said to be a concept in Object Oriented Programming for combining methods and properties in a single unit.

Encapsulation helps the programmers in following a modular approach for the software development as each object has its unique set of variables and methods and serves its functions independent of other objects. Encapsulation is also used for data hiding purpose.

3. What is the difference between an Inner class and a Subclass?

An inner class has been nested within the other class. An inner class has access rights for the class that is nesting it and can access all methods and variables that are defined in the outer class.

A subclass inherits from the other class known as the superclass. Sub-class can access all protected and public fields and methods of its superclass.

4. What is the purpose of static variables and static methods?

When there is a need to share a variable or a method between multiple objects of a class rather than creating separate copies for each object. To make a variable or method shared for all objects, we use.

5. What is a singleton class? Give an example.

In Java, a singleton class can have only one instance and thus all its variables and methods belong to one instance.

The concept of a singleton class is used for the situations while there is a requirement to limit the no of objects for a class.

The good example of singleton usage scenario is while there is a limit of having an only single connection to a database due to some limitations of the driver or any licensing issues.

6. What is called Loops in Java? What are the three types of loops?
  1. For Loops
  2. while Loops
  3. Do While Loops
  • For Loops : For loops are used in java to implement statements regularly for a given no of times. For loops are used when no of times to implement the statements is refer to the programmer.
  • while Loops : While loop is used if certain statements require being implemented regularly until a condition is satisfied. The condition is tested first before implementation of statements in while loops.
  • Do While Loops : Do while loop is similar to while loop with an only difference which condition is examined after implementation of statement blocks. Thus in do while loop case, statements are implemented at least once.
7. What is known as infinite loop? Explain how infinite loop is declared?

An infinite loop runs infinitely and without any condition. An infinite loop may be broken by explaining any breaking logic in the statements blocks body.

Infinite loop is proclaimed as follows

for (;;)
{
// Statements to execute

// Add any loop breaking logic
}

8. What is known as a final keyword in Java? Give an example

A constant is proclaimed using the keyword final in Java. The value may be assigned once and after the assignment, the value of a constant cannot be changed.

A constant with the name const_val has been assigned and declared a value in the below example: Private Final int const_val=100

It cannot be overridden by the subclasses if a method is declared as final. This method is considered to be faster than any other method because they have been resolved at the compiled time.

It can’t be subclassed, when a class is declared as final. (e.g., integer, string, and other wrapper classes).

9. Explain the difference between break and continue statement?

Break and continue statements are the two main keywords used in loops. When a break keyword is used in a loop, instantly the loop is broken while when continue keyword is used, the current iteration is broken and the loop continues with the next iteration.

When counter reaches 4, loop is broken in the below example.

For (counter=0; counter<10; counter++)
System.out.println(counter);
if (counter==4) {
break;
}}

When counter reaches 4, loop just jumps to the next iteration in the below example and after the continue keyword any statements are 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”);
}

10. Explain the difference between float and double variables in Java?

Float always takes about 4 bytes in memory while double takes about 8 bytes in memory in Java. The float is said to be single precision floating point decimal number and the double is the double precision decimal number.

11. How will you generate random numbers in Java?

You can generate random numbers using Math.random() in the range which is greater than or equal to 0.1 and less than 0.1 and also by using Random class in package java.util.

12. What is known as the base class in Java from which all the classes are derived?

Java.lang.object

13. What is known as a ternary operator? Provide an example.

Based on a Boolean value evaluation, Ternary operator is also called conditional operator which is used to make the decision that value to assign to a variable.

If rank is 1, the status is assigned a value of “Done” else “Pending” in the below example.

public class conditionTest {
public static void main(string args[]) {
string status;
int rank = 3;
status = (rank = = 1) ? “Done” : “Pending”;
System.out.println(status);
}
}

14. What are known as Java Packages? What is the significance of packages?

The package is referred to the collection of interfaces and classes that are bundled together as they are relevant to each other in Java.

Use of package helps the developers to modularize the code and assemble the code for proper re-use. Once the code is packed in packages, it may be imported into other classes and used.

15. What is a default switch case?

The default case is implemented when no other switch condition matches in a switch statement. The default case is referred as an optional case.

It may be declared only once all the other switch cases have been coded.

16. Does main() method in java return any data?

main() method cannot return back any data and thus, it is always declared with a void return type in Java.

17. Are we able to declare a class as Abstract class without having any abstract method?

Yes, using the abstract keyword before the class name even it doesn’t contain any abstract method, we can create an abstract class.

Although, when a class contains even one abstract method, it should be revealed as abstract otherwise it gives an error.

18. How will we pass an argument to a function through reference rather than pass value?

In Java, we are able to pass argument only by value to a function, and not by reference.

19. When are we supposed to use serialization?

These Java interview questions for freshers will brush up your knowledge and help you get through your interview easily.

Serialization has been used when data requires being transmitted through the network. With serialization, state of the object is converted and saved into a byte stream. The byte stream is transferred through the network while the object is recreated at the destination.

20. How can an object be serialized in java?

In Java, an interface using the name serialization is executed by the class in order to change an object into byte stream through serialization.

Almost all the objects of a class executing serializable interface get serialized, and state is stored in the byte stream.

21. When is the class constructor invoked?

Every time an object is built with new keywords, the constructor of a class has been invoked.

For example : two objects has been created with new keyword and thus, constructor is invoked two times in the following class.

public class const_example {const_example() {
}
public static void main(String args[]) {
const_example c1=new const_example();
const_example c2=new const_exam
}
}

22. Will a class contain multiple constructors?

Yes, a class comprises of multiple constructors with various parameters. Which constructor receives used for the creation of object depends on the arguments passed during the object creation.

23. In java why strings are known as Immutable?

String objects are known immutable in java once the value is assigned to a string, it cannot be changed. If it is changed, a new object is created.

24. What is the difference between Vector and an array?

Vectors are said to be dynamic in nature and hold the data of various data types while an array gathers data of a similar primitive type that is static in nature.

25. Define multi-threading

It is one of the significant and frequently asked Java Interview Questions

Multi-threading is said to be a programming concept to generate multiple tasks within a single program in a concurrent manner.

Threads share a stack of the same process and running in parallel. It helps in performance enhancement of any program.

26. Why is the Runnable Interface used in Java?

Runnable interface has been used in java for executing multi-threaded applications. Java.Lang.Runnable interface is executed by a class to support the multi-threading.

27. What are the two methods of executing multi-threading in Java?

Multi-threaded applications are built in Java with any of the following two ways:

  • With Java.Lang.Runnable Interface. Classes execute this interface to authorize multi-threading. There occurs a Run() method in the interface that is executed.
  • Through writing a class which expands Java.Lang.Thread class.
28. How can we create copy of a Java object?

We can use the cloning concept to make a copy of an object. We make copies with the actual state of an object using clone.

Clone() is said to be a method of Cloneable interface and thus, Cloneable interface must be implemented for making object copies.

29. What is the advantage of using inheritance?

The key advantage with inheritance is reusability of code as inheritance enables sub classes to reuse the code of its super class.

Polymorphism is considered to be another great advantage that allows new functionality to be introduced without effecting existing derived classes.

30. Give an example of Pointers use in Java class.

There are no pointers in Java. So we cannot use pointers concept in Java.

These Java Programming Interview Questions helps you in preparing well for the interview.

Justify the statement Java is platform independent ?

Changes and upgrades in operating systems, processors and system resources will not trace any changes in java programs. This is why it is used on internet which interconnects different kinds of systems worldwide.

Read More Topics
Object oriented programming Q&A
Synchronous machine Q&A
Special machines Q&A
Transformer Q&A

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply