Basic Interview Questions Flashcards
Explain JDK
It stands for Java Development Kit.
It is the tool necessary to compile, document and package Java programs.
It contains JRE + development tools.
Explain JRE
It stands for Java Runtime Environment.
JRE refers to a runtime environment in which Java bytecode can be executed.
It’s an implementation of the JVM which physically exists.
Explain JVM
It stands for Java Virtual Machine.
It is an abstract machine. It is a specification that provides a run-time environment in which Java bytecode can be executed.
JVM follows three notations: Specification, Implementation, and Runtime Instance.
Explain public static void main(String args[]) in Java.
main() in Java is the entry point for any Java program. It is always written as public static void main(String[] args).
public: Public is an access modifier, which is used to specify who can access this method. Public means that this Method will be accessible by any Class.
static: It is a keyword in java which identifies it is class-based. main() is made static in Java so that it can be accessed without creating the instance of a Class. In case, main is not made static then the compiler will throw an error as main() is called by the JVM before any objects are made and only static methods can be directly invoked via the class.
void: It is the return type of the method. Void defines the method which will not return any value.
main: It is the name of the method which is searched by JVM as a starting point for an application with a particular signature only. It is the method where the main execution occurs.
String args[]: It is the parameter passed to the main method.
Why is Java 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 is Java 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.
What are constructors in Java?
In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.
There are two types of constructors:
1, Default Constructor: In Java, a default constructor is the one which does not take any inputs. In other words, default constructors are the no argument constructors which will be created by default in case you no other constructor is defined by the user. Its main purpose is to initialize the instance variables with the default values. Also, it is majorly used for object creation.
2, Parameterized Constructor: The parameterized constructor in Java, is the constructor which is capable of initializing the instance variables with the provided values. In other words, the constructors which take the arguments are called parameterized constructors.
What is singleton class in Java and how can we make a class singleton?
Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.
What is the difference between Array list and vector in Java?
ArrayList:
ArrayList is not synchronized.
ArrayList is fast as it’s non-synchronized.
If an element is inserted into the ArrayList, it increases its Array size by 50%.
ArrayList does not define the increment size.
ArrayList can only use Iterator for traversing an Array List.
Vector:
Vector is synchronized.
Vector is slow as it is thread safe.
Vector defaults to doubling the size of its array.
Vector defines the increment size.
Vector can use both Enumeration and Iterator for traversing.
What is the difference between equals() and == in Java?
Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic.
“==” or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two objects.
What are the differences between Heap and Stack Memory in Java?
The major difference between Heap and Stack memory are:
Memory: (Stack) Stack memory is used only by one thread of execution. (Heap) Heap memory is used by all the parts of the application.
Access: (Stack) Stack memory can’t be accessed by other threads. (Heap) Objects stored in the heap are globally accessible.
Memory Management: (Stack) Follows LIFO manner to free memory. (Heap) Memory management is based on the generation associated with each object.
Lifetime: (Stack) Exists until the end of execution of the thread. (Heap) Heap memory lives from the start till the end of application execution.
Usage: (Stack) Stack memory only contains local primitive and reference variables to objects in heap space. (Heap) 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 are pointers not used in Java?
Java doesn’t use pointers because they are unsafe and increases the complexity of the program. Since, Java is known for its simplicity of code, adding the concept of pointers will be contradicting. Moreover, since JVM is responsible for implicit memory allocation, thus in order to avoid direct access to memory by the user, pointers are discouraged in Java.
What is JIT compiler in Java?
JIT stands for Just-In-Time 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. Once the method has been compiled, the JVM summons the compiled code of that method directly rather than interpreting it. This is why it is often responsible for the performance optimization of Java applications at the run time.