C960 Discrete
Math 2 Exam
Questions and
Complete Solutions
Graded A+
Denning [Date] [Course title]
,1.4.3: If-else-statement
1)
What is the value of abs after the following lines of code are run?
x := 2
If ( x > 0 )
abs := x
Else abs := -x
End-if - Answer: 2
The condition ( x > 0 ) evaluates to true because the value of x is 2. The line abs := x is executed and the
line abs := -x is skipped. Therefore the final value of abs is 2.
1.4.3: If-else-statement
2)
What is the value of abs after the following lines of code are run?
x := -2
If ( x > 0 )
abs := x
Else
abs := -x
End-if - Answer: 2
The condition ( x > 0 ) evaluates to false because the value of x is -2. The line abs := x is skipped and the
line abs := -x is executed. Therefore the final value of abs is -x, which is -(-2) = 2.
1.5.2: For-loops
Consider the following pseudocode fragment:
sum := 0
,For i = 2 to 5
sum := sum + i
End-for
1) What is the value of sum after the second iteration?
2) How many iterations will the for-loop execute?
3) What is the final value for sum after executing the for-loop? - Answer: 1) 5
In the first iteration, i = 2. In the second iteration, i = 3. 2 + 3 = 5
2) 4
i = 2, 3, 4, 5. Four iterations
3) 14
i = 2, 3, 4, 5. sum = 2 + 3 + 4 + 5
1.6.2: While-loops
product := 1
count := 5
While ( count > 0 )
product := product⋅count
count := count - 2
End-while
1) What is the value of product after the second iteration?
2) How many iterations will the while-loop execute?
, 3) What is the final value for product? - Answer: 1) 15
In the first iteration, count = 5. In the second iteration, count = 3. 5⋅3 = 15
2) 3
count = 5, 3, 1. Three iterations
3) 15
product = 5⋅3⋅1 = 15
1.7.2: Nested loops - example 1
count := 0
For i = 1 to 3
For j = 1 to 4
count := count + i ⋅ j
End-for
End-for
1) How many times is the variable count increased?
2) What is the final value of count? - Answer: 1) 12
For each iteration of the outer loop, the inner loop iterates 4 times. The outer loop iterates 3 times. 3 ×
4 = 12
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller Dants. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $14.49. You're not tied to anything after your purchase.