...
Database-Management-System
September 19, 2024
Logic-Gates-and-operators
September 20, 2024
Database-Management-System
September 19, 2024
Logic-Gates-and-operators
September 20, 2024

UGC NET CS 2018-DEC Paper-2

Question 70
What does the following java function perform ? (Assume int occupies four bytes of storage)
Public static int f(int a)
{
//Pre-conditions : a>0 and no overflow/underflow occurs
int b=0;
for(int i=0; i<32; i++)
{
b=b <<1;
b= b | (a & 1);
a = a >>> 1; // This is a logical shift
}
Return b;
}
A
Return the int that represents the number of 0’s in the binary representation of integer a.
B
Return the int that represents the number of 1’s in the binary representation of integer a.
C
Return the int that has the reversed binary representation of integer a.
D
Return the int that has the binary representation of integer a.
Question 70 Explanation: 
→ Consider the a value is 5 .
The initial value of b is 0 and b<<1 means b will get value of 2
b=b|(a&1) which means 2|(5&1) then b will get value “2”
a=a<<1 means value a will reduce to 3
→ Repeat the above procedure for 31 iterations .
→ Iteration-2: b=b<<1 then b is 4 and b=b|(a&1)= 4|(3&1) then b is 5 and a becomes 1
→ Iteration-3: b=b<<1 then b is 10 and b=b|(1&1)= 4|(3&1) then b is 11 and a becomes 0
→ And this procedure will repeat until “i” value is 32.
→ The final value is 11 (Number of 1’s in the result is 2) and the number of 1’s in the input 5 are 2.
Correct Answer: C
Question 70 Explanation: 
→ Consider the a value is 5 .
The initial value of b is 0 and b<<1 means b will get value of 2
b=b|(a&1) which means 2|(5&1) then b will get value “2”
a=a<<1 means value a will reduce to 3
→ Repeat the above procedure for 31 iterations .
→ Iteration-2: b=b<<1 then b is 4 and b=b|(a&1)= 4|(3&1) then b is 5 and a becomes 1
→ Iteration-3: b=b<<1 then b is 10 and b=b|(1&1)= 4|(3&1) then b is 11 and a becomes 0
→ And this procedure will repeat until “i” value is 32.
→ The final value is 11 (Number of 1’s in the result is 2) and the number of 1’s in the input 5 are 2.
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
error: Alert: Content selection is disabled!!