TD Tutor
Magic Numbers Quiz
Replacing Magic Numbers
Question
1
of 5
1
Why are magic numbers problematic?
Hard to understand purpose
Changes require hunting through code
Can lead to inconsistencies
All of the above
Correct!
Incorrect. The correct answer is:
2
Where should constants be defined?
In the class where used
In a dedicated constants class
In configuration files
Depends on scope and usage
Correct!
Incorrect. The correct answer is:
3
When might magic numbers be acceptable?
In mathematical formulas where the number is part of the logic
In test code
When the meaning is immediately obvious in context
All of the above
Correct!
Incorrect. The correct answer is:
4
What is the advantage of using named constants over configuration files?
Better performance
Compiler can optimize better
Type safety
All of the above
Correct!
Incorrect. The correct answer is:
5
Which of these is NOT a good candidate for replacing with a constant?
Maximum login attempts
Default timeout value
Pi (3.14159)
Loop counter in a simple for loop
Correct!
Incorrect. The correct answer is:
Previous
Next
Submit Quiz
Quiz Results
0
/5
Back to Tutorial