Spring 2017 Name: TUID: Page Points Score 1 28 2 18 3 12 4 12 5 15 6 15 Total: 100 Instructions The exam is closed book, closed notes. You may not use a calculator, cell phone, etc. i
Some API Reminders Scanner nextint() // reads and returns next token as int nextdouble() // reads and returns next token as double next() // reads and returns next token as String nextline() // reads everything up to \n // and returns as String String class int length(); // returns the number of characters of the string int indexof(string s); // returns position of s, -1 if not there int indexof(string s, int k); //returns position of s at or past position k char charat(int k); // returns character at position k String substring(int k); //substring starting at k up to end String substring(int k, int h); //substring starting at k ending at h excluded int compareto(string anotherstring); String touppercase( ); boolean equals(string s); boolean equalsignorecase(string s); Random nextint() // returns a random integer nextint(max) // returns a random int from 0 incl to max excl nextdouble() // returns random double from 0 incl to 1.0 excl
1. (3 points) Write the line of code which declares a constant PI which is set to 3.14159. 2. (3 points) How much is a gigabyte? 3. (3 points) What is the most important job of the Java compiler? (Note: it is not to find errors in your java program.) 4. (3 points) Suppose that we have the String s. Translate the English phrase s is longer than 10 characters and begins with the letter k into a Java expression. 5. (3 points) Suppose that we have the integers x, y, z. Translate the English phrase x is odd, and is larger than the sum of y and z into a Java expression. 6. (3 points) Is OneFineDayR2 a legal Java identifier? Circle one: A. Yes B. No If not, why not? 7. What is the result of each of the following? Write ERROR if the expression contains some kind of error. (a) (2 points) 10/3 (b) (2 points) int x=10, y=20; x+10*5<y+2 x%2==0 (c) (2 points) (char)( a +1) (d) (2 points) 10%3 (e) (2 points) 4 % 5 * 6 + 297 % 10 + 4 % 8 (a) (b) (c) (d) (e) 1 of 7 question 7 continues...
(f) (2 points)!(true false) (g) (2 points) "1"+5/2+3%4*5+(6+7) (h) (2 points) 6+7*3/2+"+"+5 (f) (g) (h) 8. What s the most appropriate Java data type for each of the following? (a) (1 point) The average Philadelphia annual rainfall. (b) (1 point) The number of times that a student is absent this semester. (c) (1 point) The number of days it rained in Philadelphia last year. (d) (1 point) Your middle initial. (e) (1 point) The US national debt. (f) (1 point) Your last name. (a) (b) (c) (d) (e) 9. (6 points) What if anything is printed? Write ERROR if the code does not compile due to a compiler error. int x=55, y=66, z=77; if (x<100) if (y<80) if (z>80) System.out.println("Monday"); else System.out.println("Tuesday"); (f) out of a possible 18 2 of 7 exam continues...
10. (6 points) What if anything is printed? Write ERROR if the code does not compile due to a compiler error. int x=71; if (x>50) System.out.println("Bert"); else if (x>60) System.out.println("Ernie"); else if (x<40) System.out.println("Kermit"); else System.out.println("Elmo"); 11. (6 points) What s printed? Write ERROR if the code does not compile due to a compiler error. public class WhatsPrintedIfs { public static void main(string args[]) { int a=4; int b=11; int c=17; if (a <= b) { a = b + 1; else { a = a + 5; if (a <= c) { c++; System.out.println(a + " " + c); out of a possible 12 3 of 7 exam continues...
12. (6 points) What s printed? Write ERROR if the code does not compile due to a compiler error. public class WhatsPrinted { public static void main(string args[]) { int i, j; char c; for (i=0, j=5; i<j; i++, j--) { for (c= A ; c< D ; c++) { if (i%2==0) { System.out.println(j + " " + i + " " + c); i++; else { System.out.println(i + " " + c + " " + j); j--; 13. (6 points) What s printed? Write ERROR if the code does not compile due to a compiler error. public class WhatsPrintedLoop { public static void main(string args[]) { int i=10; do { System.out.println(i); i++; while (i<10); out of a possible 12 4 of 7 exam continues...
14. Write the few lines of code which prints the integers from 3 to 999 (inclusive) counting by 3 s (3, 6, 9, 12,... ). (a) (5 points) as a for loop (b) (5 points) as a while loop (c) (5 points) as a do-while loop out of a possible 15 5 of 7 exam continues...
15. (15 points) Write a program that asks the user to enter a series of temperatures followed by the temperature of -100 to quit. The program then prints the average of the cold temperatures, where the cutoff between cold and not cold is some constant that you ve defined in your program. (As you ve done in your homework assignments, use named constants.) If no cold temperatures are entered, print summer vacation. You may assume that you re in a region where temperatures will never be as low as -100. import java.util.scanner; public class SummerVacationSoon { public static void main(string args[]) { out of a possible 15 6 of 7 question 15 continues...
(extra space) 7 of 7 end of exam