Logic Programming with Prolog Concepts Quiz Quiz

Challenge yourself with this logic programming quiz focused on Prolog concepts, including predicates, recursion, unification, backtracking, and data representation. Enhance your understanding of Prolog syntax, inference mechanisms, and logical reasoning in declarative programming.

  1. Understanding Prolog facts and rules

    In Prolog, which of the following correctly represents a rule that defines 'ancestor' recursively, given 'parent' facts?

    1. ancestor(X, Y) = parent(X, Y) or ancestor(Z, Y).
    2. ancestor(X, Y) :- parent(X, Y). ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
    3. ancestor(X, Y) :- parent(X, Y), parent(Z, Y).
    4. ancestor(X, Y) if parent(X, Y); ancestor(Z, Y) and parent(X, Z).

    Explanation: The correct answer shows the standard Prolog syntax using ':-' for rules and a recursive definition, which is how ancestor relationships are commonly expressed. The other options are incorrect as they either misuse syntax with '=' or 'if', mix logical connectors improperly, or do not capture recursion accurately. Option B fails to express recursion, and options C and D do not follow Prolog's rule format.

  2. Purpose of unification in Prolog

    What is the primary role of unification in Prolog’s pattern matching during query resolution?

    1. It organizes predicates into logical groups.
    2. It sorts the database of facts alphabetically.
    3. It determines operator precedence rules.
    4. It assigns variable values to match terms in clauses.

    Explanation: Unification in Prolog binds variables to values so that terms become identical, which is essential for matching queries with facts and rules. Grouping predicates—option B—is more about code organization, not pattern matching. Sorting facts (option C) and operator precedence (option D) are unrelated to the unification process.

  3. Behavior of backtracking in Prolog

    When executing the query 'likes(alice, X).' in a Prolog program, how does backtracking affect the search for solutions?

    1. It returns only the first value for X and ignores others.
    2. It prevents any variable from being assigned to X.
    3. It randomly selects a matching value for X.
    4. It tries all possible values for X that make the predicate true.

    Explanation: Backtracking allows Prolog to systematically explore alternatives for X, finding every possible solution to the query. Returning only the first value (option B) contradicts Prolog’s exhaustive search, unless explicitly stopped. Random selection (option C) is never used in backtracking, and preventing assignment (option D) is the opposite of variable binding in Prolog.

  4. Data structure representation in Prolog

    Which statement best describes how lists are represented in Prolog, such as [a, b, c]?

    1. They are internally stored as nested terms using the dot operator.
    2. They use comma-separated values enclosed by parentheses only.
    3. They require each element to be declared as a separate fact.
    4. They must be written with curly braces like {a, b, c}.

    Explanation: Prolog represents lists as recursive data structures using the dot operator (a head and a tail), such as '.'(a, '.'(b, '.'(c, []))). Parentheses (option B) are not used for lists, curly braces (option C) are not valid list syntax, and declaring elements as separate facts (option D) does not represent lists or their structure.

  5. Predicate arity in Prolog

    If a Prolog predicate is defined as sibling(X, Y), what is the arity of this predicate and how is it generally written?

    1. 2, written as sibling/2
    2. 2, written as sibling-2
    3. 1, written as sibling/1
    4. 2, written as sibling_two

    Explanation: The predicate sibling(X, Y) takes two arguments, so its arity is 2 and is denoted sibling/2 in Prolog notation. sibling/1 (option B) implies only one argument. sibling-2 (option C) and sibling_two (option D) are not standard ways to indicate arity in Prolog.