โ† All Picat Flashcard Decks

Imperative Programming Constructs Flashcards

6 cards from real Picat practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 6 Imperative Programming Constructs flashcards as text
  1. Which of the following Picat operators is used for destructive assignment of a value to a variable within an imperative loop?

    Answer: :=

    In Picat, the `:=` operator is specifically used for imperative-style destructive assignment, where the value of a variable is updated. The `=` operator is used for unification, `==` is for structural equality testing, and `=>` is part of a rule definition.

  2. What is the output of the following Picat code snippet? main => Counter = 4, Result = 1, while (Counter > 1) Result := Result * Counter, Counter := Counter - 1 end, println(Result).

    Answer: 24

    The `while` loop initializes `Result` to 1 and `Counter` to 4. The loop continues as long as `Counter` is greater than 1. The loop executes for `Counter` values of 4, 3, and 2. The `Result` is updated as follows: `1 * 4 = 4`, then `4 * 3 = 12`, then `12 * 2 = 24`. When `Counter` becomes 1, the loop condition is false and it terminates, printing the final value of `Result`.

  3. Consider the following Picat code. What value will be printed to the console? main => S = 0, foreach (X in [10, 5, 3]) S := S + X end, println(S).

    Answer: 18

    The `foreach` loop iterates through each element of the list `[10, 5, 3]`. The variable `S` is initialized to 0. In each iteration, the current element `X` is added to `S`. The calculation proceeds as: `0 + 10 = 10`, then `10 + 5 = 15`, and finally `15 + 3 = 18`. The final value, 18, is printed.

  4. Which of the following imperative constructs in Picat is used to mark the end of a block for loops (`while`, `foreach`) and conditionals (`if-then-else`)?

    Answer: end

    In Picat, the `end` keyword is consistently used to terminate imperative blocks, including `foreach` loops, `while` loops, and `if-then-else` statements. This provides a clear and uniform syntax for defining the scope of these constructs.

  5. A developer writes the following code, intending to set a variable `Status` based on a `Score`. What is the final value of `Status`? main => Score = 85, Status = '', if (Score >= 90) then Status := high elseif (Score >= 70) then Status := medium else Status := low end, println(Status).

    Answer: medium

    The code evaluates the `if-elseif-else` conditions sequentially. The first condition, `Score >= 90` (85 >= 90), is false. The program proceeds to the `elseif` condition, `Score >= 70` (85 >= 70), which is true. The code block for this condition is executed, assigning `medium` to `Status`. The `else` block is then skipped, and the program prints the value 'medium'.

  6. Which of the following Picat code snippets will result in an instantiation error?

    Answer: main => while (N > 0) N := N - 1 end, println(N).

    The `while` loop in this snippet attempts to evaluate the condition `N > 0` before `N` has been given an initial value (instantiated). In Picat, a variable must be bound to a value before it can be used in an arithmetic comparison. The other snippets all correctly instantiate their variables before use.