coding pdfvbb
coding pdfvbb
Questions
30 Multiple Choice Questions with
Solutions
Questions
1. Consider the following recursive function used in an Infosys assessment. What will be the output for input values
x = 4 and y = 3?
calculateSum(Integer x, Integer y)
x=4 & y=3
if(y EQUALS 0)
return 0 return calculatesum(8 , 1)+4
if(y mod 2 EQUALS 0)
return calculateSum(x + x, y/2)
return calculateSum(x + x, y/2) + x x= 8 , y=1
End function return calculate sum(16,0)+8
a. 16
b. 12
c. 8
d. 20
2. In an Infosys coding round, the following function was given. What would be its output for parameters a = 3 and b
= 4? ans = 3+3+
1st iteration:
recursiveCompute(Integer a, Integer b) a = 3 , b=4
if(b EQUALS 1) return 3+rc(3,3)
return a
else
2nd iteration:
return a + recursiveCompute(a, b-1) a=3,b=3
End function 3+rc(3,2)
a. 12 3+ rc(3,1)
b. 9
c. 15
d. 6
3. A candidate was asked to analyze this code snippet in an Infosys interview. What will be the output if x = 5 and y
= 3?
x=5, y=3
processValues(Integer x, Integer y)
Integer result = 0 1st 3%2 = 1 -> res = 0+5 = 5 -> res =5
while(x > 0)
x=4 , y=4
if(y mod 2 EQUALS 1) 4%2 = 0 (false)
result = result + x
x= 3, y=5
x = x - 1
res = 5+3 = 8
y = y + 1
End while
x=2 , y=6
false
return result
End function x =1, y=7
res = 8+1 = 9
a. 9 x=0,y=8
b. 13
c. 10
d. 12
4. During an Infosys online assessment, the following code was presented. What will be printed if initial value of n =
16?
Integer findPattern(Integer n)
if(n <= 1)
16%2=0
return 1
call function(8)
if(n mod 2 EQUALS 0) callfunction(4)
return findPattern(n/2)
callfunction(2)
callfunction(1) -> return 1
else
return findPattern(3*n + 1)
End function
a. 4
b. 1
c. 2
d. 8
5. In an Infosys technical round, candidates were given this code. What's the output for a = 7?
a=7
6. The following bit manipulation code appeared in an Infosys test. What's the output for n = 42?
a. 3
b. 4
c. 5
d. 6
7. An Infosys interviewer asked about this pattern-generating code. What's printed for n = 4?
*
void printPattern(Integer n) i= 1 , j = 1 to 1 -> *
**
i=2 , j= 1 to 2(1&2) -> **
for i = 1 to n ( 1-4) ***
i=3, j= 1 to 3 -> ***
****
for j = 1 to i i=4 , j= 1 to 4 -> ****
print "*"
End for
print newline
End for
End function
b-> * *
** *
*
*** **
*
***** -> c *** -> d
a. 4 stars in a line **** *
b. 4 stars in a column
c. 10 stars in a triangle
d. 6 stars in a triangle
8. This array manipulation code was part of an Infosys assessment. What's the result for arr = [1,2,3,4,5]?
a. 15
b. 6
c. 9
d. 12
9. During an Infosys coding test, this function was given. What's returned for str = "INFOSYS"?
10. Candidates faced this logical operation code in an Infosys exam. What's the output for a = 15, b = 12?
a. 27
b. 23
c. 25
d. 24
11. What will be the output of this Infosys assessment code for x = 8?
12. This pattern matching code appeared in an Infosys test. What's the output for n = 3?
13. An Infosys interviewer presented this recursive code. What's the output for n = 5?
a. 9
b. 15
c. 6
d. 12
14. This bit manipulation question appeared in an Infosys test. What's returned for x = 36?
a. 3
b. 2
c. 4
d. 5
15. During an Infosys coding round, this array processing code was given. What's the output for arr = [2,4,6,8,10]?
a. 2
b. 3
c. 1
d. 4
16. What will be the output of this string manipulation code from an Infosys assessment for str = "PROGRAM"?
Integer analyzePattern(String str)
Integer count = 0
for i = 0 to length(str)-1
if str[i] EQUALS str[length(str)-1-i]
count = count + 1
End for
return count
End function
a. 1
b. 2
c. 3
d. 4
17. This mathematical sequence appeared in an Infosys test. What's the output for n = 6?
Integer generateSequence(Integer n)
if(n <= 2)
return 1
return generateSequence(n-1) + generateSequence(n-3)
End function
a. 5
b. 4
c. 6
d. 7
18. An Infosys interviewer asked about this array transformation code. What's returned for arr = [1,3,5,7,9]?
a. 15
b. 25
c. 8
d. 12
19. What will be the output of this logical operation code from an Infosys test for a = 6, b = 9?
0110 -> 6
1001 ->9
a. 15 15+15->30 or
1111 -> 15 (a^b) a&b -> 0000 -> 0 -> not(0) -> 1
b. 13 15+1=16
c. 14 or
a&b -> 0000 -> not(0000) -> 1111 ->15
d. 16
20. During an Infosys assessment, this pattern recognition code was given. What's the output for n = 4?
Integer recognizePattern(Integer n)
i=1 -> res = 0+1 =1
Integer result = 0 i=2 -> res = 1+2 =3
for i = 1 to n
i=3 -> skip
i=4 -> res =3+4 =7
if(n mod i EQUALS 0)
result = result + i
End for
return result
End function
a. 7
b. 8
c. 9
d. 10
21. This recursive pattern appeared in an Infosys coding test. What's returned for n = 5?
a. 6
b. 8
c. 10
d. 12
22. An Infosys interviewer presented this string counting code. What's the output for str = "INFOSYS2024"?
Integer countSpecial(String str)
Integer count = 0
for each char in str
if char is digit OR char in ['A','E','I','O','U']
count = count + 1
End for
return count
End function
a. 5
b. 6
c. 7
d. 8
23. What will be the output of this array processing code from an Infosys test for arr = [2,3,4,5,6]?
a. 48
b. 24
c. 36
d. 12
24. During an Infosys coding round, this mathematical sequence was given. What's returned for n = 7?
a. 84
b. 77
c. 91
d. 105
25. This bit counting code appeared in an Infosys assessment. What's the output for n = 15?
Integer countSetBits(Integer n) 15 -> 1111 c=1
Integer count = 0 14-> 1110 -> 1110
while(n > 0)
1110
n = n AND (n-1) 1101
count = count + 1
End while
return count
End function
a. 3
b. 4
c. 5
d. 6
26. An Infosys interviewer asked about this array manipulation code. What's returned for arr = [1,2,3,2,1]?
a. true
b. false
c. 1
d. 0
27. What will be the output of this pattern generation code from an Infosys test for n = 3?
a. 23
b. 25
c. 27
d. 29
28. During an Infosys assessment, this string manipulation code was given. What's the output for str = "TESTING"?
a. "TEST"
b. "TSIG"
c. "TING"
d. "ESTN"
29. This mathematical computation appeared in an Infosys coding test. What's returned for a = 4, b = 3?
a. 64
b. 81
c. 16
d. 32
30. An Infosys interviewer presented this array processing code. What's the output for arr = [4,2,7,1,9]?
Integer analyzeArray(Array arr)
Integer max_diff = 0
4-2 = 2>0 so max_diff =2
for i = 0 to length(arr)-2
abs(2-7) =5>2 sp max_diff =5
if abs(arr[i] - arr[i+1]) > max_diff
max_diff = abs(arr[i] - arr[i+1])
End for
return max_diff
End function
a. 5
b. 6
c. 7
d. 8