Top Theoretical Java Interview Questions for SDET

Beknazar
8 min readDec 10, 2023

--

Hi there! I put together the top theoretical Java interview questions for SDET or Test Automation roles.

Please consider taking my Udemy course Interview Preparation for SDET(Java) if you prefer video sessions with more detailed explanations.

What are JDK, JRE, and JVM?

Java Development Kit (JDK) — a set of tools necessary to write Java programs.

Java Runtime Environment (JRE) — allows to run compiled Java programs. It is part of the JDK.

Java Virtual Machine (JVM) — runs java-byte code. It is part of the JRE.

What does the Java compiler do?

Java compiler compiles Java source code into a java-byte code. Java-byte code is an intermediate language and not quite machine code.

What makes the Java platform independent?

Java is platform-independent because the Java compiler converts the source code to java-byte code, which is an intermediate language. Java-byte code can be then executed on any platform (OS) by using JVM. But you still need to install different JVMs for different platforms. The compilers are complex and expensive to build programs but JVMs are less complex and faster to build. So JVM makes the Java platform independent.

That’s why we have this famous slogan about Java

Write once, debug everywhere

oh no sorry :) the correct one is

Write once, run everywhere

What’s new in Java 8? Java 11?

Java 8:

  • Lambda Expressions.
  • Default methods in the interface.
  • Stream API to support functional-style operations on streams of elements.
  • A new set of packages that provide a comprehensive date-time model (local date times).

Java 11:

  • New String methods (isBlank, lines, strip, stripLeading, stripTrailing, and repeat).
  • New File methods (readString and writeString).
  • Collection to an Array (toArray method).
  • Support for using the local variable syntax (var keyword) in lambda parameters.
  • The new HTTP client from the java.net.http package was introduced in Java 9. It has now become a standard feature in Java 11.
  • A major change in this version is that we don’t need to compile the Java source files with javac explicitly anymore.

What are the primitives in Java?

In Java, we have in total eight primitive data types. The first group is byte, short, int, long. They can hold only whole numbers where a byte is the smallest one and long is the biggest. Next, we have float and double. They hold floating numbers. For single-character we have char primitive, and boolean to hold logical true or false value.

What’s the difference between String and StringBuilder?

String is immutable and StringBuilder is mutable.

When do you use String and when StringBuilder?

Every change of the String creates a new object in the memory because it is immutable. So when we have a code where we change our string often (especially inside the loop), it is better to use StringBuilder.

What’s StringBuffer?

A thread-safe version of StringBuilder.

How do you find a number of characters in the String?

By using length() method.

How to compare two Strings on equality?

By using equals() method.

What does == do when used with object references?

It checks if these two references point to the same object or not.

What are the wrapper classes in Java for primitives?

Each primitive in Java has corresponding object representations. Some Java features (collections, generics) work only with objects so when using them we can use wrapper classes. Additionally, these wrapper classes have many useful methods.

When do you use a while loop? And when do you use it for loop?

I use the while loop when I don’t know the exact number of iterations and I use the for loop when I know it.

What’s the difference between Array and ArrayList?

  • Array can work with primitives and objects but ArrayList works with objects only.
  • Array is a fixed-size collection and ArrayList has a dynamic size.

What’s Varargs in Java?

  • It gives the ability for the method to accept a dynamic number of arguments (or even an array).
  • It acts like an array inside the method and it can be used as an argument of the method only.

What’s a constructor in Java?

Constructor is a special method to create an object in Java. It has the same name as the class name and is executed during object creation. It can accept arguments and, usually, it is used to assign object properties.

Can you have overloaded constructors in the same class?

Yes, we can overload the constructor in the same class.

Can we override the constructor?

No, we cannot override the constructor.

What is the default constructor?

The default constructor is an empty constructor that is provided by Java if we don’t define any constructors. Once we define our customer constructor, java will not provide the default one.

What is a static keyword in Java?

We can tag our methods, class-level variables, and inner classes with static keyword. Those members with static keyword belong to the class and not to specific objects. The correct way of using static members is by class name. Usually, we make static our util methods and constant variables.

