JavaRush /Java Blog /Random EN /Coffee break #101. 19 Essential Java Interview Questions ...

Coffee break #101. 19 Essential Java Interview Questions and Short Sample Answers

Published in the Random EN group
Source: Hackernoon Here are some questions and answers that often come up in Java interviews.

1. What is Java Development Kit (JDK)?

The JDK is a software development kit that includes the tools and libraries needed to develop Java applications.

2. What does the Java Runtime Environment (JRE) do?

JRE refers to the Runtime Environment in which Java bytecode runs. The JRE maintains files and libraries for the runtime.

3. What does the Java Virtual Machine (JVM) do?

The JVM is an abstract machine that provides a runtime environment in which Java bytecode can execute.

4. Java is platform independent. Why?

In any other programming language, the source code is compiled into executable code, and the code may not run on all platforms. By using JVM we can make the bytecode understandable for any platform and this bytecode is platform independent. Although the JVM is different for each platform, Java is platform independent as it has no dependency on any type of platform.

5. Java is not 100% object oriented. Why?

Because Java uses eight primitive data types like boolean, byte, char, int, float, double, long, short which are not objects.

6. What are constructors in Java?

A constructor is a block of code used to initialize objects. Syntax:
class DemoClass
{
   // constructor name is same as class name
   DemoClass()
   {
      ....
   }
}
// calls DemoClass() constructor
DemoClass obj = new DemoClass();
Example:
public class ConstructorDemo
{
   int a; // class attribute
   // create constructor for class ConstructorDemo
   ConstructorDemo()
   {
      a = 26; // initial value for class attribute 'a'
   }
   public static void main(String[] args)
   {
      // creating object for ConstructorDemo class
      // here we're calling constructor ConstructorDemo()
      ConstructorDemo obj = new ConstructorDemo();
      System.out.println(obj.a);
   }
}
Result:
26

7. What does a singleton class do?

Singleton classes can only have one object (class instance) at a time. After the object is created and we try to instantiate the singleton class , the new variable also points to the first object created.

8. What are wrapper classes in Java?

Wrapper classes allow primitive data types to be converted to an object and vice versa. Example:
int a = 7; // using primitive datatype
Integer a = new Integer(7); // using wrapper class

9. What is the difference between the == operator and the equality method in Java?

In general, == is an operator and equals() is a method. We use the == operator to reference compare objects on the heap. There is no concept of operator overloading in Java. Despite this, the == operator is used to check whether the address of two objects is the same or not. That is, the == operator checks whether both objects point to the same memory location or not. The == operator is also used to compare object types and primitive types such as boolean. While the equals() method of String class compares the contents of two objects.

10. What are the concepts of OOP in Java?

  • Abstraction is defined as hiding the internal implementation and displaying only the necessary information.
  • Inheritance is the procedure of converting all the properties and behavior of a parent class (superclass) into a child class (subclass).
  • Encapsulation is the procedure of binding data or variables and methods together.
  • Polymorphism literally means many forms. Polymorphism is the ability of a method to perform different tasks. This can be achieved using methods.

11. Why is the main() method always static in Java?

Because there is no need to call a static method on an object. If main() were to be a non-static method, the Java Virtual Machine must first create its object and then call the main() method , which will cause additional memory to be allocated.

12. Why are strings immutable in Java?

Strings in Java are immutable because String objects are cached in the string constant pool .

13. What are arrays and ArrayList in Java?

An array is an object that contains a fixed number of elements of a similar type.
  1. ArrayList is part of the collection structure.

  2. ArrayList implements the list interface.

  3. An ArrayList is a scalable array that dynamically grows as elements are added and shrinks as elements are removed.

  4. For frequent retrieval operation, Java ArrayList is the best choice . Because the elements of an ArrayList are stored in consecutive memory locations.

  5. ArrayList cannot contain primitive data types such as int, double, char and long.

  6. ArrayList can contain String and wrapper class objects ( Double , Integer ).

  7. ArrayList allows you to duplicate elements.

  8. ArrayList preserves insertion order.

  9. ArrayList is widely used due to its functionality and flexibility. It is designed to store heterogeneous collections of objects.

  10. An ArrayList can have any number of null values.

14. What is the difference between HashSet and HashMap in Java?

In HashSet :
  1. We can store objects in a HashSet . For example, HashSet: {"Hello", "World"} .

  2. The insertion order is not preserved. It is hash code based.

  3. Has an add() method .

  4. Implements the Set interface .

  5. Does not allow duplicate elements.

  6. Allows a single null value.

In HashMap :
  1. In HashMap we can store key and value pairs. For example, {1 -> “Hello”, 2 -> “World”} .

  2. Does not support insertion order. It is based on the hashing function.

  3. Has a put() method .

  4. Implements the map interface.

  5. Allows duplicate values. Does not allow duplicate keys.

  6. Allows a single null key and any number of null values.

16. What is the difference between this and super in Java?

The this keyword in Java:
  1. this is a keyword that is a reference variable that refers to the current object.

  2. Can be used to implicitly call the current class method.

  3. The this() keyword , used to call the current class's constructor.

  4. This can be used to pass as an argument when calling a method.

Super keyword in Java:
  1. The super keyword is a reference variable that is used to refer to the immediate object of the parent class.

  2. super can be used to directly call a method of a parent class, a constructor of a parent class, and to access methods of a base class.

17. What is the break and continue statement in Java?

If a break statement occurs in a loop, the loop will end and control will pass to the next statement in the same loop. Example:
public class Example
{
   public static void main(String[] args)
   {
      for(int a = 1; a <= 10; a++)
      {
         if(a == 3)
         {
            // breaking loop
            break;
         }
         System.out.println(a);
      }
   }
}
Result:
12
The continue statement moves to the next iteration of the loop depending on a certain condition. Example:
public class Example
{
   public static void main(String[] args)
   {
      for(int a = 1; a <= 10; a++)
      {
         if(a % 2 != 0)
         {
            continue;
         }
         System.out.println(a + " ");
      }
   }
}
Result:
2 4 6 8 10

18. What are access modifiers in Java?

Access modifiers define the limit or scope of a variable, constructor, class, or method. There are four types of access modifiers in Java:
  1. Private access modifier.
  2. Protected access modifier.
  3. Sharing modifier.
  4. Default access modifier.

19. What is the difference between for loop and for each loop in Java?

for loop :
  1. Does not have an executable sequence. In the for loop, we can change the counter as we wish.
  2. Appeared at the very beginning, in JDK 1.
  3. There is no need to implement the interface.
  4. Can have access to index. Therefore, it is possible to replace an element in an array.
  5. The counter can increase and decrease.
For each loop :
  1. Performed sequentially. The counter increases by one.
  2. First appeared in JDK 5.
  3. To iterate over containers using a for-each loop, the container must implement the Iterable interface.
  4. It is not possible to replace the element at this index because the array index cannot be accessed.
  5. Can only iterate in increasing order, cannot decrease.
Example of the difference between for each and for loops :
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class DifferenceBetween
{
   public static void main(String[] args)
   {
      List<String> players = new ArrayList<>(Arrays.asList("Virat", "Rohit", "Dhoni"));
      // iterate over List using for loop
      System.out.println("using for loop: ");
      for(int a = 0; a < players.size(); a++)
      {
         System.out.println(players.get(a));
      }
      // iterate over List using enhanced for loop
      System.out.println("using for each loop: ");
      for(String str : players)
      {
         System.out.println(str);
      }
   }
}
Result for for and for each loops:
Virat Rohit Dhoni.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION