ProgrammingHardTCS NQT Advanced · Accenture
What will be the output of the following pseudocode? (parameters are passed by value; inside g, x is the global x; operands are evaluated left to right)
What will be the output of the following pseudocode? (parameters are passed by value; inside g, x is the global x; operands are evaluated left to right)
Integer x = 5
Function f(Integer x)
x = x + 1
return x * 2
End Function
Function g()
x = x + 3
return x
End Function
Integer y = f(x) + g()
Print y, x- A. 20 5
- B. 21 9
- C. 20 8
- D. 18 8
Correct answer: C. 20 8
f gets a copy of x, so its x = 6 is local and it returns 12 while the global x stays 5. g changes the global x to 8 and returns 8. y = 12 + 8 = 20 and x = 8.
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? (All variables are integers, so / is integer division; + joins strings) Function…
- What is the time complexity of the following pseudocode, as a function of n? i = n while (i > 1) j = i while (j < n) j = j 2 end while i =…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer c = 0, i = 1, j while…
- What will be the output of the following pseudocode? (stop ends the program; code(c) gives a letter's character code) String s =…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[8] = {1, 1, 2, 2, 2, 3, 5, 5} Integer k = 1 for…
- What will be the output of the following pseudocode? (strings are indexed from 0) String x = "AGGTAB", y = "GXTXAYB" Integer L[0..6][0..7]…