Imperative Programming Constructs Flashcards
7 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 7 Imperative Programming Constructs flashcards as text
In Picat, what is the scope of a variable assigned inside a `foreach` loop body?
Answer: It is visible in the enclosing function after the loop ends
Variables assigned inside a foreach loop body are visible in the surrounding function scope after the loop completes.
Which operator tests value equality (not unification) in Picat's imperative code?
Answer: ==
The == operator tests structural/value equality without binding variables, while = performs unification.
What is the Picat equivalent of a C-style `for(i=0; i<n; i++)` loop?
Answer: foreach(I in 0..n-1)
Picat uses `foreach(I in 0..n-1)` to iterate over a range, equivalent to C's for loop from 0 to n-1.
In Picat, what does the `!` (cut) operator do in an imperative clause?
Answer: It commits to the current clause, pruning remaining alternatives
The cut (!) in Picat, inherited from Prolog, commits to the current clause and eliminates remaining choice points.
How does Picat handle an `if` condition that raises an exception during evaluation?
Answer: The exception propagates up the call stack
Uncaught exceptions in an if condition propagate normally up the call stack just as they would in any other position.
What is the syntax to read an integer from standard input in Picat?
Answer: X := read_int()
Picat provides read_int() as a built-in function; the result is assigned with := in imperative mode.
In a Picat function, what happens if execution reaches the end of the body without a `return`?
Answer: The function returns the last evaluated expression
In Picat, if no explicit return is reached, the value of the last expression in the function body is returned.