nerdexam
Oracle

1Z0-909 · Question #45

Examine these lines of Python code: You must add a line of code to complete the code to return data to the variable d. Which line will do this?

The correct answer is D. d = cursor.fetch(query, (hire_start, hire_end)). Option D passes both required arguments - the query and the parameter tuple - to a fetch method that retrieves and returns actual row data. The key insight is that you need two things: a retrieval method (not just execution) and the parameterized values (hire_start, hire_end)…

Application Development

Question

Examine these lines of Python code:

You must add a line of code to complete the code to return data to the variable d. Which line will do this?

Options

  • Ad = cursor.execute(query)
  • Bd = cursor.execute(query, (hire_start, hire_end) )
  • Cd = cursor.fetch(query % (hire_start, hire_end))
  • Dd = cursor.fetch(query, (hire_start, hire_end))
  • Ed = cursor . f etchall (query)
  • Fd = cursor.fetchall(query, (hire_start, hire_end))

How the community answered

(49 responses)
  • B
    2% (1)
  • D
    92% (45)
  • E
    2% (1)
  • F
    4% (2)

Explanation

Option D passes both required arguments - the query and the parameter tuple - to a fetch method that retrieves and returns actual row data. The key insight is that you need two things: a retrieval method (not just execution) and the parameterized values (hire_start, hire_end).

Why each distractor fails:

  • A & B use cursor.execute(), which runs the query but returns None (or a row count), not the actual data - A also omits the parameters entirely.
  • C uses string formatting (%) to embed parameters directly into the query, which is dangerous (SQL injection risk) and cursor.fetch() doesn't accept a pre-formatted string like that.
  • E calls cursor.fetchall() correctly as a method name, but passes query as an argument - fetch methods take no arguments in standard usage; they operate on already-executed results.
  • F has the same problem as E: fetchall() takes no arguments, making the parameter tuple meaningless.

Memory tip: Think of the database cursor workflow as a two-step handshake - execute sends the query, fetch collects the results. Options that use execute to "get" data or fetch with wrong/missing arguments are breaking one half of that handshake.

Topics

#Python DB-API#parameterized queries#cursor fetch#SQL injection prevention

Community Discussion

No community discussion yet for this question.

Full 1Z0-909 Practice