Question: public class MyClass { private Map map; public MyClass() { map = new HashMap (); map.put(foo, 1); map.put(bar, 3); } public int getValue(String input, int

public class MyClass { private Map map; public MyClass() { map = new HashMap<>(); map.put("foo", 1); map.put("bar", 3); } public int getValue(String input, int numRetries) throws Exception { try { return map.get(input); } catch (Exception e) { if (numRetries > 3) { throw e; } return getValue(input, numRetries + 1); } } } Question: How many times will 'getValue()' execute with the following inputs, and what will be the result? (1) getValue("foo", 0); (2) getValue("bar", 2); (3) getValue("baz", 0); (4) getValue("fubar", 1);

My answer is as follows, I would just like to see if it is correct:

1) runs getValue 1 time and returns 1 2) runs getValue 1 time and returns 3 3) runs getValue 3 times and throws an exception after the third run 4) runs getValue 2 times and throws an exception after the second run

In total getValue runs 7 times

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!