CMPS 200 Final, Fall 2001

American University of Beirut · Introduction to Programming · 10 pages

All CMPS 200 files
CMPS 200 Final, Fall 2001, page 1

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

CMPS 200 Final, Fall 2001, page 2 CMPS 200 Final, Fall 2001, page 3 CMPS 200 Final, Fall 2001, page 4 CMPS 200 Final, Fall 2001, page 5 CMPS 200 Final, Fall 2001, page 6 CMPS 200 Final, Fall 2001, page 7 CMPS 200 Final, Fall 2001, page 8 CMPS 200 Final, Fall 2001, page 9 CMPS 200 Final, Fall 2001, page 10

More CMPS 200 files

Show the text of this final exam
American University of Beirut Math200- Introduction to Programming Final - Fall 2001-2002 Time :1 Hour & 30 min. Name Id Section eo _ _ A. Multiple choice. Choose the correct 1. The following code will print . inti = 10 + 20 - 20/20 * 40 % 5; System.out.printinG++); a) 10 b) 30 c) 20 d) Il e) None of the above 2. Given the following sequence of Java statements StringBuffer sb = new StringBuffer("abe"): | 2 String s = new String("abe"); 3 sb.append("def"): 4 sb.insert(1, "22ZZ"): 5 s.append("def"), 6 s.concat(sb): 7 s.trimQ); Which of the following statements are true: a) The compiler would generate an error for line |. b) The compiler would generate an error for line 2. c) The compiler would generate an error for line 3. d) The compiler would generate an error for line 4. e) The compiler would generate an error for line 5. f) The compiler would generate an error for line 6. g) The compiler would generate an error for line 7. 3. The difference between call by reference and call a) The return type of the method b) The type of formal parameters c) The number of parameters d) none of the above answer (36 pts) ERICAN UNIVERSIT LIBNARY OF BEIRUE Please place your answers here clearly: Question Answer -by-value depends on: 4. What is the output of the following String s] = new String ("Amit"): String s2= new String ("Amit"): String s3 = "Amit"; String s4 = “Amit”; System.out.print (sl==s2+ “\U~): System.out.print (s1.equals(s3) + “Xt “); System.out.print (s3!=s4 + “\t “); System.out.printin("Poddar" substring(3)): a)false true false dar b) false true false Poddar c) true true false dar d)true true’ false — pod e) None of the above 5. How many lines of output will be generated by the following java program i=l; while (i> 0) { if (i> 4) i=i-4: else isit+l: System.out.println(‘*’); } a) 0 b) c) d) e) More than 7 Read the following code. Choose the correct answer. public int ABC () { System.out.printIn("Inside Method ABC"); public int XYZ¢ ) { System.out.printinc Inside Method XYZ"); } } Nothing wrong with above code, the code will compile and run fine. Code will compile, but will not run. XYZ method should not be defined in method ABC. XYZ method should not have a return type. XYZ method should be declared private 7. Given the following code what is the effect when a = 5 ? public class Test { public void add¢int a) { for (inti= 1,1<3;i4++) | for (int j= Ie j < 3: j++) { if (a ==>) break : System.out.print(i * j& “"): } Generate a runtime error Print the values: | 2 Print the values: | 2 2 4 Produces no output None of the above Look at the following program and pick the correct answer. 1 public class Test { 2 public static void main (String[ ] args) { 3 for (inti = 0:1 <args.length: i++) System.out.printin(args[ 1): } 6 The program was run with command ¢:\> java Test Above program will not run: throw exception Above program will run fine Above program will not compile. Code at Line number 3 is incorrect. Above program will not run: The command line must include one or more file names after Test. e) None of the above. Look at the following class and pick the correct answer(s). public class test { private static int a: private int b= 12: public test() { }; public static void abet) a= 10; b = b+ 20; } a) Above class will compile. b) Method abc cannot use an instance variable c) Method abe cannot use a static variable d) The constructor method should have a body B. Output (39 pts) SHOW THE STEPS OF YOUR WORK FOR ALL THE EXERCISES IN THIS SECTION 1. What will be output by the following code? public class MyFor { public static void main(String args[]) { inti, j; outer: for (i=1;1 <3 314+) { \ inner:forG=ls j<t: j++) { if G==2) continue inner; System.out.println(’ Value for i=" +14" Value for j=" +)); } System.out.printin("**#*#"); } 2. What is the output of the following code? String sl = new String("HELEO"),; String s2 = s|.replace(E.'V): String s3 = ~World”: sl.concat ("RAM"): System.out.printin(s 1); System.out.printin(s2): System.out.printIn((s2+s3).charAt(5)); 3. What is the output of the following code? . inta=S,j, i; for(i=t:1<4;i++) { jal while( j++ <= i) System.out.print("#"); System.out.printinti * J); } Given the following class: class Bidding { public static void maint String args[] ) { House h], h2, h3: hl= new House(300): h2 = new House(400. 150000); h1.setPrice(90000): ; h3=h2.upgradeHouse(); System.out.print(“the price for the first and second house are’+ hl.getPrice + “,"+ h2.getPrice): } } class House { private double area. int price: public House(double a) { area =a; price=0; } public House(double a,-int p) { area = a; price = 100000: i public void setPricecint p) { price = p; } public int getprice() | return price; } public House upgradeHouse() { return new House(area+200, price+!0000); } } (a) What is the output of the application Bidding? (b) Write an overloaded method upgradeHouse that takes two integer arguments v1 and v2, and returns a new house with urea incremented by v1, and the price incremented by v2. (c) Add a static variable largestArea to the class House and update the first constructor to allow the class House to keep track of the largest area of all the houses created so far. class House{ double area, int price; public House(double a) | area = ar price=0; } (d) Write a static method that prints the largest area. 5. Write a program segment that sets a boolean variable dangerous to true and stops reading data if pressure (a double variable being read in) exceeds 510.0 . Use dangerous as a flag to control the loop. Note: (using a do while loop) _ C. Programming (25 pts) You are the accountant of a big company. The manager sent you a file “ raiseFile.txt” containing the list of employees who are entiticd for a raise this year, along with the raise percentage they will get over their original salaries. The original salaries of the employees are stored in array salaries of type double where the salary of employee 0 is stored in salaries|(}] , employee | is stored in salaries[1], and so on... Write an application that A- reads from “raiseFile.txt” the employee Numbers and their raise percentages and updates their salaries in the array salaries accordingly. B- prints the list of all the employees with their salaries on the file “Salaries.txt”. The file raiseFile.txt looks like this: empNum space Raise percentage + > +> +> 7 | 3 emps 20 Note: e Each employee Number consists of the word “emp” followed directly by a 4 digit number. e Your application should throw Exceptions. Sample input: Sample Output: salaries 750 ie 2 Sataries.tHt. Napa 1000 File Edit Format Help File Edit Format Help 550 fampe 8 ampo ¥50 2 400 ‘emp 2u empl Lose [a5e. | empl 5 emp2 578 375 amps 10 ama3 400 1500 preze 7 omos i 250 oo) emp 2000 i ampd 2000 650 na netemeence see ene te en emp? 650 2 emps 881 800 ame? 850 850 emplo 642 ol 600 empll 400 + 400 — ~ LIBRAR OF BUIRUT public class Company { public static void main (String args[] ) { // declaration and initialization of a sample array of the original salaries int salaries []= {750, 1000, 550. 400, 375, 1500, 2000, 650, 800, 850. 600, 400}; // code to open existing file “raiseFile.txt” for reading 4 code to read from the “raiseFile.txt” the employee numbers, and raise percentages, and to update the salaries of those employees StringTokenizer_methods: StringTokenizer(String s) StringTokenizer(String s, String d) int CountTokens() Boolean hasMoreTokens(): Suing nextToken(); Some String Methods boolean equals(String s) boolean equalsIgnoreCase(String s) int compareTo(String s) String substring(int f, int t ) int index Of(char c) int lastIndexOf(char c) String concat(String s) char charAt(int i) Some StringBuffer Methods StringBuffer() SuingBufer(String s) void insert(int i,String s) void append(String s) char charAt(int i); rm the following: (10 pts) 3. Write an application that perto [EN [F YOU DO NOT CALL THE METHODS OF DATE NO CREDIT WILL BE GIV public class UseDat= { public static “oid main ( String args{] ) { eres 24/01/2000 and 22/02/2001 //Creates the two Ce int method date2 using the method before() //Compare datel anc silt of comparison /fand print the res //Sets the day/montiuryear for /fusing the setbate method the first siate using the Print method