1 Test 1. CSC 121 Lecture 21199 Lecturer: Howard Rosenthal March 4, 2014 Your Name: KEY The following questions (or parts of questions) in numbers 1-16 are all worth 2 points each. 1. Fill in the following table for the 8 primitive data types. Spell the types exactly correctly.(16 points total) Data Type Number of Bytes byte 1 short 2 int 4 long 8 float 4 double 8 char 2 boolean 1 1. Answer the following as true or false a. Retrieving data from RAM usually takes more time than retrieving data from a hard drive. F b. A compiler translates source code into machine instructions. T c. The statement int byte = 350 generates an error. T d. If x has type int and y has type float, then the assignment y=x; is legal. T e. (true&&false) always evaluates as false. T f. Every if must have a corresponding else. F g. ( AB == ( A + B )) is true. F h. Java characters are represented in bytecode. F i. The relational operators cannot be used with character data. F
2 2. The Java Virtual Machine (JVM) accepts data generated by the java compiler. 3. The smallest addressable unit of memory is called a byte. 4. Case values in switch statements can be of type int or type char. 5. The one type of primitive variable that cannot be casted is boolean. 6. Convert 36 to binary showing 8 bits 00100100 7. What is the decimal value of: 01101001 105 8. Show -73 as a sixteen bit binary in two s complement format 0000000001001001 = 73 1111111110110110 flipped 1111111110110111 one is added
3 9. Calculate the following assuming java rules apply with respect to typing 3 (4%3) + (3/4) + (1/2.0) = 3-1+0+.5 = 2.5 10. Evaluate the following assuming java rules apply with respect to typing ((5%3) > (5/3)) = true 11. Place parentheses according to the Java precedence for operators. Then evaluate as Java would. 1+2+3+4/5 = 6 12. Find the value of num at the end of the following segment: int newnum, num = 8; newnum = --num; num = newnum++; 7 13. What is the value of:!((t&&f) (F&&T)) = true
4 14. What does the segment below print to the screen? int firstint = 30, secondint=20; if ((firstint > 40 ) (secondint <30)) { System.out.print("It is a true "); } else { System.out.print("It is a false "); } System.out.println("statement."); It is a true statement Extra Scratch Paper
5 Programs: 21 points each pick two out of three to submit e-mail two.java files to csc121csudh@gmail.com Do not mail these files until you are done with the entire exam. No second mailing will be allowed 1. GreatestLeast. Write a program that asks the user to enter four integers. The program will then write out the greatest and the least of these numbers as integers, and the average of the four numbers as a floating point number. Output would like this with the proper values inserted: Greatest Number: Least Number: Average of all the numbers: 2. InchConversion Write a program that prompts for the number of inches a quarterback threw for in his previous game. Convert that into yards, feet, and inches, and prints the results. For instance, The output for 197 inches would be: Yards: 5 Feet: 1 Inches: 5 3. GAS Al's Last Chance Gas station sits on Route 190 on the edge of Death Valley. There is no other gas station for 200 miles. Write a program to help drivers decide if they need gas. The program asks for: The capacity of the gas tank, in gallons. The indication of the gas gauge in percent (full= 100, three quarters full = 75, and so on). The miles per gallon of the car. The program then writes out "Get Gas" or "Safe to Proceed" depending on if the car can cross the 200 miles with the gas remaining in the tank. Tank capacity: 12 Gage reading: 50 Miles per gallon: 30 Get Gas! Use integers for all input and all arithmetic.
6