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