CS 101 Spring 2006 Midterm 1 Name: _______________________________ Email ID: ________ Page 1 of 8 This exam is open text book but closed-notes, closed-calculator, closed-neighbor, etc. Questions are worth different amounts, so be sure to look over all the questions and plan your time accordingly. Please sign the honor pledge here: Page 1 ____ / 4 Page 2 ____ / 9 Page 3 ____ / 9 Page 4 ____ / 24 Page 5 ____ / 12 Page 6 ____ / 12 Page 7 ____ / 10 Page 8 ____ / 20 Total ____ / 100 Note: When an integer type is required use int, when a floating-point type is required use double. 1. (4 points) What section are you in? ____ CS 101-E ____ CS 101-7 (lab 2-3:15 p.m. Thu) ____ CS 101-3 (lab 8-9:15a.m. Thu) ____ CS 101-8 (lab 3:30-4:45 p.m. Thu) ____ CS 101-4 (lab 9:30-10:45 a.m. Thu) ____ CS 101-9 (lab 5-6:15 p.m. Thu) ____ CS 101-5 (lab 11-12:15 a.m. Thu) ____ CS 101-10 (lab 6:30-7:45 p.m. Thu) ____ CS 101-6 (lab 12:30-1:45 p.m. Thu ____ CS 101-11 (lab 8-9:15 p.m. Thu) ___ / 4CS 101 Spring 2006 Midterm 1 Name: _______________________________ Email ID: ________ Page 2 of 8 Basic concepts Answer the following in 20 words or less. Going over 20 words will result in a loss of points. We are looking for basic understanding here, not a long-winded essay. Also, when we ask for the difference between two things, we are looking for how they are the same and/or different, not separate definitions. 1. (3 points) Name two things discussed in class that make a particular algorithm "bad". 2. (3 points) What's the difference between a variable and a type? 3. (3 points) What is the difference between a class and an object? ___ / 9CS 101 Spring 2006 Midterm 1 Name: _______________________________ Email ID: ________ Page 3 of 8 4. (3 points)What's the difference between a keyword and an identifier? 5. (3 points) How does an object differ from a numeric value? 6. (3 points) Why would you define something to be a long as compared to an int? ___ / 18 ___ / 9CS 101 Spring 2006 Midterm 1 Name: _______________________________ Email ID: ________ Page 4 of 8 Expressions (24 points) 7. Given the following definitions: int a = 12; int b = 18; int c = 3; double d = 1.0; double e = 2.0; double f = 9.0; boolean p = true; boolean q = false; boolean r = true; String s = "abc"; String t = "ABC”; String u = new String("abc“); What is the value of the following expressions? a / b _________________ a % b _________________ d / e _________________ p && q _________________ b / f _________________ p || q _________________ !r _________________ a == b _________________ d != q _________________ s == t _________________ s.equals(t) _________________ s.equals(u) _________________ ___ / 24CS 101 Spring 2006 Midterm 1 Name: _______________________________ Email ID: ________ Page 5 of 8 Truth Tables 8. (6 points) Show, via a truth table, the following: !p && !q || r. You may or may not need to use all the columns in the provided table Basic Statements Assume the following definitions are in effect for the following four questions. Thus, you don’t need to declare the variables listed below. int a = 12; int b = 18; int c = 3; double x = 9.0; 9. (3 points) Define and initialize an appropriately named integer variable that is initialized to be the sum of variables a and b. 10. (3 points) Define and initialize an integer variable ratio that is initialized to be the value of the quantity b plus c divided by the quantity a plus b plus c. ___ / 12CS 101 Spring 2006 Midterm 1 Name: _______________________________ Email ID: ________ Page 6 of 8 11. (3 points) Define and initialize an appropriately named double constant with value 1.25. The constant will hold the number of widgets per ounce. 12. (3 points) Write an assignment statement that updates the value of int variable b with a truncated value of double variable x. Strings Assume the following definition is in effect for the following three questions (the String contains 26 letters of the alphabet in order): String letters = "abcdefghijklmnopqrstuvwxyz"; 13. (3 points) What is the output of the following two-line code segment? String s = letters.substring(1, 2); System.out.println("s = " + s); 14. (3 points) What is the output of the following two-line code segment? String s = letters.substring(24); System.out.println("s = " + s); ___ / 12CS 101 Spring 2006 Midterm 1 Name: _______________________________ Email ID: ________ Page 7 of 8 15. (3 points) What is the output of the following two-line code segment? int n = letters.indexOf("*"); System.out.println("n = " + n); if-then-else 16. (7 points) Assume that a, b, j, k have already been declared and initialized. Write an if statement that sets variables a and b to 0 when j is less than k; otherwise it sets a to 2. ___ / 9 ___ / 10CS 101 Spring 2006 Midterm 1 Name: _______________________________ Email ID: ________ Page 8 of 8 Programming 17. (20 points) Write a complete Java program that performs the following steps: • Prompt the user for the temperature in Fahrenheit, and read in that value (as a double) • Convert that temperature to Celsius • Print out the converted temperature The program should be in a class called Exam1. Due to the time constraints on the exam, you do not have to worry about most of the good programming practices here (i.e., comments, header, legend, echoing input, etc.). The only ones you do need to follow are proper variable names and appropriate indentation. The formula for converting the temperature is 8.1/)32(−=fc , where c is the temperature in Celsius and f is the temperature in Fahrenheit. ___ /
View Full Document