Picat Imperative Programming Constructs Questions and Answers — Questions and Answers
Question 1: Which of the following Picat operators is used for destructive assignment of a value to a variable within an imperative loop?
- =
- := (Correct answer)
- ==
- =>
Correct 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.
Question 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).
- 1
- 10
- 24 (Correct answer)
- 0
Correct 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`.
Question 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).
- 15
- 18 (Correct answer)
- 0
- 3
Correct 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.
Question 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`)?
- stop
- fi
- done
- end (Correct answer)
Correct 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.
Question 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).
- high
- low
- medium (Correct answer)
Correct 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'.
Question 6: Which of the following Picat code snippets will result in an instantiation error?
- main => X = 1, Y = X + 5, println(Y).
- main => foreach(I in 1..5) X = I*I, println(X) end.
- main => while (N > 0) N := N - 1 end, println(N). (Correct answer)
- main => X = 10, if (X > 5) then println(ok) end.
Correct 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.
Which of the following Picat operators is used for destructive assignment of a value to a variable within an imperative loop?