File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exam questions on inner classes -help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exam questions on inner classes -help!" Watch "Exam questions on inner classes -help!" New topic

Exam questions on inner classes -help!

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
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();
public static void main(String args[]) {
new Q4a39();

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.

Matt DeLacey
Ranch Hand

Joined: Oct 12, 2000
Posts: 318
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.

Rajiv Ranjan
Ranch Hand

Joined: Sep 28, 2000
Posts: 61
Question 1 :
A] c=b
Prints 1 //hence valid choice
B] c=this.a
Prints 2 (Since this refers to the instance of Inner class
Compiler error cuz b is not defined in Inner class
Prints 1 //hence valid choice
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

[This message has been edited by Rajiv Ranjan (edited November 09, 2000).]
I agree. Here's the link:
subject: Exam questions on inner classes -help!
It's not a secret anymore!