ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (checking if one string is a rotation of another)?
What will be the output of the following pseudocode (checking if one string is a rotation of another)?
Function isRotation(String a, String b)
return length(a) == length(b) and contains(a + a, b)
End Function
Print isRotation("waterbottle", "erbottlewat"), isRotation("abc", "acb")- A. true true
- B. false false
- C. true false
- D. false true
Correct answer: C. true false
Concatenating a string with itself contains every rotation of it as a substring: 'waterbottlewaterbottle' does contain 'erbottlewat', so the first is true. 'abcabc' does not contain 'acb' anywhere, so the second is 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 (counting total set bits across 1..n)? Function countSetBitsRange(Integer n) Integer…
- What will be the output of the following pseudocode (depth-first traversal of a graph given as an adjacency list)? Map graph = {0: [1, 2]…
- What will be the output of the following pseudocode (breadth-first traversal using a queue)? Map graph = {0: [1, 2], 1: [0, 3], 2: [0, 3]…
- What will be the output of the following pseudocode? Integer x = 1, i = 1 while (i <= 5) x = x + i i i = i + 1 end while Print x
- What will be the output of the following pseudocode? Integer c = 0, n = 5 for (each i from n down to 1) for (each j from 1 to i) if (j mod…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[6] = {10, 20, 30, 40, 50, 60} Integer…