OCA Java SE 8 Method - OCA Mock Question Method 8








Question

Given the following com.java2s.Student and com.java2s.data.Main class definitions, which line numbers in main() generate a compiler error? (Choose all that apply)

     1: package com.java2s; 
     2: public class Student { 
     3:   private int myNum; 
     4:   protected String myName; 
     5:   static int myValue = 5; 
     6:   public int myLevel = 3; 
     7:   Student(int r, String t) { 
     8:     myNum = r; 
     9:     myName = t; } 
     10:} 

     1: package com.java2s.data; 
     2: import com.java2s.*; 
     3: public class Main { 
     4:   public static void main(String[] args) { 
     5:     System.out.println(Student.myValue); 
     6:     Student s = new Student(101, "A"); 
     7:     System.out.println(s.myNum); 
     8:     System.out.println(s.myLevel); 
     9:     System.out.println(s.myName); } 
     10:} 
  1. None, the code compiles fine.
  2. Line 5
  3. Line 6
  4. Line 7
  5. Line 8
  6. Line 9




Answer



B, C, D, F.

Note

The two classes are in different packages therefore private access and default (package private) access will generate compile error.

Protected access will not compile since Main does not inherit from Student.

Line 8 will compile because it uses public access.