M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014 Question One: Choose the correct answer and write it on the external answer booklet. 1. Java is. a. case sensitive b. object-oriented c. function-oriented d. a and b 2. Which of the following is not a valid identifier in Java? a. Char b. _book c. 6students d. $money 3. What is the exact output after executing the following statements? String word = "Hello"; System.out.println("It is " + word.charat(0)); a. It is H b. It is 'H' c. "It is H" d. It is "H" 4. What is the value of x after executing the following statement? double x = Math.floor(-1.8); a. -1.0 b. -2.0 c. 1.8 d. -1.8 5. What is the value of e after executing the following statement? double e = 9 % 2 / 2; a. 0.0 b. 0.5 c. 1.0 d. 2.0 6. What is the exact output after executing the following statements? int c = 1, d = 6; System.out.println("Sum = " + "c" + d); a. Sum = 7 b. Sum = cd c. Sum = c6 d. Sum = 16 Question Two: [11 marks] Write a Java class called Ellipse that reads from the user 2 integers that represent lengths of the semi-major axis and the semi-minor axis of an ellipse. Then calculates and prints the area of this ellipse (rounded to 1 decimal place) according to the following formula: M105 MTA Makeup Spring 2013/2014 Page 1 of 6
Question Three: [13 marks] a. For each of the following, write an equivalent Java statement assuming that all variables have been declared correctly and initialized: i. [2 marks] ii. using the method printf() instead of the method println() if you know that c is a String variable [2 marks] System.out.println("word = " + c); iii. using the ternary operator? : instead of the if statement if (x <= 25) y = x / 2; else y = 1; [2 marks] b. Trace the following pieces of code and give their exact output. [7 marks] i. [2 marks] int a = 7; double f = a + 2; System.out.printf("ans = %.2f\n", a + f); ii. [3 marks] String MTA = "Makeup"; if (MTA.equals("make-up")); System.out.println("YES"); if (MTA.equals("Makeup")) System.out.println("OK"); int x = 5; if (! (x > 9) && x > 3) System.out.println("NICE"); else System.out.println("NO"); iii. [2 marks] int m = 7; switch (m + 5) { case 1: System.out.println("ONE"); case 2: System.out.println("TWO"); default: System.out.println("???"); } System.out.printf("Now, m = %d\n", ++m); M105 MTA Makeup Spring 2013/2014 Page 2 of 6
Question Four: [8 marks] To get a driving license, you need to be at least 18 years old. Write a Java program that prompts the user to enter his/her age (as an integer) and displays a message on the screen if it is allowed for the user to get a driving license or not. Question Five: [22 marks] Write a Java program that prompts the user to enter 2 non-negative integers and print on the screen their average (rounded to 2 decimal places) and the largest of them. If the user enters 2 equal numbers, the message "The 2 numbers are equal" should be printed instead of the largest of them. 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: 6 Sample Output: Average = 7.50 9 is larger Sample Input: Enter the first number: 9 Enter the second number: 9 Sample Output: Average = 9.00 The 2 numbers are equal Sample Input: Enter the first number: -3 Enter the second number: 9 Sample Output: Invalid Input M105 MTA Makeup Spring 2013/2014 Page 3 of 6
Solution Question One: Choose the correct answer and write it on the external answer booklet. Question No. Answer 1 D 2 C 3 A 4 B 5 A 6 C Question Two: [11 marks] Write a Java class called Ellipse that reads from the user 2 integers that represent lengths of the semi-major axis and the semi-minor axis of an ellipse. Then calculates and prints the area of this ellipse //Marks are distributed in the comments import java.util.scanner; //1 public class Ellipse { //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; //1 double area; //0.5 System.out.print("Enter length of the semi-major axis: "); //0.5 a = input.nextint(); //1 System.out.print("Enter length of the semi-minor axis: "); //0.5 b = input.nextint(); //1 area = Math.PI * a * b; //2 System.out.printf("The area of the ellipse = %.1f\n", area); //1.5 } // end main } // end class Question Three: [13 marks] a. For each of the following, write an equivalent Java statement assuming that all variables have been declared correctly and initialized: i. [2 marks] //1.5 for the expression //0.5 for using parentheses correctly z = Math.sqrt(x*x + y*y) / (2*x*y); M105 MTA Makeup Spring 2013/2014 Page 4 of 6
ii. [2 marks] //1 for %s //0.5 for \n //0.5 for, System.out.printf("word = %s\n", c); iii. [2 marks] // () are redundant and no marks should be deducted for not using them y = (x <= 25)? (x / 2) : 1; b. Trace the given pieces of code and give their exact output. iv. [2 marks] //1.5 for the correct output //0.5 for printing 2 digits after the decimal point ans = 16.00 v. [3 marks] //1 for each correct line of output //deduct 1 mark for each extra line YES OK NICE vi. [2 marks] //1 mark for each correct line of output //deduct 0.5 mark for each extra line??? Now, m = 8 M105 MTA Makeup Spring 2013/2014 Page 5 of 6
Question Four: [8 marks] //Marks are distributed in the comments import java.util.scanner; //1 public class Q4_MU { public static void main(string[] args) { Scanner input = new Scanner(System.in); //1 int age; //0.5 System.out.print("Enter your age: "); //0.5 age = input.nextint(); //1 if (age >= 18) //2 System.out.println("You can get a driving license"); //0.5 else //1 System.out.println("You cannot get a driving license"); //0.5 } // end main } // end class Question Five: [22 marks] //Marks are distributed in the comments import java.util.scanner; //1 public class Q5_MU { 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) //4 { //0.5 int sum; //0.5 double average; //0.5 sum = num1 + num2; //1 average = (double) sum / 2; //1.5 System.out.printf("Average = %.2f\n", average); //1.5 if (num1 == num2) //2 System.out.println("The 2 numbers are equal"); //0.5 else //1 System.out.println(Math.max(num1, num2) + " is larger"); //2.5 // Students could use?: or if-else to calculate the max } else //1 System.out.println("Invalid Input"); //0.5 } // end main } // end class M105 MTA Makeup Spring 2013/2014 Page 6 of 6