Forums Register Login

Forwrad Referncing

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Nishant,
Please look at the following post:
initialization
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 729 times.
Similar Threads
Access Modifiers
from abhilash's quiz
Angelfire.com....quiz 2
forward referencing doubt
Angelfire.com....quiz 2
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:31:55.