CSE8 - Final Examination CSE 8A UCSD Practice RULES 1 Don't start the exam until you are told to 2 This is a closed-book closed-notes no-calculator

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Final Examination CSE 8A, UCSD Practice RULES: 1. Don't start the exam until you are told to. 2.. This is a closed-book, closed-notes, no-calculator exam. Don't refer to any materials other than the exam itself. 3. Write your name, and your cs8 login name, on each page of the exam when you get to it. 4. Do not talk to anyone but an exam proctor during the exam. Do not look at anyone else's exam. If you're wearing a billed cap, turn the bill around back or take it off. Turn off cell phones and pagers. 5. If you have a question, raise your hand and an exam proctor will come to you. 6. You have 2 hours 30 minutes to finish the exam. When you are done, give your exam to a proctor. The proctor will check your picture ID and sign the ID check below. 7. Exam and course grades will be emailed to your cs8 account by Monday next week. 8..Exams will be returned the third week of next quarter in 3218 APM. Times will be posted in APM. Regrading policy: If, when your exam is returned, you discover an error in grading, immediately attach a note to the exam explaining your reason for requesting a regrade and return the exam to the proctor. Do not write on the exam itself; exams bearing extraneous marks will not be considered for re-grading. NO exams will be considered for re-grading after being removed from the room. Regrades should be requested only if there is clearly a mistake in scoring your paper, such as tallying your score. Also note that the whole exam will be regraded, which could result in a lower score. # Topic max pts 25 8 10 12 15 18 4 16 12 15 5 140 actual pts 1. General T/F 2. Identifiers 3. Literals 4. Expressions 5. Array or file use 6. Loop execution 7. Generic FB 8. Member access 9. Class features 10. Function definition 11. The future TOTAL PROCTOR ID CHECK:________________ GRADER:____________ FINAL EXAMINATION CSE 8APRACTICE REAL NAME LAST:______________________________FIRST:________________ LOGIN NAME:___________________________ Page 0 of 7 1. [25 pts.] (1 pt each) True or False: a. ______ Java is an object-oriented programming language. b. ______ In Java, the integer value 0 is used to represent the Boolean value false. c. ______ In Java, constructors are inherited. d. ______ Syntax errors in programs are detected by the compiler. e. ______ "Put method headers in one file and method definitions in another" is a good rule of Java programming. f. ______ "Implement your algorithm before designing it" is a good rule of Java programming. g. ______ "Use descriptive function names" is a good rule of Java programming. h. ______ "Make instance variables private" is a good rule of Java programming. i. j. ______ In Java, static methods, but not instance methods, can be overloaded. ______ In Java, a constructor for a class always has the same name as the class. k. ______ A private method of an object can access public instance variables of that object. l. ______ A public method of an object can access private instance variables of that object. m. ______ A static method of a class can access private data members of an object of that class. n. ______ If a method of an object is declared final, it cannot access private instance variables of the object. o. ______ A constructor is called when an object is passed as argument to a method. p. ______ Every Java method definition must contain a return statement somewhere in it. q. ______ To select among a small set of alternatives, it is usually better to use a switch statement than a for statement. r. s. t. ______ The method call s1.compareTo(s2) returns boolean true if s1 and s2 are the same String. ______ In Java, a declaration of a variable of class type creates an object. ______ To use a static method of a class, there must first be an instance of the class. u. ______ Command line arguments are available to a Java program through a formal parameter of method main. v. ______ If arr is an array of ints, arr[1] refers to the first element of the array. w. ______ If arr is an array of ints, System.out.print(arr) prints out all the elements of the array. x. ______ In Java, you cannot declare a method to returned a class type. y. ______ In Java, an identifier declared within a method body has scope to the end of the file. 2. [8 pts.] (2 pts each) For each of the following, write YES if it is a legal Java identifier, NO if not. a. ______ b. ______ c. ______ d. ______ _my_favorite_identifier_ test.C cs8w DOUBLE Page 1 of 7 3. [10 pts.] (2 pts each) For each of the following, write I if it is an integer literal constant; F if it is a floating literal constant ; C if it is a character literal constant; S if it is a string literal constant; N if it is none of these. a. ______ b. ______ c. ______ d. ______ e. ______ 3.14159 "float" 2+2 0E0 '\n' 4. [12 pts] (2 pts each) Consider these declaration statements: final int num = 4; double foo = {1.5, 0.5, -1.5, 2.5}; char c= "ABCDE".toCharArray(); Now consider each of the following expressions, in the scope of those declarations. For each, write the VALUE of the expression as a literal constant of the appropriate type; or ERROR if the expression is illegal in Java, or includes an array index out of range. (Consider each expression separately: when determining the value of an expression, ignore side effects of other expressions in the list, if any.) a. __________ b. __________ c. __________ d. __________ e. __________ f. __________ c[6] c[0] == c[1] foo[1] - foo[0] c[num - 2] foo[(2 < 3)] num++ 5. [15 pts.] For each of the following problems, the user will enter a list of 1000 numbers from the keyboard. Suppose that you want to solve the problem with a Java program containing fewer than 100 statements (but without using files, or any objects other than an array). For each problem, say whether it is NECESSARY or NOT NECESSARY to use an array to solve the problem, with these constraints. (Think carefully about each one!) a. _______________Print out the only the numbers in the list that are larger than the tenth number entered. b. _______________Print out "true" just in case more than half of the numbers are exactly divisible by 3. c. _______________Print out "true" just in case the smallest of the numbers in the list was entered more than once. d. _______________Print out "true" just in case the list of numbers was entered in sorted (increasing) order. e. _______________Print out the square root of the average of the numbers. FINAL EXAMINATION CSE 8APRACTICE REAL NAME LAST:______________________________FIRST:________________ LOGIN NAME:___________________________ Page 2 of 7 6. [18 pts.] (3 pts each blank) Consider each of the following Java program fragments (a.)-(c.) separately. For each one, state how many lines of output it prints out, and what the largest number printed out would be; or write "INF" in both spaces if there is an infinite loop. ___________________________________________________________________________ a. int i=100; while (--i>0) if (i>0) break; else System.out.println(i); How many lines printed?________ Largest number printed?________ ___________________________________________________________________________ b. int a = {2,4,6,8,10}, j = 0; do { System.out.println(a[j/2]); j = j+1; } while (j < 5); How many lines printed?________ Largest number printed?________ ___________________________________________________________________________ c. for (int i=0; i<5; i++) for (int j=0; j<2; j++) for (int k=0; k<10; k++) System.out.println(i--); How many lines printed?________ Largest number printed?________ ___________________________________________________________________________ 7. [4 pts.] Fill in the blanks with the best answer. a. A(n) __________________________ is an aggregate data type consisting of a sequence of elements all of the same type. b. An array of arrays is sometimes called a(n) _______________________________ array. c. If a method definition is labelled __________________________, it can be called using just the class name, without creating an object. d. ____________________________ is a JDK program that can create hyperlinked interface documentation from suitably commented Java source code. Page 3 of 7 8. (Note: This question covers iheritance, which was not a topic in the CSE 8A syllabus Winter 2005.) [16 pts.] (2 pts each) Consider the following class definitions: public class BB{ public BB() {x = 0;} public double getX() {return x;} private void setX(double arg) {x = arg;}; private double x; } public class CC extends BB { public CC() {this(0);} public CC(double y) {this.y = y;} public CC(CC o) {y = o.y;} public boolean testX() {return getX()>98.6;} private double y; } Now consider the marked statements in the program below. For each one, say whether it is LEGAL or ILLEGAL in Java. public class T { public static void main(String args) { CC x,y=new CC(),z; double q=3.14159; a. ____________ b. ____________ c. ____________ e. ____________ f. ____________ g. ____________ h. ____________ i. ____________ CC p = new CC(); BB w = new CC(q); q = x.x; if (w.testX()) System.out.println("passed test\n"); x = p; System.out.println("x's x is " + x.getX()); if (! x.equals(y)) System.out.println("Wait a minute!\n"); if (! (x==y)) System.out.println("Wait a minute!\n"); } FINAL EXAMINATION CSE 8APRACTICE REAL NAME LAST:______________________________FIRST:________________ LOGIN NAME:___________________________ Page 4 of 7 9. [12 pts.] Consider the following program: public class Final { public static int fun(int i) { System.out.println(i + 2); i = i + 4; return i; } public static void main(String args) { int j=3; int i; i = fun(j); System.out.println(i + " " + j); } } When this program runs, three numbers are printed out. What are they? First _________, then _________, and finally ________. Now consider this somewhat similar program: public class Final { public static int fun(int i) { System.out.println(i[0] + 2); i[0] = i[0] + 4; return i[0]; } public static void main(String args) { int j = {3}; int i = {99}; i[0] = fun(j); System.out.println(i[0] + " " + j[0]); } } What numbers are printed when this program runs? First _________, then _________, and finally ________. Page 5 of 7 10. [15 pts.] Consider this function declaration: // The function strfmt takes 1 argument, a String. // strfmt prints the characters in the String on the console, // 5 per line, and with a space between each character. For example: // strfmt("abcd"); prints // a b c d // strfmt("1234567"); prints // 1 2 3 4 5 // 6 7 // strfmt("Hello there how are you?"); prints // H e l l o // t h e r // e h o w // a r e // y o u ? void strfmt(String s); Write a definition for the function strfmt. For full credit, briefly comment your definition. 11. [5 pts.] As a programmer, what would you like to do with computers that has never been done before? (A brief answer -- 2 or 3 sentences -- in clear English is worth 5 points. Say what you think; we're interested in hearing about it. Continue on the back of this page if you want.) FINAL EXAMINATION CSE 8APRACTICE REAL NAME LAST:______________________________FIRST:________________ LOGIN NAME:___________________________ Page 6 of 7 blank page.... Have a good holiday break! FINAL EXAMINATION CSE 8APRACTICE REAL NAME LAST:______________________________FIRST:________________ LOGIN NAME:___________________________ Page 7 of 7 ...
View Full Document

This note was uploaded on 06/12/2008 for the course CSE 8 taught by Professor Marx during the Fall '08 term at UCSD.

Ask a homework question - tutors are online