What is the purpose of the main function in a Kotlin program?
The main function is the starting point of a Kotlin program. When you run the program, the code inside main is executed first.
Click to reveal answer
beginner
How do you declare the main function in Kotlin that takes no arguments?
You declare it like this:
fun main() {
// code here
}
This function runs when the program starts.
Click to reveal answer
intermediate
Can the main function in Kotlin accept command-line arguments? If yes, how?
Yes, it can accept arguments as an array of strings:
fun main(args: Array<String>) {
// use args here
}
This lets you use inputs from the command line.
Click to reveal answer
beginner
What happens if you write a Kotlin program without a main function?
The program will not run because Kotlin needs a main function as the entry point to start executing code.
Click to reveal answer
intermediate
Is it possible to have multiple main functions in a Kotlin project?
Yes, but only one main function is used when you run the program. You can have multiple in different files for testing or different programs, but you must specify which one to run.
Click to reveal answer
What is the correct way to declare a main function in Kotlin that accepts command-line arguments?
Amain() fun {}
Bfun main(args: Array<String>) {}
Cvoid main() {}
Dfun main() args: String[] {}
✗ Incorrect
The correct syntax is fun main(args: Array) {} to accept command-line arguments.
What role does the main function play in a Kotlin program?
AIt compiles the program.
BIt stores variables.
CIt is used only for testing.
DIt is the entry point where the program starts running.
✗ Incorrect
The main function is the entry point where the program begins execution.
Which of these is NOT a valid main function declaration in Kotlin?
Afun main() {}
Bfun main(args: Array<String>) {}
Cfun main(args: List<String>) {}
Dfun main(vararg args: String) {}
✗ Incorrect
The main function must accept Array<String>, not List<String>.
What happens if you run a Kotlin program without a main function?
AThe program will not run and shows an error.
BThe program runs normally.
CThe program runs but skips the <code>main</code> function.
DThe program runs but prints nothing.
✗ Incorrect
Kotlin requires a main function as the entry point; without it, the program cannot run.
Can you have multiple main functions in a Kotlin project?
AYes, but only one is used when running the program.
BNo, only one <code>main</code> function is allowed in the whole project.
CYes, and all run simultaneously.
DNo, <code>main</code> functions are not allowed.
✗ Incorrect
You can have multiple main functions in different files, but only one is used when you run the program.
Explain the role of the main function in Kotlin and how to declare it.
Think about where the program begins and how you write the function.
You got /4 concepts.
Describe what happens if a Kotlin program does not have a main function.
Consider what Kotlin needs to start running code.
You got /3 concepts.
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
Step 1: Understand the role of main in Kotlin
The main function is where Kotlin programs begin execution.
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.
Final Answer:
It is the entry point where the program starts running. -> Option A
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
Step 1: Recall Kotlin function syntax
Kotlin functions start with fun, followed by the name and parentheses.
Step 2: Check each option
Only fun main() { println("Hello") } uses fun main() correctly; others use wrong keywords or miss fun.
Final Answer:
fun main() { println("Hello") } -> Option C
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
Step 1: Read the print statements order
The program prints "Start" first, then "End" on separate lines.
Step 2: Understand println behavior
println adds a new line after each output, so outputs appear on separate lines.
Final Answer:
Start\nEnd -> Option A
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
Step 1: Check main function parameter type
Main function parameters must be Array<String> or Array<String>, not a single String.
Step 2: Verify other options
Function name is lowercase as required; println is valid; return type is optional for Unit.
Final Answer:
The parameter should be an array of strings, not a single string. -> Option B
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
Step 1: Identify correct main function parameter type
Main function must accept Array<String> to receive command-line arguments.
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.
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.
Final Answer:
fun main(args: Array<String>) { for (arg in args) println(arg) } -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.