Sail E0 Webinar
Question


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;
}
}
Options:
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.



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers