Java Interview Questions
Differentiate a String, StringBuffer, and StringBuilder
Storage Area: String is stored in the string pool while StringBuilder and StringBuffer are stored in the heap. Mutability: A String is immutable while the StringBuilder and StringBugger are mutable.
What is the scope of member level variables
The member variables must be declared inside class (outside any function). They can be directly accessed anywhere in class
What is Constructor Overloading?
The process of creating multiple constructors in the class consisting of the same name with different parameters. Depending upon the number of parameters and their types, distinguishing of the different types is done by the compiler.
What happens if you remove the static modifier from the main method?
The program compiles successfully, but at runtime throws an error. NoSuchMethodError.
What cant you overload a method by changing the return type?
The return type of functions is not part of the mangled name which is generated by the compiler for uniquely identifying each function.
What is the Super Keyword used For?
The super keyword is used to access hidden fields and overridden methods or attributes of the parent class. For example if you want to access a variable of a parent class that shares a name with a variable of your class.
What is the "this" keyword?
The this keyword refers to the current instance of the class it is called in.
Why does Java not use Pointers?
They are rather complicated, super unsafe to use by beginner programmers, cause a lot of potential errors, and are a massive security risk as they allow users to interface directly with a computers memory.
Can you Overload/Override static methods in java
Trick question, while you can have 2+ static methods with the same name and different inputs and get an overloaded method you cannot override them. The reason is that if you declare static method with a same signature in a subclass there isnt any run time polymorphism. So its more of a technicality but no.
Can Static Methods be Overloaded?
Yes, you can have 2+ static methods in a single class with the same name but different input parameters.
Besides the security aspect, why are Strings immutable in Java?
1. Its far more optimized which is important given how often they are used. 2. You dont need to do external synchronization when multithreading as you dont have to worry about strings being different in each thread. You bypass an entire step other languages have to take into account. 3. Without immutable strings Hash tables and Hash maps wouldn't have immutable keys to facilitate reliable retrieval of data.
What is a Class?
A blueprint that defines an objects variables and methods.
What is Object Oriented Programming?
A design philosophy based on objects which contain data in attributes and code in the form of methods. Objects are an instance of a class, which is a template for creating objects. Data is restricted to be used only when needed by abstraction.
What is the scope of Loop Variables
A variable declared inside a pair of brackets {} in a method has scope within the brackets only
What is an abstract class?
Abstract classes, unlike interfaces, are classes. There are more expensive to use because there is a lookup to do when you inherit from them. Abstract classes look a lot like interfaces, but they have something more : you can define a behavior for them.
What is an Interface?
An interface is a class that only includes method signatures. The methods are not implemented in the interface. Another class must be created to supply the implementation.
Why is the main method static?
Because the object is not required to call the static method. If we make the main method non-static, JVM will have to create its object first and then call main() method which will lead to the extra memory allocation.
What is Data Encapsulation?
Data encapsulation, also known as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user. The user can only perform a restricted set of operations on the hidden members of the class by executing special functions commonly called methods
What is the main objective of garbage collection?
Free up memory space occupied by unnecessary and unreachable objects during the Java program execution by deleting those unreachable objects.
How can single and multiple catch blocks co-exist in Java?
If you have a single try catch block linked to multiple catch blocks the first catch block to trigger will be activated.
What is Aggregation?
It is a type of weak relation you can create between two classes, where one contains references to another class contained within it.
What is Enumeration?
It is an interface you can use to access origional data structure from which enumeration is obtained.
Why is Java a platform independent language?
It was developed in such a way that it does not depend on any hardware or software due to the fact that the compiler compiles the code and then converts it to platform independent byte code which can be run on multiple systems. The only condition to run that byte code is for the machine to have a runtime environment JRE installed in it.
What is JIT Compiling?
JIT stands for Just-In-Time and it is used for improving the performance during run time. It does the task of compiling parts of byte code having similar functionality at the same time thereby reducing the amount of compilation time for the code to run.
Why is Java not a pure object oriented language?
Java supports primitive data types - byte, Boolean, char, short, int, float, long, and double and hence it is not a pure object-oriented language.
What are Static Methods?
Methods declared with the keyword static as modifier are called static methods or class methods. They are so called because they affect a class as a whole, not a particular instance of the class. Static methods are always invoked without reference to a particular instance of a class. Note: The use of a static method suffers from the following restrictions: A static method can only call other static methods. A static method must only access static data. A static method cannot reference to the current object using keywords super or this.
What part of memory is cleaned up in the garbage collection process?
The Heap
What is the scope of local variables
Variables declared inside a method have method level scope and can't be accessed outside the method
Is it possible to restrict inheritance?
Yes use of the final keyword or a private constructor will prevent a class from being inherited.