Pergunta de entrevista da empresa Goldman Sachs

public class WhichReturns{ public String whichReturns(){ try{ return "1"; }finally{ return "2"; } public static void main(String argv){ System.out.println("Return val: " + WhichReturns.whichReturns()); } }

Respostas da entrevista

Sigiloso

12 de mai. de 2010

2

Sigiloso

23 de set. de 2010

The runtime system always executes the statements within the finally block regardless of what happens within the try block. So it's the perfect place to perform cleanup. 2 is the answer

Sigiloso

29 de set. de 2012

whichReturns() needs to be declared as static for this code to compile, i.e. public static String whichReturns() Once that's done then the program outputs "2"

Sigiloso

29 de set. de 2012

In main, WhichReturns.whichReturns() can only work if whichReturns() is declared static