Midterm 2
What is the loop invariant for each variable in the following code segment? x = -20; while (x < 10) { x += 5; }
-20 <= x <=10
How many three letter initials are there with no repetition in letters?
15600 = 26 ∗ 25 ∗ 24
What is the value of f(4) if f(0) = 5 and f(n) is defined recursively by f(n+1) = 5f(n)/5?
5
How many ways are there to scramble "ababa"
5! / (3! * 2!)
In the recurrence a0=1 n>1; an = 3 an-1 + 2 What is a3?
53
In the recurrence a0=1 a1=1 an=3an-1+2an-2 What is a4
61
How many 5-permutations of a set with nine elements are there?
9!/4!
Suppose a password for a computer system must have at least eight but no more than twelve lowercase English letters. how many different passwords are available for this computer system?
9.9246107 ∗ 10^16 = 26^8 + 26^9 + 26^10 + 26^11 + 26^12
True or false: the sequence ()(())() is uniquely defined.
F
Corvettes are fun to drive. James's car is not a Corvette, therefore it's not fun to drive
False
What is the sequence 2, 1, 1/2, 1/4, . . .
Geometric
Given pre-condition: x is an integer; and the following program segment: if ((x/3)*3 != x) x = x - ( x % 3); What is the post-condition?
Post-condition: x is divisible by 3
Given pre-condition: x is an integer; and the following program segment: if ((x/2)*2 != x) x = x + 1; What is the post-condition?
Post-condition: x is even.
Given pre-condition: x is an integer, y is an integer; and the following program segment: if (x < y) z = y - x; else z = x - y; What is the post-condition for z?
Post-condition: z = absolute difference between x and y
Given pre-condition: x is an integer, y is an integer; and the following program segment: if (x < y) z = x; else z = y; What is the post-condition for z?
Post-condition: z = minimum of x and y
True or false: the sequence (()()) is uniquely defined.
T
It is raining; therefore it is snowing or raining.
addition
Given the sequence 1, 3, 5, 7 ... What kind of sequence is this?
arithmetic
Is the following situation best represented by a permutation or a combination? A coin is flipped six times, producing either heads or tails. How many possible outcomes contain exactly three heads?
combination
Is the following situation best represented by a permutation or a combination? How many ways are there to find three student council representatives out of 100 students?
combination
The sun shines; the wind blows. Therefore the sun shines and the wind blows.
conjunction
Which of the following functions functions f: Z → Z are not onto?
f(x) = 2x f(x) = x^2
Which of the following functions f: R → R are not onto?
f(x) = ceiling(x) f(x) = floor(x)
Which of the following functions f: R → R are valid?
f(x) = floor(x) f(x) = 3
Which of the following functions f: Z → Z are not one to one?
f(x) = x % 3 (% is modulo) f(x) = |x|
Which of the following functions f: Z → Z are not one to one?
f(x) = |x| f(x) = x % 3 (% is modulo)
Corvettes are fun to drive. James's car is not a Corvette, therefore it's not fun to drive.
false
Loop invariants. Given the following exponentiation function iExp() and invariant function invariant(), where x, n, e and i are all integers: def iExp(x, n): # precondition: x>0 AND n>=0 if x <= 0 or n < 0 : raise "rExp: invalid inputs!" e=1 i=0 assert invariant(e,x,i,n) while i < n: assert invariant(e,x,i,n) e = e*x i=i+1 assert invariant(e,x,i,n) assert i==n and invariant(e,x,i,n) return e def invariant(e,x,i,n): return "loop invariant expression" which is the correct "loop invariant expression"
i<=n and e==x**i
If it is cold I need a coat; it is cold. Therefore I need a coat.
modus ponens
If it is cold I need a coat; I do not need a coat. Therefore it is not cold.
modus tollens
Is g(n) bijective
no
Is g(n) surjective
no
n is in the domain of integers Z g(n) = n2 Is g(n) injective
no
P(0) and P(1) are true. For all nonnegative integers n, if P(n) and P(n + 1) are true, then P(n + 2) is true. We can now infer that P(n) is true for:
nonnegative integer
P(0) and P(1) are true. For all nonnegative integers n, if P(n) and P(n + 1) are true, then P(n + 2) is true. We can now infer that P(n) is true for:
nonnegative integers
Is the following situation best represented by a permutation or a combination? How many ways are there to find a student council president, vice president, and treasurer out of 100 students?
permutation
P(1) and P(2) are true. For all positive integers n, if P(n) and P(n + 1) are true, then P(n+2) is true. We can now infer that P(n) is true for:
positive integer
P(1) is true. For all positive integers n, if P(n) is true, then P(n + 1) is true. We can now infer that P(n) is true for:
positive integer
P(1) and P(2) are true. For all positive integers n, if P(n) and P(n + 1) are true, then P(n+2) is true. We can now infer that P(n) is true for:
positive integers
Given ¬p, ¬q and ¬(p ∨ q) → r, which of the following is true?
r
In class we showed that breaking up a chocolate bar with n squares into the individual squares takes n-1 breaks. Did we show this using regular induction or strong induction?
strong induction
What is wrong with this "proof" by strong induction? Proposition: For every nonnegative integer n, 5n = 0. Basis step: For n = 0, we see that 5 ⋅ 0 = 0. Inductive step: Suppose that 5 j = 0 for all nonnegative integers j with 0 ≤ j ≤ k. We will show that 5 ( k + 1 ) = 0. Write k + 1 = i + j where i and j are natural numbers less than k + 1. By the inductive hypothesis,5 ( k + 1 ) = 5 ( i + j ) = 5 i + 5 j = 0 + 0 = 0. Since the basis step and the inductive step hold, for very nonnegative integer n, 5n = 0.
the inductive step is not correct
What is the value of the proposition ((P → Q ) → Q) → Q for P = false and Q = false?
true
What is the value of the proposition ((P → Q ) → Q) → Q for P = false and Q = true?
true
What is the value of the proposition ((P → Q ) → Q) → Q for P = true and Q = true?
true
What is the value of the proposition (P → Q ) → Q for P = true and Q = false?
true
What is the value of the proposition (P → Q ) → Qfor P = true and Q = true?
true
What is the value of the proposition (P → Q ) → for P = false and Q = true?
true
Given the precondition that x is a floating point number, and given the following program segment: if (x < 0) x = 0; What is the postcondition?
x >= 0
True or false: the sequence (()()) is uniquely defined.
True
Is f(n) surjective
yes
Is h(n) bijective
yes
Is h(n) surjective
yes
n is in the domain of integers Z f(n) = -n Is f(n) injective
yes
n is in the domain of integers Z h(n) = n+1 Is h(n) injective
yes
Consider the set of binary strings, B ∗ defined recursively by Base case: The empty string, λ ∈ B ∗ Recursive rule: if x ∈ B ∗, then x 1 ∈ B ∗ x 0 ∈ B ∗ What elements does the subset ofB ∗ consisting of strings of length three that start with one contain?
{ 100 , 101 , 110 , 111 }
Consider the set S of strings defined recursively by Base case: the empty string, λ ∈ S and a ∈ S Recursive rule: if x ∈ S then, x b ∈ S x b a ∈ S What are all the strings of length at most 3 in S?
{ λ , a , b , a b , b b , b a , a b b , b b b , b a b , a b a , b b a }
The set A is defined as A = {a, b, c, d}. Let the function f: A → Z (the integers) be defined as f = {(a, -1), (b, 0), (c, 1), (d, 2)}. What are the Domain [ Select ] ["Z (the integers)", "{a, b, c, d}", "{-1, 0, 1, 2}"] Range [ Select ] ["{-1, 0, 1, 2}", "{a, b, c, d}", "Z (the integers)"] Target Z (the integers) of this function?
{a, b, c, d} {-1, 0, 1, 2} Z (the integers)
Which of the following equivalences is true?
¬(k ∧ r) ∨ s ≡ (r ∧ ¬s) → ¬k
Which of the following equivalences is true
¬(k∧¬r)∨s ≡ ¬k∨r∨s ≡ k → r∨s
The statement (p ∨ q) ∧ (p → r) is logically equivalent to:
¬p ∧ r ***answer***None of the above are true q ∨ ¬r p ∨ ¬r q ∨ r
Given ¬p, ¬q and r → (p ∨ q), which of the following is true?
¬r
Given k and r and ¬(k ∨ r) ∨ s. Which of the following is false?
¬s
Let w i be the string created by concatenating i copies of w. For example, w 2 = w w. Which of the following are correct recursive definitions for the set W = { w i : i ∈ N }?
* Base case: the empty string, w 0 ∈ W . Recursive rule: w i = { w w i − 1 , i is odd w i / 2 w i / 2 , i is even ∈ W . * Base case: the empty string, w 0 ∈ W . Recursive rule: w i = w w i − 1 ∈ W . * Base case: the empty string, w 0 ∈ W . Recursive rule: w i = w i − 1 w ∈ W .
What is the loop invariant for each variable in the following code segment? x = 22; while (x > 0) { x /= 2; } assume x is an integer and that the division is integer division.
0 <= x <= 22
A coin is flipped six times, producing either heads or tails. How many possible outcomes contain two or fewer heads?
1 + 6!/5! + 6!/(4!*2!)
Given the following weighted graph: Answer this and the following 5 questions. 1.) Is (A,B) in the Minimum Spanning Tree? 2.) Is (B,C) in the Minimum Spanning Tree? 3.) Is (E,D) in the Minimum Spanning Tree? 4.) Is (A,B) in the Single Source Shortest Path Tree? 5.) Is (A,D) in the Single Source Shortest Path Tree? 6.) What is the shortest path length from A to E?
1.)no 2.)yes 3.)yes 4.)yes 5.)yes 6.) 4
Given the following directed graph: Answer this and the following 5 questions. 1.) Is A, C, E, B, D a DFS traversal? 2.) Is A, C, E, B, D a BFS traversal? 3.) Is A, C, E, B, D a Topological Sort? 4.) Is A, D, C, B, E a DFS traversal? 5.) Is A, D, C, B, E a BFS traversal? 6.) Is A, D, C, B, E a Topological Sort?
1.)yes 2.)no 3.)no 4.)no 5.)yes 6.)yes
How many ways are there to find three student council representatives out of 100 students?
100!/(3!*97!)
A combination lock has a password that is three numbers between 0 and 99. Assuming the same number can't be used more than once in a password, how many potential passwords are there?
100!/97!
How many strings are there over the set {a, b, c, d} that have length 4 in which no two consecutive characters are the same?
108 = 4 ∗ 3^3
Consider the following recursive definition for the following set A of bitstrings: Base case: The empty string, λ ∈ A, 1 ∈ A, and 0 ∈ A Recursive rule: if x ∈ A then 1 x 1 ∈ A 0 x 0 ∈ A Which of the following are elements of A?
1101011 101 110011
Consider the following recursive definition for the following set A of bitstrings: Base case: The empty string, λ ∈ A, 1 ∈ A, and 0 ∈ A Recursive rule: if x ∈ A then 1 x 1 ∈ A 0 x 0 ∈ A Which of the following are elements of A?
1101011 110011 101
A person wants to go from Denver to Los Angeles. There are two trains and three buses that go from Denver to Grand Junction, and three trains and two buses that go from Grand Junction to Los Angeles. This person has to either purchase a bus pass (which allows them to take only buses) or a train pass (which allows them to take only trains). How many ways are there for them to get to Los Angeles from Denver?
12 = 2 ∗ 3 + 3 ∗ 2
How many even sized subsets of the letters are there (the empty set is even sized)
16
How many odd sized subsets of the 5 letters are there
16
What is f(3) if f(0) = 1 and f(n) is defined recursively by f(n+1) = 2f(n)?
16
A building has 14 floors and 12 offices per floor. How many offices are in the building?
168 = 14*12
How many different three-letter initials are there?
17576 = 26^3
In this and the following 5 questions there are four quantified predicates: a: ¬∀ x:(P(x)∧Q(x)) b: ¬∃ x: (P(x) ∧ Q(x)) c: ∃ x:(¬P(x) ∨ ¬Q(x)) d: ∀ x:(¬P(x) ∨ ¬Q(x)) 1:a=b 2:a=c 3:a=d 4:b=c 5:b=d 6:c=d
1:no 2:yes 3:no 4:no 5:yes 6:no
How many bit strings are there of length 1, 2, 3, or 4?
30 = 2 + 2^2 + 2^3 + 2^4
A shot put event has a first, second, and third prize. If 30 competitors enter the shot put competition, how many combinations of first, second, and third place winners can there be?
30!/27!
How many strings of five English letters are there that start with 'x' and do not have any repeated letters?
303600 = 1 ∗ 25 ∗ 24 ∗ 23 ∗ 22
How many bit strings of length 7 that start with 11 are there?
32
How many bit strings are there of length five?
32 = 2^5
How many license plates can be made using either three digits followed by three uppercase English letters or three uppercase English letters followed by three digits?
35152000 = 10^3 ∗ 26^3 + 26^3 ∗ 10^3
How many functions are there from the 5 letters to the 3 digits
3^5
How many permutations are there of the set {a, b, c, d, e} that start with a?
4!
In the above sequence, what is the sum of the first 6 elements
4-1/16
Counting. In the following questions, there are always 5 letters (a,b,c,d,e) and 3 digits (0,1,2) How many permutations of the letters are there in which a is left of c, but not necessarily directly left of c, and c is left of e but not necessarily directly left of e
5!/3!
How many permutations of the 5 letters are there in which a is directly left of b
5!/5
A club with 10 students elects a president, vice president, secretary, and treasurer. No student holds more than one position. How many ways are there to select the class officers?
5040 = 10 ∗ 9 ∗ 8 ∗ 7
A multiple-choice test contains 20 questions. Each question has 5 possible answers. In how many ways can a student answer the questions on the test if they answer every question?
5^20
Given a set of 7 elements, how many even sized sub-sets are there? (empty set has 0 thus even number of elements)
64
How many bit strings of length 7 that start with 11 or end with 1 are there?
80
What is the value of f(4) if f(0) = 5 and f(n) is defined recursively by f(n+1) = -2f(n)?
80
In a race of 10 runners the out come is determined by the first and second place. How many outcomes are there?
9! / 7!
What step best completes the following proof if substituted for the "???"? Proposition: A postage price of n ≥ 8 cents can be payed entirely using 3 cent and 5 cent stamps.
By the inductive hypothesis, ( k − 2 ) can be represented using 3 and 5 cent stamps.
What step best completes the following proof if substituted for the "???"? Proposition: A postage price of n ≥ 8 cents can be payed entirely using 3 cent and 5 cent stamps. We
By the inductive hypothesis, ( k − 2 ) can be represented using 3 and 5 cent stamps.
A 10 people CS faculty and a 15 people Biology faculty need to create a Bioinformatics Committee of 2 CS and 2 Biology people. How many committees can be formed? P(n,k) stands for the number of k out of n permutations. C(n,k) stands for the number of k out of n combinations.
C(10,2)*C(15,2)
How many bit strings of length 10 contain an equal number of 0s and 1s?
10!/(5!*5!)
How many bit strings of length 10 contain exactly four 1s?
10!/(6!*4!)
Is the following situation best represented by a permutation or a combination? A coin is flipped six times, producing either heads or tails. How many possible outcomes contain exactly three heads?
Combination
Is the following situation best represented by a permutation or a combination? How many ways can a set of ten letters from the English alphabet be chosen?
Combination
How many bit strings of length 7 that end with 1 are there?
64
How many surjective functions are there from the 3 digits to the 5 letters
0
In the above sequence, what is the sum of the first 10 elements?
100
how many 5 digit strings have exactly three ones
C(5,3)*4
What is the value of the proposition ((P → Q ) → Q) → Q for P = true and Q = false?
false
What is the value of the proposition (P → Q) →Q for P = false and Q = false?
false
What is wrong with this "proof" by strong induction? Proposition: For every nonnegative integer n, 5n = 0. Basis step: For n = 0, we see that 5 ⋅ 0 = 0. Inductive step: Suppose that 5 j = 0 for all nonnegative integers j with 0 ≤ j ≤ k. We will show that 5 ( k + 1 ) = 0. Write k + 1 = i + j where i and j are natural numbers less than k + 1. By the inductive hypothesis,5 ( k + 1 ) = 5 ( i + j ) = 5 i + 5 j = 0 + 0 = 0. Since the basis step and the inductive step hold, for very nonnegative integer n, 5n = 0.
The inductive step is not correct.
P(0) and P(1) are true. For all nonnegative integers n, if P(n) and P(n + 1) are true, then P(n + 2) is true. We can now infer that P(n) is true for:
The nonnegative integers
Assume a flower has an even number of petals, and two players, A and B take turns removing either one or two petals, and assume they proceed as follows: while remaining number of petals is greater than zero: A removes one or two petals if there are petals remaining, B removes the same number of petals that A did Which of the following is a correct loop invariant for this process? Also note that zero is an even number.
The number of remaining petals is even
P(1) is true. For all positive integers n, if P(n) is true, then P(n + 2) is true. We can now infer that P(n) is true for:
The odd positive integers
P(1) is true. For all positive integers n, if P(n) is true, then P(n + 1) is true. We can now infer that P(n) is true for:
The positive integers
P(1) and P(2) are true. For all positive integers n, if P(n) and P(n + 1) are true, then P(n+2) is true. We can now infer that P(n) is true for:
The positive integers.
P(1) is true. For all positive integers n, if P(n) is true, then P(2n) is true. We can now infer that P(n) is true for:
The powers of two: {2^x : x E N} = {1,2,4,8,...}
What is wrong with this "proof" by strong induction? Proposition: a n = 1 for all nonnegative integers n. Basis step: For n = 0, we see that a 0 = 1 by definition of a 0. Inductive step: Assume that a j = 1 for all nonnegative integers j with j ≤ k. We will show that a k + 1 = 1. We see that a k + 1 = a k ⋅ a k a k − 1 = 1 ⋅ 1 1 = 1. Since the basis step and the inductive step hold, a n = 1 for all nonnegative integers n.
This proof requires a basis step for n = 1, and for which the claim is not correct.
Every student in the university has lived in the dorms. Moses has not lived in the dorms. Therefore Moses was not a student in the university.
True
True or false: The function f:R → R defined by f(x) = sqrt(x^2 + 1) is a well-defined function.
True
Given the universal set U = {0,1,2,3,4,5} and subsets A = {1,3,5}, B = {0,2,4}, C = {1,2,3}, D={2,3,4}, E={3,4,5}, match the following: where - stands for difference & stands for intersection, and | stands for union
U-B is A A&C is {1,3} B | D | E is U-{1} D - E is B&C A | B is U
You have an infinite container with blue and red marbles. At each step you randomly pick three marbles from the container. You have a pile of marbles in front of you that is initially empty. At each step you randomly pick three marbles from the container. If all of them are the same color, you discard them. Otherwise, you place the marbles that form the majority (two that are either blue or red) in the pile in front of you. What is the loop invariant that describes the state of the pile you are creating?
Your pile contains an even number of marbles.
Let the function f: Z → Z (the integers) be defined as f(x) = |x|. What are the Domain [ Select ] ["Z (the integers)", "N (the natural numbers)", "R (the real numbers)", "The positive integers"] Range N (the natural numbers) Target [ Select ] ["R (the real numbers)", "Z (the integers)", "N (the natural numbers)", "The positive integers"] of this function?
Z (the integers) N (the natural numbers) Z (the integers)
Consider the following snippet of code: # precondition: a and d are positive integers r = aq = 0 while (r >= d) : r = r − d q = q + 1 Which of the following is a correct loop invariant for this code?
a = dq + r
In the recurrences a0 = a 1 = 1 n>1: an = an-1 + an-2 What is a5?
8
How many ways are there to select five countries in the United Nations to serve on a council if two are selected from a block of 20 countries, and four are selected from a block of 40 countries?
20!/(18!*2!) * 40!/(36!*4!)
In the recurrence a0=1 an=2 an-1 + 2 What is a3
22
The available colors to paint a car are red, orange, blue, black, and gold. If a car is to be painted using one of these colors for the main details and another for the highlights, how many possible ways of painting the car are there?
25 = 5*5
How many ways can a set of five letters from the English alphabet be chosen?
26!/(21! * 5!)
Which rule do you need to conclude (A ∨ C) from the hypotheses (A ∨ B) (¬B ∨ C)?
Resolution
Why is the function f:R → R defined by f(x) = sqrt(x) not well defined?
Some elements in the domain do not map to an element in the range.
Why is the function f:R → R defined by f(x) = 1/(x-1) not well defined?
Some elements in the domain do not map to an element in the target.
Why is the function f:R → R defined by f(x) = ±sqrt(x^2) not valid?
Some elements in the domain map to more than one element in the target.
In class we showed that breaking up a chocolate bar with n squares into the individual squares takes n-1 breaks. Did we show this using regular induction or strong induction?
Strong INduction
In class we showed that all natural numbers n ≥ 2 can be represented as the product of primes. Did we show this using regular induction or string induction?
Strong Induction
In class we showed that breaking up a chocolate bar with n squares into the individual squares takes n-1 breaks. Did we show this using regular induction or strong induction?
Strong Induction
In class we showed that the Fibonacci sequence given by f ( 0 ) = 0 f ( 1 ) = 1 f ( n ) = f ( n − 1 ) + f ( n − 2 ) , n ≥ 2 has the property that f ( n ) ≤ 2 n. Did we show this using regular induction or strong induction?
Strong Induction
In class we showed that the Fibonacci sequence given by f ( 0 ) = 0 f ( 1 ) = 1 f ( n ) = f ( n − 1 ) + f ( n − 2 ) , n ≥ 2 has the property that f ( n ) ≤ 2 n. Did we show this using regular induction or strong induction?
Strong induction
In regular induction, showing P(n) is true for n greater than or equal to zero requires showing the basis step, P(0), and the inductive step, i.e. that for every natural number k, if P(k) then P(k+1). True or false: In strong induction, the only difference from regular induction is in the inductive step where you instead prove that for every natural number k, if P(0)∧P(1)∧P(2)∧...∧P(k) then P(k+1).
T
True or false: A proof using regular induction can be considered a special case of proof by strong induction.
T
True or false: The function f:R → R defined by f(x) = sqrt(x^2 + 1) is a well-defined function.
T
True or false: the sequence ()(()) is uniquely defined.
T
P(0) is true. For all nonnegative integers n, if P(n) is true, then P(n + 2) is true. We can now infer that P(n) is true for:
The even nonnegative integers
Is f(n) bijective
yes
Given the following predicates, which is not equivalent with the others?
∀x ¬P(x))
Given the following predicates, which is not equivalent with the others?
∃x(¬P(x)∧¬Q(x))