aspose file tools*
The moose likes Mock Exam Errata and the fly likes forward referencing doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "forward referencing doubt" Watch "forward referencing doubt" New topic
Author

forward referencing doubt

Archana Nair
Greenhorn

Joined: Jul 24, 2001
Posts: 13

Hi
Q1)public class AQuestion
{
private int i = j;
private int j = 10;
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
Answers
a)Compiler error complaining about access restriction of private variables of AQuestion.
b)Compiler error complaining about forward referencing
c)No error - The output is 0;
d)No error - The output is 10;
answer b
Q2)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

a)Compiler error complaining about access restriction of private variables of AQuestion.
b)Compiler error complaining about forward referencing.
c)No Compilation error - The output is 0;
d) No Compilation error - The output is 10;
Answer c
these are 2 questions i found in one of the mock exams.The first once answer is b.i thgt the second onces answer would also be b.But its c.Please do explain why the answer for the 2nd question is c.
Archana
Asma Zafar
Ranch Hand

Joined: May 11, 2001
Posts: 49
The reason why answer is 'c' is:
As you have seen in the main method an object of the class is created and at that time all the values of the instance variables will be initialized to defaults. Since 'i' is assigned the value of method giveMeJ(), its not the case of forward referencing. As you might have obsrved the output is 0 and not 10.
Hope this helps,

------------------
Asma Zafar,
Sun Certified Programmer for Java2 Platform


Asma Zafar,<BR>Sun Certified Programmer for Java2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forward referencing doubt