Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Finding the output (Q.No. 12)
12.
What will be the output of the program?
class Test
{
static int s;
public static void main(String [] args)
{
Test p = new Test();
p.start();
System.out.println(s);
}
void start()
{
int x = 7;
twice(x);
System.out.print(x + " ");
}
void twice(int x)
{
x = x*2;
s = x;
}
}
Answer: Option
Explanation:
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.
Discussion:
12 comments Page 2 of 2.
Raj Patel said:
7 years ago
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.
So x=7 and s=14.
Deepak bansal said:
7 years ago
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers