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.