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);
}
}
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.
Join The Discussion
Comments ( 17 )
Related Questions on Data Types and Variables
What is the maximum value that can be stored in a byte variable in Java?
A. 127
B. 255
C. 32767
D. 64
What is the default value of an int variable in Java if it's not explicitly initialized?
A. 0
B. 1
C. -1
D. Null
Which of the following is not a valid identifier for a Java variable?
A. my_var
B. _myVar
C. 3rdVar
D. $var
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.
Unable to understand
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.
how it is possible???
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.
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.
how it is possible explain?
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
how it is possible
how?
argument values are given higher priority than the instance variables
how it is possible???
very bad
a=5 because method pass the value a is 3.
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
how?
how?