Bird
0
0

Which is the correct function and call to get output "Info: Hello"?

hard📝 Application Q15 of 15
Swift - Functions
You want to write a Swift function formatMessage that takes a text and an optional prefix with default value "Info:". It should return a string combining prefix and text separated by a space. Which is the correct function and call to get output "Info: Hello"?
Afunc formatMessage(text: String, prefix: String = "Info:") -> String { return "\(prefix) \(text)" } print(formatMessage(text: "Hello"))
Bfunc formatMessage(prefix: String = "Info:", text: String) -> String { return "\(text) \(prefix)" } print(formatMessage(text: "Hello"))
Cfunc formatMessage(text: String, prefix: String) -> String { return "\(prefix) \(text)" } print(formatMessage(text: "Hello"))
Dfunc formatMessage(text: String, prefix: String = "Info:") -> String { return "\(text) \(prefix)" } print(formatMessage(text: "Hello"))
Step-by-Step Solution
Solution:
  1. Step 1: Define function with default parameter last

    Parameter with default 'prefix' must come after 'text' without default.
  2. Step 2: Check function call and output

    Calling with only 'text' uses default prefix "Info:" and returns "Info: Hello".
  3. Final Answer:

    func formatMessage(text: String, prefix: String = "Info:") -> String { return "\(prefix) \(text)" } print(formatMessage(text: "Hello")) -> Option A
  4. Quick Check:

    Default param last + call with one arg = correct output [OK]
Quick Trick: Default params last, call with missing arg uses default [OK]
Common Mistakes:
  • Placing default parameter before non-default
  • Calling function without required parameters
  • Swapping order of prefix and text in output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes