Anonymous Classes — Concept Check
Questions on Anonymous Classes. Anonymous classes are nameless one-shot class declarations that implement an interface or extend a class inline, without a separate file.
Questions in this quiz
8 questions on Anonymous Classes — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer needs to implement a one-off Comparator for sorting strings by length. Which approach is more concise and preferred?
- ACreating a named class
- BUsing a static method
- CUsing a lambda expression
- DUsing an anonymous class
2.In an anonymous class, what must be true about variables captured from the enclosing scope?
- AThey can be modified
- BThey must be effectively final
- CThey must be static
- DThey must be private
3.A developer uses an anonymous class to implement MouseListener with multiple methods. Why might they choose this over a lambda?
- ALambdas can't implement multiple methods
- BAnonymous classes are more secure
- CAnonymous classes are faster
- DLambdas are more verbose
4.Why might a developer use an anonymous class instead of a lambda for a Runnable?
- ALambdas cannot be used with Runnable
- BAnonymous classes are more efficient
- CTo maintain local state within the Runnable
- DLambdas do not support multithreading
5.A developer needs to capture a generic type at runtime using Jackson. Which approach should they use?
- ALambda expression
- BNamed class with TypeReference
- CAnonymous class with TypeReference
- DStatic method
6.What happens if you try to define a constructor in an anonymous class?
- ACompilation error
- BIt works as expected
- CIt gets ignored
- DRuntime error
7.A developer mistakenly believes lambdas can replace anonymous classes in all cases. Which scenario proves this wrong?
- AImplementing an interface with multiple methods
- BCreating a simple comparator
- CImplementing a single-method interface
- DRunning a background task
8.A developer uses an anonymous class for a Runnable and prints 'this.getClass().getSimpleName()'. What will it output?
- AThe name of the enclosing class
- BThe name of the anonymous class
- CA runtime error
- D'Runnable'