nerdexam
Oracle

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.

Working with Methods and Encapsulation

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

1Z0-803 question #202 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)
  • A
    9% (2)
  • B
    14% (3)
  • C
    68% (15)
  • D
    5% (1)
  • E
    5% (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.

AList update (String[] strs)

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.

BStatic ArrayListupdate(String [] strs)

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.

CStatic List update (String [] strs)Correct

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".

DStatic void update (String[] strs)

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".

EArrayList static update(String [] strs)

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

#method signatures#static methods#return types#List interface

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice