This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Hi , following questions are from jxam mock exam. Q#1 Given the following definition: String s = null;
Which code fragment will cause an exception of type NullPointerException to be thrown. 1. if ((s != null) & (s.length()>0)) 2. if ((s != null) && (s.length()>0)) 3. if ((s != null) | (s.length()>0)) 4. if ((s != null) | | (s.length()>0)) 5. none given answer is 1,2,3,4. i am sure '2' is not a valid answer. please do varify it. Q#2 Which of the following statements are true regarding inner classes. 1. Variables defined inside inner classes cannot be static. 2. Variables defined inside inner classes cannot be static unless the inner class itself is static. 3. Non-static inner classes (which are not defined in a method) have access to all class and instance variables, regardless of the access qualifier of those variables. 4. An inner class can actually be a subclass of the outer class 5. Inner classes can be declared as private. Top level, outer classes cannot. given answer is 2,3,4,5. but what is wrong with '1'. please explain it to me.
vivek
Harry Chawla
Ranch Hand
Joined: Jun 03, 2000
Posts: 97
posted
0
You are right Vivek, for Q1) choice 2 can be compiled and run without error as it evaluates only the Right Handside of the expression and returns false.
Lancy Mendonca
Ranch Hand
Joined: Aug 08, 2000
Posts: 54
posted
0
What does option 3 within question 2 mean? I did not understand Non Static Inner class (not within a method). I feel Non Static Inner class whether within or outside a method can access the class and instance variables.
Sun Certified Java Programmer<BR>Oracle Certified DBA
Marcela Blei
Ranch Hand
Joined: Jun 28, 2000
Posts: 477
posted
0
I agree with you in Q1 1. Variables defined inside inner classes cannot be static. True. From JLS: Inner classes may not declare static members, unless they are compile-time constant fields. Inner classes may inherit static members that are not compile-time constants. 2. Variables defined inside inner classes cannot be static unless the inner class itself is static. True. From JLS: Nested classes that are not inner classes may declare static members freely. 3. Non-static inner classes (which are not defined in a method) have access to all class and instance variables, regardless of the access qualifier of those variables. True, because you have an associated outer class with every inner that is not static, remember that static nested classes can�t access non-static members of it�s outer class 4. An inner class can actually be a subclass of the outer class I read in some place that this is false, but it compiles without problem. I didn�t find any futher documentation about this topic. 5. Inner classes can be declared as private. Top level, outer classes cannot. True
[This message has been edited by Marcela Blei (edited August 17, 2000).]
Savithri Devaraj
Ranch Hand
Joined: Jun 26, 2000
Posts: 103
posted
0
Originally posted by Marcela Blei: I agree with you in Q1 Q2) I think that answer 1 is right and 2 is wrong 2. Variables defined inside inner classes cannot be static unless the inner class itself is static. True. From JLS: Nested classes that are not inner classes may declare static members freely.
Are you not contradicting yourself? 2 of Q2 is right? Savithri
Marcela Blei
Ranch Hand
Joined: Jun 28, 2000
Posts: 477
posted
0
Originally posted by Savithri Devaraj: Are you not contradicting yourself? 2 of Q2 is right? Savithri
Yes, you are right, i missed a correction on myself. I edited the message, sorry.
RKB
Greenhorn
Joined: Aug 14, 2000
Posts: 9
posted
0
Hi , Which of the following statements are true regarding inner classes. 1. Variables defined inside inner classes cannot be static. Ans: False Explanation: Inner classes includes: - static inner classes ( Variables defined inside a static inner classes can be static.) - non static inner classes (Variables defined inside a non static inner classes cannot be static.) So we can't generalize "1. Variables defined inside inner classes cannot be static". We can only say " 2. Variables defined inside inner classes cannot be static unless the inner class itself is static." That is why - Option 1 is false - Option 2 is true.