Sunday, September 22, 2019

JAVA Interview Questions- Core Java + General Questions

1. Describe SOLID principles.
2. Define some Design Patterns you have worked upon?
3. Difference between green thread and native thread in Java?
Green threads refers to a model in which the Java virtual machine itself creates, manages, and context switches all Java threads within one operating system process. No operating system threads library is used.
Native threads refers to a in which the Java virtual machine creates and manages Java threads using the operating system threads library – named libthread on UnixWare – and each Java thread is mapped to one threads library thread.

4. Why String is special in JAVA?
5. What are immutable classes and how to make a class immutable.
6. Why other classes are not Immutable?
7. Does static break encapsulation in Java?

static breaks encapsulation rule because it is possible to access it without settters or getters but directly. If you declare a variable like this : public static or static, when calling it from another class you can write : object.variable = value;.

8. Where are the variables of interface getting stored.

9. what is the difference between : String s="sid" and String s=new ("Sid");

Both expression gives you String object, but there is subtle difference between them. When you create String object using new() operator, it always create a new object in heap memory. On the other hand, if you create object using String literal syntax e.g. "a", it may return an existing object from String pool (a cache of String object in Perm gen space, which is now moved to heap space in recent Java release), if it's already exists. Otherwise it will create a new string object and put in string pool for future re-use.

Also :

new String("text"); explicitly creates a new and referentially distinct instance of a String object; String s = "text"; may reuse an instance from the string constant pool if one is available.

You very rarely would ever want to use the new String(anotherString) constructor. From the API:

String(String original) : Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since strings are immutable.

String s = "a";
String q = "a";
String w = new String("a");
String w1 = new String("a");

System.out.println(s == q);
System.out.println(s == w);
System.out.println(w1 == w);

Output :
true
false
false

w/w1 is then lost (eligible for GC) if later unused. String literals on the other hand are reused.

10 What and where is string constant pool? where are int stored in memory.

11. How is + overrided for string in java? ex : StringBuffer s="hi".append(s1).append(s2); and
String x=s+s1+s2? how many objects are being created in both cases.

Strings computed by constant expressions are computed at compile time and then
treated as if they were literals.
Specs : here

String s="Sachin"+" Tendulkar";
So in the case you have specified only one String literal will be created(created at compile time itself) and that is "SachinTendulkar". So there will be only one interned String in the String pool.

In case you try to concatenate separate explicit literals then only you will have separate interned Objects in the String pool. Eg.

String s1 = "Sachin";
String s2 = "Tendulkar";
String s3 = s1 + s2;
In above case you will have 3 different interned Objects in String pool.


12. Level Order traversal?
13. Improvements in hashmap in java 8?
14. treeify time complexity.
15. What is better HashSet or HashMap for getting distinct list of items? Consider Hashset internally implements HashMap.
16. How does Spring work internally?
17. Memory management in Java 8.
18 Give an example of Memory Leak and Out Of memory error.
19 Java is Pass by Value or Pass by reference.
Java is Pass by Value

20. Stored Areas Of Static, Instance & Local Variables

class HackTheJava
{
    static int i=0;     //Static Variable
    int j=0;            //Instance Variable
   
    void properties()
    {
        int k=0;        //Local Variable
        return ;
    }
}

All static & instance variables are stored in PermGen space of heap memory. If variables are primitive type then its value as well stored with them as name-value pair. But if variable is user-defined(objects) then its reference is stored in PermGen as name-reference pair but actually it is stored in other heap spaces like Young/Old Generation.

But Local Variables are stored in Stack of its corresponding thread. Similarly method calls are also stored in stack. So each thread has separate copy of local variables.Know more about Memory Allocation.


No comments:

Post a Comment