Link Search Menu Expand Document

10 Java Interview Questions You Must Learn

Q1. Why is Java not 100% Object-Oriented?

Java uses eight primitive data types such as char, boolean, byte, short, int, long, float, double, and others, which are not objects. Hence, Java is not a pure 100% Object-Oriented language.

Q2. What is the difference between Heap and Stack Memory?

While heap memory can be accessed by all the parts of the application or are globally accessible, stack memory is only available through one thread of execution. Only this one thread stores the stack memory while the heap memory can be accumulated through all the applications. Hence, stack memory is only available until the thread is being executed, but heap memory remains accessible till the end of the application execution.

Q3. What are a few advantages of packages in Java?

Foremostly, name clashes can be avoided using packages, and easier access to the code is obtained. Further, hidden classes, invisible to outer classes, can be stored in packages and also facilitates the easy locating process due to the set hierarchy.

Q4. Why aren't pointers used in Java?

Java is a simple programming language and thrives on keeping the coding simple. Hence, avoiding the use of pointers not only eliminates pointless complexity but also ensures that the program is safe. Avoiding the use of pointers also inhibits the direct access of the users to the system memory.

Q5. What is an example of an infinite loop in Java?

public class InfiniteForLoopDemo
{
public static void main(String[] arg) {
for(;;)
System.out.println("Hello World!");
// To terminate this program press ctrl + c in the console.
}
}

Here, the loop continues indefinitely until a function does not exit. An infinite loop may occur by a coding error or may be the result of intentional actions.

Q6. What is the Static Method?

Static methods do not use any variables from objects but apply to the class. The usage of the static method does not require the creation of objects and be used to change the value of the static variables.

Q7. What restrictions can be seen in Java static methods?

Non-static data members are not accessible to the static methods and hence can not call upon them directly. As ‘this’ and ‘super’ are non-static, they can not be used in static methods.

Q8. How can you execute a program without using the main() method?

A static block is one of the methods that can be implied to execute a program without using the main() method. When a class is loaded into the memory using the ClassLoader, the static block can be performed once only. A static block is a set of statements in Java. The process can also be called the static initialization block.

Q9. What are the advantages of using inheritance in Java?

The code reusability feature of Java is accessible due to inheritance, which excludes the derived class from redefining the method of the base class. Inheritance also facilitates data hiding. As the base class is made private, it can hide data from the derived class.

Q10. Why is a super keyword used in Java?

The keyword can be used to refer to the immediate parent class instance variable and can also be invoked using the same. The immediate parent class constructor can also be invoked using super().

Other useful articles:


Back to top

© , Java Interviews — All Rights Reserved - Terms of Use - Privacy Policy