CMPS 200 Midterm, Spring 2001

American University of Beirut · Introduction to Programming · 10 pages

All CMPS 200 files
CMPS 200 Midterm, Spring 2001, page 1

Stuck on a question? A tutor can walk you through it.

CMPS 200 Midterm, Spring 2001, page 2 CMPS 200 Midterm, Spring 2001, page 3 CMPS 200 Midterm, Spring 2001, page 4 CMPS 200 Midterm, Spring 2001, page 5 CMPS 200 Midterm, Spring 2001, page 6 CMPS 200 Midterm, Spring 2001, page 7 CMPS 200 Midterm, Spring 2001, page 8 CMPS 200 Midterm, Spring 2001, page 9 CMPS 200 Midterm, Spring 2001, page 10

More CMPS 200 files

Show the text of this exam
a 7 ‘| Sf 2 ‘ 260! i be American University of Beirut Department of Mathematics Faculty of Arts & Sciences are oo aerk Liat rv Math 200 - Introduction to Programming os wr) Midterm - Spring-2001 Time: Ih 30 min (—e ° ' Student Id Number Student Name | Professor's name Last Name First Name [ [ | —] | | | NO CREDITS WILL BE GIVEN IF THE JUSTIFICATION IS MISSING WHEN ASKED A. General - 18 points 1. The UltraEdit: (2 pts) (a) Compiles programs and stores them in memory. (b) Creates programs and stores them in memory (c) Creates programs and stores them on disk (d) translates the java programs into class files. 2 Which of the following are invalid identifier(s)? Justify your answer — (3 pts) (a) oNe ye (b) my Window a ae (c) [23price ep, (d) Give2you % la ny (e) public Sy ee (g) AccountIn$ 3. What are the values of the Java variables a and b after the following code is executed: (2 pts) b = false: a= 'b: if (bila) b= 'b: ‘else a= 'b: (a) ais false and b is false (b) ais false and b ts true (c) vis true and b is false (d) ais true and b ts true B. In the space provided. write T or F to indicate whether the statement is true or false (2 pts) . It is an error if the file name is not identical in both spelling and capitalization to the class name with the java file name extension. After the first trial of compilation, even if you have syntax errors, the class file is generated and stored on the disk. Assuming Z has the value 5. The statement System.out.print.n( “2+ 3 + zs displays (3 pts) = + =e + = LO = 235 = 23z NM NM WN NY ) ) ) ) ) What is the result of the tollowing expression? 4 % 5 +3 * 4/ (double) 5 + | (3 pts) care] (b) 7. (c) (ad) 6 (e) none of the sbove Which of the following are wrong instance methods? Justity — (3 pts) 7} ; } =l1) ; } return { 1} return ( 1) return ( 1= return; } ) public sqrti ) public void .nt ml1() }) public boolean radius () ) public void ::3() ( { ( { ( { ( { Find the bugs — 12 points The following application is supposed to read 2 double numbers. compute their average and output the result. The code as given contuns 6 errors. import javax. swing.” public class Average | public static void main (String| ] args) { double x. ve double z: String x1 = JOptionPane.showMessageDialogt “enter the first number’); x= Integer.parseint(x 1): — double z = \+v /2.0: System.exitiy: ] List the errors indicating for cach wether it is a syntax error, a runtime error, or a logical error. Output — 26 points 1. Assuming a= 1, b = 9. and ¢ = 3. What is the output of the following piece of code? (3 pts) } ) ) ut.printin( “Linel" "Linea" else System.out.printin( “Line3" System.out.printin( "Line4" Assuming a= 9, b= 9. and c = 3, What is the output of the following piece of code? (3 pts) if (a System. . a "Linel"” else if ( b it ( ¢ System. . "Line2" else System.out.printin( "Line3" System.out.printin( "Line4" 3. Assuming a = 6 and b = 8. Modify the following code to produce the output shown. You may not make any changes other than inserting braces and changing indentation of the code. (3 pts) ifF(as= 10) \ if (b == 7) System.out printin"@@@@@% ¥ else System.out. print (“HAT 1: System.out.printIn(’SSS$S"): System.out.printinG&&&e&er): Output ‘ Assume that x=4, and y=7. What are the values of x and y after the following if statement. (3pts) sy ( if («312 || wtee5 && --y<7) 5. What is the output of the applet Scoping? (8 pts) import javax.swing.JApplet; import java.awt.Graphics; public class Scope: extends JApplet { int y=25, x=25; public void paint (raphics g) { g.dGrawString({"point at ("+ x y=yt10; method (g); method2 (g) ; methodl (g); g.drawString("point at ("+x public void method: (Graphics g) { g.drawString("point at ("+ x #","+ y +") ", x,y); g.drawString("point at ("+ x #","+ y +#")",x+100,y); ye=yt10; public void method iGraphics g) { int »=10; g.drawString("point at ("+ x +#","+ y +")"|x,y); w*=10; g.drawString("moint at ("+ x +#","+ y +") ",x,y); vt=10; cs } SEE NEXT PAGE Note:you don’t have to provide exact positions! 1B} x! 6. Consider the Java class below: (6 pts} public class Tryit private int numi; private int numc: public TryIt(int paraml, int param2) { numl = paraml; num2 = param2; public void firg=t(int paraml) { int a; int b; a = paraml; b=at 2; display(a, b); second (param1} ; display(this.numl, this.num2); } private void second(int paraml) { int a; int b; a = paraml + b = a + 10; this.num2 = display (a, private void dis; int num2) ¢ System.out. i System.out. System.out. System.out.£ System.out.pr: this.numl); System.out. n "); System.out. ntln(this.num2) ; } } What is the output of the following application Test? public class Test { public static void main( String args[} ) { TryIt varl; varl = new Tryit(5,10); vari.first(10): } D. Rewrite -— 5 points Rewrite the following /f stalement using a switch statement if ((grade > 8) && (grade < 11)) { a=l: b= 2: } ; : else if (grade == 8)~ a= 3: b=4: } else a= 5: E. Programming - 39 points When you say you are 18 years old, you are really saying that the earth has circled the sun eighteen times. Since other planets take less or more days than the earth to travel around the sun. your age would be different on other planets. You can compute how old you are on other planets by the formula: (15 pts) yo SX 365 d where x is the age on earth. y is the age on planet Y, and d is the number of earth days the planet Y takes to travel around the sun. Write an application that inputs the user’s Caith age, a planet number (1 for Mercury, 2 for Venus, 3 for Jupiter. 4 for Saturn). and prints out his/her age on the chosen planet. The values for d are listed in the table below: Planet d = approximate number of Earth days _ for this planet to travel uround the sun Mercury “88 Venus 235 Jupiter 4380 Saturn 10767 public class Age { public static void main (String args [] ) { /eonstant declaration (for the + values of d) 2. Define aclass called Date that will keep track of a date represented in the standard fashion as three integers: day (between | and 31), month (between | and 12). and a four-digit year (e.g., 2000). (14 pts) Fill in this class definition with the following methods: class Date { // instance variables private int day, onth, year; // constructor that takes 3 integer parameters d,m, and y to form a date // instance method setDate // Ic takes 3 parameters d. in. y and update the date(day . month. and year) of an // existing object. it does not return any value // instance method getDay // It has no parameters and returns the day as an integer /Ainstance method getMonth //it has no parameters and returns the month as an integer // instance method getYcar // it has no parameters and returns the year as an integer // instance method print // it has no parameters and prints the date in standard format:(day/month/year) // using the getDay, getMonth, and get Year methods // instance method before // it takes one parameter of type Date and returns true if this date’s year // comes before the othe: