Friday, January 3, 2014

HCL Interview For Java

Please share the questions asked in HCL interview and any other interview.

1. Why String class is immutable?
Answer : Only String designer can answer this question. But I see following reasons behind the scene.
1. When you ask the OS to give you some memory to store data (like a string) that space can be anywhere in the memory address space...
So, let's assume you asked the OS to store a 10 character string (10 * 2 bytes = 20 bytes), so you have your 20 bytes and you place your String data in there.
Now the programmer thinks, let's place another character at the end of that 10 character string... So you ask the OS for another 2 bytes (a single character) but find that those two extra bytes are located at some generic and random location in memory.... Problem; you can't just have 10 characters at one place and the extra 1 at another... So to solve this each time you increase the size of a string the string is destroyed then the total length is requested from the memory pool.
Because internally a string is represented by a char array which can’t be resized! they have no choice but to make it immutable, except maybe for changing chars at different position in an array.
2. Which is the subclass of String?
3. Why Java is Object Oriented Language?
4. What is Program to Interface?
5) What is difference between JDK and JRE?
6) Generics in java?

JDK or the Java Development Kit contains JRE along with the tools like Java compiler, a Java interpreter, developer tools, debuggers, Java API libraries, and the documentation which can be used by developers to develop Java-based software/applications.

The JRE (Java Runtime Environment) is the subset of JDK. i.e JRE is an implementation of JVM which actually execute/run the programs.

JRE: Java Runtime Environment. It is basically the Java Virtual Machine where your Java programs run on. It also includes browser plugins for Applet execution.

JDK: It's the full featured Software Development Kit for Java, including JRE, and the compilers and tools (like JavaDoc, and Java Debugger) to create and compile programs.

Usually, when you only care about running Java programs on your browser or computer you will only install JRE. It's all you need. On the other hand, if you are planning to do some Java programming, you will also need JDK.

6) Integer Class is immutable or not?

7) Garbage Collection?
8) Java automatic type conversion?
9) HashMap is synchronized or not?

No comments: