Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Faculty Of Computer Studies M257 - Putting Java to Work MOCK Exam Units 1 to 4 Instructions: 1. 2. 3. 4. 5. This exam has 3 parts and ALL questions must be answered. Write your answer in the answer booklets. Answers given on the exam paper will not be marked. Student handbooks are NOT permitted in this examination. The use of electronic devices that could have a memory is NOT permitted. At the end of the examination, check that you have written your student ID, name and your section number on the first page. M257 – MTA Spring 2012 – Form A و جميع، هذا االمتحان مكون من ثالث أجزاء .اسئلتها مطلوب حلها علما بأنه لن يتم،االجابة تكون على كراسة االجابات تصحيح اي اجابات معطاه على ورقة األسئلة ممنوع اصطحاب اي كتب او مذكرات الى قاعة .االمتحان .ممنوع استخدام اي اجهزة الكترونية بها ذاكره في نهاية االمتحان تأكد من كتابة اسمك و رقمك الجامعي و رقم شعبتك على كال من كراسة االجابة .و ورقة االمتحان .1 .2 .3 .4 .5 Page 1 of 4 Part 1. The MTA will include 10 Multiple Choice Questions similar to the following: (15 marks) 1. Based on the standard Java conventions for identifiers, the class name ‘FIRST FACTORY’ is written as _______ a. FIRSTFACTORY c. FIRSTFactory b. FirstFactory d. first factory 2. After evaluating the expression double x = (3.0+4*(5/2))%5 , the value of x is _______ a. 0.0 c. 2.5 b. 1.0 d. 3.0 3. After evaluating the following expressions: int x=10; int y=++x; the values of x and y are____ a. x = 10 , y = 10 c. x = 11, y = 10 b. x = 10 , y = 11 d. x = 11, y = 11 4. Which of the following is used to translate and execute Java bytecode? a. Java interpreter c. Java debugger b. Java compiler d. Java disassembler 5. Which of the following is a piece of code that contains instructions on how to initialize objects of a class? a. method c. Constructor b. Function d. Module 6. Class members with _______ access modifier can be accessed only within their defining class. a. private c. Public b. protected d. Default 7. Which of the following terms is used when defining a primitive data type? a. integer c. character b. String d. double 8. Which of the following is an example of an unchecked exception? a. EOFException c. IOException b. MalformedURLException d. NullPointerException M257 – MTA Spring 2012 – Form A Page 2 of 4 Part2. The MTA will also 3 essay similar to the following: (30 marks) 1. Briefly explain the difference between the following pairs: a) Overriding versus overloading. b) Accessor versus mutator methods. (a) 5 marks Overloading means to have more than one method in a class with the same name, but with a different signature. Overriding means that subclasses modifies the methods they inherit from a superclass AND the method signature must stay the same (b) 5 marks Mutator Methods: have an effect on the state of the object Accessor Methods: methods correspond to a simple request for information and cannot alter the state of the object. 2. Answer the following: a) What is a Java stream? b) What are the three steps required to read or write using streams in Java? 2.5 marks A stream is a sequence of bytes, representing a flow of data from a source to a destination. 7.5 marks Step 1. Open the stream :you need to define an object of a special class used for streaming. Step 2. keep reading (or writing): you need to use the methods of the objects defined in step1. Step 3. Close the stream. M257 – MTA Spring 2012 – Form A Page 3 of 4 Part 3. The MTA will include a 3 problem solving questions similar to the following: (55 marks) 1. Answer the following : (a) What is the output of the code below? private void start() throws ArrayIndexOutOfBoundsException{ String[] names = new String[3]; for (int i = 0; i < names.length; i++) //first for loop names[i] = "abc"; for (int i = 0; i < names.length; i++) //second for loop System.out.println(names[i]); } (b) Rewrite the above code after doing the following: i. Use a try-catch statement to handle an ArrayIndexOutOfBoundsException. An error message “ERROR!” should be displayed if the exception is thrown. ii. Replace the first for loop with a while loop. iii. Replace the second for loop with a “for each” loop. (a) (Total: 2 marks) abc abc abc ----------------------------(b) (Total: 11 marks) private void start(){(1 mark for removing the throws part) String[] names = new String[3]; try {(1 mark) //first loop int i = 0; (1 mark) while (i < names.length) {(1 mark) names[i] = "abc";(1 mark) i++;(1 mark) } //second loop for (String n : names) (2 mark) System.out.println(n); (1 mark) } catch (ArrayIndexOutOfBoundsException e) {(1 mark) System.out.println("Error!"); (1 mark) } } 2. In this question, you will model a stock of a business that is identified by a name and a symbol, and has previous and current prices. For doing so, do the following: (a) Develop a class Stock to the following specification: The class has four private instance variables, String previousPrice, and double currentPrice. M257 – MTA Spring 2012 – Form A symbol, String name, double Page 4 of 4 A two-argument constructor that sets symbol and name to given values. The class has getter methods for all its instance variables. The class has setter methods for previousPrice and currentPrice. The class has a method getChangePercent() which returns a double value calculated by the following equation: 𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑝𝑟𝑖𝑐𝑒 − 𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 𝑝𝑟𝑖𝑐𝑒 𝑐ℎ𝑎𝑛𝑔𝑒 𝑝𝑒𝑟𝑐𝑒𝑛𝑡𝑎𝑔𝑒 = × 100 𝑝𝑟𝑒𝑣𝑖𝑜𝑢𝑠 𝑝𝑟𝑖𝑐𝑒 The class overrides the Object’s toString()method in order to return a string representation of symbol, name, previousPrice, currentPrice, and the change percentage value in a year in the format shown by the following example: Symbol: ABC Name: Name Inc. Previous Price: 60.0 Current Price: 66.0 Change Percentage: 10.0% (b) Develop a class StockTest to run that program. The class includes only the main method which is used to do the following: Create an object named st of the type Stock with a stock symbol "ABC" and a name "Name Inc.". Sets the previous price of st to 60. Ask the user to enter the current price (using Scanner class), and set the current price of st to the value entered by the user Display to the user the state of st using its toString() method. Don’t forget to import any necessary libraries in both of the above classes. (a) public class Stock { (1.5 mark) private String symbol, name; (1.5 mark for all variables declaration) private double previousPrice, currentPrice; //constructor public Stock(String symbol, String name) { (1.5 marks) this.symbol = symbol; this.name = name; } //Accessors and mutators public String getSymbol() {return this.symbol;} (1.5 mark) public String getName() {return this.name;} (1.5 mark) public double getPreviousPrice() {return this.previousPrice;} (1.5 mark) public double getCurrentPrice() {return this.currentPrice;} (1.5 mark) public void setPreviousPrice(double price){this.previousPrice = price;}(1.5mark) public void setCurrentPrice(double price){this.currentPrice = price;} (1.5 mark) public String toString(){(1.5 marks) return "Symbol: " + symbol + "\nName: " + name + "\nPrevious Price: " + previousPrice+ "\nCurrent Price: " + currentPrice+ "\nChange Percent: " + getChangePercent()+"%"; } //mandatory method public double getChangePercent() { (2 mark) return ((currentPrice - previousPrice) / previousPrice)*100; M257 – MTA Spring 2012 – Form A Page 5 of 4 } } (b) import java.util.Scanner; (1 mark) public class StockTest {(1 mark) public static void main(String[] args) { (1 mark) Stock st = new Stock("ABC", "Name Inc."); (1 mark) st.setPreviousPrice(60); (1 mark) Scanner in = new Scanner(System.in); (1 mark) System.out.print("Enter current price: "); (1 mark) JOptionPane is also ok st.setCurrentPrice(in.nextDouble());(2 marks) System.out.println(st.toString()); (1 mark) JOptionPane is also ok } } 3. Consider the following code: (10 marks) import java.io.*; import java.util.*; public class RewriteCode { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new File("c:/text.txt")); while(in.hasNextLine()) System.out.println("you wrote: " + in.nextLine()); } } Rewrite the above code according to the following: a. Handle the IOException using try…catch clause instead of declaring it in the main method header. The program should display the message “I/O error!” if an IOException is thrown. b. Replace the Scanner class with another class to read text from the text file. Make any necessary changes to the code, and remove any unnecessary import statements. import java.io.*; import java.util.*; public class RewriteCode { public static void main(String[] args){ 1 mark (for not throwing IOException here) try{1 mark BufferedReader in =new BufferedReader(new FileReader("c:/text.txt"));2marks String s = in.readLine();1 mark while(s !=null){ 1 mark System.out.println("you wrote" + s); 1 mark s = in.readLine();1 mark } }catch(IOException e){ 1 mark System.out.println("I/O error!"); 1 mark } } } M257 – MTA Spring 2012 – Form A Page 6 of 4