• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exam questions on inner classes -help!

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here are a couple of questions that i would like an explanation on.
1.Which statements can be inserted at the indicated position in the following code to make the program write 1 on the standard output when run?
public class Q4a39 {
int a = 1;
int b = 1;
int c = 1;
class Inner {
int a = 2;
int get() {
int c = 3;
// insert statement here
return c;
}
}
Q4a39() {
Inner i = new Inner();
System.out.println(i.get());
}
public static void main(String args[]) {
new Q4a39();
}
}
A]c=b
B]c=this.a
C]c=this.b
D]c=Q4a39.this.a
E]c=c

2.36]Which statements concerning the correlation between the inner and outer instances of non-static inner classes are true?
A]Member variables of the outer instance are always accessible to inner instances, regardless of their accessibility modifiers
B]Member variables of the outer instance can never be refered to using only the variable name within the inner instance
C]More than one inner instance can be associated with same outer instance
D]All variables from the outer instance that shouls be accessible in the inner instance myst be declared final
E]A class that is declared final cannot have any inner classes.

Thanks!
 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is a.) only.
In answer b.), this.a refers to the inner class instance of a
Answer c and d cause compile problems because it can't determine WHICH variable you are talking about (I guess because you are trying to access an outer clas variable but using this makin it think it should be in the inner class).
Answer e.) compiles fine but prints 3 because it's using the value of three in the inner class. Hope this helps.

Matt
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 1 :
Ans:
A] c=b
Prints 1 //hence valid choice
B] c=this.a
Prints 2 (Since this refers to the instance of Inner class
C]c=this.b
Compiler error cuz b is not defined in Inner class
D]c=Q4a39.this.a
Prints 1 //hence valid choice
E]c=c
Prints 3 This is same as this.c and hence it refers to Inner class.
For question 2 , I think options A and C are valid.
Comments are welcome
Thanks


[This message has been edited by Rajiv Ranjan (edited November 09, 2000).]
 
BWA HA HA HA HA HA HA! Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic