Java Core Junior interview questions and answers

75 questions for a Java Core (Junior) interview - with correct answers, explanations and links to the docs.

  1. 1.What does the JVM do?
  2. 2.What's the difference between the JDK and the JRE?
  3. 3.Which of these is a primitive type in Java?
  4. 4.What is the default value of an int field in a class?
  5. 5.What does this code print?
  6. 6.Why are Java strings called immutable?
  7. 7.Why use StringBuilder when building a string in a loop?
  8. 8.What are the four main principles of OOP?
  9. 9.What is encapsulation?
  10. 10.What does the private modifier mean for a field?
  11. 11.What is a constructor?
  12. 12.What does static mean on a field?
  13. 13.What does final mean on a variable?
  14. 14.What is method overloading?
  15. 15.What is method overriding?
  16. 16.What does the @Override annotation give you?
  17. 17.Can a Java class extend two classes?
  18. 18.What is an abstract class?
  19. 19.What is an interface in Java?
  20. 20.What is polymorphism in this code?
  21. 21.What is the difference between ArrayList and LinkedList for get(i)?
  22. 22.Which collection keeps only unique elements?
  23. 23.What does a Map store?
  24. 24.What happens here?
  25. 25.What do generics like List<String> give you?
  26. 26.Why can't you write List<int>?
  27. 27.What is autoboxing?
  28. 28.What does this Integer comparison print?
  29. 29.What is the difference between checked and unchecked exceptions?
  30. 30.When does the finally block run?
  31. 31.What does try-with-resources do?
  32. 32.What causes a NullPointerException?
  33. 33.What is Optional for?
  34. 34.What's the contract between equals and hashCode?
  35. 35.What does toString() return by default?
  36. 36.What does a lambda expression replace?
  37. 37.What is a functional interface?
  38. 38.What does this stream return?
  39. 39.When does a stream pipeline actually run?
  40. 40.What does var do in var list = new ArrayList<String>();?
  41. 41.What is a record in Java?
  42. 42.What is the difference between == and equals for objects?
  43. 43.What does this refer to inside an instance method?
  44. 44.What does super let you do?
  45. 45.What is the main method signature that starts a classic Java program?
  46. 46.What is a package in Java?
  47. 47.What does import do?
  48. 48.What are Maven and Gradle used for?
  49. 49.What does mvn test do?
  50. 50.Which annotation marks a test method in JUnit 5?
  51. 51.What does @BeforeEach in JUnit 5 do?
  52. 52.What does assertEquals(expected, actual) check?
  53. 53.Why do many QA teams use AssertJ assertions?
  54. 54.What is TestNG?
  55. 55.What does @DataProvider in TestNG give you?
  56. 56.What is a mock object, e.g. from Mockito?
  57. 57.What does this loop print?
  58. 58.What does integer division 7 / 2 give in Java?
  59. 59.What happens when an int overflows?
  60. 60.What is the index of the first element of an array?
  61. 61.What is the difference between length, length() and size()?
  62. 62.What does the garbage collector do?
  63. 63.Can there be a memory leak in Java despite garbage collection?
  64. 64.What does switch with arrows do here?
  65. 65.What are text blocks (""") used for?
  66. 66.What is an enum?
  67. 67.What is a thread?
  68. 68.What problem can two threads incrementing the same counter cause?
  69. 69.What does synchronized on a method do?
  70. 70.How do you compare two strings ignoring case?
  71. 71.What does Comparable define?
  72. 72.What does this code print after calling concat?
  73. 73.What is the classpath?
  74. 74.What is JIT compilation?
  75. 75.What does Arrays.asList(...) return when you call add on it?