What is the result?


What is the result?
A. myStr: 9009, myNum: 9009
B. myStr: 7007, myNum: 7007
C. myStr: 7007, myNum: 9009
D. Compilation fails

Download Printable PDF. VALID exam to help you PASS.

2 thoughts on “What is the result?

  1. String myStr = “7007”;

    void doStaff(String str){
    int myNum = 0;
    String myStr=str;//new variable
    }catch(NumberFormatException e){
    System.out.println(myStr + ” ,” + myNum);//7007 ,9009


    public static void main(String[] args) {
    Q97 app = new Q97();

  2. Answer C:

    Note that in the try block a new local String variable is created, and it happens to have the same name as the instance variable “myStr”. This variable is scoped only within the try block. So the value of the instance variable does not get re-assigned; it remains 7007. But what get changed though is the myNum variable, which is a local main() variable.
    Since the println() method is local in main(), then both variables (i.e. the instance variable myStr, and the local main() variable myNum) get printed.

Leave a Reply

Your email address will not be published. Required fields are marked *

The reCAPTCHA verification period has expired. Please reload the page.