Exam 1z0-808 All QuestionsBrowse all questions from this exam
Question 43

Given:

Acc.java:

Which statement is true?

    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
iSnoverOption: C

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

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

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.

odzio33Option: B

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); } }

haisacoOption: B

B is correct.

jimcounOption: B

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

tawa_z58Option: C

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

Ankit1010Option: B

B is the right answer.

iSnoverOption: C

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.

carlosworkOption: B

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

carloswork

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); } }

kkaayyyyOption: B

B - only s is accessible

KrokOption: B

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

deyvi25Option: B

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

7df49fbOption: B

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;

yassineRaddaoui

good question

anmoldev2javaOption: B

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

hhuoOption: B

Selected Answer: B Tested - only s is accessible.