logo CodeStepByStep logo

OneTwoThree

Language/Type: Java parameters return
Author: Mehran Sahami (on 2017/10/03)

For the following console program, trace through its execution by hand to write the four lines of output that are produced when it runs:

/*
 * File: OneTwoThree.java
 * -------------------
 * This program is designed to test your understanding of parameters,
 * return values, and instance variables
 */
public class OneTwoThree {
    public static void main(String[] args) {
        int a = 100;
        addOne();
        addTwo(a);
        a = addThreeAndReturnResult(a);
        println("run: a = " + a + ", b = " + b);
    }

    private static void addOne() {
        int a = 101;
        b++;
        println("addOne: a = " + a + ", b = " + b);
    }

    private static void addTwo(int a) {
        a += 2;
        b += 2;
        println("addTwo: a = " + a + ", b = " + b);
    }

    private static int addThreeAndReturnResult(int a) {
        a += 3;
        b += 3;
        println("addThreeAndReturnResult: a = " + a + ", b = " + b);
        return a;
    }

    /* Private instance variable */
    private int b = 200;
}
line 1
line 2
line 3
line 4

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.