The following post is a collection of Java knowledge-testing questions that I happened to encounter during several recruitment processes over the last few years. I thought it would be worthwhile to write down and share those among them which turned out to be somewhat tricky.
Below each question, I’ve listed the correct answer. I encourage you to test the provided solutions yourself in order to understand and remember them well.
List of questions
- Can a constructor be private?
- What changes have been made in Java 8?
- Which Java collection stores unique values?
- What is the difference between an abstract class and an interface?
- What is the difference between
==, and.equals()? - Can a private method be overridden?
- In Java, what is the difference betweenoverriding andoverloading?
- Explain what a String Pool is
- Checked, unchecked exceptions – what’s the difference?
- Must each block
trybe followed by a blockcatch? - What is the difference between a parameter and an argument?
List of answers
Can a constructor be private?
The correct answer is: Yes, a constructor can be private, i.e., marked with the private access modifier. This approach allows control over the creation of new instances of the class. Private constructors are used, for example, in conjunction with the Factory Method pattern or the Singleton pattern (which is arguably an anti-pattern).
Below is an example:
public class Car {
private Car() {}
public static Car getCarInstance() {
return new Car();
}
public void showMessage() {
System.out.println("Just some message to print anything");
}
}public class Main {
public static void main(String[] args) {
Car.getCarInstance().showMessage(); // "Just some message to print anything"
}
}What changes have been made in Java 8?
The answer: there are, among others:
- lambda expressions
- stream API (streams)
- default methods in interfaces
- optional values
Which Java collection stores unique values?
Answer: It is Set, which is a collection that contains only unique elements. Sets are implementations of the Set interface and guarantee that each element is stored only once. This means that attempting to add a duplicate element to the set will not succeed.
Set<String> fruits = new HashSet<>();
// Add elements to the set
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Apple"); // Duplicate - will be ignored
// Check the size of the set
System.out.println("Number of fruits: " + fruits.size()); // 3What is the difference between an abstract class and an interface?
The most important differences are:
- An abstract class is a class that can contain both abstract methods and regular methods with implementation. Traditionally, an interface could only have abstract methods (but since Java 8, it has been possible to create default and static methods within interfaces, which contain method implementations).
- A class can only inherit from one abstract class, but can implement multiple interfaces.
- An abstract class can contain
final, non-final, static i non-staticvariables. - Abstract class methods can have access modifiers such as
public, private, protected,andstatic, whereas interface methods arepublicby default, and no other access modifiers can be used there - An abstract class can implement an interface, but an interface cannot implement an abstract class.
What is the difference between ==, and .equals()?
Simply put, == compares references, i.e., it checks if both object variables point to the same memory location. In contrast, .equals() compares the objects’ values. An example is provided below:
String s1 = "HELLO";
String s2 = "HELLO";
String s3 = new String("HELLO");
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // trueCan a private method be overridden?
The answer is: No, a method marked with the private modifier cannot be overridden because it is not visible to any other class.
In Java, what is the difference between overriding and overloading?
Correct answer: When the method signature (name and parameters) is the same in the parent class and the child class, it’s called overriding. When two or more methods in the same class have the same name but different parameters, it’s called overloading. Below, I’ve provided examples of both these concepts:
Overloading:
class MathOperations {
// Overloaded method that adds two integers
int add(int a, int b) {
return a + b;
}
// Overloaded method that adds three integers
int add(int a, int b, int c) {
return a + c + b;
}
}
public class Main {
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println(math.add(2, 3)); // 5
System.out.println(math.add(2, 3, 4)); // 9
}
}
Overriding:
// Parent class
class Animal {
// Method to be overridden in the child class
void makeSound() {
System.out.println("Animal makes a sound");
}
}
// Child class
class Dog extends Animal {
// Overriding the makeSound method from the parent class
@Override
void makeSound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound(); // "Dog barks"
}
}Explain what the String Pool is
The String Pool, also known as the String Intern Pool, is a special memory area managed by the JVM that is used for storing unique String objects. The main purpose of the String Pool is memory optimization by storing only one copy of each immutable string.
Checked, unchecked exceptions – what’s the difference?
This is actually one of the more common questions in job interviews, but it’s worth reviewing.
Checked exceptions are checked by the compiler at compile time. This means the programmer must handle them; otherwise, the program will not compile. Example:

We see here that the compiler requires us to handle the possibility of the file not being present at the specified location. When we wrap the code in a try-catch block (or when we add the exception to the method signature using throws), the compiler no longer reports an error:

The situation is different with unchecked exceptions. These are exceptions that typically result from programming errors – e.g., division by zero. They are not checked at compile time, and the compiler does not require us to handle them. Example:

Above, you can see how attempting to call the length() method on a null reference causes a NullPointerException to be thrown. Wrapping this code snippet in a try-catch block allows the exception to be handled:

Is it necessary to put catch statements after a try block?
The answer is no, it doesn’t have to. Although the try-catch block is often used together for exception handling, there are scenarios where catch is not required. An example is using a finally block after the try block; this finally block will always execute, regardless of whether an exception occurred or not:
int[] numbers = {1, 2, 3, 4, 5};
try {
System.out.println(numbers[5]); // Calls ArrayIndexOutOfBoundsException
} finally {
System.out.println("The finally block will always be executed");
}What is the difference between a parameter and an argument?
Correct answer: Parameter and argument, in the context of Java methods, are two concepts that are often confused. A parameter is a variable declared in the method definition and is used to accept a value when the method is called. It can be thought of as a placeholder for the value that will be passed later.
public int addSomeNumbers(int a, int b) {
return a + b;
}In the above case, a and b are the parameters of the addSomeNumbers method .
If we call this method:
addSomeNumbers(3, 4);This 3 and 4 will become the arguments of the method. Simple, right? 🙂