What’s the final keyword in Java?

We can tag our variables, methods, and classes as final:

  • we cannot change (reinitialize) our final variables.
  • we cannot extend our final classes.
  • we cannot override our final methods.

How to prevent your method from being overridden?

Make it final.

What’s a static block?

Static block runs one time in the very beginning when the class is first time referenced in the code. Usually, we use it to prepare/initialize our class.

What’s the initializer block?

The initializer block is a block of code that runs before the constructor call.

What OOP concepts do you know in Java?

  • Encapsulation
  • Inheritance
  • Polymorphism
  • Abstraction

What’s encapsulation in Java?

Encapsulation is a data-hiding mechanism. We achieve encapsulation by making our properties private and providing public getters and setters.

What’s inheritance in Java?

Inheritance is the ability of one class to inherit properties from another class. We can say it is the parent-child relationship between classes. Java supports single inheritance only.

What happens when a child class defines the same method or property?

  • When a child class defines the same method, it overrides the method of the parent class.
  • When a child class defines the same property, it shadows (hides) parent class property.

What is a super Object class? And why do we need it?

Object class is the parent class for all classes in Java. Java automatically injects extended Object syntax after every class declaration. Java wants to give generic behaviors for every object that ever will be created in Java. The java.lang.Object has 11 methods(Java 8) so every class will inherit these methods.

What’s an abstraction in Java?

Abstraction is the way we can design our code to make it more reusable by focusing on what an object does instead of how it does. We achieve abstraction with abstract methods.

What’s polymorphism in Java?

It is an ability of an object to take many different forms.

What’s the difference between interface and abstract class?

Both interface and abstract class are used to achieve abstraction in Java, but there are some differences between them:

  • we can define abstract and non-abstract methods in the abstract class but we can define only abstract methods in the interface (except static and default methods).
  • we can have regular property variables in the abstract class and only public static final contacts in the interface.
  • we can implement multiple interfaces but we can extend only one abstract class.

What’s the difference between overriding and overloading?

Overriding happens when we override the parent class method in the child class. We override by defining exactly the same method (the same name, arguments, the same or coherence return type, the access modifier should be the same or more visible, and the exception declaration should be the same or smaller).

Overloading happens in the same class when we define multiple methods with the same name but different arguments.

How to get a number of elements in the array?

By using length instance variable.

How to get a number of elements in ArrayList?

By using size() method.

How does ArrayList work internally?

It is based on an array internally.

What’s the initial capacity of ArrayList?

The initial capacity of ArrayList is 10.

What’s Set in Java?

It is a collection of unique elements. It has three implementations: 1) HashSet 2) LinkedHashSet 3) TreeSet

What’s Map in Java?

It is a key-value data structure. It has three implementations: 1) HashMap 2) LinkedHashMap 3) TreeMap

How do you iterate over the map?

We can iterate over the map using entrySet() method or keySet() method.

What’s the difference between HashMap and Hashtable?

  • Hashtable doesn’t allow a null key and null value whereas HashMap can have one null key and multiple null values.
  • Hashtable is thread-safe and HashMap is not thread-safe.

What’s the helper class for arrays (for sorting and searching)?

Arrays is a helper class for arrays.

java.util.Arrays

What’s the helper class for the collection framework in Java (for sorting and searching)?

Collections is a helper class for collection framework classes.

java.util.Collections

How do you handle exceptions in Java?

We handle exceptions in Java by using a try-catch statement.

What’s the difference between runtime and checked exceptions?

Runtime exceptions are optional to handle and checked exceptions are a must to handle.

How do you know if some exception is checked or runtime?

Checked exceptions extend from the Exception class and runtime exceptions extend from RuntimeException class

Can we handle multiple exceptions with the same try-catch?

Yes, we can.

What is try with resources?

It is a try statement that automatically closes resources (File, DB connection, etc).

Can we have a try without a catch statement?

Yes, try with resources can be declared without a catch statement.

What is an Error in Java?

Error is a system-level failure. We don’t handle and throw errors.

Thank you for your attention. Please consider taking my online courses:

--

--

Beknazar
Beknazar

No responses yet