Top Java Interview Questions Flashcards
Why Java is platform independent?
Java is called platform independent because of its byte codes which can run on any system irrespective of its underlying operating system.
Why Java is not 100% Object-oriented?
Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.
What are wrapper classes in Java?
Wrapper classes convert the Java primitives into the reference types (objects). Every primitive data type has a class dedicated to it. These are known as wrapper classes because they “wrap” the primitive data type into an object of that class. Refer to the below image which displays different primitive type, wrapper class and constructor argument.
What is the importance of reflection in Java?
Java Reflection allows you to analyze classes, interfaces, fields, and methods during runtime without knowing what they are called at compile time.
Contiguous memory locations are usually used for storing actual values in an array but not in ArrayList. Explain
An array generally contains elements of the primitive data types such as int, float, etc. In such cases, the array directly stores these elements at contiguous memory locations. While an ArrayList does not contain primitive data types. An arrayList contains the reference of the objects at different memory locations instead of the object itself. That is why the objects are not stored at contiguous memory locations.
Why is synchronization necessary?
Java allows multiple threads to execute. They may be accessing the same variable or object. Synchronization helps to execute threads one after another. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
Why is it said that the length() method of String class doesn’t return accurate results?
length() simply takes into account the number of characters within in the String (and ignores characters outside BMP - U+10000 or above).
What are the differences between Heap and Stack Memory in Java?
Memory Stack memory is used only by one thread of execution. Heap memory is used by all the parts of the application.
Access Stack memory can’t be accessed by other threads. Objects stored in the heap are globally accessible.
Memory Management Follows LIFO manner to free memory. Memory management is based on the generation associated with each object.
Lifetime Exists until the end of execution of the thread. Heap memory lives from the start till the end of application execution.
Usage Stack memory only contains local primitive and reference variables to objects in heap space. Whenever an object is created, it’s always stored in the Heap space.
What is a package in Java? List down various advantages of packages.
Packages in Java, are the collection of related classes and interfaces which are bundled together. By using packages, developers can easily modularize the code and optimize its reuse. Also, the code within the packages can be imported by other classes and reused. Below I have listed down a few of its advantages:
Packages help in avoiding name clashes
They provide easier access control on the code
Packages can also contain hidden classes which are not visible to the outer classes and only used within the package
Creates a proper hierarchical structure which makes it easier to locate the related classes
Why pointers are not used in Java?
- Increases complexity of code
- JVM is responsible for implicit memory allocation; don’t want users to have direct access to memory
What is JIT compiler in Java?
It is a program that helps in converting the Java bytecode into instructions that are sent directly to the processor. By default, the JIT compiler is enabled in Java and is activated whenever a Java method is invoked. The JIT compiler then compiles the bytecode of the invoked method into native machine code, compiling it “just in time” to execute.
Inheritance
Inheritance is a process where one class acquires the properties of another.
Encapsulation
Encapsulation in Java is a mechanism of wrapping up the data and code together as a single unit.
Abstraction
Abstraction is the methodology of hiding the implementation details from the user and only providing the functionality to the users.
Polymorphism
Polymorphism is the ability of a variable, function or object to take multiple forms.
What is the difference between a local variable and an instance variable?
A local variable is bound to its local scope (i.e., its code block).
An instance variable is bound to an object (or instance thereof).
Differentiate between the constructors and methods in Java?
Methods Constructors
- Used to represent the behavior of an object 1. Used to initialize the state of an object
- Must have a return type 2. Do not have any return type
- Needs to be invoked explicitly 3. Is invoked implicitly
- No default method is provided by the compiler 4. A default constructor is provided by the compiler if the class has none
- Method name may or may not be same as class name 5. Constructor name must always be the same as the class name
What is final keyword in Java?
final variable When the final keyword is used with a variable then its value can’t be changed once assigned. In case the no value has been assigned to the final variable then using only the class constructor a value can be assigned to it.
final method
When a method is declared final then it can’t be overridden by the inheriting class.
final class When a class is declared as final in Java, it can’t be extended by any subclass class but it can extend other class.
Difference between String, StringBuilder, and StringBuffer.
Factor String StringBuilder StringBuffer
Storage Area Constant String Pool Heap Area Heap Area
Mutability Immutable Mutable Mutable
Thread Safety Yes No Yes
Performance Fast More efficient Less efficient
What are the common interfaces of the Collection class
List, Queue, Set
What is multiple inheritance? Is it supported by Java?
If a child class inherits the property from multiple classes is known as multiple inheritance. Java does not allow to extend multiple classes.
The problem with multiple inheritance is that if multiple parent classes have the same method name, then at runtime it becomes difficult for the compiler to decide which method to execute from the child class.
Therefore, Java doesn’t support multiple inheritance.
What is a marker interface?
A Marker interface can be defined as the interface having no data member and member functions. In simpler terms, an empty interface is called the Marker interface
Explain Bean in Spring
A bean is an object that is instantiated, assembled, and managed by a Spring IoC container.
What is autowiring in Spring?
Autowiring enables the programmer to inject the bean automatically. We don’t need to write explicit injection logic.