Java Interview Question

Junior/Entery Java Interview Questions

public class HelloWorld {

   public static void main(String[] args) {

       System.out.println("Hello, World!");

   }

}

String s1 = new String("Hello");

String s2 = new String("Hello");

System.out.println(s1 == s2); // false (different memory locations)

System.out.println(s1.equals(s2)); // true (same content)



import java.util.ArrayList;

import java.util.LinkedList;


public class ListExample {

   public static void main(String[] args) {

       ArrayList<Integer> arrayList = new ArrayList<>();

       LinkedList<Integer> linkedList = new LinkedList<>();


       // Adding elements to ArrayList

       arrayList.add(1);

       arrayList.add(2);

       arrayList.add(3);


       // Adding elements to LinkedList

       linkedList.add(1);

       linkedList.add(2);

       linkedList.add(3);


       System.out.println("ArrayList: " + arrayList);

       System.out.println("LinkedList: " + linkedList);

   }

}


public class Calculator {

   public int add(int a, int b) {

       return a + b;

   }


   public double add(double a, double b) {

       return a + b;

   }

}



class Animal {

   public void sound() {

       System.out.println("Animal makes a sound");

   }

}


class Dog extends Animal {

   @Override

   public void sound() {

       System.out.println("Dog barks");

   }

}


Java Interview Questions Doc

Java Interview .docx

Advanced Java Interview Questions

public class Singleton {

 private static final Singleton instance = new Singleton();

 

 private Singleton() {}

 

 public static Singleton getInstance() {

 return instance;

 }

}