Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Another java.ditmas problem

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
going through the ditmas mock again, looking over some results, (did better this time yay) but look at this:
9. What will be the output when the following code is run?

1. class ThisClass{
2. public static void main(String args[]){
3. Object o=(Object)new ThisClass();
4. Object s=new Object();
5. if(o.equals(s))
6. System.out.println("true");
7. }
8. }
a. Prints true
b. Nothing will be printed
c. Compiler error at line 3
d. Compiler error at line 5
e. ClassCastException will be thrown
answer = A
---
18. What will be the output when the following code is run?
1. class ThisClass{
2. public static void main(String args[]){
3. Object o=(Object)new ThisClass();
4. Object s=new Object();
5. if(o.equals(s))
6. System.out.println("true");
7. }
8. }
a. Prints true
b. Nothing will be printed
c. Compiler error at line 3
d. Compiler error at line 5
e. ClassCastException will be thrown
answer = b
once again i am not at my own computer (my work computer does not have the java compiler on it, can you believe it?) so could you help with this.
also please explain how the answer to this is GREEN?!?!
16.What will be the content of baz after the the following code is run?
class A{
public static void main(String args[]){
String bar=new String("blue");
String baz=new String("green");
String var=new String("red");
String c=baz;
baz=var;
bar=c;
baz=bar;
System.out.println(baz);
}
}
a. red
b. Compiler error
c. blue
d. null
e. green
I thought strings immutable
This one I'm sure I'm wrong on, so can someone please explain String immutability one more time to me, because I'm getting so confused.
All help appreciated,
Travis B.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what I see question 9 and 18 are the same and the correct answer is definitely B since o and s are references to 2 distinct objects.
Concerning question 16, String objects are immutable not references to String objects. You have to make the difference. A reference variable is what you have on the left side of the assignment and an Object is what you create on the right side of the assignment. A reference variable is used to reference (point to) objects. You can make a reference variable of type String point to whatever String object you want to. However, anytime you invoke a mutator method on an object of type String, a new String object will be created an returned by that method (not always though).
String bar=new String("blue");
String baz=new String("green");
String var=new String("red");
After that code runs, you have 3 different references to 3 different objects:
bar ----> "blue"
baz ----> "green"
var ----> "red"
Then after this, c (a reference to a String object), will reference the same String object referenced by baz which contains the literal "green".
String c=baz;
c ----> "green"
Then, baz (a reference to a String object), will reference the same String object referenced by var which contains the literal "red".
baz=var;
baz ----> "red"
Then, bar (a reference to a String object), will reference the same String object referenced by c which contains the literal "green". At this time, the String object containing "blue" is lost and eligible for garbage collection (because created dynamically with the new keyword).
bar=c;
bar ----> "green"
Then, baz (a reference to a String object), will reference the same String object referenced by bar which contains the literal "green".
baz=bar;
Hence the output.
[ April 12, 2002: Message edited by: Valentin Crettaz ]
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I go with Valentin for both the answers.
Adding some flavor to that.
Yes strings are immutable.
See this code.

class ex1{
public static void main(String args[]){
String bar="blue";
String baz="green";
String var="red";
String c=baz;
baz = var;
bar=c;
baz = bar;
baz.replace('g', 'n');
System.out.println(baz);
}
}
it still gives output as "green".
.
Regards
Hima
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!