0
0
Kotlinprogramming~5 mins

Main function as entry point in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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[] {}
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.
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) {}
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.
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.
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.