Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Jxam

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Hope this may help,
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic