exam questions

Exam 1z0-829 All Questions

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

Exam 1z0-829 topic 1 question 32 discussion

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

Given:

Which action enables the code to compile?

  • A. Replace 2 with static String name;
  • B. Replace 7 with public static void display(String design) {
  • C. Replace 3 with private static void display() {
  • D. Replace 15 with Item.display("Flower");
Show Suggested Answer Hide Answer
Suggested Answer: A 🗳️

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
supersquax
Highly Voted 5 months, 2 weeks ago
Correct answer is A, tested in online compiler. static methods cannot access instance variable unless they are static. That is to say: - either the "name" variable must be made static so that it can be accessed by the display() method on line 3 - or the display() method on line 3 must be made non-static (remove the static keyword) so that it can access the "name" variable Only alternative that achieves one of these two is A. Note that despite this change, i1.display("Flower") will still call the display() method on line 7. Non-static methods are allowed to access static instance variable regardless. In this case, since both "this.name" and "name" are null, "this.name += name;" will simply do "null += null;" and the result will be "nullnull".
upvoted 5 times
...
SrinivasJasti
Most Recent 2 weeks, 2 days ago
Selected Answer: A
The name field is an instance variable (non-static), but you are trying to access it directly from the static method display().So add static to the variable
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