Answer :
String objects are cached in string pool and shared between multiple thread, So it will lead to risk of value changed by one thread will affect value of other thread.
Example :
// find "abc" in string pool or create new object.
String str = "abc";
//It is available in string pool, This will point the same value in String pool.
String str1 = "abc";
// find "abcxyz" in string pool or create new object.
str = str + "xyz";
// Now str is pointing only "abcxyz" in string pool and str1 is pointing "abc".
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
Thursday, February 7, 2019
Some Useful Java Programs for Technical Interview Part 2
February 07, 2019
Answer
Q7 : Difference between name.equals("Syntaxios") and "Syntaxios".equals(name) ?
Answer
Answer :
name.equals("Syntaxios") = compare unknown value(that means 'name') with known value (that means 'Syntaxios') and "Syntaxios".equals(name) = compare known value (that is "Syntaxios") with the unknown value (that is 'name') and it is null safe, This is used to avoid java.lang.NullPointerException.
name.equals("Syntaxios") = compare unknown value(that means 'name') with known value (that means 'Syntaxios') and "Syntaxios".equals(name) = compare known value (that is "Syntaxios") with the unknown value (that is 'name') and it is null safe, This is used to avoid java.lang.NullPointerException.
Q8 : How to convert string to char [] array and char [] array to string ?
Answer
Answer :
String str = "Syntaxios";
char []CharArray = str.toCharArray();
String strfromCharArray = String.valueOf(CharArray);
String.toCharArray(); // string to char[]
String.valueOf(char[]Data); // char[] to string
String str = "Syntaxios";
char []CharArray = str.toCharArray();
String strfromCharArray = String.valueOf(CharArray);
String.toCharArray(); // string to char[]
String.valueOf(char[]Data); // char[] to string
Q9 : Can we write try without catch ?
Answer
Answer :
Yes, we can write try without catch, But we must write finally.
try{
}
finally{
}
Yes, we can write try without catch, But we must write finally.
try{
}
finally{
}
Q10 : What is finally Block ?
Answer
Answer :
Finally Block used to close resources used in try. Finally Block will be executed even If exception occurs try block. Finally Block will be executed even you write return variable from try block. Finally block will not be executed when System.exit(exitcode) executed in try block.
Example :
try{
int a = 1/0; // finally will be execute
return a;
System.exit(0); // finally will not execute
}
finally{
System.out.println("Finally Block");
}
Finally Block used to close resources used in try. Finally Block will be executed even If exception occurs try block. Finally Block will be executed even you write return variable from try block. Finally block will not be executed when System.exit(exitcode) executed in try block.
Example :
try{
int a = 1/0; // finally will be execute
return a;
System.exit(0); // finally will not execute
}
finally{
System.out.println("Finally Block");
}
Tuesday, August 14, 2018
Some Useful Java Programs for Technical Interview
August 14, 2018
Rules : Rules are very tough, I am just kidding Don't take it seriously,
1. You have four options,
2. You should choose any one options within explanation,
So Lets Start
"To Ye Raha Apka Agla Saval Apki Screen Per" (#Hahahahaha)
Q 1. What will be the output of the following Program ?
class QuestionOne{
public static void main(String arg[]){
int a=7,b=9;
int add = 0;
if(add == 0){
add = a+b;
System.out.println("Addition is : "+add);
}
else{
int sub = a-b;
}
System.out.println("Subtraction is : "+sub);
}
}
public static void main(String arg[]){
int a=7,b=9;
int add = 0;
if(add == 0){
add = a+b;
System.out.println("Addition is : "+add);
}
else{
int sub = a-b;
}
System.out.println("Subtraction is : "+sub);
}
}
A) 16
B) -2
C) print error
D) print Nothing
Answer
Answer : C
It will through a error symbol can not fild because of the sub variable have local scope, Or we print it outside of the else part.
It will through a error symbol can not fild because of the sub variable have local scope, Or we print it outside of the else part.
Q2 : What will be the Output of the given Program ?
for( ; ; ){
System.out.println("Hello World!");
}
System.out.println("Hello World!");
}
A) print Hello World!
B) Runtime Error
C) Compilation Error
D) Create Infinite Loop
Answer
Answer : D
This Programe will create Infinite Loop, Because if You leave all the parts empty of for, There is no condition under where it will terminate the this loop.
This Programe will create Infinite Loop, Because if You leave all the parts empty of for, There is no condition under where it will terminate the this loop.
Q3 : What is string pool in java ?
Answer
Answer :
It is a special area in java heap to store the string literals like "java Query". When you create new string JVM first checks string in pool, If It's available in pool then it will return the same object from pool.
It is a special area in java heap to store the string literals like "java Query". When you create new string JVM first checks string in pool, If It's available in pool then it will return the same object from pool.
Q4 : How to move string object from heap to string pool ?
Answer
Answer :
string intern() is used
to move string object from heap to string pool.
string intern() is used
Q5 : Does string is thread-safe in java ?
Answer
Answer :
Yes, It is, string is immutable and we can not change the value of string, so it can be used in multi-thread environment.
Yes, It is, string is immutable and we can not change the value of string, so it can be used in multi-thread environment.
Subscribe to:
Posts (Atom)