Java Phone Interview Questions - Beginner Level

Java Phone Interview Questions - Beginner Level

·

8 min read

Do you have a phone interview coming up soon? This article can help you!

I will cover common beginner-level phone interview questions, and I will show you how you can best address these questions to increase your chances of succeeding at the interview.

Related: Java Interview Questions

What is a class?

A class is a set of instructions used to create a specific object. A class describes what an object will be. However, it is NOT the object itself.

personClass.png

What is an object?

An object is an entity that encapsulates data (attributes or states) and actions (behaviour).

An example of an object can be a Person object with a name as an attribute and an action such as walking.

An object doesn't always have to be something physical. It can be something invisible (for example, a bank account).

What is object-oriented programming?

When answering this question, don't be afraid to reply extensively.

Object-oriented programming is a paradigm in which we use classes and objects to organize software.

The four key pillars of object-oriented programming are:

  • Inheritance: when a class (child/subclass) inherits the attributes and behaviours of another class (parent/superclass). Inheritance is possible through the extends keyword. For example, we have a person superclass and a student subclass.

  • Polymorphism: when a class can be inherited and overridden.

  • Encapsulation: also known as data hiding. It's the principle by which we combine attributes and behaviour in a class.

  • Abstraction: when we hide the implementation details and leave the functionalities visible to the user.

What's the difference between method overloading and method overriding?

To answer this question, it's best to:

  • Explain method overloading conceptually and then provide an example.

  • Explain the method overriding conceptually and then provide an example.

  • In the end, compare both.

With method overloading, you have methods with the same name but different parameters.

int addNumber(int number)
float addNumber (float number)
double addNumber(double number, double number1, double number2)

With method overloading, you have a subclass method with the same signature as the parent class.

public class Animal() {

void sound() {
System.out.println("Animals make a sound");
}

class Dog extends Animal {

void sound() {
System.out.println("Animals make a sound");
}
}
}

In method overloading, methods have the same name but different argument lists (this is a must!). The return type may or may not be different. It happens in the same class. Method overloading is how we achieve compile-time polymorphism.

In method overriding, methods have a "personalized" implementation already provided by the parent class. As mentioned earlier about superclass and subclass, method overriding only happens when there is an inheritance relationship between two classes.

Method overriding is how we achieve run-time polymorphism. The return type must be the same or covariant.

Does Java support multiple inheritance?

The answer to this question is no. Extend the answer by explaining the Diamond problem and what you would do to address multiple inheritance.

Java doesn't support a class inheriting properties from more than one class.

To solve this problem, we can use default methods, which have been introduced in Java 8.

Before Java 8, interfaces could only have methods with no implementation.

From Java 8, you can have a method with implementation. There's no need to override these methods.

What does static keyword mean?

Explain the concept and then provide an example.

Static is a keyword (meaning that it has a special meaning for the compiler), meaning that members of a class (a variable or a method) belong to the class itself.

You don't need to create an object to access a class member. When you declare a class member as static, only a single instance of that member gets created and distributed among all objects. You don't need to use the new keyword normally used to create an object to access that static member.

Please look at one of my previous articles to learn more about the static keyword.

What does final mean?

When addressing this question, specify that a final member cannot be reassigned but is still mutable.

Final is a keyword that can be applied to variables, methods and classes.

Variables: a final variable becomes a constant.

final double PI = 3.14;

Methods: a final method is a method that you cannot override.

public class Circle {

    final double PI = 3.14;

     final double calculateArea(int radius) {
        double areaOfCircle = PI  * radius * radius;
        return areaOfCircle;
    }
}

Class: a final class is a class that you cannot override. It gives a compilation error.

image.png

What does "thread safety" mean?

Thread is an advanced topic, but it's good to have a basic answer so you don't lose "points".

Thread safety is the code’s ability to deal with and handle concurrent/multi-threaded access to resources.

It's also good to know that a hash map is NOT thread-safe. An alternative would be to use a ConcurrentHashMap.

How do you handle exceptions in Java?

This question requires you to discuss try/catch block, checked and unchecked exceptions.

A try/catch is a block split into two sections:

  1. The try block, where you write the code that may or may not throw an exception.

  2. The catch block is the block that will handle the exception.

Other questions that the interviewer may ask you are:

Can a try/catch block have multiple catch blocks?

The answer is YES.

Something like this can exist:

try {

} catch {

} catch {

}

Can you have a try without a catch?

The answer is YES. You can have a try block without a catch block if you add a finally block.

try {

} finally {

}

The finally block executes regardless of whether there will be an exception thrown or not.

It's also worth knowing about checked and unchecked exceptions.

A checked exception is an exception that happens during compile-time. You can do two things to handle checked exceptions:

  • Enclose the code within a try/catch block
    public void checkedException() {
        File file = new File("This_File_Does_Not_Exist.txt");

        try {
            FileInputStream fileInputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
  • Use the throws keyword in the method signature:
    public void checkedException() throws FileNotFoundException {
        File file = new File("This_File_Does_Not_Exist.txt");

        FileInputStream fileInputStream = new FileInputStream(file);
    }

An unchecked exception is an exception that says there is something wrong with the logic of the program. A popular example is when you try to divide a number by zero.

Common examples of unchecked exceptions are:

  • NullPointerException: when an object points to nothing.

  • ArrayIndexOutOfBounce: when you try to access an element out of the scope of the array

  • IllegalArgumentException: when you pass an illegal or inadequate argument to a method.

How come Strings are immutable in Java?

"Immutable" means that when you create a String, you won't be able to change it. Instead, you're going to create a new one.

Let's look at this scenario:

    public static void main(String[] args) {

        String laptop = "MacBook";

        System.out.println("Printing " + laptop);

        laptop.concat(" Pro");

        System.out.println(" ' Pro' is not printed " + laptop);

        laptop = laptop.concat(" Pro");

        System.out.println("Printing full laptop name " + laptop);

     }

The result is:

Printing MacBook
 ' Pro' is not printed MacBook
Printing full laptop name MacBook Pro

immutableString.png

The reasons why a String is immutable are:

  • String pool: with immutability, we save memory in the heap. The compiler will check if a String literal already exists in the pool. If it does, we match the reference of the already existing String and avoid creating a new object.

  • Thread safety: A string is unchangeable. This means that different threads can use a String without synchronization.

  • Caching: a String generates a hashcode stored at the time of creation.

  • Security: We always use String, for example, when creating a new password. If a String is mutable, it will pose a major security threat.

How does an interface work?

To address this question, explain what an interface is. Give an example and explain in which context you would use one. Mention that interfaces use the implements keyword instead of extends (used with inheritance).

An interface is a fully abstract class. This means that not all the interface methods have a body (also known as implementation).

To benefit from an interface, we need to implement it. This means that you'll have to use the implements keyword to create a class where the methods will have a body.

public interface Animal {

    public void makeSound();

    class Dog implements Animal {

        @Override
        public void makeSound() {
            System.out.println("Uof Uof!");
        }
    }
}

An interface can implement multiple interfaces.

An interface can extend another interface.

Interfaces are great because they allow you to define the specifications that a class has to follow.

Please note that with Java 8, an interface can have methods with an implementation. These methods are known as default methods.

Conclusion

These are some beginner-level Java phone interview questions.

Have you ever been asked any of these questions during an interview? Let me know in the comments below.

Until next time, and good luck!

🙋🏾‍♀️

ADDITIONAL RESOURCES:

Did you find this article valuable?

Support Maddy by becoming a sponsor. Any amount is appreciated!