Bird
Raised Fist0
Kotlinprogramming~20 mins

Main function as entry point in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kotlin Main Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:00remaining
What is the output of this Kotlin program?
Consider this Kotlin program with a main function. What will it print when run?
Kotlin
fun main() {
    println("Hello, Kotlin!")
}
ANo output
Bhello, kotlin!
CHello, Kotlin!
DCompilation error
Attempts:
2 left
💡 Hint
The main function is the starting point of a Kotlin program.
❓ Predict Output
intermediate
1:30remaining
What happens if main has parameters but is called without arguments?
Look at this Kotlin main function with parameters. What will happen if you run this program without passing any command-line arguments?
Kotlin
fun main(args: Array<String>) {
    println("Arguments count: ${args.size}")
}
ACompilation error
BArguments count: 0
CRuntime error
DArguments count: null
Attempts:
2 left
💡 Hint
The args array is always passed, even if empty.
❓ Predict Output
advanced
2:00remaining
What is the output of this Kotlin program with multiple main functions?
This Kotlin file has two main functions. Which one will run and what will be printed?
Kotlin
fun main() {
    println("First main")
}

fun main(args: Array<String>) {
    println("Second main")
}
AFirst main
BNo output
CCompilation error due to duplicate main
DSecond main
Attempts:
2 left
💡 Hint
Kotlin prefers main with parameters as entry point.
❓ Predict Output
advanced
1:30remaining
What error does this Kotlin program produce?
This Kotlin program tries to define main with a wrong signature. What error will it cause?
Kotlin
fun main(args: String) {
    println(args)
}
ARuntime error: main function must have Array<String> parameter or no parameters
BRuntime error: wrong argument type
CPrints the string argument
DNo output
Attempts:
2 left
💡 Hint
Main function signature must be exact.
🧠 Conceptual
expert
2:00remaining
How many main functions can a Kotlin program have without causing errors?
Select the correct statement about the number of main functions allowed in a Kotlin program.
ATwo main functions are allowed: one with no parameters and one with Array<String> parameter
BOnly one main function is allowed, with Array<String> parameter
COnly one main function is allowed, with no parameters
DMultiple main functions with any signatures are allowed
Attempts:
2 left
💡 Hint
Kotlin supports main function overloading with specific signatures.

Practice

(1/5)
1. What is the purpose of the main function in a Kotlin program?
easy
A. It is the entry point where the program starts running.
B. It stores global variables for the program.
C. It defines classes used in the program.
D. It handles user input throughout the program.

Solution

  1. Step 1: Understand the role of main in Kotlin

    The main function is where Kotlin programs begin execution.
  2. Step 2: Compare options with this role

    Only It is the entry point where the program starts running. correctly states that main is the program's starting point.
  3. Final Answer:

    It is the entry point where the program starts running. -> Option A
  4. Quick Check:

    Main function = program start [OK]
Hint: Remember: main() is where Kotlin starts running code [OK]
Common Mistakes:
  • Thinking main stores variables
  • Confusing main with class definitions
  • Assuming main handles all user input
2. Which of the following is the correct syntax for the main function in Kotlin?
easy
A. def main() { println("Hello") }
B. function main() { println("Hello") }
C. fun main() { println("Hello") }
D. main() { println("Hello") }

Solution

  1. Step 1: Recall Kotlin function syntax

    Kotlin functions start with fun, followed by the name and parentheses.
  2. Step 2: Check each option

    Only fun main() { println("Hello") } uses fun main() correctly; others use wrong keywords or miss fun.
  3. Final Answer:

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

    fun keyword + main() = correct syntax [OK]
Hint: Kotlin functions start with 'fun' keyword [OK]
Common Mistakes:
  • Using 'function' instead of 'fun'
  • Using 'def' like Python
  • Omitting 'fun' keyword
3. What will be the output of this Kotlin program?
fun main() {
    println("Start")
    println("End")
}
medium
A. Start\nEnd
B. End\nStart
C. Start End
D. No output

Solution

  1. Step 1: Read the print statements order

    The program prints "Start" first, then "End" on separate lines.
  2. Step 2: Understand println behavior

    println adds a new line after each output, so outputs appear on separate lines.
  3. Final Answer:

    Start\nEnd -> Option A
  4. Quick Check:

    Print order matches output lines [OK]
Hint: println prints with a new line after output [OK]
Common Mistakes:
  • Assuming outputs print on same line
  • Mixing order of print statements
  • Thinking println does not add new line
4. Identify the error in this Kotlin program:
fun main(args: String) {
    println("Hello")
}
medium
A. The function name must be capitalized.
B. The parameter should be an array of strings, not a single string.
C. println cannot be used inside main.
D. Missing return type declaration.

Solution

  1. Step 1: Check main function parameter type

    Main function parameters must be Array<String> or Array<String>, not a single String.
  2. Step 2: Verify other options

    Function name is lowercase as required; println is valid; return type is optional for Unit.
  3. Final Answer:

    The parameter should be an array of strings, not a single string. -> Option B
  4. Quick Check:

    Main args = Array<String> [OK]
Hint: Main args must be Array<String>, not String [OK]
Common Mistakes:
  • Using String instead of Array<String> for args
  • Capitalizing main function name
  • Expecting return type declaration
5. You want to write a Kotlin program that prints all command-line arguments passed to it. Which main function signature and code snippet is correct?
hard
A. fun main(args: List<String>) { args.forEach { println(it) } }
B. fun main(args: String) { for (arg in args) println(arg) }
C. fun main() { for (arg in args) println(arg) }
D. fun main(args: Array<String>) { for (arg in args) println(arg) }

Solution

  1. Step 1: Identify correct main function parameter type

    Main function must accept Array<String> to receive command-line arguments.
  2. Step 2: Check code to print each argument

    fun main(args: Array<String>) { for (arg in args) println(arg) } uses a for loop over args correctly and prints each argument.
  3. Step 3: Evaluate other options

    fun main(args: String) { for (arg in args) println(arg) } uses wrong parameter type; C lacks args parameter; D uses List which is not standard for main.
  4. Final Answer:

    fun main(args: Array<String>) { for (arg in args) println(arg) } -> Option D
  5. Quick Check:

    Main args = Array<String> + loop print [OK]
Hint: Main args must be Array<String> to access command-line inputs [OK]
Common Mistakes:
  • Using String or List instead of Array<String>
  • Omitting args parameter
  • Incorrect loop syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard