Picat Data Structures and Types 2 — Questions and Answers
Question 1: How do you create a new empty map in Picat?
- new_map() (Correct answer)
- {}
- empty_map()
- make_map()
Correct answer: new_map()
Picat provides the built-in `new_map()` function to create an empty hash map.
Question 2: What is the correct syntax to access element at index 2 of array A in Picat?
- A.(2)
- A[2] (Correct answer)
- get_array(A, 2)
- nth(2, A)
Correct answer: A[2]
Picat arrays use bracket notation `A[I]` for indexed access, with indexing starting at 1.
Question 3: Starting from which index do Picat arrays begin?
- 0
- Depends on the declaration
- 1 (Correct answer)
- -1
Correct answer: 1
Picat arrays are 1-indexed by default, so the first element is at index 1.
Question 4: What does `to_array([a, b, c])` return in Picat?
- (a, b, c)
- [a, b, c]
- Error: use new_array instead
- {a, b, c} (Correct answer)
Correct answer: {a, b, c}
`to_array/1` converts a list to a Picat array represented with curly-brace notation `{a, b, c}`.
Question 5: Which function retrieves a value from Picat map M using key K?
- get(M, K) (Correct answer)
- fetch(M, K)
- M.K
- lookup(M, K)
Correct answer: get(M, K)
`get(M, K)` is the built-in to retrieve the value associated with key K from map M.
Question 6: How do you insert key K with value V into Picat map M?
- M[K] = V
- insert(M, K, V)
- put(M, K, V) (Correct answer)
- map_put(M, K, V)
Correct answer: put(M, K, V)
`put(M, K, V)` inserts or updates the key-value pair in the map, which is a mutable operation in Picat.
Question 7: What does `array({1, 2, 3})` evaluate to in Picat?
- false
- true (Correct answer)
- {1, 2, 3}
- Error
Correct answer: true
The `array/1` predicate succeeds (returns true) when its argument is a Picat array denoted with curly braces.
How do you create a new empty map in Picat?