Programming Basics (Pseudocode Tracing)

Learn to trace short programs line by line with multiple-choice questions, written in the same pseudo-language as Part B of Japan's Fundamental Information Technology Engineer Examination. Topics include variables and assignment, loops and conditional branching, arrays and strings, integer division and remainders, recursion, bitwise operations, stacks and queues, linked lists, off-by-one errors, short-circuit evaluation, floating-point error, and classes and instances.

Practice 10 questionsRandom questions from this topic · about 5 minutes Practice this topicAll questions in this topic, in random order Weak questionsRetry only the questions you last got wrong

Bitwise Operations Calculator →

Pseudocode Runner and Tracer →

Programming Basics (Pseudocode Tracing): key points

To read a program's behavior accurately, you need to be able to follow by hand (trace) how the values of variables change line by line. Small oversights, such as miscounting a loop by one, losing a value through the order of assignments, or forgetting that integer division discards the fractional part, lead to wrong results and bugs.

This topic centers on questions that ask you to trace short programs and give the result. The programs follow the pseudo-language used in Part B of Japan's Fundamental Information Technology Engineer Examination, with type declarations, assignment with ←, if, while and for, and array indices that start at 1. The wrong choices are values produced by typical mistakes, such as getting the final value of a loop wrong or misreading operator precedence, and the explanations show which mistake each choice comes from.

All Programming Basics (Pseudocode Tracing) questions (40)

  1. The two assignments in the following program were meant to swap the values of x and y. What are the values of x and y after it runs?
  2. The following program passes values along three variables. What are the values of a, b and c after it runs?
  3. The following program repeatedly multiplies x by 3. What is the value of n after it runs?
  4. The following for loop increases i by 2. What is the value of s after it runs?
  5. In the following nested loop, the inner loop starts from the outer variable i. What is the value of c after it runs?
  6. The following program has a condition inside a nested loop. What is the value of c after it runs?
  7. The following program was meant to compute the sum of all elements of array a. Array indices start at 1. What is the value of s after it runs?
  8. Array indices start at 1. The following program looks for the largest element. What is the value of m after it runs?
  9. The following program was meant to reverse the elements of array a. Array indices start at 1. What does a contain after it runs?
  10. Array indices start at 1. What does array a contain after the following program runs?
  11. Array indices start at 1. The following program compares adjacent elements. What is the value of c after it runs?
  12. m[i, j] is the element in row i, column j of the 2D array m, and indices start at 1. What is the value of s after the following program runs?
  13. The following program copies the elements of the 2D array m with 4 rows and 5 columns into the 20-element array b. m[i, j] is the element in row i, column j, and indices start at 1. At which index of b is m[3, 2] stored?
  14. Array indices start at 1, and + concatenates strings. What is the value of r after the following program runs?
  15. The following function isPal returns true if the characters in the string array s read the same forward and backward (a palindrome). Array indices start at 1. What should replace 【a】 in the program?
  16. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. What does the following function f return when called as f(9087)?
  17. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. What does the following function toBin return when called as toBin(12)?
  18. The following program subtracts the two integers a and b from each other until they are equal. What is the value of k after it runs?
  19. The following function isLeap returns true if the year y is a leap year. and is evaluated before or. For which y does isLeap(y) return true?
  20. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. When the following function pow is called as pow(2, 10), how many times is pow called, including the first call?
  21. What does the following function s return when called as s(7)?
  22. When the following procedure p is called as p(3), what sequence of values is output?
  23. What does the following function fib return when called as fib(10)?
  24. When the following function c is called as c(4, 2), how many times is c called, including the first call?
  25. ⊕ is bitwise exclusive OR. What are the values of x and y after the following program runs?
  26. x and y are 8-bit unsigned integers. << and >> are logical shifts: vacated bits are filled with 0, and bits shifted out of the 8 bits are discarded. ∨ is bitwise OR. What is the value of y after the following program runs?
  27. ∧ is bitwise AND. What is the value of c after the following program runs?
  28. x and y are 8-bit unsigned integers. << and >> are logical shifts: vacated bits are filled with 0, and bits shifted out of the 8 bits are discarded. ∨ is bitwise OR. With x = 75, what is the value of y after the following assignment?
  29. The following procedure bubble is a bubble sort that sorts the integer array data in ascending order. swap(data, j, k) is a procedure that swaps the values of data[j] and data[k]. Array indices start at 1. What should replace 【a】 in the program?
  30. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. The following program finds in lo the index of the first value that is at least x in the integer array data, which is sorted in ascending order (indices start at 1). x is no greater than the last element of data. What should replace 【a】 in the program?
  31. A stack is represented by the 5-element integer array st and the integer variable top. All elements of st start at 0 and top starts at 0. push(v) adds 1 to top and then does st[top] ← v; pop() returns the value of st[top] and then subtracts 1 from top (the elements of st are not changed). What are top and st after the following program runs?
  32. A queue uses the 4-element string array q as a ring. head and tail start at 1. enq(v) does q[tail] ← v and then tail ← tail mod 4 + 1; deq() returns the value of q[head] and then does head ← head mod 4 + 1. When the following program runs, in which element of q is "E" stored?
  33. The following program outputs a linked list, whose elements are linked by the array next, by following it from the element at index 1. Array indices start at 1. A next value of 0 marks the end of the list. What sequence of values is output?
  34. The following program inserts "F", in val[6], right after "C" (index 3) in the linked list A → C → E → D → B, linked by the array next, and then outputs the list from the element at index 1. Array indices start at 1. A next value of 0 marks the end of the list. What should replace 【a】 so that the output is A, C, F, E, D, B?
  35. The following for loop increases its control variable by 5. What is the value of c after it runs?
  36. In the following program, and does not evaluate its right side when the left side is false (short-circuit evaluation). cnt is a global variable. What is the value of cnt after the program runs?
  37. The following program searches the n-element array a for the value x and stores the index where it is found in i. Array indices start at 1. and and or do not evaluate their right side when the left side alone decides the result. Which condition in 【a】 reads a[n + 1], outside the array, when x is not in a?
  38. Real values are represented in IEEE 754 double precision (binary64), and each sum is rounded to the nearest representable value. What is the value of c after the following program runs?
  39. Class Counter gives each instance its own integer member variable count (0 when created), and its method inc() adds 1 to its own count. What is the value of x after the following program runs?
  40. Class Counter gives each instance its own integer member variable count (0 when created), and its method inc() adds 1 to its own count. A variable of type Counter holds a reference to an instance, not the instance itself. What is the value of x after the following program runs?

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

Other topics in Computer systems