Selected interview questions for experiance developers.
How String became immutable?
It's non-changing behaviour,
means, the value once assigned cannot be updated in any other way.
String class
internally holds data in character array.
String class is
created to be immutable.
It would be mutable if
you can update the character array which is behind the scene in String class.
But in reality, that array will be initialized once and throughout the program
it remains the same.
Why
StringBuffer is mutable?
StringBuffer class is mutable itself as you can update it's stated directly.
Similar to String it also holds value in
character array and you can manipulate that array by different methods i.e. append, delete, insert etc.
which directly changes the character value array.
Can use super keyword in constructor?.
Yes
Can be called static this.method?.
Yes, But we can not use
without static variable in the static method.
String literal:
String a = "abc";
String b = "abc";
System.out.println(a == b); // true
String Object
String c = new String("abc");
String d = new String("abc");
System.out.println(c == d); // false
Volatile variable?
Volatile
Keyword in Java. Volatile keyword is used to modify the value of a variable by different threads.
It is also used to make classes
thread-safe. It means that multiple threads can use a method and instance of
the classes at the same time without any problem.
Object class methods?
toString(), clone(), equals(),
finilize(), hashcode()
Can an abstract class create an instance?
No, The abstract class can not create
an object because the abstract class is incomplete and containing an abstract method
without body.
What is the thread?
Thread is a lightweight
sub-process. It can execute multiple processes without interrupting the user.
This is used two types:- By Extends
and by implements
Wrapper class?
Wrapper class provide the
mechanism converting primitive to object
and object to primitive.
Autoboxing
Int in=0;
Integer i= Integer.valueof(ii)
Integer i1=in// autoboxing
compiler internally working Integer.valueof(in)
Unboxing
Integer in=new Integer(10)
Int i=in.intvalue();
Int i=in; // unboxing compiler
internally work as in.intvalue()
Aggregation in java?
Code reusability is the best for
aggregation. Aggregation is represented Has-a relationship.
Constructor?
Constructor(s)
of a class must have the same name as the class name in which it resides.
A
constructor in Java can not be abstract, final, static and Synchronized.
Access
modifiers can be used in constructor declaration to control its access i.e
which other class can call the constructor
How constructors are different from methods in Java?
·
Constructor(s) must have the same name as the class within which it
defined while it is not necessary for the method in java.
·
Constructor(s) does not return any type while method(s) have the return
type or void if does not return any value.
·
Constructor is called only once at the time of Object creation while
method(s) can be called any numbers of time.
Iterator in java?
Iterator is an interface and this
is a part of the collection. It is used for traverse the list data. It is containing
many method like next(), hashNext() and remove().
Next() the method is returning an object like element
hashNext() the method is returning Boolean
remove()
method is less useful and it is return void.
Internal working of
HashMap?
HashMap is use array and link list data structure internally for storing key-value pair. We also say that this is
based on hashing principle and internally used hashCode(). Default HashMap size
is 16 that means Array size is 16 starting from 0 to 15.
And now real programming structure:
1. Default Hashmap size is 16. It means, default array size is 16, starting
from index 0 to 15,
like
shown below,
2. Let's try to put key-value
pair hashmap.put("hello","world");
Above line says, value
"world" needs to be stored against key
"hello".
Step 1: Hashmap will compute the
hashcode of key "hello",
Step 2: Say, hashcode of key
"hello" computed to integer value "12345".
[In Employee Letter Box example, hashcode was
"first alphabet of employee name",
So employee directly jumps to the corresponding Letterbox as he/she was aware that
Letterbox with the same alphabet will be present.]
In the case of Hashmap, after computing
hashcode 12345, we can not jump directly to the array
index 12345, because the array size is 16(from 0 to 15) and index
12345 is not present.
So, hashcode 12345 need to be converted into a number between 0 to 15 to put it
in array.
So here comes the use of indexFor() method.
indexFor(hash, table.length) is used to calculate exact index in the table array
for
storing the Key-Value pair.
Step 3: So, hashcode is further
computed to get array index also known as Bucket in
hashmap terminology. Once, the bucket is known, Key-Value pair is placed in that
bucket.
After placing the object in hashmap, it will look like
below.
3. Let's try to put key-value pair hashmap.put("jayesh","patel");
Step 3
will be repeated, hashcode of key "Jayesh" is computed and let's say
hashcode
evaluated
is 450, and the bucket or index of array evaluated is 11.
Note:
Items that have the same
hashcode will fall in the same bucket,
So if more than one item is
falling in the same bucket then those items will be
stored in a Linked list like
shown below.
4. Let's try to put key-value
pair hashmap.put("khyati","patel");
Step 3
will be repeated, hashcode of key "khyati" is computed and let's say
hashcode
evaluated
is 1200, and the bucket or index of array evaluated is 2.
After placing the object in hashmap, it
will look like below.
5. Let's try to put
key-value pair hashmap.put("khyati","pokar");
Step 3
will be repeated, hashcode() method will be called to compute hashcode of
"khyati".
In Step
4, hashcode evaluated for "khyati" gave 1200, then the second
time if
you calculate hashcode of "khyati", it will always return same
hashcode 1200.
and
ultimately, the bucket will also be same for the same key. So in our example it will be
Bucket 2.
Point to note here is, hashcode() method always return the same hashcode for
same key, irrespective of a number of times it is called.
If we compare the same with our Employee Letter box example, then hashcode of
Employee "Daniel" will be evaluated to "D", irrespective of
number of times it is
calculated.
Bucket 2 is
not empty, So for placing Key-Value pair, "khyati"-"pokar",
it first
goes to Bucket 2 and from there,
GET
Operation:
Let's try to get already stored key-value pair in
hashmap using key,
1. hashMap.get("khyati");
Get operation will follow the same the procedure which is used in put operation.
Step 1: First hashcode of key "khyati" is
computed,
Step 2: Remember, hashcode of key "khyati" was
evaluated to 1200 in put operation,
If we calculate hashcode of "khyati" again, then it always evaluates
to 1200.
Step 3: Now. bucket/array index need to be calculated
from hashcode.
indexFor() method will be used to get exact bucket/index for the key
"khyati".
Hashcode for the same key will always evaluate the same, similarly, bucket/index
calculated
for the same hashcode will always return the same index.
indexFor() method will always return the same index 2 for hashcode 1200.
How to know
Linklist has a loop?
Linklist is
store data in memory in Head and address format like.
public boolean ifLoopExists() {
Node fastPtr = head;
Node slowPtr = head;
while (fastPtr != null && fastPtr.next !=
null) {
fastPtr = fastPtr.next.next;
slowPtr = slowPtr.next;
if (slowPtr == fastPtr)
return true;
}
return false;
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
// Creating a linked list
list.addToTheLast(new Node(5));
list.addToTheLast(new Node(6));
list.addToTheLast(new Node(7));
list.addToTheLast(new Node(1));
list.addToTheLast(new Node(2));
list.printList();
// Test if loop existed or not
System.out.println("Loop existed-->"
+ list.ifLoopExists());
}
Java finally block run when the return statement is
encounter?
Finally, always run without any problem
class FinallyDemo
public static
int myMethod()
{
try {
//try block
return 0;
}
finally {
//finally
System.out.println("Inside Finally block");
}
}
public static
void main(String args[])
{
System.out.println(FinallyDemo.myMethod());
}
}
OUTPUT:
Inside Finally block
0
No comments:
Post a Comment