Top 10 Interview Questions & Answers for Java Developers

Top 10 Interview Questions & Answers for Java Developers
...

Ketkee

Last updated November 29, 2016


  • 3423 Views

Java is one of the most popular programming languages among developers. Its uncomplicated syntax and compatibility with all the major operating systems makes it an ideal choice. Today, there are more than 10 million developers who work with the Java language.

With competition this intense, organizations hiring Java developers are quite resolute in their hiring priorities. Only professionals with proven technical competencies in Java and who can design, code, build, and deploy any kind of applications are hired.

But no matter how great your coding skills, acing the interview is essential to land the Java role of your dreams.

We’ve compiled 10 of the top questions and model answers often asked at Java Developer interviews – read on to find out where you stand!

#1 Can you differentiate between J2SDK 1.5 and J2SDK 5.0?

Your answer: There is really no difference between J2SDK 1.5 and J2SDK 5.0. The versions have been rebranded by Sun Microsystems.

#2 What is the number of bits used to represent ASCII, Unicode, the UTF-16, and the UTF-8 characters?

Your answer: Unicode requires 16 bits.

ASCII requires 7 bits – however, it is usually represented as 8 bits.

UTF-8 presents characters in 8, 16, and 18 digit patterns.

UTF-16 requires 16 bit or larger patterns.

#3 Is it possible to import same package or class twice? Will the JVM load the package twice at runtime?

Your answer: It is possible to import the same package or class more than once. It will not have any effect on the compiler or JVM. The JVM will load the class more than once, irrespective of the number of times you import the same class.

#4 IS JVM an overhead?

Your answer: No and Yes. JVM is an extra layer that translates Byte code into Machine code. Java provides an additional layer of translating the Source code when compared to languages like C.

C++ Compiler – Source Code -> Machine Code

Java Compiler – Source Code -> Machine Code

JVM – Byte Code -> Machine Code

Though it looks like an overhead, this additional translational allows Java to run Apps on all platforms since JVM provides the translation to Machine code as per the underlying Operating System.

#5 There are two objects – a and b – with the same hashcode. I will insert two objects inside the hashmap.

hMap.put(a,a);

hMap.put(b,b);

where a.hashCode()==b.hashCode()

How many objects will be inside the hashmap?

Your answer: There can be 2 elements with the same hashcode. When two elements have the same hashcode, Java uses the equals to further differentiation. So there can be one or two objects depending on the content of the objects.

#6 Could you provide some implementation of a Dictionary having a large number of words?

Your answer: The simplest implementation that can be given is that of a List wherein one can place ordered words and perform a Binary search. The other implementation with a better search performance is HashMap where the key is used as the first character of the word and the value as a LinkedList.

Up another level, there are HashMaps like:

hashmap {

a ( key ) -> hashmap (key-aa , value (hashmap(key-aaa,value)

b ( key ) -> hashmap (key-ba , value (hashmap(key-baa,value)

z( key ) -> hashmap (key-za , value (hashmap(key-zaa,value)

}

Up to n levels where n is the average size of the word in the dictionary.

#7 What do you think the output of this code will be?

enum Day {

 MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY

}

public class BuggyBread1{

    public static void main (String args[]) {

     Set mySet = new TreeSet();

     mySet.add(Day.SATURDAY);

     mySet.add(Day.WEDNESDAY);

     mySet.add(Day.FRIDAY);

     mySet.add(Day.WEDNESDAY);

     for(Day d: mySet){

      System.out.println(d);

     }

    }

}

Your answer: WEDNESDAY

FRIDAY

SATURDAY

Only one FRIDAY will be printed since the Set doesn’t allow duplicated.

Elements will be printed in the order that the constants are declared in in the Enum. TreeSet maintains the elements in the ascending order which is identified by the defined compareTo method. The compareTo method in the Enum has been defined such that the constant declared later are greater than the constants declared prior.

#8 What is the difference between System.out, System.err, and System.in?

Your answer: System.out and System.err represent the monitor by default and thus can be used to send data or results to the monitor. System.out is used to display normal messages and results. System.eerr is used to display error messages. System.in represents InputStream object which by default represents standard input device, i.e., keyboard.

#9 How does the substring() method of String class create memory leaks?

Your answer: The substring method can build a new String object with reference to the while char array, to avoid copying it. Thus, you can inadvertently keep a reference to a very big character array with just one character string.

#10 Why is Char array preferred over String to store a password?

Your answer: String is immutable in Java and stored in the String pool. Once it is created it stays in the pool until the garbage is created making the password available in the memory. It is a security risk because anyone who has access to the memory dump can find the password as clear text.

So there you go. These 10 questions should prepare you well for your interview.

Good Luck!

About the Author

Ketkee is a Java evangelist and an influencer with more than a decade of experience in Java/J2EE technologies. She has led successful projects for Fortune 500 companies in the past and also comes with a rich background of web technology.


{{detail.h1_tag}}

{{detail.display_name}}
... ...

{{author.author_name}}

{{detail.full_name}}

Published on {{detail.created_at| date}} {{detail.duration}}

  • {{detail.date}}
  • Views {{detail.downloads}}
  • {{detail.time}} {{detail.time_zone_code}}

Registrants:{{detail.downloads}}

Downloaded:{{detail.downloads}}

About the On-Demand Webinar

About the Webinar

Hosted By

...

{{author.author_name}}

{{author.author_name}}

{{author.about_author}}

About the E-book

View On-Demand Webinar

Register Now!

First Name*
Last Name*
Email*
Company*
Phone Number*

View On-Demand Webinar

Register Now!

Webinar Expired

Download the Ebook

Email
{{ queryPhoneCode }}
Phone Number {{ detail.getCourseAgree?'*':'(optional)'}}

Show full article video

About the Author

{{detail.author_biography}}

About the Author

{{author.about_author}}