CS 1401, Quiz 5

Date: Wednesday, October 11, 2006
Name (please type legibly, ideally in block letters): ______________________________________________________________________

1. Draw the truth tables for "and", "or", and "not" operations. Use these truth tables to find the truth value of the statement 3 < 5 && 1 < 4 || 4 < 1. Explain precedence of different logical operations, and how this precedence is related to precedence of different arithmetic operations.

For extra credit: what is the difference between && and &? What is the advantage of each of these operations? Give an example when they lead to different results.

Turn over, please.

2. Use the while loop to write a piece of code that, given an integer n, finds the smallest natural number k for which k^3 > n. In other words, your code should find the smallest cube which is larger than a given integer n. For example, for n = 7, we should return 2, because 2^3 = 8 > 7, and 2 is the smallest such power: 1^3 = 1 < 7. Trace your code step-by-step for n = 7. Idea: start with k = 0, and while the condition k^3 > n is not satisfied, keep increasing k by 1.

Reminder: to compute the power a^b in Java, you can use the operation Math.pow(a,b).

For extra credit: when you want to increase k by 1, what is the advantage of using k++ or ++k instead of k = k+1?