exam questions

Exam 1z0-808 All Questions

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

Exam 1z0-808 topic 1 question 201 discussion

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

Given the code fragment:

You want the code to print:

Sold: 5 items. Stock in Hand: 5 -
Purchased: 5 items. Stock in Hand: 10?
Which action enables the code to print this?

  • A. Declare the stock variable and the purchase(), sell(), and printStock() methods static.
  • B. Declare the stock variable and the printStock() method static.
  • C. Declare the stock and qty variables and the printStock() method static.
  • D. Declare the stock variable static.
Show Suggested Answer Hide Answer
Suggested Answer: C 🗳️

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
af8c3f3
3 months, 4 weeks ago
isn't qty a local variable? and local variables can't be static?
upvoted 1 times
...
carloswork
2 years, 2 months ago
To test: class StockRoom { private int stock = 10; public void purchase(int qty) { stock += qty; } public void sell (int qty) { stock -= qty; } public void printStock (String action) { System.out.println( action + ":" + qty + " items. Stock in Hand: " + stock); // qty needs to be static variable to compile. } } public class Test { public static void main(String[] args) { StockRoom k1 = new StockRoom(); k1.sell(5); k1.printStock("Sold"); StockRoom k2 = new StockRoom(); k2.purchase(5); k2.printStock("Purchased"); } }
upvoted 2 times
...
carloswork
2 years, 2 months ago
Selected Answer: C
Answer is C. Although executing the code does not produce the exact print reported. The only plausible answer is option C. Because the 'qty' variable needs to be static for the code to at least compile. Note that this variable is not declared as an attribute of the class, so it could not be called in the printStock() method, which causes the compilation error. For it to be enclosed in this method, it needs to be declared in the class, which is what option C proposes.
upvoted 3 times
...
iSnover
2 years, 3 months ago
Selected Answer: C
The answer is C, to perform the operation, the variables must be static to refer to a single value and the method must also be static, otherwise, variables that are static cannot be used inside it.
upvoted 1 times
alex_au
2 years, 3 months ago
Methods no need to be static in order to use static variables. However, static methods cannot use instance variables.
upvoted 2 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