Loading…
Programming · short notes, key points and practice questions.
Language-neutral pseudocode snippets whose output you trace by hand, as asked in the programming-logic sections of TCS NQT, Accenture, Capgemini, Infosys and Cognizant tests. They cover loops and nested loops, recursion, bitwise operators, arrays, integer division and modulo.
/ drops the fraction (7 / 2 = 3); tests follow C rules, so −7 / 2 = −3 and −7 mod 2 = −1*, / and mod share one precedence level and are evaluated left to right: 10 / 3 * 3 = 9&&, and && tighter than ||&& and || short-circuit: in a || f(), f is never called when a is truex++ uses the old value then increments; ++x increments firstbreak exits only the innermost loop; continue jumps to the next pass of the innermost loopn mod 10 gives the last digit, n / 10 removes it – used for sum, reverse and palindrome checksstatic (or global) counter is shared by every call; a local variable is fresh each call& AND, | OR, ^ XOR, ~ NOT, << left shift, >> right shiftx << k = x × 2ᵏ, x >> k = ⌊x / 2ᵏ⌋ for non-negative x~x = −(x + 1) in two's complement, so ~5 = −6x ^ x = 0 and x ^ 0 = x, so XOR of a list cancels pairs and leaves the odd one outn & (n − 1) clears the lowest set bit (loop count = number of 1s); it is 0 only for powers of twon & −n keeps only the lowest set bit; n & 1 tests odd/evena + b * c mod d in the wrong order – multiply and mod go left to right before the addbreak leaves all nested loops&&/|| may never run, so a counter inside it is not incrementedInteger x, y Set x = 17, y = 5 Print x / y, x mod y
Answer: A. 3 2
Integer n = 4521, r = 0
while (n > 0)
r = r * 10 + n mod 10
n = n / 10
end while
Print rAnswer: D. 1254
Integer n = 45, c = 0
while (n > 0)
n = n & (n - 1)
c = c + 1
end while
Print cAnswer: B. 4
Integer memo[0..20] = all -1, calls = 0
Function f(Integer n)
calls = calls + 1
if (n <= 1)
return n
end if
if (memo[n] != -1)
return memo[n]
end if
memo[n] = f(n - 1) + f(n - 2)
return memo[n]
End Function
Print f(15), callsAnswer: A. 610 29
Integer s = 0
for (each i from 1 to 10)
if (i mod 2 == 0)
s = s + i
end if
end for
Print sAnswer: B. 30