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.
Radistao said:
10 years ago
System.out.print(x + " ");
Hey, there is no place for second variable output! So, only x is being printed. please, fix the task description.
Hey, there is no place for second variable output! So, only x is being printed. please, fix the task description.
Tulshiram said:
1 decade ago
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers