exam questions

Exam 1z0-808 All Questions

View all questions & answers for the 1z0-808 exam

Exam 1z0-808 topic 1 question 43 discussion

Actual exam question from Oracle's 1z0-808
Question #: 43
Topic #: 1
[All 1z0-808 Questions]

Given:
Acc.java:

Which statement is true?

  • A. Both p and s are accessible via obj.
  • B. Only s is accessible via obj.
  • C. Both r and s are accessible via obj.
  • D. p, r, and s are accessible via obj.
Show Suggested Answer Hide Answer
Suggested Answer: B 🗳️

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
iSnover
Highly Voted 2 years, 4 months ago
Selected Answer: 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.
upvoted 8 times
TheeOne
5 days, 6 hours ago
Incorrect: Field r (protected) is accessible in the subclass (Test) via inheritance. However, it is not accessible via the object reference obj because obj is of type Acc, not Test. In Java, protected members are only accessible directly through inheritance or within the same package.
upvoted 1 times
...
...
Ru_H33
Highly Voted 1 year, 11 months ago
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 .
upvoted 7 times
...
jp_ofi
Most Recent 1 week, 2 days ago
Selected Answer: B
It would be C if Test obj = new Test();
upvoted 1 times
...
vic88
2 months ago
Selected Answer: B
B is right
upvoted 1 times
...
MPignaProTech
2 months, 3 weeks ago
Selected Answer: C
public and protected classes are accessible, C is the correct anwer
upvoted 1 times
TheeOne
5 days, 6 hours ago
Incorrect Field r (protected): It is accessible in the subclass (Test) via inheritance. However, it is not accessible via the object reference obj because obj is of type Acc, not Test. In Java, protected members are only accessible directly through inheritance or within the same package.
upvoted 1 times
...
...
Krok
7 months, 1 week ago
Selected Answer: 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
upvoted 1 times
...
deyvi25
9 months, 2 weeks ago
The correct Answer is B. C is incorrect because 'obj' is out of hirarchy three.
upvoted 1 times
...
7df49fb
10 months, 1 week ago
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;
upvoted 1 times
...
yassineRaddaoui
1 year, 11 months ago
good question
upvoted 1 times
...
tawa_z58
1 year, 11 months ago
C is the answer only if the child class extends the class which the variable being accessed even if they are in different packages
upvoted 2 times
...
Ankit1010
1 year, 11 months ago
B is the right answer.
upvoted 2 times
...
odzio33
2 years ago
Selected Answer: 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); } }
upvoted 4 times
...
haisaco
2 years, 1 month ago
Selected Answer: B
B is correct.
upvoted 4 times
...
anmoldev2java
2 years, 2 months ago
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
upvoted 1 times
...
jimcoun
2 years, 3 months ago
Selected Answer: B
B is the correct answer. Because the type is created as Acc, the protected field cannot be accessed.
upvoted 3 times
...
iSnover
2 years, 3 months ago
Selected Answer: 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.
upvoted 2 times
...
carloswork
2 years, 3 months ago
Selected Answer: B
On test, answer is B. The best way to know this, write the code and compile.
upvoted 2 times
carloswork
2 years, 2 months ago
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); } }
upvoted 1 times
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago