Picat Imperative Programming Constructs 3 — Questions and Answers
Question 1: In a Picat `if-then-else` expression, what is required if the else branch is omitted?
- The condition must always be true
- An implicit else true is added
- Omitting else is a syntax error in function mode (Correct answer)
- The if expression returns a free variable on failure
Correct answer: Omitting else is a syntax error in function mode
In Picat's functional/imperative mode, if without else is a syntax error; both branches must be present.
Question 2: How are multi-dimensional arrays indexed in Picat?
- A[I,J] (Correct answer)
- A[I][J]
- A.(I,J)
- A->get(I,J)
Correct answer: A[I,J]
Picat uses comma-separated indices inside a single bracket pair, e.g., A[I,J], for multi-dimensional array access.
Question 3: What built-in predicate fills an array with a default value in Picat?
- fill(A, V)
- new_array(N, V)
- A = new_array(N) then foreach i fill
- There is no fill built-in; use a foreach loop to initialize (Correct answer)
Correct answer: There is no fill built-in; use a foreach loop to initialize
Picat allocates arrays with new_array but does not have a fill built-in; initialization requires iterating and assigning each element.
Question 4: Which statement about `return` in a Picat function is correct?
- return must appear at the end of every clause
- return is not a keyword; the last expression's value is returned
- return Expr exits the function with value Expr (Correct answer)
- Functions cannot return values; only predicates can
Correct answer: return Expr exits the function with value Expr
Picat supports the return Expr statement to exit a function early and produce a return value.
Question 5: In Picat, how is standard output written from an imperative function?
- print(X)
- write(X)
- printf("%w", X)
- Both print(X) and printf(Fmt,Args) are valid (Correct answer)
Correct answer: Both print(X) and printf(Fmt,Args) are valid
Picat provides both print/println for simple output and printf/writef for formatted output in imperative functions.
Question 6: What does `new_array(5)` produce in Picat?
- A list of 5 free variables
- A 1-D array of size 5 with uninitialized (free) elements (Correct answer)
- An array initialized to zeros
- A 5×5 matrix
Correct answer: A 1-D array of size 5 with uninitialized (free) elements
new_array(5) creates a one-dimensional array of 5 elements, each an unbound (free) variable.
Question 7: How do you iterate over both index and value of a list in a Picat `foreach` loop?
- foreach(I:V in List)
- foreach(I-V in List)
- foreach({I,V} in zip(1..len(List), List))
- foreach(I in 1..List.len, V = List[I]) (Correct answer)
Correct answer: foreach(I in 1..List.len, V = List[I])
The standard idiom is to iterate over an index range and access List[I], since foreach over a list gives values only.
In a Picat `if-then-else` expression, what is required if the else branch is omitted?