1.10. Answers to sample exam questions

Q1-1.

Given:

class EJava {
    //..code
}

Which of the following options will compile?

  1. package java.oca.associate;
    class Guru {
        EJava eJava = new EJava();
    }
  2. package java.oca;
    import EJava;
    class Guru {
        EJava eJava;
    }
  3. package java.oca.*;
    import java.default.*;
    class Guru {
        EJava eJava;
    }
  4. package java.oca.associate;
    import default.*;
    class Guru {
        default.EJava eJava;
    }
  5. None of the above

Answer: e

Explanation: A class that isn’t defined in a package gets implicitly defined in Java’s default package. But such classes can’t be accessed by classes or interfaces, which are explicitly defined in a package.

Option a is incorrect. The EJava class isn’t defined in a package, so it can’t be accessed ...

Get OCA Java SE 8 Programmer I Certification Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.