Bird
0
0

Which of the following is the correct way to declare a simple function named greet in Kotlin that takes no parameters and returns no value?

easy📝 Conceptual Q11 of 15
Kotlin - Functions
Which of the following is the correct way to declare a simple function named greet in Kotlin that takes no parameters and returns no value?
Afunction greet() { println("Hello") }
Bfun greet() { println("Hello") }
Cdef greet() { println("Hello") }
Dfun greet { println("Hello") }
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct keyword for function declaration

    Kotlin uses the keyword fun to declare functions. Options using function or def are incorrect.
  2. Step 2: Check the function syntax for parameters and braces

    The function must have parentheses even if there are no parameters, so fun greet { println("Hello") } is incorrect because it misses parentheses.
  3. Final Answer:

    fun greet() { println("Hello") } -> Option B
  4. Quick Check:

    fun + name + () + body = B [OK]
Quick Trick: Remember: Kotlin functions start with 'fun' and need parentheses [OK]
Common Mistakes:
MISTAKES
  • Using 'function' or 'def' instead of 'fun'
  • Omitting parentheses for parameter list
  • Missing curly braces for function body

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes