Win a copy of Microservices Testing (Live Project) this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Abilash's mock exam, #11

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 11.
public class AQuestion
{
private int i = giveMeJ();
private int j = 10;
private int giveMeJ()
{
return j;
}
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
Answers

1.Compiler error complaining about access restriction of private variables of AQuestion.
2.Compiler error complaining about forward referencing.
3.No Compilation error - The output is 0;
4.No Compilation error - The output is 10;
Correct answer is 3.
Can anyone please explain me the above answer? How does J get initialized?
Thanks,
Snehal

[This message has been edited by snehal shah (edited January 24, 2001).]
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Snehal,
This is how I see it, somebody correct me if I am wrong,
when int i = givemeJ(); is executed the method givemeJ() returns a value 0.That is because any int member variable if not initialized explicitly ,is assigned 0.After this int j = 10; is executed which then has no effect on the value of i.
So (new AQuestion()).i gives 0.
Hope that helped.
All the Best.
Gazala.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
when i ran this code it gave answer 10 that means that when i is assigned default value 0 and now in my code it is assigned value of 10.
public class AQuestion
{
private int j = 10;
private int i = giveMeJ();
private int giveMeJ()
{
return j;
}
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
i hope this is clear
 
snehal shah
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. Thanks for the input. I still have some confusion though. Maybe you can help me. Can you explain why we don't get forward referencing error? I thought since j is declared after the method call, it should give compilation error because we are trying to access a vriable which is not declared.
Thanks
Snehal
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi snehal
u will get forward referencing error at compile time only if u reference a variable directly i an initializer block or initializing expression before the variable is declared. i.e a variable has to be declared before using it in the initialization. This is not binding on a method. so ur code will not give a forward reference error.
I have modified the code to illustrate:
public class AQuestion
{
private int i = giveMeJ(); //no error -- but maybe logic error as u may actually have wanted i to get value 10 but it will get 0.
private int giveMeJ()
{
return j; //no error
}
{
j++;//illegal forward reference of j
}
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
private int z = i*j;//illegal forward reference of j
private int j = 10;
}
see khalid chapter 8... its got great explanation on initialization.
-Aj
[This message has been edited by Aj Patel (edited January 24, 2001).]
 
snehal shah
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Aj. Now it's makes sense.
Snehal
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, snehal
don't get confused ,you see it's so simple, when you are creating the object of AQuestion
the JVM will call the default constructor which will check for all the defined member variables
and assign them their default values, since you have not coded one for desired initialization
then the value of variable "i" is determined by calling giveMeJ() and at this time j is initialised to
0; the default for int hence you see i as 0 in output.
even if you have called for i value to be printed in the giveMeJ() method it would have printed its default
value which is also 'o'.
::
private int giveMeJ()
{ System.out.println(i);
return j;
}
HOPE THIS HELPS
MUSTAFA
------------------
reply
    Bookmark Topic Watch Topic
  • New Topic