Compilers
What is the main benefit of compile-time type checking in a statically typed language?
- AMismatched operations and assignments are found before the program runs
- BType declarations disappear, so programs always get shorter
- CEvery error that could occur at run time is eliminated at compile time
Show answer and explanation
Answer: A. Mismatched operations and assignments are found before the program runs
Type checking catches errors such as adding an integer to a string or passing an argument of the wrong type before the program runs. Errors are found early, and type information also helps the compiler choose efficient machine code. It cannot, however, eliminate value-dependent errors such as division by zero or out-of-bounds array accesses. Declaring types does not necessarily make programs shorter either.
- A. Correct. Type errors are found before execution.
- B. Declaring types does not necessarily shorten programs.
- C. Value-dependent run-time errors cannot be eliminated.