Java Programming - Operators and Assignments - Discussion


What will be the output of the program?

class Test 
    static int s;
    public static void main(String [] args) 
        Test p = new Test();

    void start() 
        int x = 7;
        System.out.print(x + " ");

    void twice(int x) 
        x = x*2;
        s = x;

[A]. 7 7
[B]. 7 14
[C]. 14 0
[D]. 14 14

Answer: Option B


The int x in the twice() method is not the same int x as in the start() method. Start()'s x is not affected by the twice() method. The instance variable s is updated by twice()'s x, which is 14.

Tulshiram said: (Apr 26, 2013)  
In start () method x is local variable so, when twice() function call it update only 's' as class variable not x and also twice as return type is void means as know every one, that the reason answer will be 7 14.

Radistao said: (Dec 5, 2015)  
System.out.print(x + " ");

Hey, there is no place for second variable output! So, only x is being printed. please, fix the task description.

Jayashree said: (Jul 24, 2016)  
Anyone can explain it briefly.

Akshay S Y said: (Nov 10, 2016)  
In the start() method, int x variable is different for compiler, in other words if we can declare twice(int z) and z = z*2; which is assigned to s=(7*2)=14;

But print method local variable x is called so value 7.
In simple words, the above program they have declared with same variable name x instead of some other variable name which is treated as a different variable by the compiler.

Aashish Chugh said: (Nov 26, 2016)  
When I executing this code the output came as (7, 0), anyone please explain why this come?

Msheliga said: (Jan 28, 2017)  
The solution states that s is an instance variable. It should be corrected to note that s is a class (static) variable.

Sachin said: (Mar 3, 2017)  
The int x in the twice() method is not the same int x as in the start() method. Why? Explain.

Kajal said: (Nov 24, 2017)  
The output after executing the code:
false true
Why? Please explain.

Rasmi Ranjan Choudhury said: (Jun 1, 2018)  

When you call p.start() method, it go to start method create local variable x=7.

When you call twice(x) then x value 7 go to twice(int x) method and create another local variable.

which is x scope is within the twice method.
x=x*2 means x=7*2=14 ,
x=14 ,and 14 store in twice method x variable nt start() method bcoz twice method
scope with in twice (){ to close bracket.}
s=x, here 14 assign to s and s is a static variable so class level variable s value changes the default value to 14,

then finally it print x=7 and s=14.
So, x 7 because start() value of x is 7 and twice method() x value not change start() x. Because after twice () end x value destroyed.

Raj Patel said: (Aug 9, 2018)  
A local variable is for only their function so, after twice method new x value destroys and then consider the original x value is 7.
So x=7 and s=14.

Deepak Bansal said: (Sep 19, 2018)  
As I know a static variable can call by static method only but here twice is not a static method so how can it call s?

Nikhil Pattanaik said: (Mar 18, 2022)  
Here, s is static variable. And static variables can only used by static methods or static blocks. So how can a non static twice method update the value of a static variable s. The s should be a class variable only not static.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.