Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubts from the john hunt's mock test

 
rajani peddi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a few doubts in john hunt's test. http://www.javaranch.com/maha/_Mock_Exams/JohnJuntMockExam.htm
1.Which variables can an inner class access from the class which encapsulates it?
a) All static variables
b) All final variables
c) All instance variables
d) Only final instance variables
e) Only final static variables
Select all correct answers.

the answer given is a,b,c. but i think that its only c.

2.Which of the following correctly illustrate how an InputStreamReader can be created:
a) new InputStreamReader(new FileInputStream("data"));
b) new InputStreamReader(new FileReader("data"));
c) new InputStreamReader(new BufferedReader("data"));
d) new InputStreamReader("data");
e) new InputStreamReader(System.in);
Select all correct answers.
the answers given are a,e. but i answered as b,e
correct me if i am wrong

3.What is the result of compiling and executing the following Java class:
public class ThreadTest extends Thread {
public void run() {
System.out.println("In run");
suspend();
resume();
System.out.println("Leaving run");
}

public static void main(String args []) {
(new ThreadTest()).start();
}
}

a) Compilation will fail in the method main.
b) Compilation will fail in the method run.
c) A warning will be generated for method run.
d) The string "In run" will be printed to standard out.
e) Both strings will be printed to standard out.
f) Nothing will happen.
Select the most appropriate answer.
what is the answer and why?
 
Bharatesh H Kakamari
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answers :
1.Which variables can an inner class access from the class which encapsulates it?
the answer given is a,b,c.
2.
Refer : http://www.jchq.net/discus/messages/1/20387.html?975462558
InputStreamReader does not have any constructor to take a FileReader as parameter. Hence b is wrong.
3.
The answer is 'd'. Once a thread is suspended it cannot resume itself because it is in the suspended state. It is only some other thread that thas to resume this thread.
 
rajani peddi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank u Bharatesh. i got the last two. can you explain the first one?
an inner class can access only final variables if it is inside a method. similarly it can access only static variables if it is a static inner class. otherewise it can access all instance variables, isn't?
so the answer should be c
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you said is correct in that inner classes inside of methods can only access final variables in the method and static inner classes can only access static variables, but the question doesn't say it is an inner class defined in a method or that it is static.
So if it is just a non-static inner class, then they can access all final variables, static variables, and instance variables with no problems. The only variables they can't access are variables local to methods.
 
rajani peddi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks bill....its clear now
 
Mugdha Deo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aren't suspend and resume methods depricated?
So, shouldn't the answer be c ?
Thanks,
Mugdha.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suspend() and resume() are deprecated, and the java 2 exam shouldn't ask you questions on this. However, you can still use them, so it will compile. I wouldn't worry much about those questions, since you shouldn't see them.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic