CS201 Name: Midterm Exam, Total = 100 Points Please write neatly ...

75 downloads 278 Views 45KB Size Report
Midterm Exam, Total = 100 Points. Please write neatly and show as much of your work as possible for partial credit. Scan through all problems first, and attack ...
CS201 Midterm Exam, Total = 100 Points

Name: _____________________

Please write neatly and show as much of your work as possible for partial credit. Scan through all problems first, and attack the easiest problems first. Use the last page or the back if you need more space. 1. (15 pts) Short Answer. Provide brief (1-3 sentence) answers to the following: a) What is the role of a stub in modular program design?

b) If s1 and s2 are both String variables, why doesn’t s1==s2 compare if the contents of the two strings are identical?

c) What is the difference between an interpreted programming language and a compiled programming language?

d) If a Java program is created and compiled on a Windows machine, is it possible to run the resulting class file on a Macintosh or Unix machine? Explain your answer.

e) How do you compile and run a Java program named “foo.java” under Unix?

2. (6 pts) Operator Precedence. Show the output of the following code fragment: System.out.println(2 + 2 % 2 - 2 * 2 / 2); System.out.println(1 / 4 + 1.0); System.out.println(2.0 / 4 + (int) 1.8);

3. (12 pts) Find the Bugs All of the following code snippets have a bug. Identify each one and fix the bug. Assume the necessary code is in place to make a working program (e.g. a main method, a class, etc.). a) The following code should loop indefinitely until the user types in “yes”: BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); String s; do { System.out.println(“Give up?”); s = inFromUser.readLine(); } while (s != “yes”);

b) The following should continue until the user enters a number from 1-3: BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); String s; int i = 0; while ((i!=1) || (i!=2) || (i!=3)) { System.out.println(“Enter either 1, 2, or 3”); s = inFromUser.readLine(); i = Integer.parseInt(s); }

c) The following should sum the numbers from 1 to 10: int sum=0, ctr=1; while (ctr