CS636 – Formal Methods: Midterm Important Subjective (28 Solved Q&As)
Computer Science • Read every solved subjective question with its model answer directly on this page. Free, no login required.
All 664 Midterm Packs · CS636 Course Page · CS636 MCQ Pack (161 MCQs)
-
What are formal methods? Write any four advantages of formal methods.
Topic: Introduction to Formal Methods
Formal methods are methods that use formulas. A formula is a text or diagram constructed from predefined symbols combined according to explicit rules; a good working definition of a formula is anything whose appearance or syntax can be checked by a computer. Formal methods constitute a branch of software engineering that incorporates the use of mathematics for software development. Advantages of formal methods: 1. Formal specifications can help considerably in generating suitable test cases. 2. Formal specifications allow for feedback on system specifications at early development stages. 3. Important properties (such as internal consistency) of the initial specification can be checked mathematically. 4. A proof of program correctness can be constructed. 5. Proofs can help uncover design errors as soon as they are made.
-
Why are specifications written in natural language considered problematic? Explain with the help of the BankAccount 'Withdraw' example.
Topic: Introduction to Formal Methods
Specifications are currently written in natural language and diagrammatic notations (UML for example). Such specifications are ambiguous, incomplete and inconsistent, while the correctness of the entire software depends on the specification, so a defect in the specification has a ripple effect on the whole system. In the 'Withdraw' method example, the specification says the method receives a requested amount and, if there are sufficient funds, meets the request and returns a boolean indicating success or failure. It is ambiguous because it is not clear whether the bank account must contain at least the amount requested, whether there is a minimum balance that must be maintained, or whether there is an agreed overdraft limit. It is incomplete because it does not state what should happen when the amount in the account is insufficient. It is inconsistent because the overdraft facility may be defined somewhere else in the system.
-
Write down the limitations of software testing.
Topic: Introduction to Formal Methods
Testing aims to locate the errors in software, but it has the following limitations: 1. Testing cannot take place until some implementation is available. 2. Correcting errors identified by testing may introduce new errors. 3. Testing can uncover errors, but it does not guarantee the absence of errors. 4. Testing is based on specifications, so ambiguous specifications may make testing ineffective.
-
When are formal methods useful? Explain briefly.
Topic: Introduction to Formal Methods
Formal methods are useful in the following situations: 1. Novel projects: Novel projects are about building something new, the like of which does not exist before. Formal methods help to compare alternative ideas and select the best one through in-depth analysis, because we cannot afford building all the alternative versions. 2. Difficult projects: Projects are difficult when they address something profound or deep; they may not need to be long — even a simple page of code can be too difficult. 3. Critical projects: In critical projects so much is at stake, such as money, security and safety. Formal methods may not be needed for simple and routine jobs which are obvious and too easy.
-
How can we use formal methods? Explain modeling, design and verification.
Topic: Introduction to Formal Methods
Formal methods can be used in the following ways: 1. Modeling: Models describe the actual product behavior (before implementing it) through symbols and notations. A model is a simplified representation; it may skip the irrelevant properties. A mathematical model that represents the intended behavior of a program can be used as a formal specification. 2. Design: The system is designed using approaches such as top-down and bottom-up design, using formal languages such as the Z language. 3. Verification: Verification checks that the code does what it is intended for. A convincing product of verification is a proof, and a proof provides greater confidence than testing.
-
What is a Petri net? How are places, transitions, arcs and tokens represented in it?
Topic: Petri Nets
Petri nets were introduced by Carl Adam Petri in 1962. A Petri net is a diagrammatic tool to model concurrency and synchronization, and it also provides mathematical modeling. Its components are represented as follows: 1. Place (represents a condition or state) — denoted by a circle. 2. Transition (represents events) — denoted by a rectangle, a vertical bar or a filled vertical rectangle. 3. Arc (connects transitions and places) — denoted by a directed arrow. 4. Tokens (represent the state at time t) — denoted by a filled circle (dot) inside a place.
-
Define enabled and disabled transitions. When can a transition fire, and what happens when it fires?
Topic: Petri Nets
Each transition has input places and output places. An input place is also called a pre-condition and an output place is also called a post-condition. A transition can be fired when there is at least one token on each of the transition's input places. A transition that is able to fire is said to be enabled, and otherwise it is said to be disabled. When a transition is fired, one token is removed from each of its input places and a single token is produced on each of its output places. If there is more than one enabled transition, any one of the enabled transitions may be the next one to fire.
-
Give the formal definition of a Petri net. Also define marking, reachability, boundedness and liveness.
Topic: Petri Nets
Formal definition: A Petri net is PN = (P, T, I, O), where P = set of places, T = set of transitions, I = inputs and O = outputs. Marking: A marking M represents the state of a Petri net (the distribution of tokens over its places). Reachability: A marking Mi is reachable from M0 if there exists a firing sequence that transforms M0 to Mi. Boundedness: A Petri net is K-bounded if the number of tokens in any place does not exceed K for any marking reachable from M0. Liveness: A Petri net with initial marking M0 is live if, for any marking Mi (after M0), we can still fire a transition. If a Petri net is live, it is deadlock safe.
-
What are automata? Define language, Kleene Star Closure and the Plus operation with examples.
Topic: Automata and Regular Expressions
Automata are abstract mathematical models of machines that perform computations on an input by moving through a series of states or configurations. A language describes the conditions imposed on its words. For example, the language L of strings of odd length, defined over Σ = {a}, can be written as L = {a, aaa, aaaaa, ...}. Kleene Star Closure: Given Σ, the Kleene Star Closure of the alphabet Σ, denoted by Σ*, is the collection of all strings defined over Σ, including Λ. For example, if Σ = {0, 1}, then Σ* = {Λ, 0, 1, 00, 01, 10, 11, ...}. Plus operation: The Plus operation is the same as the Kleene Star Closure except that it does not generate Λ (the null string) automatically. For example, if Σ = {0, 1}, then Σ+ = {0, 1, 00, 01, 10, 11, ...}.
-
What is a regular expression? Write the rules for constructing regular expressions.
Topic: Automata and Regular Expressions
A regular expression is a notation used to express a language. For example, a* generates Λ, a, aa, aaa, ... and a+ generates a, aa, aaa, aaaa, ..., so the languages L1 = {Λ, a, aa, aaa, ...} and L2 = {a, aa, aaa, aaaa, ...} can simply be expressed by the regular expressions a* and a+ respectively. Rules for regular expressions: 1. Every letter of Σ including Λ is a regular expression. 2. If r1 and r2 are regular expressions, then (r1), r1 r2, r1 + r2 and r1* are also regular expressions.
-
Define a Deterministic Finite Automaton (DFA) as a 5-tuple and explain each of its components.
Topic: Automata and Regular Expressions
A DFA is a 5-tuple (S, Σ, T, s, A), where: 1. S represents the finite set of states. 2. Σ represents the set of input symbols. 3. T is the set of transitions. 4. s ∈ S represents the initial state. 5. A ∈ S represents the final state. A finite automaton is a state machine that takes a string of symbols as input and changes its state accordingly; a string is accepted if it reaches a final state.
-
Differentiate between a DFA and an NFA with the help of an example.
Topic: Automata and Regular Expressions
Both DFA and NFA are described as a 5-tuple (S, Σ, T, s, A). The difference appears in their transitions. In a DFA, for every state and every input symbol there is exactly one next state. In an NFA, a state can have more than one next state for the same input symbol (or none). Example: Let S = {a, b, c}, Σ = {0, 1}, s = a and A = {c}. In the DFA, from state a on input 0 the machine goes only to b, and on input 1 it stays at a. In the NFA, from state a on input 0 the machine can go to a or b; from state b on input 1 it can go to a or c; and from state c on input 0 it can go to b or c. Thus the NFA offers a choice of next states, while the DFA is fully determined.
-
What is model checking? Differentiate between qualitative and timing properties with examples.
Topic: Model Checking
Model checking is an automated technique that, given a finite-state model of a system and a formal property, systematically checks whether this property holds for (a given state in) that model. Model checking explores all possible system states in a brute-force manner, so it can be shown that a given system model truly satisfies a certain property. Model checking requires a precise and unambiguous statement of the properties to be examined. Qualitative properties ask whether something is possible or correct in principle. Examples: Is the generated result OK? Can the system reach a deadlock situation? Timing properties involve time bounds. Examples: Can a deadlock occur within 1 hour after a system reset? Is a response always received within 8 minutes?
-
Describe the phases of the model-checking process.
Topic: Model Checking
The model-checking process consists of three phases: 1. Modeling phase: Model the system under consideration using the model description language of the model checker at hand; as a first sanity check and quick assessment of the model, perform some simulations; and formalize the property to be checked using the property specification language. 2. Running phase: Run the model checker to check the validity of the property in the system model. 3. Analysis phase: If the property is satisfied, check the next property (if any). If the property is violated, analyze the generated counterexample by simulation, refine the model, design or property, and repeat the entire procedure. If the model checker runs out of memory, try to reduce the model and try again.
-
Write down the strengths and weaknesses of model checking.
Topic: Model Checking
Strengths of model checking: 1. It has a wide range of applications. 2. It supports partial verification. 3. It is not vulnerable to the likelihood that an error is exposed. 4. It provides diagnostic information in case a property is invalidated. 5. It is a potential 'push-button' technology. 6. It enjoys a rapidly increasing interest by industry and can be easily integrated in existing development cycles. 7. It has a sound and mathematical underpinning. Weaknesses of model checking: 1. It is mainly appropriate to control-intensive applications and less suited for data-intensive applications. 2. Its applicability is subject to decidability issues. 3. It verifies a system model, and not the actual system (product or prototype) itself. 4. It checks only stated requirements. 5. Its usage requires some expertise in finding appropriate abstractions and stating properties. 6. It is not guaranteed to yield correct results, as a model checker may contain software defects, and it does not allow checking generalizations.
-
Define a transition system formally and explain each component of its tuple.
Topic: Transition Systems
Transition systems are used as models to describe the behavior of systems. They are basically directed graphs where nodes represent states and edges model transitions, i.e., state changes. A state describes some information about a system at a certain moment of its behavior, and transitions specify how the system can evolve from one state to another. Formally, a transition system TS is a tuple (S, Act, →, I, AP, L) where: 1. S is a set of states. 2. Act is a set of actions. 3. → ⊆ S × Act × S is a transition relation. 4. I ⊆ S is a set of initial states. 5. AP is a set of atomic propositions. 6. L : S → 2^AP is a labeling function.
-
Define terminal state, reachable state, initial execution fragment and maximal execution fragment.
Topic: Transition Systems
Terminal state: A state s in a transition system TS is called terminal if and only if Post(s) = ∅, i.e., it has no outgoing transitions (successors). Execution fragment: A finite execution fragment of TS is an alternating sequence of states and actions ending with a state. Initial execution fragment: An execution fragment is called initial if it starts in an initial state. Maximal execution fragment: A maximal execution fragment is either a finite execution fragment that ends in a terminal state, or an infinite execution fragment. Reachable state: A state s ∈ S is called reachable in TS if there exists an initial, finite execution fragment ending in s.
-
What is the labeling function in a transition system? Explain with the help of the beverage vending machine example.
Topic: Transition Systems
The labeling function L : S → 2^AP associates with every state the set of atomic propositions that hold (are true) in that state. Example: In the beverage vending machine, let AP = {paid, drink}. The labeling is: L(pay) = ∅, L(select) = {paid}, and L(soda) = L(beer) = {paid, drink}. Here the proposition 'paid' characterizes exactly those states in which the user has already paid but not yet obtained a beverage, while 'drink' holds in the states where the user has obtained the beverage.
-
What are data-dependent systems? What is a guard? Explain the Effect function with an example.
Topic: Concurrent Systems and Interleaving
In data-dependent systems, the executable actions typically result from conditional branching, as in: if x % 2 = 1 then x := x + 1 else x := 2·x fi. The labels of conditional transitions are of the form g : α, where g is a Boolean condition called a guard, and α is an action that is possible once g holds. The function Effect : Act × Eval(Var) → Eval(Var) indicates how the evaluation η of variables is changed by performing an action. Example: Let α denote the action x := y + 5, where x and y are integer variables, and let η be the evaluation with η(x) = 17 and η(y) = −2. Then Effect(α, η)(x) = η(y) + 5 = −2 + 5 = 3, and Effect(α, η)(y) = η(y) = −2.
-
What is interleaving? How are the states and transitions of TS1 ||| TS2 formed?
Topic: Concurrent Systems and Interleaving
Interleaving is a widely adopted paradigm for parallel systems in which the actions of an independent component are merged (also called weaved), or 'interleaved', with the actions of other components. An important justification for interleaving is that the effect of concurrently executed, independent actions α and β is identical to the effect when α and β are successively executed in arbitrary order: Effect(α ||| β, η) = Effect((α ; β) + (β ; α), η). The transition system TS1 ||| TS2 represents the parallel system resulting from the weaving of the actions of the components TS1 and TS2. It is assumed that no communication and no contentions (on shared variables) occur at all. The (global) states of TS1 ||| TS2 are pairs ⟨s1, s2⟩ consisting of the local states si of the components TSi, and the outgoing transitions of a global state ⟨s1, s2⟩ consist of the outgoing transitions of s1 together with those of s2.
-
What is well-formedness? Differentiate between intra-diagram, inter-diagram and inter-model consistency constraints.
Topic: Well-Formedness of Models
Well-formedness: A model or a diagram is well-formed if its structure satisfies the syntactic rules; especially, its elements are type compatible if a type system is defined. A consistency constraint C is a predicate defined on models such that C(M) = true means that the model is consistent with respect to the constraint. Consistency constraints are of the following types: 1. Intra-diagram constraint: A consistency constraint defined on a specific type T of diagram of the model. 2. Inter-diagram constraint: A consistency constraint defined on two or more diagrams (for example, on two diagrams of the same type T). 3. Inter-model constraint: A consistency constraint defined on diagrams of more than one type, say between diagrams of types T1 and T2.
-
What is a proposition? Differentiate between primitive and compound propositions with examples.
Topic: Propositional Logic
A proposition is a statement that is either TRUE or FALSE. For example, 'There are seven days in a week', 'There are 12 months in a year' and '2 + 4 = 6' are propositions that evaluate to TRUE, while 'London is the capital of France' and '2 − 4 = 7' are propositions that evaluate to FALSE. In mathematics, a proposition is often represented symbolically by a variable name such as P or Q. Primitive proposition: A proposition is said to be primitive if it cannot be broken down into simpler propositions, i.e., if it is not composite. Examples: '2 + 2 = 4' and 'Ice floats in water'. Compound proposition: Many propositions are composite, i.e., composed of subpropositions and various connectives; such composite propositions are called compound propositions. Example: 'Roses are red and violets are blue'. The fundamental property of a compound proposition is that its truth value is completely determined by the truth values of its subpropositions together with the way in which they are connected.
-
Define conjunction, disjunction and negation, giving their symbols and truth conditions.
Topic: Propositional Logic
Conjunction: Any two propositions can be combined by the word 'and' to form a compound proposition called the conjunction of the original propositions, symbolically p ∧ q. If p and q are true, then p ∧ q is true; otherwise p ∧ q is false. Disjunction: Any two propositions can be combined by the word 'or' to form a compound proposition called the disjunction of the original propositions, symbolically p ∨ q. If p and q are false, then p ∨ q is false; otherwise p ∨ q is true. Negation: Given any proposition p, another proposition called the negation of p can be formed by writing 'It is not true that...' or 'It is false that...' before p, or by inserting the word 'not' in p; it is symbolized as ¬p. If p is true, then ¬p is false; and if p is false, then ¬p is true.
-
Construct the truth table of the proposition ¬(p ∧ ¬q).
Topic: Propositional Logic
The truth table is constructed by first listing all combinations of truth values of p and q (for 2 variables, 4 rows are necessary), then evaluating the elementary stages ¬q and p ∧ ¬q, and finally the whole proposition ¬(p ∧ ¬q). p = T, q = T : ¬q = F; p ∧ ¬q = F; ¬(p ∧ ¬q) = T p = T, q = F : ¬q = T; p ∧ ¬q = T; ¬(p ∧ ¬q) = F p = F, q = T : ¬q = F; p ∧ ¬q = F; ¬(p ∧ ¬q) = T p = F, q = F : ¬q = T; p ∧ ¬q = F; ¬(p ∧ ¬q) = T Hence the final column of the truth table of ¬(p ∧ ¬q) is T, F, T, T.
-
State De Morgan's laws and prove that ¬(p ∧ q) ≡ ¬p ∨ ¬q using a truth table.
Topic: Propositional Logic
De Morgan's laws are: (10a) ¬(p ∨ q) ≡ ¬p ∧ ¬q (10b) ¬(p ∧ q) ≡ ¬p ∨ ¬q Two propositions are logically equivalent if they have identical truth tables. The truth table of both sides is: p = T, q = T : p ∧ q = T, so ¬(p ∧ q) = F; ¬p = F and ¬q = F, so ¬p ∨ ¬q = F p = T, q = F : p ∧ q = F, so ¬(p ∧ q) = T; ¬p = F and ¬q = T, so ¬p ∨ ¬q = T p = F, q = T : p ∧ q = F, so ¬(p ∧ q) = T; ¬p = T and ¬q = F, so ¬p ∨ ¬q = T p = F, q = F : p ∧ q = F, so ¬(p ∧ q) = T; ¬p = T and ¬q = T, so ¬p ∨ ¬q = T The final columns of ¬(p ∧ q) and ¬p ∨ ¬q are identical (F, T, T, T), hence ¬(p ∧ q) ≡ ¬p ∨ ¬q. For example, if p is 'Roses are red' and q is 'Violets are blue', then the statement 'It is not true that roses are red and violets are blue' has the same meaning as 'Roses are not red, or violets are not blue'.
-
Define the implication, equivalence and exclusive or operators, giving their symbols and truth conditions.
Topic: Propositional Logic
Implication operator: It gives meaning to the expression 'P implies Q' and is represented by the symbol ⇒. The statement P ⇒ Q can alternatively be expressed as 'if P then Q'. The implication p ⇒ q is false only when p is TRUE and q is FALSE; in all other cases it is true. Equivalence operator: It is represented by the symbol ⇔. It states that P is equivalent to Q, written P ⇔ Q; it is true when P and Q have the same truth value. Exclusive or operator: The (inclusive) 'or' operator evaluates to TRUE if either p or q or both are true, but the natural language 'or' usually implies that only one or the other of the statements is TRUE but not both. This corresponds to the logical operator known as exclusive or (sometimes referred to as xor), which is represented by the symbol ⊕. Thus p ⊕ q is true exactly when one of p and q is true and the other is false.
-
What are tautologies and contradictions? When is an argument valid? Give one example each of a valid argument and a fallacy.
Topic: Propositional Logic
Tautology: A proposition P(p, q, ...) that contains only T in its evaluation, i.e., it is true for any truth values of its variables, is called a tautology. Contradiction: A proposition P(p, q, ...) is called a contradiction if it contains only F in its evaluation, i.e., it is false for any truth values of its variables. Argument: An argument is an assertion that a given set of propositions P1, P2, ..., Pn yields another proposition Q. An argument P1, P2, ..., Pn ⊢ Q is said to be valid if Q is TRUE whenever all the premises P1, P2, ..., Pn are true; equivalently, the argument is valid if and only if the proposition (P1 ∧ P2 ∧ ... ∧ Pn) → Q is a tautology. Valid argument example: p, p → q ⊢ q. Fallacy: An argument which is not valid is called a fallacy. Example: p → q, q ⊢ p is a fallacy.
-
What is three-valued logic? Why is it needed? Write the values of the AND, OR and negation operators when one operand is UNDEFINED.
Topic: Three-Valued Logic
Classical logic assumes that all expressions evaluate to TRUE or FALSE. In reality this is not always the case, because sometimes an expression can be undefined — for example, the expression 0/0. Undefined terms are very common in programming situations, for example when a variable is first declared and has not yet been assigned a value. Three-valued logic accounts for such situations by allowing a proposition to take the value UNDEFINED as well as TRUE and FALSE. The operators behave as follows when UNDEFINED is involved: AND operator: T ∧ UNDEFINED = UNDEFINED; F ∧ UNDEFINED = F; UNDEFINED ∧ UNDEFINED = UNDEFINED. OR operator: T ∨ UNDEFINED = T; F ∨ UNDEFINED = UNDEFINED; UNDEFINED ∨ UNDEFINED = UNDEFINED. Negation operator: ¬UNDEFINED = UNDEFINED.