Given:
What is the result?
A.
myStr: 9009, myNum: 9009
B.
myStr: 7007, myNum: 7007
C.
myStr: 7007, myNum: 9009
D.
Compilation fails
Given:
What is the result?
A.
myStr: 9009, myNum: 9009
B.
myStr: 7007, myNum: 7007
C.
myStr: 7007, myNum: 9009
D.
Compilation fails
C. myStr: 7007, myNum: 9009
Indeed, C is correct. Scope of the variables is important here
package q046;
public class App {
String myStr = “7007”;
public void doStuff(String str) {
int myNum = 0;
try {
String myStr = str; //”9009 -> scope myStr inside try-block only
myNum = Integer.parseInt(myStr);//9009
} catch (NumberFormatException ne) {
System.err.println(“Error”);
}
System.out.println(“myStr: “+ myStr //”7007 -> not local variable
+”, myNum: “+ myNum); //9009
}
public static void main(String[] args) {
App obj = new App();
obj.doStuff(“9009”);
}
}
C