5 Crazy Java Interview Questions

92

Going for an interview can be a great pain. Here are the crazy 5 Java questions that is very uncommon outside and also experienced by the developer in our team itself. Of course you can also browse the Java posts in this blog to familiar yourself with some Java syntax and changes.

java black background

Java Questions

Differences between ArrayList & Vector?

ArrayList is not synchronized while a Vector is synchronized thus taking more space.

Vectors double the size of when adding new elements while ArrayList adds another 50% more.

Adding Elements While Iterating ArrayList?

In general, it is not possible, but it is possible with manual. I went for an interview. The interviewer actually said it will throw an error ConcurrentModificationException if we were to add into the list while iterating.

List<Integer> test = new ArrayList<>();

test.add(2);
test.add(3);
test.add(5);
test.add(5);
test.add(6);
test.add(7);


for (int i = 0; i < test.size(); i++) {
	test.add(8);
	
	if (i == 50) {
		break;
	}
	
	test.remove(test.get(i).equals(5) ? test.remove(i) : "");
}

The above code shows that it is possible to add and remove entries while looping in the ArrayList. This can only be done on a random access List and ArrayList is random access.

That is false and the interviewer would perhaps never know that she/he is wrong. Java has evolved over time.

laptop work

Catch Exception?

For Java, here are some syntax.

try {
    ...
} catch (Exception ex) {
    ...
}

If you are given this question, the above would run without any errors. Of course, this is basically a test to see your coding practice. In particular companies, writing a try catch with the all catch Exception is not encouraged.

The reason behind it is to be specific. Do not catch everything under a general Exception. If it’s an IOException, do catch that exception instead.

Interface Test

public interface MainTest {
    System.out.println(1);
}

public class Test1 implements MainTest {
    Test1() {
        System.out.println(2);
    }
}

public class Test2 {
    Test2() {
        Test1 test = new Test1();
    }
}

The above looks just like every normal code. Though there is something you have to notice.

An Interface class cannot have any syntax other than method name or variable. So that means that this code would not even compile. Most of the time, the question misleads you by putting in many other things. Crazy!!

Inheritance in Java?

public class A {
    A() {
        System.out.println(1);
    }
}

public class B extends A {
    B() {
        System.out.println(2);
    }
}

public class C {
    B b = new B();
}
// Question: What would be the output if we call C?

This is another trick question. Commonly we do not usually see people writing constructors in their code in most of the companies. This is however another trick where the answers are

  • 1
  • 2
  • 2
    1
  • Throws an error

The correct answer for this is 2. Even though it is extending another class, parent class was not called at all. Crazy!!

Bonus Question: Method Overriding?

public class A {
    public void test() {
        System.out.println(1);
    }
}

public class B extends A {
    public void test() {
        System.out.println(2);
    }

    public static void main(String[] args) {
        A a = new B();
        a.test();
    }
}

// What is the result if we call a.Test()

As usual another tricky question in Java. If you are not sure about your Java code, do read up about OOP (Object Oriented Programming) concepts. By calling a.test() it would only print 2.

Many people would be confused with the declaration of A a = new B();

Method overriding is all about having the same method name, arguments and return, but it will only call the method which was declared.

Frustrated

Last Words

These are the kind of tricky questions you hardly see on websites as these are usually questions being tested first handed. Of course if you would like to brush up your knowledge in programming be it in Java or any other languages, there are many websites which can help a person to be more prudent in that language.

Leave a Reply