# CPSC 120 -- Test #2 Review

### Topics

• Machine Representation of Integers (Class notes, handout on twos complement, Lab 5 Prelab and Lab, exercise handout, Section 2.4 and Appendix B of the text)
• Be able to convert a number in any base to its equivalent base 10 representation.
• Be able to convert a base 10 number to any other base.
• Be able to convert among bases 2 (binary), 8 (octal), and 16 (hexadecimal) without using base 10.
• Be able to find the n-bit (where n is specified) twos complement representation of a base 10 integer (positive or negative).
• Be able to find the base 10 value of a number represented in twos complement.
• Be able to find the largest (and smallest) base 10 unsigned number that can be stored in n-bits; be able to find the largest (and smallest) base 10 signed twos complement number that can be stored in n-bits.
• Be able to add base 2 and twos complement numbers; understand the problem of overflow and be able to demonstrate it.

• Selection Statements (if ... else ...) and Boolean Expressions (Sections 3.1 - 3.4 of the text plus labs and prelabs)
• Know how to write if ... else ... statements in their various forms (with no else clause, nested, cascading)
• Know how to write and evaluate boolean expressions using relational operators (such as == and <=) and boolean operators (&&, ||, and !)
• Know about the boolean data type -- how to declare a boolean variable and assign the result of a boolean expression to the variable
• Know how to use the equals method of the String class to compare String objects.
• Know that the switch statement is another selection statement that can be used when your selection condition is to test to see if a single variable is equal to one of several different possible values (the cases in the switch). Understand the role of the break statement in the switch -- without it once a condition is true all remaining statements in the switch would be executed rather than just the one where the condition is true. You will not have to write a switch statement on the test but you may be asked some general question or given a switch statement and asked something about it.

• Logic (Handouts)
• Know the logic operators AND, OR, and NOT and both the mathematical and Java symbols for these.
• Be able to construct truth tables for boolean expressions (propositions/ logic statements).
• Know the basic laws of logic (DeMorgan's laws, distributive law, associative law, commutative law, idempotent law, etc.).
• Be able to show two boolean expressions are equivalent using truth tables and the laws of logic.
• Understand the connection between logic and computer circuits -- be able to write a logic statement equivalent to a given circuit, be able to simplify the statement using the laws of logic and then draw the simpler circuit, be able to construct circuits given either a truth table for the output or a verbal description of the output of the circuit.

• While Loops (Section 3.6)
• Be able to write loops -- be able to correctly control the loop (write the appropriate boolean expression for the loop control condition, initialize any variables in that expression (loop control variables) before the loop, update those variables correctly inside the loop), be able to sum and to count in a loop, be able to use a loop to find averages (you need to sum and count!)
• Be able to trace the execution of a loop

1. Self-Review questions 3.3 - 3.10 on page 197 plus exercises 3.1 - 3.9, 3.14, 3.15, 3.21 on pages 198 - 200.

2. Write a segment of code that takes an integer between 0 and 15 and assigns the character variable hexDigit the corresponding hex digit (as a character -- so if the integer is 1 hexDigit will be '1'; if the integer is 11 hexDigit will be 'B'). You should use a cascading if ... else... statement (you could also try a switch).

3. Trace the execution of the following segment of code and determine what is printed for each of the following values of answer: (a) 3; (b) 0; (c) 8; (d) 1 (NOTE: This requires a very careful trace because it doesn't quite do what expected due to missing braces.)
```
System.out.print("Enter a number: ");
System.out.println ("Hello");
System.out.println("There");
else
System.out.println("You");
System.out.println("What's");
else
System.out.println("Up");

```
4. Trace the execution of the following loop by showing the value of the variables at each step.
```          int count1 = 0;
int count2 = 0;
int num = 2157;
while ( num != 0)
{
if (0 == num % 2)
count1++;
else
count2++;
num = num / 2;
}

```
5. Assume the variable seconds has been declared to be of type int and a value has been assigned to it. Write a declaration of a variable named validSeconds of type boolean and assign to this variable a boolean expression that determines whether or not seconds contains a valid number of seconds (a number between 0 and 59 inclusive).

6. Assume the variable yearsExperience (type int) contains the number of years computer experience of a person and the variable passed120 (type boolean) is true if the person passed CPSC 120 at Roanoke College. Assume a person is eligible for a computer related job if they have at least 5 years computer experience or they passed CPSC 120. (a) Write a declaration of a boolean variable eligible and assign it the boolean expression that is true if the person is eligible for the job. (DON'T use IFs!!) (b) Write a boolean expression that would be true if the person is NOT eligible for the job -- write this two different ways (one way with ANDS and one with ORS -- and of course some NOTS).

7. Write a segment of code that asks the user to enter a month (as an integer 1 - 12). If the number entered isn't valid the code should print a message and should ask the user to try again. It should keep doing this until a valid integer (1-12) is entered.

8. Write a segment of code to read in a sequence of rainfall data (each data item represents the number of inches of rain in a month) and find the average rainfall, the maximum rainfall, the minimum rainfall, the number of months in which the rainfall was over 4 inches and the number of months in which the rainfall was under 1 inch. Do this two different ways:
• The code should keep reading until the user enters -2 for the rainfall
• The code should explicitly ask the user if he/she has another rainfall amount to enter

9. Write a loop to print out the first n positive multiples of 3 and to find and print the sum of them. So, for example, if n = 7, the program would print out the numbers 3, 6, 9, 12, 15, 18, 21 and the sum 84. Is this a count-controlled loop?

10. Modify the loop above to print out, count, and sum all multiples of 3 less than n. So, in this case, if n = 7, the program would print 3, 6 and the sum of 9 and count of 2. Is this a count-controlled loop?