Link Search Menu Expand Document

TOP-10 Elementary Java Interview Questions

Q1. What is the primary difference between Java and C++?

Java is a platform-independent programming language. It is used for application programming and also in the window/web-based and mobile applications. It is majorly used to support network computing. On the other hand, C++ is platform dependent. It is majorly used for system programming or application programming.

Q2. What is meant by being a platform-independent programming language?

A platform-independent language (or here, Java) refers to a language that does not operate depending on the operating system used. Hence, Java codes need not be individually compiled for every device it will be run on. JVM (Java Virtual Machine), accessible in all operating systems, helps easily convert the Java code into the equivalent machine code of any functional device, thus, resulting in the universal nature of the language.

 Q3. Does Java support multiple inheritance?

Java supports multiple inheritance only through its interfaces but not through classes. Because Java is a Simple Object-Oriented Language, it needs to keep its codes complexity-free and straightforward. Inhibiting the use of Java multiple inheritance completely avoids the rise of complexity and conflict.

Q4. What is Object-Oriented Programming?

OOP or Object-Oriented Programming is a design used for programming which focuses on conforming around objects rather than logic or functions. Hence, the manipulation of logic is overtaken by a focus directed towards logic.

Q5. Can you protect or privatize a top-level class?

No, top-level classes can not be protected or set as private in Java, but inner classes can be. A private class makes it invisible, which defies the logic of creating a code as it will then not be usable. Also, as package inheritance is not plausible in Java, protecting a class, too, does not work.

Q6. How does 'throw' and 'throws' differ in Java Exception Handling?

Though both the keywords are used in the context of exceptions, the ‘throw’ keyword is used to throw Exception from any method or static blocks, while the ‘throws’ keyword is used to indicate an exception that can be thrown by the throw method. Upon throwing a checked Exception, a user can rethrow it by using the ‘throws’ keyword or use the try-catch block for handling the exception.

Q7. In Java, what are a few differences seen in constructors and methods?

While methods are used to denote an object’s behavior and must have a return type, constructors are used as initializers for the state of the object and need not have a return type. Also, a default constructor is usually prescribed by the compiler itself, while this never happens in the case of methods.

Q8. What is Java String Pool?

The Java String Pool has the job of searching if a newly created object is already present in the pool or not, which is conducted by searching the heap memory. Only if the newly created object is not present will it be made actually; otherwise, the String Pool will return the reference to the existing variable.

Q9. What is a Map in Java?

It is used to map unique keys to values while duplicate keys are not allowed. Each key can only map once and is not a subset.

Other useful articles:


Back to top

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