Examveda
Examveda

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

C. 12

D. 11

E. None of these

Answer: Option B

Solution(By Examveda Team)

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.

This Question Belongs to Java Program >> Data Types And Variables

Join The Discussion

Comments ( 17 )

  1. Syed Sameer
    Syed Sameer :
    11 months ago

    Normally 1st priority will be given to local variable, so as we are passing 3 as an argument that will be considered 1st instead of Class identifier.

  2. Anoop Kumar
    Anoop Kumar :
    11 months ago

    Unable to understand

  3. Kumar Shivakar
    Kumar Shivakar :
    2 years ago

    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);
    }
    }
    Hey, here main is calling method with object reference t by passing the value 3, so method will calculate the value of a, because a+=1; can be written as a=(int)(a+1), so here method receiving 3 in a and will calculate the value and will assign to a is nothing but a=(int)(3+1), i.e nothing but 4, and its increasing the value of a by using pre increment operator, so it will increase first from 4 to 5 then it will print 5.

    public class Test
    {
    int a = 10;
    {
    a += 1;
    System.out.println(++a);
    }
    public static void main(String args[])
    {
    Test t = new Test();
    }
    }

    if your code as same above then it will print 12.

    Thank you.

  4. Swapnali Mane
    Swapnali Mane :
    4 years ago

    how it is possible???

  5. Rohan Singh
    Rohan Singh :
    4 years ago

    Guys See in Last there Method Call so its like .....t.method(3); it means value a is assigned as 3 so its 5 at time of sopln.

  6. Rajesh C
    Rajesh C :
    5 years ago

    See it is simple the excusion of program first finds the 1) static variable
    2) static block
    3) static method ( public static void main()) in this example.
    Control goes to 3rd one..
    t.method(3)
    Hence a value become 3.
    Control not goes to int a = 10.

  7. Sangee Geetha
    Sangee Geetha :
    6 years ago

    how it is possible explain?

  8. Joystan Rolan
    Joystan Rolan :
    6 years ago

    Bcz parameter in method(a) is local variable. Once the execution comes to this line, the activation record for method(a) in stack will be created and instance variable a=10 will be in the heap memory for which the control will not be there

  9. Bagoria Saabh
    Bagoria Saabh :
    6 years ago

    how it is possible

  10. JAGADEESH T
    JAGADEESH T :
    6 years ago

    how?

  11. CHANDNIKA RAVICHANDRAN
    CHANDNIKA RAVICHANDRAN :
    7 years ago

    argument values are given higher priority than the instance variables

  12. Chitra Preethi
    Chitra Preethi :
    7 years ago

    how it is possible???

  13. Sukanya R
    Sukanya R :
    7 years ago

    very bad

  14. Sunil Kumar
    Sunil Kumar :
    7 years ago

    a=5 because method pass the value a is 3.

  15. Aditya Patwardhan
    Aditya Patwardhan :
    7 years ago

    here output is 5 because
    first the original input a is 3 then a+=1 operation is performed on a which is calculated as a=a+1 so now a =3+1 =4 and then at the print statement ++a is to be printed so in ++a operation first value of a is incremented by 1 and then it is printed so answer is 5

  16. Arka Maji
    Arka Maji :
    7 years ago

    how?

  17. Arka Maji
    Arka Maji :
    7 years ago

    how?

Related Questions on Data Types and Variables