Sei sulla pagina 1di 2

CIS 118 First Midterm Exam Solutions

Questions 1-4 are to be answered on paper. 1. Write assignment statements that perform the following operations with the variables a, b and c, which are already declared. a and b are doubles, and c is a char. a) b) c) d) e) Adds 2 to a and stores the result in b. Multiplies b times 4 and stores the result in a. Divides a by 3.14 and stores the result in b. Subtracts 8 from b and stores the result in a. Stores the character 'K' in c. b = a+2; a = b * 4; b = a / 3.14; a = b 8; c = 'K';

2. Evaluate the following expressions (do each calculation independently from the others). int a = 2, b = -3, c = 5, d = -7, e = 11; Expression d = a / b / c; a = 7 + c * d / e; d = 2 * a % - b + c + 1; a = 39 / - e - 29 % c; c = 7 + a % (3 + b);

Value d= a= d= a= c= 0 4 7 -7 undefined (3+(-3))=0

3. Write the code for the function median that takes three integers, a, b and c as parameters and returns the middle value in terms of size (e.g., the call median(5, 2, 4) should return the value 4). public static int median(int a, int b, int c) { int max = Math.max(Math.max(a,b),c); int min = Math.min(Math.min(a,b), c); int med = (a+b+c)-(max+min); return med; } 4. What gets printed by the code below? public static void main(String [] args) { int a, b = 1, total = 0; for (a = 0; a + b < 8; ++a) { total += a * b; b += 2; } System.out.println("a = " + a + " } a = 3 b = 7 total = 13

b="+b+"

total = " + total);

CIS 118 First Midterm Exam Solutions


Questions 5-8 are to be answered on a computer. 5. Write an input validation loop that asks the user to enter the whole words "yes" or "no" in lowercase Letters. Scanner input = new Scanner(System.in); String answer = ""; do{ System.out.println("Enter yes or no(Case sensitive): "); answer = input.nextLine(); }while(!answer.equals("yes") && !answer.equals("no")); 6. Write code that does the following: Opens a le named oddNumberList.txt, uses a loop to write the odd numbers 1 through 99 to the le, and then closes the le. PrintWriter pw = new PrintWriter("oddNumberList.txt"); for (int i = 1; i < 100; ++i){ if (i % 2 != 0){ pw.println(i); } } pw.close(); 7. Write code that does the following: Opens a le named oddNumberList.txt, reads the rst 10 integers in the le, closes the le and then prints the sum of the squares of the integers read. File file = new File("oddNumberList.txt"); Scanner input = new Scanner(file); int sum = 0; for (int i = 0; i < 10; ++i){ sum += Math.pow(input.nextInt(), 2); } System.out.println(sum); 8. The length of a year is actually slightly less than 365.25 days (actually 365.242199 days). That means that adding a leap day every four years would throw the calendar off. So a few years which are multiples of 4 are not leap years. The rule for deciding if a year is a leap year is the following: To be a leap year, the "year must be divisible by 4 but not by 100, unless it's divisible by 400." For example, the years 1700, 1800 and 1900 were not leap years even though they were divisible by 4 because they were divisible by 100 but not 400. The years 1600 and 2000, on the other hand, were leap years because they were a multiple of 4 and even though they were divisible by 100, they were also divisible by 400, so that made them OK. Write a function public static boolean isLeapYear(int year) that returns true if its parameter year is a leap year and returns false if its parameter year is not a leap year. public static boolean isLeapYear(int year){ return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0); }

Potrebbero piacerti anche