Sail E0 Webinar
Question


What will be the output of the program?


public class Test
{
public static void leftshift(int i, int j)
{
i
Options:
A .  2
B .  4
C .  8
D .  16
Answer: Option B

Java only ever passes arguments to a method by value (i.e. a copy of the variable) 

and never by reference. Therefore the value of the variable i remains unchanged in 

the main method.

If you are clever you will spot that 16 is 4 multiplied by 2 twice, (4 * 2 * 2) = 16. If you 

had 16 left shifted by three bits then 16 * 2 * 2 * 2 = 128. If you had 128 right shifted by

2 bits then 128 / 2 / 2 = 32. Keeping these points in mind, you don't have to go converting 

to binary to do the left and right bit shifts.



Was this answer helpful ?
Next Question

Submit Solution

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

Latest Videos

Latest Test Papers