CS 1401, Exam #2, MWF version

Date: Friday, February 29, 2008
Name (please type legibly, ideally in block letters): ______________________________________________________________________

Tomorrow March 1 is the beginning of the Women's Month. This is the occasion to celebrate women's success in all the filed, including the field of computer science. Let us help in this celebration.

1. Three departments are trying their best to hire female faculty. The names of the three departments are stored in the variables dept1, dept2, and dept3; the number of female faculty in each of these departments is stored, correspondingly, in the variables female1, female2, and female3. Use if-statements to write down a code that, given this information, prints the name of the department with the largest number of female faculty, and the number of female faculty in this department.

Hint: You do not need to read anything from the keyboard or from a file, just compute and print. Assume that the variables dept1, dept2, dept3, female1, female2, and female3 have been assigned some values already.







































2. Suppose that the name of the currently winning department is stored in the variable dept1, and the name of the currently second-place department (who has so far got fewer female faculty) is stored in the variable dept2. After the new round of hirings, the total numbers of female faculty changed. These new numbers of female faculty are stored in the variable female1 for the previously winning department, and in the variable female2 for the previously second-place department. If the number of female faculty in the previously second-place department is now larger than the number of votes for the previously winning department, we need to swap their names, i.e., place the name of the previously second-place department into dept1, place the name of the previously first-place department into dept2, and similarly swap the variables female1 and female2. Write down a code that performs this conditional swap.

Example: if dept1 is "Art", dept2 is "CS", female1 = 4, and female2 = 5, then after your code, dept1 should contain "CS", dept2 should contain "Art", female1 should contain 5, and female2 should contain 4.

Trace your code, step by step, on this example: draw all the boxes and show how their values change after each operation.










































3. A person is eligible for a CS faculty position vote if he or she has a PhD in Computer Science or related discipline and has publications in Computer Science. Assume that we have a boolean variable phdInCS, a boolean variable phdInRelatedField, and an integer variable numberOfPublications. Write down a Java expression for being eligible for a CS faculty position. Draw the truth tables for "and", "or", and "not" operations. Use these truth tables to find the truth value of your expression for our Chair Dr. Ann Gates who has a PhD in Computer Science and more than 20 publications in Computer Science. Explain precedence of different logical operations, and how this precedence is related to precedence of different arithmetic operations.


















































4. To test faculty candidates, CS Department decided to ask them a simple programming question: to compute the sum of squares of all the numbers from 7 to 10. Write down a program that use the for-loop for this computation. Trace your program step by step.

Comment: no need to be too wordy, just draw the boxes corresponding to all the variables, and show how the values of these variables change and what values will be printed.




















































5. The following algorithm can be used to convert a decimal positive integer n into the binary form: Divide n by 2, keep the remainder. Divide the result by 2, keep the remainder, etc. until we get 0. The remainders, when read bottom to top, form the desired binary number.

Example of conversion: for n = 13, we have

13 / 2 = 6 rem 1
 6 / 2 = 3 rem 0
 3 / 2 = 1 rem 1
 1 / 2 = 0 rem 1
When we read the remainders from bottom to top, we get 1101, which is exactly the binary representation of 13 -- since 1 * 8 + 1 * 4 + 0 * 2 + 1 * 1 = 8 + 4 + 1 = 13.

Use this algorithm to convert number 42 into the binary form. Check the result by converting the binary number back into the decimal code.

Reminder: 137 in the decimal form means 1 * 10^2 + 3 * 10^1 + 7 * 10^0. Similarly, 101 in the binary form means 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 1*4 + 0*2 + 1*1 = 4 + 1 = 5.

For extra credit: write down a code that converts a decimal number into the binary form (and returns this binary form as a string).