ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function isPalindrome(String s)
Integer i = 0, j = length(s) - 1
while (i < j)
if (s[i] != s[j])
return false
end if
i = i + 1
j = j - 1
end while
return true
End Function
Print isPalindrome("racecar"), isPalindrome("hello")- A. false true
- B. true false
- C. true true
- D. false false
Correct answer: B. true false
The two-pointer check compares characters from both ends inward. 'racecar' mirrors perfectly at every step, so it returns true. 'hello' fails at the very first comparison ('h' != 'o'), returning false.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[5] = {1, 2, 3, 4, 5} Integer s = 0 for (each…
- What will be the output of the following pseudocode? (Matrices are indexed from 0.) Integer mat[3][2] = {{1, 2}, {3, 4}, {5, 6}} Integer…
- What will be the output of the following pseudocode (merging two sorted arrays)? (Arrays are indexed from 0.) Integer a[4] = {1, 3, 5, 7}…
- What will be the output of the following pseudocode? Integer c = 0, n = 5 for (each i from 1 to n) for (each j from 1 to 2 i - 1) c = c +…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[6] = {4, 8, 15, 16, 23, 42} Integer target =…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function countDigits(Integer…