GATE CSE 2020 Q58: Value of pp(3, 4) for binary exponentiation code
- Programming and Data Structures
- 2 marks
- NAT
- No negative marking
Question
Consider the following C functions.
int tob(int b, int* arr) {
int i;
for (i=0; b>0; i++) {
if (b%2) arr[i]=1;
else arr[i]=0;
b = b/2;
}
return(i);
}
int pp(int a,int b) {
int arr[20];
int i, tot = 1, ex, len;
ex = a;
len = tob(b,arr);
for (i=0; i<len; i++) {
if (arr[i]==1)
tot = tot * ex;
ex = ex * ex;
}
return(tot);
}The value returned by pp(3,4) is _______.
Try it: numerical answer: type the value
Numerical answer. No negative marking.
Show answer and explanation
Answer: 81 (official GATE 2020 final answer key)
Concept used: tob writes the binary digits of b (least significant first); pp is square-and-multiply exponentiation.
First step: tob(4) gives bits 0, 0, 1 and length 3; follow tot and ex through the loop with ex = 3, 9, 81.
Full step-by-step solution
Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.
Share this question
Source: official GATE 2020 CS question paper and final answer key, organised by IIT Delhi. Question paper · Answer key
More Programming and Data Structures PYQs
- 2026 Session 1 · Q23Non-leaf indices in a 1-indexed binary heap with n odd
- 2026 Session 1 · Q24Empty slots after linear-probing inserts with h(x) = (x+7) mod 11
- 2026 Session 1 · Q33Maximum height of a full binary tree with 23 nodes
- 2026 Session 1 · Q34Output of a C function with a shadowed block variable
- 2026 Session 1 · Q39Recursive length of a non-empty singly linked list
- 2026 Session 1 · Q40Third insertion that can never yield a complete BST of 1..15