Bird
0
0

Consider the function:

medium📝 query result Q5 of 15
PostgreSQL - Advanced PL/pgSQL
Consider the function:
CREATE FUNCTION multiply_all(VARIADIC nums int[]) RETURNS int AS $$
  SELECT COALESCE(product, 1) FROM (
    SELECT exp(sum(ln(n::float))) AS product FROM unnest(nums) AS n
  ) sub;
$$ LANGUAGE sql;

What will SELECT multiply_all(2, 3, 4); return?
A0
B9
C24
DError: invalid input
Step-by-Step Solution
Solution:
  1. Step 1: Understand function logic

    The function multiplies all integers by using logarithms and exponentials to avoid overflow.
  2. Step 2: Calculate product

    Input is (2, 3, 4), product is 2 * 3 * 4 = 24.
  3. Step 3: COALESCE handles empty input

    If no numbers, returns 1; here input is non-empty.
  4. Final Answer:

    24 -> Option C
  5. Quick Check:

    Multiplying 2,3,4 equals 24 [OK]
Quick Trick: VARIADIC packs ints into array for multiplication [OK]
Common Mistakes:
  • Confusing sum with product
  • Expecting error on multiple arguments
  • Ignoring COALESCE default

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes