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
Which Picat construct is used to handle runtime exceptions in imperative code?
Answer: try ... catch ... end
Picat uses try ... catch(Pattern) ... end blocks to intercept and handle exceptions.
What does `string_to_atom(S)` do when used in a Picat imperative function?
Answer: Converts a string (char list) to an atom
string_to_atom/1 converts a Picat string (list of character codes or chars) into an atom.
How do you concatenate two lists in Picat within an imperative function?
Answer: L1 ++ L2
The ++ operator performs list concatenation in Picat, returning a new combined list.
In Picat's imperative mode, what does `X @= Y` mean?
Answer: X is assigned the value of Y only if X is unbound
The @= operator in Picat assigns Y to X only when X is an unbound (free) variable, leaving it unchanged otherwise.
What is the output of the Picat code: `X := 3, (X > 2 -> Y := "big" ; Y := "small"), print(Y)`?
Answer: big
Since X is 3 which is greater than 2, the then-branch executes and Y is assigned "big", which is then printed.
In Picat, which of the following correctly swaps the values of two mutable variables A and B?
Answer: Tmp := A, A := B, B := Tmp
A temporary variable must hold one value during the swap; the naive two-step assignment would lose A's original value.
What does a `foreach` loop do when given an empty list in Picat?
Answer: Skips the body entirely and continues normally
Iterating over an empty list with foreach simply skips the loop body; this is the standard and expected behavior.