ProgrammingHardProduct company OA
What will be the output of the following pseudocode? (A static variable is initialised once and keeps its value between calls; operands are evaluated left to right.)
What will be the output of the following pseudocode? (A static variable is initialised once and keeps its value between calls; operands are evaluated left to right.)
Function f()
static Integer c = 0
c = c + 2
return c
End Function
Print f() + f() * f()- A. 14
- B. 18
- C. 12
- D. 26
Correct answer: D. 26
With left-to-right evaluation the three calls return 2, 4 and 6 in that order; precedence then groups them as 2 + 4 * 6 = 26. If the two calls of the multiplication ran first (2·4, then + 6) the result would be 14 – precedence decides grouping, not the order of the calls. Without the static keyword every call would return 2.
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? (^ is bitwise XOR, & is AND, >> is a right shift.) Integer x = 29, y = 30 Integer gx…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; the matrix has 3 rows and 4 columns.) Integer m[3][4] =…
- What will be the output of the following pseudocode? (calls counts every call of f, including the first.) Integer calls = 0 Function…
- What will be the output of the following pseudocode? (/ is integer division; the division in the loop is always exact.) Integer n = 10, c…
- What will be the output of the following pseudocode? ('ref' parameters are passed by reference; ^ is bitwise XOR; arrays are indexed from…
- What will be the output of the following pseudocode? (Arrays are passed by reference and indexed from 0.) Procedure p(Integer arr[]…