M105: Introduction to Programming with Java Midterm Examination (MTA) Spring 2013 / 2014 Question One: [6 marks] Choose the correct answer and write it on the external answer booklet. 1. Compilers and Interpreters are used to convert programs into machine language? a. high-level language b. CPU c. assembly language d. a and c 2. Which of the following is a valid identifier in Java? a. HowAreYou? b. M105-MTA c. pricein$ d. static 3. What is the value of e after executing the following statement? double e = 9 / 2 * 2; a. 2.0 b. 2.25 c. 8.0 d. 9.0 4. What is the value of c after executing the following statement? double c = Math.ceil(2.4); a. 0.4 b. 2.0 c. 2.4 d. 3.0 5. What is the value of x after executing the following statement? boolean x =!(1 >= 1 && 0 <= 1); a. true b. false c. 0 d. 1 6. What is the value of y after executing the following statement? int y = (9 > 5)? 1 : 2; a. 1 b. 2 c. 12 d. 21 M105 MTA Spring 2013/2014 Page 1 of 6
Question Two: [13 marks] Write a Java class called Trapezoid that reads from the user 3 integers that represent the two base lengths of a trapezoid and its height. Then calculates and prints the area of this trapezoid (rounded to 1 decimal place) according to the following formula: ( ) Question Three: [12 marks] a. The following code includes 2 errors: [6 marks] int g = 6; if (g = 6) System.out.printf("g = %.2f\n", g); i. Find these errors and classify them as a compilation error, a fatal logical error or a non-fatal logical error. [4 marks] ii. Rewrite the above code after correcting the errors. [2 marks] b. Trace the following pieces of code and give their exact output. [6 marks] i. [2 marks] int d = 3; double f = 5.684; System.out.printf("d = %d\nf = %.2f\n", d, f); ii. [2 marks] String course = "M105"; System.out.println(course.charAt(0)); if (course.equals("m105")) System.out.println("YES"); else System.out.println("NO"); iii. [2 marks] int z = 7; switch (z % 3) { case 0: System.out.println("ZERO"); case 1: System.out.println("ONE"); break; default: System.out.println("???"); } System.out.printf("Now, z = %d\n", z++); M105 MTA Spring 2013/2014 Page 2 of 6
Question Four: [12 marks] Write a Java program that prompts the user to enter 2 words and displays on the screen a message if the 2 words start with the same character or not. Question Five: [17 marks] Write a Java program that prompts the user to enter 2 non-negative integers and print on the screen their sum and a message explains if this sum is odd or even. If the user enters a negative value for one of the integers (or for both), nothing should be calculated and the message "Invalid Input" should be displayed. Sample Input: Enter the first number: 9 Enter the second number: 7 Sample Output: Sum = 16 which is even Sample Input: Enter the first number: 9 Enter the second number: 8 Sample Output: Sum = 17 which is odd Sample Input: Enter the first number: -3 Enter the second number: 9 Sample Output: Invalid Input M105 MTA Spring 2013/2014 Page 3 of 6
Solution Question One: [6 marks] Choose the correct answer and write it on the external answer booklet. Question No. Answer 1 A 2 C 3 C 4 D 5 B 6 A Question Two: [13 marks] Write a Java class called Trapezoid that reads from the user 3 integers that represent the two base lengths of a trapezoid and its height. Then calculates and prints the area of this trapezoid //Marks are distributed in the comments import java.util.scanner; //1 public class Trapezoid { //1 for the header (including naming the class correctly) public static void main(string[] args) { Scanner input = new Scanner(System.in); //1 int a, b, h; //1.5 double area; //0.5 System.out.print("Enter the first base length of the trapezoid: "); //0.5 a = input.nextint(); //1 System.out.print("Enter the second base length of the trapezoid: "); //0.5 b = input.nextint(); //1 System.out.print("Enter the height of the trapezoid: "); //0.5 h = input.nextint(); //1 area = (double)(a+b) / 2 * h; //2 System.out.printf("The area of the trapezoid = %.1f\n", area); //1.5 } // end main } // end class M105 MTA Spring 2013/2014 Page 4 of 6
Question Three: [12 marks] a. The given code includes 2 errors [6 marks] 1 i. Find these errors and classify them as a compilation error, a fatal logical error or a non-fatal logical error. [4 marks] if (g = 6) The code The error The classification 2 System.out.printf( "g = %.2f\n", g); The condition of if should be a boolean expression. //1 mark g is an integer variable and the format specifier %.2f is not suitable for it. //1 mark compilation error //1 mark fatal logical error //1 mark ii. Rewrite the given code after correcting the errors. //1 mark for each correction int g = 6; if (g == 6) System.out.printf("g = %d\n", g); [2 marks] b. Trace the given pieces of code and give their exact output. [6 marks] i. [2 marks] //1 mark for each correct line of output d = 3 f = 5.68 ii. [2 marks] //1 mark for each correct line of output //deduct 1 mark for each extra line M NO iii. [2 marks] //1 mark for each correct line of output //deduct 0.5 mark for each extra line ONE Now, z = 7 M105 MTA Spring 2013/2014 Page 5 of 6
Question Four: [12 marks] //Marks are distributed in the comments import java.util.scanner; //1 public class Q4 { public static void main(string[] args) { Scanner input = new Scanner(System.in); //1 String word1, word2; //1 System.out.print("Enter the first word: "); //0.5 word1 = input.next(); //1 System.out.print("Enter the second word: "); //0.5 word2 = input.next(); //1 if (word1.charat(0) == word2.charat(0)) //4 System.out.println("The 2 words start with the same character"); //0.5 else //1 System.out.println("The 2 words do not start with the same character"); //0.5 } // end main } // end class Question Five: [17 marks] //Marks are distributed in the comments import java.util.scanner; //1 public class Q5 { public static void main(string[] args) { Scanner input = new Scanner(System.in); //1 int num1, num2; //1 System.out.print("Enter the first number: "); //0.5 num1 = input.nextint(); //0.5 System.out.print("Enter the second number: ");//0.5 num2 = input.nextint(); //0.5 if (num1 >= 0 && num2 >= 0) //3.5 { //0.5 int sum; //0.5 sum = num1 + num2; //1 System.out.print("Sum = " + sum); //1 if (sum % 2 == 0) //2 System.out.println(" which is even"); //0.5 else //1 System.out.println(" which is odd"); //0.5 } else //1 System.out.println("Invalid Input"); //0.5 } // end main } // end class M105 MTA Spring 2013/2014 Page 6 of 6