Java Tricky Question: Does finally override try return value?

Java Tricky Question Most developers think they know what finally does… until they see this. What do you think this program prints? class Test { public static void main(String[] args) { System.out.println(test()); } static int test() { try { return 10; } finally { return 20; } } } Hint: finally always executes but does it override the value returned from try? #Java #CodingChallenge #Developers #ProgrammingPuzzle #JavaTrickyQuestions #LearnJava #BackendDeveloper

  • No alternative text description for this image

True: The finally block always runs — it’s like a rule in Java that it must execute before finishing the method. False: The value from try isn’t final — because if finally also returns something, it replaces whatever try was going to return.

To view or add a comment, sign in

Explore content categories