Java OCA OCP Practice Question 955

Question

Given the following my.hotel.ClassRoom and my.city.Hotel class definitions, which line numbers in main() generate a compiler error? (Choose all that apply)


1: package my.hotel; 
2: public class Room { 
3:   private int roomNumber; 
4:   protected String guestName; 
5:   static int globalKey = 54321; 
6:   public int floor = 3; 
7:   Room(int r, String t) { 
8:     roomNumber = r; /*from   ww  w.ja v  a 2 s . c om*/
9:     guestName = t; } } 


1: package my.city; 
2: import my.hotel.*; 
3: public class Hotel { 
4:   public static void main(String[] args) { 
5:     System.out.println(Room.globalKey); 
6:     Room room = new Room(101, ""Mrs. Anderson"); 
7:     System.out.println(room.roomNumber); 
8:     System.out.println(room.floor); 
9:     System.out.println(room.guestName); } } 
  • A. None, the code compiles fine.
  • B. Line 5
  • C. Line 6
  • D. Line 7
  • E. Line 8
  • F. Line 9


B, C, D, F.

Note

The two classes are in different packages, which means private access and default (package private) access will not compile.

Additionally, protected access will not compile since Hotel does not inherit from Room.

Therefore, only line 8 will compile because it uses public access.




PreviousNext

Related