• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

question from JQuest

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
QUESTION : 21
What is the output of the following program
1. public class Test {
2. private int i = giveMeJ();
3. private int j = 10;
4.
5. private int giveMeJ() {
6. return j;
7. }
8.
9. public static void main(String args[]) {
10. System.out.println((new AQuestion()).i);
11. }
12. }
Select one correct answer
a. Compile error complaining about access restriction of private variables of AQuestion
b. Compile error complaining about forward referencing.
c. No compile error. The output is 0
d. No compilation error - The output is 10
==========================================================================================
The given answer is 'c'
Please explain this someone!
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Are you thinking about forward referencing. Please dont make people read your mind, be more specific ?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi !
In line 2, when the method giveMeJ() is called, the variable j has not been initialised to 10. However the complier using the lookup table knows that a variable called as j exists. since j is a class variable, it is assigned default 0 value. Hence the method returns 0.
Hope I am clear.
Also there is a typing mistake in ur code. You should be creating an instance of class Test and not class AQuestion in the main method !
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Minakshi,
Welcome to JavaRanch.
Your name does not comply with the JavaRanch naming policy. Please choose one that meets the requirements.
Javaranch appreciates your cooperation to comply with the official naming policy.
Ajith
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rajni.
Its now clear. As for creating a wrong instance, its a mistake in the test. I've just copied the question from JQuest.
Ajith, I've changed my name as per the policy.
I hope its ok now.
Thanks again
Minakshi
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excuse me! but i think there is a problem with your question where is this mysterious AQuestion class you r referring to. The compiler will certainly give class not found error.
 
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic