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 Technical Interview. Show all posts
Showing posts with label Technical Interview. 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.
Thursday, July 19, 2018
These 10 Useful JavaScript Tips & Tricks That Every JavaScript Developer Should Know
July 19, 2018
Here we talk about 10 JavaScript tips & tricks that every programmer should know, These tips & tricks are very useful to every JavaScript developer, these tips & tricks will help you to brush up your programming skills.
1 - Use Semicolons at the end of the line for termination :
As every programmer know about the value of Semicolon in programming world, Semicolon is used at the end of the line for line termination, But if we did not use Semicolon in JavaScript its ok with it JavaScript will not going to give you an error if you forget to use Semicolon, But its a good practice to use Semicolon at the end of the line for line termination.
2 - Use = = = instead of = = :
Here = = = and = = are two different thing in JavaScript, You should aware about this difference in both operators, The = = operator performs an automatic type conversion if needed.
3 - Don't forgot to use var keyword to assigning a variable's value first time :
var keyword is use for creating a variable in JavaScript, you can also assign your variable at the creating time, you should avoid global variable in JavaScript.
var name = " Syntaxios " ;
document.write(name);
eval() also called function constructor, eval() always called script engine each time to convert a source code into a executable code, That's by you always try to avoid this function in JavaScript.
var function1 = new Function (functionCode);
var function2 = eval(functionCode);
5 - Avoid using with() :
The with() is used for insert a variable at the global scope, Thus, If you have another variable with same name the this could cause confusion and may be overwrite the value of your another variable.
6 - Create an object constructor :
function Person(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}
var Aman = new Person("Aman","Singh");
//Here we create an object Aman.
this.firstName = firstName;
this.lastName = lastName;
}
var Aman = new Person("Aman","Singh");
//Here we create an object Aman.
7 - Create a Self-Calling Function :
Self-Calling function also called Self-invoke anonymous function or immediately invoked function. The Self-Calling function always execute automatically when you create it.
(function(){
//Put your code here that you want to execute or call automatically
})();
(function(a,b){
var answer = a+b;
return answer;
})( 7, 5 )
//Put your code here that you want to execute or call automatically
})();
(function(a,b){
var answer = a+b;
return answer;
})( 7, 5 )
8 - Get a Random Number between specific range :
To generate a random number between the specified range you have to import the Math library, Math library have random function that is used to get a random number in a specified range, This code is useful when you want to generate some fake data for testing like age between min age than max age.
var age = Math.floor(Math.random()*(max - min + 1))+min;
9 - Get random item from an array :
This code will help you to get random number or item from an array.
var items = [1, 440, 'aman', 2, 530, 'singh', 3421 119, 'rajawat', 5, 765];
var random_items = items[Math.floor(Math.random()* items.length)];
10 - Generate an array of numbers zero to max :
This code will generate the array of numbers with numbers from zero to max,<
var number_array = [];
var max = 100; // maximum number upto 100, max = 100;
for ( var i=1; number_array.push(i++)< max);
//Numbers = [ 1, 2, 3, 4, 5, ......., 100 ];
var max = 100; // maximum number upto 100, max = 100;
for ( var i=1; number_array.push(i++)< max);
//Numbers = [ 1, 2, 3, 4, 5, ......., 100 ];
Subscribe to:
Posts (Atom)