Q57
Determine output: public class Test int a = 10; public void method(int a) a += 1; System.out.println(++a); public static void main(String args[]) Test t = new Test(); t.method(3);
A.
4
B.
5
AnswerC.
12
D.
11
E.
None of these
Answer: Option B
Solution
Answer: Option B
Solution:
Here's an explanation of the code:1. The class
Test is defined with an instance variable a initialized to 10.2. The
method method is defined within the Test class, which takes an integer parameter a.3. Inside the
method method, the local variable a shadows the instance variable a because they have the same name.4. The value of the local variable
a is incremented by 1 using the += operator, making it 4.5. The value of the local variable
a is then incremented again using the pre-increment operator ++, making it 5.6. The value of the local variable
a (which is 5) is printed using System.out.println().7. In the
main method, an instance of the Test class is created.8. The
method method is invoked on the t object with the argument 3.9. This triggers the execution of the
method method, which prints 5 as explained above.Therefore, the output of the code is 5. Thus, option B is the correct answer.