Multiple choice technology programming languages

Given the following two classes, which statement is true? ---------- class-1 ---------- package pack; public class Parent { protected void test() { System.out.println("Test"); } } ---------- class-2 ---------- 1. package mypack; 2. import pack.*; 3. class ParentTest extends Parent{ 4. public static void main(String[] args){ 5. new ParentTest().test(); 6. new Parent().test(); 7. } 8. }

  1. The code will compile and run only if line 5 is removed.

  2. The code will compile and run only if line 6 is removed

  3. The code compiles and runs, printing "Test" twice

  4. The code compiles but throws an exception at runtime

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Line 5 works because ParentTest inherits test() from Parent. Line 6 fails because pack.Parent's protected method can't be called from mypack outside the inheritance hierarchy. Protected access allows subclass access but not external package access.