Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Finding the output (Q.No. 15)
15.
What will be the output of the program?
public class Test
{
public static void leftshift(int i, int j)
{
i <<= j;
}
public static void main(String args[])
{
int i = 4, j = 2;
leftshift(i, j);
System.out.println(i);
}
}
Answer: Option
Explanation:
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.
Discussion:
9 comments Page 1 of 1.
Roscoe said:
3 years ago
@All.
There is no return type in a left-shift method so the ans remains the same in the main.
i.e i=4.
There is no return type in a left-shift method so the ans remains the same in the main.
i.e i=4.
(1)
Bharati Kashid said:
6 years ago
What is the perfect answer? please tell me.
Jagdish said:
7 years ago
The correct Answer is 16.
Kunal said:
8 years ago
@Sathish.
If we print j instead of i, output will be 2. i and j are not affected in main. I agree with your explanation @Manukundloo and Pooja.
If we print j instead of i, output will be 2. i and j are not affected in main. I agree with your explanation @Manukundloo and Pooja.
Sathish said:
9 years ago
If we print j what will be the output?
Manukundloo said:
1 decade ago
@Pooja.
Your explanation is really appreciable, but there must need some correction. The output is 4 not only because the earlier function doesn't return any value;if it returns then the value of i will be same. It's all because java work on value not the reference, and here 'i' is taking value from the local variable. If the value of i will be global and static then the value of i will be same as the global variable.
Your explanation is really appreciable, but there must need some correction. The output is 4 not only because the earlier function doesn't return any value;if it returns then the value of i will be same. It's all because java work on value not the reference, and here 'i' is taking value from the local variable. If the value of i will be global and static then the value of i will be same as the global variable.
Pooja niranjane said:
1 decade ago
Hi @Ashi,
The role of public static void is you check in main function we call this function without creating object.
If function in same class and if it is static then there is no need to create object of that class,we can call directly.
i<<=j
i=i<<j
so it like 4<<2
i.e 4 * 2*2
=16.
But the function doesn't return any value.
So value in i already 4.
It is not overwriting any value in i.
So the answer is 4.
The role of public static void is you check in main function we call this function without creating object.
If function in same class and if it is static then there is no need to create object of that class,we can call directly.
i<<=j
i=i<<j
so it like 4<<2
i.e 4 * 2*2
=16.
But the function doesn't return any value.
So value in i already 4.
It is not overwriting any value in i.
So the answer is 4.
(1)
Guru said:
1 decade ago
Is operation of i<<=j; and i<<j are same?
And if so answer would be 8. Since (4<<2).
And if so answer would be 8. Since (4<<2).
Ashi said:
1 decade ago
public static void leftshift(int i, int j)
{
i <<= j;
}
what is the role of public static void in this function..??
{
i <<= j;
}
what is the role of public static void in this function..??
(2)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers