Top 30+ Java GUI Interview Questions with Answers - 1

Question: 1

Does String is thread safe in Java?

Strings are immutable, so we can’t change its value in program.

Hence it’s thread safe and can be safely used in multi-threaded environment.

Question: 2

Can we use String in switch case?

Java 7 extended the capability of switch case to use Strings also, earlier versions doesn’t support this.

Question: 3

What are different ways to create String Object?

We can create String Object using new operators like any normal java class or we can use double quotes to create a String object.

There are several constructors available in String class to get String from char array, byte array, StringBuffer and StringBuilder.

Question: 4

What is the difference between StringBuffer and StringBuilder class?

Use StringBuilder whenever possible because it is faster than StringBuffer.

But, if thread safety is necessary then use StringBuffer objects.

Question: 5

What is Hash Code collision?

The idea of hashing is to store an element into array at a position index computed as below.

obtain element_hash_code of the element by processing the element’s data and generating an integer value.

use a simple mod operation to map into the array’s range: index = hash(element) = abs(element_hash_code % array_capacity)

The hash code collision occurs when the index calculated by hash function results same for two or more elements.

Related Questions