1z0-808 Exam QuestionsBrowse all questions from this exam

1z0-808 Exam - Question 43


Given:

Acc.java:

Which statement is true?

Show Answer
Correct Answer: B

In the given scenario, there are two classes defined in different packages. The Acc class has members with different access levels: an unnamed package-private int p, private int q, protected int r, and public int s. In the Test class, which is a subclass of Acc, an object of type Acc is instantiated. Given that the object obj is of type Acc and not the subclass Test, only members accessible through the Acc class's public API are available. This means the only accessible member is s, as it is declared public. The protected member r would only be accessible directly through the subclass when used with an instance of Test, but obj here is of type Acc. Therefore, the correct answer is that only s is accessible via obj.

Discussion

17 comments
Sign in to comment
iSnoverOption: C
Sep 24, 2022

The correct Answer is C, because class (even if it is outside the package) extends from the class with the protected attribute, it will have access to it. So access is by package and by inheritance.

Ru_H33Option: B
Feb 21, 2023

Correct ans is B: Even though Test is child of ACC we are not able to access protected r variable. Reason is packages are different. If you want to access protected member in different package then it can be accessed only using "Child class object" not using parent class or Child class reference hold by parent. For protected r to be visible need to create object of Test t=new Test ; int pr=t.r; Here ACC obj=new Test //We are calling protected using parent class reference hence invalid. If protected r is in same package then it would be valid .

iSnoverOption: B
Oct 6, 2022

I apologize for my previous comment that I couldn't delete. The right answer is B, even the Test class importing and extending the Acc class, the only variable that can be accessed via obj is S because it is public, the reason for this is because the Test class is in a different package, if it were in same package there the answer would be the letter C.

haisacoOption: B
Dec 21, 2022

B is correct.

odzio33Option: B
Jan 16, 2023

package p2; import p1.Acc; public class Test extends Acc { public static void main(String[] args){ Acc obj = new Test(); // System.out.println(obj.p); // System.out.println(obj.q); // System.out.println(obj.r); System.out.println(obj.s); } }

jimcounOption: B
Oct 26, 2022

B is the correct answer. Because the type is created as Acc, the protected field cannot be accessed.

kkaayyyyOption: B
Oct 7, 2022

B - only s is accessible

carlosworkOption: B
Oct 17, 2022

On test, answer is B. The best way to know this, write the code and compile.

carloswork
Nov 8, 2022

To test: ------------------------ // Acc.java package p1; public class Acc { int p = 0; private int q = 1; protected int r = 2; public int s = 3; } ------------------------ // Test.java package p2; import p1.Acc; public class Test extends Acc { public static void main(String[] args) { Acc obj = new Test(); System.out.println(obj.p); System.out.println(obj.q); System.out.println(obj.r); System.out.println(obj.s); } }

iSnoverOption: C
Oct 22, 2022

The answer is the letter C, the variable "r" is protected and it can be accessed directly by another class if the child class extends the mother even though they are in different packages.

Ankit1010Option: B
Feb 7, 2023

B is the right answer.

tawa_z58Option: C
Feb 21, 2023

C is the answer only if the child class extends the class which the variable being accessed even if they are in different packages

hhuoOption: B
Oct 15, 2022

Selected Answer: B Tested - only s is accessible.

anmoldev2javaOption: B
Nov 17, 2022

i also had doubt like why not c but protected are available in subclass but not on objects ... on obj private is the one we can access

yassineRaddaoui
Feb 24, 2023

good question

7df49fbOption: B
Mar 25, 2024

Although the object instanciation happens to be in a Test class (subclass of Acc), it is stored in a Acc reference. We are not allowed to refer to members of Acc class since we are not in the same package of Acc;

deyvi25Option: B
Apr 14, 2024

The correct Answer is B. C is incorrect because 'obj' is out of hirarchy three.

KrokOption: B
Jun 20, 2024

Correct answer is B. C - isn't correct, this is a tricky question. This case is shown in the book "OCA: Oracle® Certified Associate Java® SE 8 Programmer I Study Guide Exam 1Z0-808" by Jeanne Boyarsky, Scott Selikoff