Mid Level Java Developer Questions — Job Interview

Bayram EKER
5 min readFeb 13, 2023

--

In this article we will answer MID LEVEL JAVA DEVELOPER job interview questions.

Mid-Level Java Developers

As a mid-level Java developer, you will have 2–5 years of professional experience and be highly knowledgeable about your business’s IT architecture. As your skills are more advanced than an entry-level Java developer, you will spend your work time:

  • Writing more complicated code
  • Fixing more advanced bugs
  • Performing (or overseeing) testing
  • Planning Java projects
  • Creating end-user-documentation
  • Working with vendors
  • Managing junior developers

We will answer:

  • What questions come up in a java developer job interview?
  • What should a java developer know ?
  • What are the critical questions in a job interview ?

Q-1 : Is Java pass-by-reference or pass-by-value?

Answer

Java is always pass-by-value. Unfortunately, when we pass the value of an object, we are passing the reference to it. There is no such thing as “pass-by-reference” in Java. This is confusing to beginners.

The key to understanding this is that something like

Dog myDog;

is not a Dog; it’s actually a pointer to a Dog.

So when you have

Dog myDog = new Dog("Rover");
foo(myDog);

you’re essentially passing the address of the created Dog object to the foo method.

Q-2: Is there anything like static class in Java?

Answer

Java has no way of making a top-level class static but you can simulate a static class like this:

  • Declare your class final — Prevents extension of the class since extending a static class makes no sense
  • Make the constructor private — Prevents instantiation by client code as it makes no sense to instantiate a static class
  • Make all the members and functions of the class static — Since the class cannot be instantiated no instance methods can be called or instance fields accessed
  • Note that the compiler will not prevent you from declaring an instance (non-static) member. The issue will only show up if you attempt to call the instance member

Q-3: What are the differences between == and equals?

Answer

As a reminder, it needs to be said that generally, == is NOT a viable alternative to equals. When it is, however (such as with enum), there are two important differences to consider:

  1. == never throws NullPointerException
enum Color { BLACK, WHITE };
Color nothing = null;
if (nothing == Color.BLACK); // runs fine
if (nothing.equals(Color.BLACK)); // throws NullPointerEx
  1. == is subject to type compatibility check at compile time
enum Color { BLACK, WHITE };
enum Chiral { LEFT, RIGHT };
if (Color.BLACK.equals(Chiral.LEFT)); // compiles fine
if (Color.BLACK == Chiral.LEFT); // DOESN'T COMPILE!!! Incompatible types!

Q-4: What do the ... dots in the method parameters mean?

Problem

What do the 3 dots in the following method mean?

public void myMethod(String... strings){
// method body
}

Answer

That feature is called varargs, and it’s a feature introduced in Java 5. It means that function can receive multiple String arguments:

myMethod("foo", "bar");
myMethod("foo", "bar", "baz");
myMethod(new String[]{"foo", "var", "baz"}); // you can eve

Then, you can use the String var as an array:

public void myMethod(String... strings){
for(String whatever : strings){
// do what ever you want
}
    // the code above is is equivalent to
for( int i = 0; i < strings.length; i++){
// classical for. In this case you use strings[i]
}
}

Q-5: What is a JavaBean exactly?

Answer

Basically, a “Bean” follows the standart:

  • is a serializable object (that is, it implements java.io.Serializable, and does so correctly), that
  • has “properties” whose getters and setters are just methods with certain names (like, say, getFoo() is the getter for the “Foo” property), and
  • has a public 0-arg constructor (so it can be created at will and configured by setting its properties).

There is no syntactic difference between a JavaBean and another class — a class is a JavaBean if it follows the standards.

Q-6: What is difference between fail-fast and fail-safe?

Answer

The Iterator’s fail-safe property works with the clone of the underlying collection and thus, it is not affected by any modification in the collection. All the collection classes in java.util package are fail-fast, while the collection classes in java.util.concurrent are fail-safe. Fail-fast iterators throw a ConcurrentModificationException, while fail-safe iterator never throws such an exception.

Q-7: What is structure of Java Heap?

Answer

The JVM has a heap that is the runtime data area from which memory for all class instances and arrays is allocated. It is created at the JVM start-up. Heap memory for objects is reclaimed by an automatic memory management system which is known as a garbage collector. Heap memory consists of live and dead objects. Live objects are accessible by the application and will not be a subject of garbage collection. Dead objects are those which will never be accessible by the application, but have not been collected by the garbage collector yet. Such objects occupy the heap memory space until they are eventually collected by the garbage collector.

Q-8: What is the JIT?

Answer

The JIT is the JVM’s mechanism by which it can optimize code at runtime.

JIT means Just In Time. It is a central feature of any JVM. Among other optimizations, it can perform code inlining, lock coarsening or lock eliding, escape analysis etc.

The main benefit of the JIT is on the programmer’s side: code should be written so that it just works; if the code can be optimized at runtime, more often than not, the JIT will find a way.

Q-9: Why Strings in Java are called as Immutable?

Answer

In java, string objects are called immutable as once value has been assigned to a string, it can’t be changed and if changed, a new object is created.

In below example, reference str refers to a string object having value “Value one”.

String str="Value One";

When a new value is assigned to it, a new String object gets created and the reference is moved to the new object.

str="New Value";

Q-10: What are the differences between vectors and arrays?

Answer

In Java, the main difference is that vectors are dynamically allocated. They contain dynamic lists of references to other objects and can hold data from many different data types. This means their size can easily change as needed. Arrays, on the other hand, are static. Arrays group data with a fixed size and a fixed primitive data type.

Q-11: Write a sample unit testing method for testing exception named as IndexOutOfBoundsException when working with ArrayList.

Answer:

@Test(expected=IndexOutOfBoundsException.class) 

public void outOfBounds() {
new ArrayList<Object>().get(1);
}

I hope the article was helpful. Don’t forget to follow to read our latest articles.

--

--

Responses (6)