Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

mock exam Q

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test026
{
static Test026 t = new Test026();
String str;
public static void main(String args[]) {
Test026 t = new Test026();
t.method("0");
}
void method(String str) {
str = str;
System.out.println(str);
System.out.println(t.str);
System.out.println(this.str);
}
}
-----------------------------------------------------------------------
A: The output is:
0
null
null
B: The output is:
0
0
0
C: The output is:
0
0
null
D: The output is:
0
null
0

what is the answer? The mock exam says A... I thought it wud be D
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Preeti
The answer is and should be A. Well its so because System.out.println(this.str); would refer to t.str, t which has been declared in the main method. If however in the main method if this is present t.str="0"; then it would print the option D.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The answer is and should be A. Well its so because System.out.println(this.str); would refer to t.str, t which has been declared in the main method. If however in the main method if this is present t.str="0"; then it would print the option D


Scope of variable t defined in the main method is limited to main method only. It can not be accessed outside the method. System.out.println(t.str) would refer to the static variable t defined earlier and thus it will print null.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yogi
I wasn't refering to System.out.println(t.str); statement. I was refering to System.out.println(this.str); and I didn't refer to the former one because in options A and D the only thing different was the last line. Both the first two line show the same thing. So I simply focussed on what was different. No doubt in method "method" t would indeed refer to the static t declared earlier.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the reason why you're confused about "System.out.println(this.str)" is because of the first line in void method (String str), which sets "str=str". If this said "this.str=str", then answer D would be correct because you would've set the instance variable str = 0. As it is, you're (rather unnecessarily) setting local str = local str, which leaves the instance version of str untouched (default value = null).
I hope this helps.
Michael
[ June 07, 2003: Message edited by: Michael Schmidt ]
[ June 07, 2003: Message edited by: Michael Schmidt ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic