• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

questions

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anybody give me ans with explanation

1)
String s1 = new String("amit");
System.out.println(s1.replace('m','r'));
System.out.println(s1);
String s3="arit";
String s4="arit";
String s2 = s1.replace('m','r');
System.out.println(s2==s3);
System.out.println(s3==s4);


2)
for what are all data types the value -0 is exist in their value range





3) Boolean b1 = new Boolean("TRUE");
Boolean b2 = new Boolean("true");
Boolean b3 = new Boolean("JUNK");
System.out.println("" + b1 + b2 + b3);

a) Comiler error
b) RunTime error
c)truetruefalse
d)truetruetrue

Correct answer is c)



4) i read the static method in the thread class are to avoid the one thread to communicate with other.
but since static is a class method then how a particular thread can have it. eg:sleep

whats the significance in having the staic method in the thread class
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prob1:
String s1 = new String("amit");
System.out.println(s1.replace('m','r')); // here you are not assigning the value to s1 so it will create new String and print new value.
System.out.println(s1); // Result is same as String is immutable and refers to same String
String s3="arit";
String s4="arit";
String s2 = s1.replace('m','r');// Here you are assigning the value back to s2 after replacecing m so S2 will be arit.
System.out.println(s2==s3); // This will always be false as you have two different references bit
System.out.println(s3==s4); //This will always be true as objects here are created in string pool and references for them will be same

Prob3:
Boolean b1 = new Boolean("TRUE"); // if you assign true or TRUE or TrUe it will always be true
Boolean b2 = new Boolean("true"); // same as above
Boolean b3 = new Boolean("JUNK"); // Any thing other then true and false Boolean will consider the value as false
System.out.println("" + b1 + b2 + b3); // So answer is true true false
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


String s1 = new String("amit");
System.out.println(s1.replace('m','r'));
System.out.println(s1);
String s3="arit"; // here new string object is created
String s4="arit"; // Internally object s3 is reused here
String s2 = s1.replace('m','r'); // return a new string arit to s2
System.out.println(s2==s3);
System.out.println(s3==s4); // so both are equal



Method s1.replace (‘m’,’r’ return a new string “arit” and return to s2. Its reference is different from s1 and s3. So s2==s3 returns false. S3==s4 returns true, bcoz object s4 is created from string pool, so it returns s3 reference.
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your problem 3 the default value of Boolean object is false.It returns true if the string argument is not null and is equal, ignoring case, to the string "true". Otherwise it returns false

Boolean b3 = new Boolean("JUNK");


so it returns false
[ April 22, 2005: Message edited by: Raghu Shree ]
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is -0 ? I seem to have only heard of negative infinity so far...
What are the answer choices for this question ?!
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-0.0 is a possible value for float and double but not for any other primitive types.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For Q 1 , the s3==s4 returns false, as interning of strings is internal from a String class's perspective and not from a string reference perspective
 
Kalyani Marathe
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But s3==s4 returns true. please rerun the code.
arit is only one string created in string pool for both the references and will always be true.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s3==s4 will return true !!
As they both refer to same string object in the string pool. Only when a string is created by using new , a seperate object is created in the heap.
 
Saranyan Narayanan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ur rite I tried this
System.out.println("s3 == s4" + s3==s4); // so both are equal
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic