153k views
2 votes
Consider the following Java program:

public class MyClass {
private int myVlaue = 5;
public void printMyVlaue() {
System.out.println(myVlaue);
}
public void setMyVlaue(int myVlaue) {
System.out.println(myVlaue);
this.myVlaue = myVlaue;
}
public static void main(String[] args) {
MyClass myClass1 = new MyClass();
myClass1.setMyVlaue(10);
myClass1.printMyVlaue();
}
}
Which of the following will the output be?
(a) 5 10
(b) 10 10
(c) 10 5
(d) 5 5
(e) 15 5

1 Answer

7 votes

Final answer:

The Java program first prints the value 10 passed to the setMyVlaue method and then prints the updated instance variable myVlaue, also 10. Therefore, the output of the program is 10 10, which corresponds to option (b).

Step-by-step explanation:

In the given Java program, when the main method is executed, a new object myClass1 of MyClass is created and setMyVlaue method is then called with the argument 10. Inside the setMyVlaue method, the line System.out.println(myVlaue); will print out the argument value, which is 10. Then, the line this.myVlaue = myVlaue; assigns the instance variable myVlaue the new value of 10. Following this, the printMyVlaue method is invoked, which prints the instance variable myVlaue now holding the value 10. Thus, the output is 10 10, which corresponds to option (b).

User Vasiliy Stavenko
by
8.9k points