1Z0-803 · Question #202
Given the code fragment: Which code fragment, when inserted at // insert code here, enables the code to compile and and print a b c?
The correct answer is C. Static List update (String [] strs). To compile and allow printing "a b c" after processing string arguments from a static context, the method must be static and return a List interface type.
Question
Given the code fragment:
Which code fragment, when inserted at // insert code here, enables the code to compile and and print a b c?
Exhibit
Options
- AList update (String[] strs)
- BStatic ArrayListupdate(String [] strs)
- CStatic List update (String [] strs)
- DStatic void update (String[] strs)
- EArrayList static update(String [] strs)
How the community answered
(22 responses)- A9% (2)
- B14% (3)
- C68% (15)
- D5% (1)
- E5% (1)
Why each option
To compile and allow printing "a b c" after processing string arguments from a static context, the method must be static and return a `List` interface type.
This method declaration is not `static`, meaning it cannot be called directly from a `static` context (like the `main` method) without first creating an instance of the class.
While `ArrayList` is a valid concrete return type, the choice uses incorrect capitalization (`ArrayListupdate`) and generally, returning the `List` interface is preferred for better code flexibility and abstraction over returning a concrete implementation.
The method must be `static` to be called directly from another `static` method (such as `main`) without requiring an instance of the class. Returning `List` is an appropriate choice as an interface type for a collection that likely processes `String[] strs` and needs to be iterated to print values like "a b c".
A `void` return type means the method does not return a value, which would not allow for subsequent processing of a returned collection to print "a b c".
The syntax `ArrayList static update` is incorrect in Java; the `static` modifier must precede the return type (e.g., `static ArrayList update`).
Concept tested: Java method signature, static modifier, return types
Source: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
Topics
Community Discussion
No community discussion yet for this question.
