Chapter 1. Lambda Expressions

1.1.  Describe Java inner classes and develop the code that uses Java inner classes (such as: nested class, static class, local class and anonymous classes)

Question 010101

Given the class:

public class Building {

    class Apartment {
        public int getLivingSpace() {
            return 990; // square feet
        }
    }

    public static void main(String[] args) {
        // line 1
        System.out.println("Living space : " + space);
    }
}
					

Which line inserted at // line 1 position will make the code print apartment's living space?

Options (select 1):

  1. int space = new Building().new Apartment().getLivingSpace();

  2. int space = new Building().Apartment().getLivingSpace();

  3. int space = new Building.Apartment().getLivingSpace();

  4. int space = new Apartment().getLivingSpace();

Question 010102

Given the Room class:

public class Room {

    private String EXIT = "an exit"; // line 1

    static class Window {
        public void useWindow() {
            System.out.printf("I'm %s!", EXIT);
        }
    }

    public static void main(String[] args) {
        ... // assume 'window' variable declaration and initialization syntax correct
        window.useWindow();
    }
}
					

What is the result?

Options (select 1):

  1. The code prints "I'm an exit!"

  2. The code fails to compile. In order to compile, change the line 1 as follows:

    public String EXIT = "an exit";

  3. The code fails to compile. In order to compile, change the line 1 as follows:

    final String EXIT = "an exit";

  4. The code fails to compile. In order to compile, change the line 1 as follows:

    static String EXIT = "an exit";

Question 010103

Given the Bag class:

public class Bag {
    public void packFood(String bagName) { // line 1
        class Food { // line 2
            private void pack() { // line 3
                System.out.println("Packing into " + bagName);
            }
        }
        Food f = new Food();
        f.pack();
    }

    public static void main(String[] args) {
        new Bag().packFood("paper bag");
    }
}
					

What is the result?

Options (select 1):

  1. Code compilation fails. To make the code compile, change line 1 as follows:

    public void packFood(final String bagName) {
    								

  2. Code compilation fails. To make the code compile, change line 2 as follows:

    static class Food {
    								

  3. Code compilation fails. To make the code compile, change line 3 as follows:

    public void pack() {
    								

  4. The code successfully runs and prints Packing into paper bag.

Professional hosting         Free 'Oracle Certified Expert Web Services Developer 6' Guide     Exam 1Z0-810: Upgrade to Java SE 8 Programmer Study Guide