JAVA Interview Questions

Rashmi Bhardwaj | Interview Questions,Programming & Software
Google ADs

ANSWERS @https://ipwithease.com/shop/

  1. Difference between Array and Array List.
  2. What is the result of the following class?
    1: public class _C {
    2: private static int $;
    3: public static void main(String[] main) {
    4: String a_b;
    5: System.out.print($);
    6: System.out.print(a_b);
    7: } }
  3. What is the result of the following code?
    String s1 = “Java”;
    String s2 = “Java”;
    StringBuilder sb1 = new StringBuilder();
    append(“Ja”).append(“va”);
    System.out.println(s1 == s2);
    System.out.println(s1.equals(s2));
    System.out.println(sb1.toString() == s1);
    System.out.println(sb1.toString().equals(s1));
  4. Differences between HashMap and HashTable in Java.
  5. What is meant by Collections in Java?
  6. What is the output of the following code?
    1: interface HasTail { int getTailLength(); }
    2: abstract class Puma implements HasTail {
    3: protected int getTailLength() {return 4;}
    4: }
    5: public class Cougar extends Puma {
    6: public static void main(String[] args) {
    7: Puma puma = new Puma();
    8: System.out.println(puma.getTailLength());
    9: }
    10:
    11: public int getTailLength(int length) {return 2;}
    12: }
  7. What is the output of the following program?
    1: public class FeedingSchedule {
    2: public static void main(String[] args) {
    3: boolean keepGoing = true;
    4: int count = 0;
    5: int x = 3;
    6: while(count++ < 3) {
    7: int y = (1 + 2 * count) % 3;
    8: switch(y) {
    9: default:
    10: case 0: x -= 1; break;
    11: case 1: x += 5;
    12: }
    13: }
    14: System.out.println(x);
    15: } }
  8. What is the output of the following code snippet?
    1: System.out.print(“a”);
    2: try {
    3: System.out.print(“b”);
    4: throw new IllegalArgumentException();
    5: } catch (RuntimeException e) {
    6: System.out.print(“c”);
    7: } finally {
    8: System.out.print(“d”);
    9: }
    10: System.out.print(“e”);
  9. What is the result of the following program?
    1: public class MathFunctions {
    2: public static void addToInt(int x, int amountToAdd) {
    3: x = x + amountToAdd;
    4: }
    5: public static void main(String[] args) {
    6: int a = 15;
    7: int b = 10;
    8: MathFunctions.addToInt(a, b);
    9: System.out.println(a); } }
  10. What is the result of the following code?
    1: int[] array = {6,9,8};
    2: List<Integer> list = new ArrayList<>();
    3: list.add(array[0]);
    4: list.add(array[2]);
    5: list.set(1, array[1]);
    6: list.remove(0);
    7: System.out.println(list);
  11. What is the output of the following code?
    1: public class Deer {
    2: public Deer() { System.out.print(“Deer”); }
    3: public Deer(int age) { System.out.print(“DeerAge”); }
    4: private boolean hasHorns() { return false; }
    5: public static void main(String[] args) {
    6: Deer deer = new Reindeer(5);
    7: System.out.println(“,”+deer.hasHorns());
    8: }
    9: }
    10: class Reindeer extends Deer {
    11: public Reindeer(int age) { System.out.print(“Reindeer”); }
    12: public boolean hasHorns() { return true; }
    13: }
  12. Which exceptions should be caught?
  13. Which objects are ready for garbage in the following code?
    1: import java.util.*;
    2: public class Grasshopper {
    3: public Grasshopper(String n) {
    4: name = n;
    5: }
    6: public static void main(String[] args) {
    7: Grasshopper one = new Grasshopper(“g1”);
    8: Grasshopper two = new Grasshopper(“g2”);
    9: one = two;
    10: two = null;
    11: one = null;
    12: }
    13: private String name; }
  14. What is the output of the following program?
    1: public class FeedingSchedule {
    2: public static void main(String[] args) {
    3: int x = 5, j = 0;
    4: OUTER: for(int i=0; i<3; )
    5: INNER: do {
    6: i++; x++;
    7: if(x > 10) break INNER;
    8: x += 4;
    9: j++;
    10: } while(j <= 2);
    11: System.out.println(x);
    12: } }
  15. What is the result of the following program?
    1: public class Egret {
    2: private String color;
    3: public Egret() {
    4: this(“white”);
    5: }
    6: public Egret(String color) {
    7: color = color;
    8: }
    9: public static void main(String[] args) {
    10: Egret e = new Egret();
    11: System.out.println(“Color:” + e.color);
    12: }
    13: }
  16. What is the output of the following program?
    1: public class BearOrShark {
    2: public static void main(String[] args) {
    3: int luck = 10;
    4: if((luck>10 ? luck++: –luck)<10) {
    5: System.out.print(“Bear”);
    6: } if(luck<10) System.out.print(“Shark”);
    7: } }
  17. Assuming we have a valid, non-null HenHouse object whose value is initialized by the
    blank line shown here, which of the following are possible outputs of this application?
    1: class Chicken {}
    2: interface HenHouse { public java.util.List<Chicken> getChickens(); }
    3: public class ChickenSong {
    4: public static void main(String[] args) {
    5: HenHouse house = ______________
    6: Chicken chicken = house.getChickens().get(0);
    7: for(int i=0; i<house.getChickens().size();
    8: chicken = house.getChickens().get(i++)) {
    9: System.out.println(“Cluck”);
    10: } } }
  18. Which of the following statements can be inserted in the blank line so that the code will
    compile successfully?
    public interface CanSwim {}
    public class Amphibian implements CanSwim {}
    class Tadpole extends Amphibian {}
    public class FindAllTadPole {
    public static void main(String[] args) {
    List<Tadpole> tadpoles = new ArrayList<Tadpole>();
    for(Amphibian amphibian : tadpoles) {
    ___________ tadpole = amphibian;
    } } }
    • What individual changes, if any, would allow the following code to compile?

    1: public interface Animal { public default String getName() { return null; } }
    2: interface Mammal { public default String getName() { return null; } }
    3: abstract class Otter implements Mammal, Animal {}

    • Which of the following lines can be inserted at line 11 to print true?

    1: public static void main(String[] args) {
    2: // INSERT CODE HERE
    3: }
    4: private static boolean test(Predicate<Integer> p) {
    5: return p.test(5);
    6: }

  19. What is the print out a date representing April 1, 2015?
  20. Bytecode is in a file with which extension?
  21. Name any two checked exceptions?
  22. What are the OOP features?
  23. What is Abstraction?
  24. What is Encapsulation?
  25. What is Inheritance?
  26. What is Polymorphism?
  27. What is Object?
  28. What is a Constructor?
  29. What is meant by Local variable and Instance variable?
  30. What is meant by Method Overriding?
  31. What is meant by Overloading?
  32. What is meant by Interface?
  33. Difference between Abstract class and Interface.
  34. What is meant by Exception?
  35. What are the types of Exceptions?
  36. Explain about Exception Propagation.
  37. What is the final keyword in Java?
  38. What is a Thread?
  39. How do you make a thread in Java?
  40. How are Java objects stored in memory?
  41. What is static variable in Java?
  42. What is object cloning?
  43. Why is String Immutable in Java?
  44. What is the size of int in 64-bit JVM?
  45. What is the difference between JRE, JDK, JVM and JIT?
  46. What’s the difference between “a == b” and “a.equals(b)”?
  47. What is a hashCode() used for? How is it related to a.equals(b)?
  48. Difference between final, finalize and finally?

ANSWERS @https://ipwithease.com/shop/

ABOUT THE AUTHOR


Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart