Inner Classes
Q: 01 Given:
10. class Line {
11. public static class Point {}
12. }
13.
14. class Triangle {
15. // insert code here
16. }
Which code, inserted at line 15, creates an instance of the Point class defined in Line ?
- Point p = new Point();
- Line.Point p = new Line.Point();
- The Point class cannot be instatiated at line 15.
- Line l = new Line() ; l.Point p = new l.Point();
Answer: B
Q: 02 Given:
11. static class A {
12. void process() throws Exception { throw new Exception(); }
13. }
14. static class B extends A {
15. void process() { System.out.println("B "); }
16. }
17. public static void main(String[] args) {
18. A a = new B();
19. a.process();
20. }
What is the result ?
- B
- The code runs with no output.
- An exception is thrown at runtime
- Compilation fails because of an error in line 15.
- Compilation fails because of an error in line 18.
- Compilation fails because of an error in line 19.
Answer: F
Q: 03 Given:
1. package geometry;
2. public class Hypotenuse {
3. public InnerTriangle it = new InnerTriangle();
4. class InnerTriangle {
5. public int base;
6. public int height;
7. }
8. }
Which statement is true about the class of an object that can reference the variable base ?
- It can be any class.
- No class has access to base.
- The class must belong to the geometry package.
- The class must be a subclass of the class Hypotenuse.
Answer: C
Q: 04 Given:
10. class Line {
11. public class Point { public int x,y;}
12. public Point getPoint() { return new Point(); }
13. }
14. class Triangle {
15. public Triangle() {
16. // insert code here
17. }
18. }
Which code, inserted at line 16, correctly retrieves a local instance of a Point object ?
- Point p = Line.getPoint();
- Line.Point p = Line.getPoint();
- Point p = (new Line()).getPoint();
- Line.Point p = (new Line()).getPoint();
Answer: D
|