Q.1 What is the difference between JDK and JRE?
JDK stands for Java Development Kit, which is a software development environment for building Java Applications whereas JRE stands for Java Runtime Environment, which is required to run Java Programs.
Q.2 Why is Java platform independent language?
Java language was developed in such a way that it does not depend on any hardware or software this means that both the Java programing language and its associated APIs are first compiled into bytecodes that can run on multiple platforms. Then the virtual machine handles any variations in how these bytecodes are executed across different platform.
Q.3 What is the difference between an abstract class and an interface?
Abstract classes and interfaces are both used in Java for abstraction, but they have some key differences. Here are the main distinctions between an abstract class and an interface:
- Definition – An abstract class is a class that may have both abstract methods (methods without a body) and concrete methods (methods with a body). It can also have instance variables. You can declare an abstract class using the abstract keyword.
An interface is a collection of abstract methods. In Java, all methods declared in an interface are implicitly public and abstract (prior to Java 8), and it can’t contain instance variables. Starting from Java 8, interfaces can have default and static methods as well. - Inheritance – In Java, a class can extend only one abstract class using the extends keyword. A class can implement multiple interfaces using the implements keyword.
- Constructor – An abstract class can have constructors, and they are called when an instance of a subclass is created. Interfaces cannot have constructors because they cannot be instantiated.
- Access Modifiers – Abstract class members (methods, fields) can have different access modifiers (public, protected, private, etc.). Interface members are implicitly public and abstract (prior to Java 8), and they are public by default starting from Java 8.
- Fields/Variables – Abstract classes can have instance variables (fields) that can be inherited by subclasses. Interfaces can only have public static final variables, which are constants and cannot be changed by implementing classes.
- Multiple Inheritance – Java supports single-class inheritance, so a class can extend only one class. Java supports multiple interface inheritance, meaning a class can implement multiple interfaces.
- Usage – Abstract classes are used when you want to provide a common base class for multiple related classes. Interfaces are used when you want to define a contract for multiple classes to implement, regardless of their inheritance hierarchy
Q.4 What is the difference between final, finally and finalize?
Final is used to make a variable or method constant and cannot be changed later. finally is used in try-catch blocks to execute a block of code regardless of whether an exception is thrown or not. finalize is a method that is called by the garbage collector when an object is no longer in use.
Q.5 What is the difference between stack and heap memory?
Stack memory is used for storing local variables and function call, while heap memory is used for storing objects and their instance variables.
Q.6 What is the difference between method overloading and method overriding ?
Method Overloading is creating multiple methods in a class with the same name but different parameters, while method overriding is creating a method in a subclass with the same name and parameters as a method in its superclass.
Q.7 What is the difference between a private and a protected modifier?
A private modifier makes a member accessible only within the same class, while a protected modifier makes a member accessible within the same class and its subclasses.
Q.8 What is constructor overloading in Java?
Constructor overloading is a concept in object oriented programing where a class can have multiple constructors with different parameter lists. Each constructors provides a different way to initialize objects of that class.
Q.9 What is the use of super keyword in Java?
The super keyword is used to access data members of the parent class when the data members names of the parent class and its child subclasses are the same, to call the default and parameterized constructor of the parent class inside the child subclass and to access parent class methods when the child subclasses have overridden them.
Q.10 What is the difference between static method, static variables and static classes in Java?
Static methods and static variables are those methods and variables that belongs to the class of the Java program, not to the object of the class. They are allocated memory when the class is loaded and can directly be called with the help of the class names. A class in the java program cannot be static except if it is the inner class. if it is an inner static class, then it exactly works like other static members of the class.
Q.11 What exactly is System.out.printIn in Java?
Syste.out.printIn() is a method to print a message on the console. System it is a class present in java.lang package. Out is the static variable of type PrintStream class present in the System class. printIn() is the method present in the PrintStream class.
Q.12 What part of memory – Stack or Heap – is cleaned in the garbage collection process?
Garbage Collection is done on heap memory to free the memory used by objects that don’t have any reference. Any object created in the heap space has global access and can be referenced from anywhere in the application.
Hi there! This blog post couldn’t be written any better!
Looking at this article reminds me of my previous
roommate! He continually kept talking about this. I’ll forward this information to him.
Pretty sure he’s going to have a great read. Thank you for sharing!