1) What does f(31415927) return, for the following function definition?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def f(x):
d=0
while x > 1:
(x,d) = (x/2,d+1)
return(d)
def f(x): d=0 while x > 1: (x,d) = (x/2,d+1) return(d)
def f(x):
   d=0
   while x > 1:  
      (x,d) = (x/2,d+1)
   return(d)
Answer(s) : 25

[quads id=1]

2) What is the value of h(6,8) for the function below?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def h(m,n):
ans = 1
while (n > 0):
(ans,n) = (ans*m,n-2)
return(ans)
def h(m,n): ans = 1 while (n > 0): (ans,n) = (ans*m,n-2) return(ans)
def h(m,n):
  ans = 1
  while (n > 0):
    (ans,n) = (ans*m,n-2) 
  return(ans)
Answer(s) :  1296

3)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def h(n):
f = 0
for i in range(1,n+1):
if n%i == 0:
f = f + 1
return(f == 2)
def h(n): f = 0 for i in range(1,n+1): if n%i == 0: f = f + 1 return(f == 2)
def h(n):
  f = 0
  for i in range(1,n+1):
    if n%i == 0:
      f = f + 1
  return(f == 2)

The function h(n) given above returns True for a positive number n whenever:

(A) n is a multiple of 2
(B) n is a composite number
(C) n is a prime number
(D) n has an even number of factors

Answer(s) : (C) n is a prime number

[quads id=1]

4) Consider the following function f

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
def f(m):
if m == 0:
return(1)
else:
return(m*f(m-1))
def f(m): if m == 0: return(1) else: return(m*f(m-1))
def f(m):
  if m == 0:
    return(1)
  else:
    return(m*f(m-1))

(A) The function always terminates with f(n) = n
(B) The function always terminates with f(n) = factorial of n
(C) The function terminates for non-negative n with f(n) = n
(D) The function terminates for non-negative n with f(n) = factorial of n

Answer(s) :  (D) The function terminates for non-negative n with f(n) = factorial of n