Mid Term Exam 1 Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Student Name: Total Marks: 50 Obtained Marks: Instructions: Do not open this exam booklet until you are directed to do so. This exam will end in ONE Hour (60 Mins). Write your full name and Student registration No. clearly on the first page. When the exam is started, write your complete Student Registration No. clearly on the top of *EVERY* page. Write your solution in the space provided. If you need more space, write on the back of the sheet containing the problem. If still you need more space then you can use extra sheets. In the case of extra sheet, clearly mention the question number whose answer you are giving and you student registration number on the extra sheet. Plan your time wisely. Do not spend too much time on any one problem. Read through all of them first and attack them in order that allows you to make the most progress. Show your work, as partial credit will be given. You will be graded not only on the correctness of your answer, but also on the clarity and method correctness with which you express your answer. Good luck. 1
Question 1: Fill in the blanks (30) a) What is a Register? (2) A small memory area in the processor b) Can the following conversions involving type castings are allowed? If yes, find the converted results. (4) NOTE: ASCII value of A is 65, B is 66, C is 67, D is 68 and so on. ASCII value of a is 97, b is 98, c is 99, d is 100 and so on. ASCII value of 0 is 48, 1 is 49, 2 is 50 and so on. (1) i = 71 (2) x = 945 (3) y = 475 (4) c = 6 char c = 'G'; int i = (int)c; float f = 945.34f; int x = (int)f; double d = 475.34; int y = (int)d; int i = 54; char c = (char)i; c) How would you write the following arithmetic expression in Java? (2) 4/(3*(r+34)) 9*(a + b*c) + (3 + d * (2 + a))/(a + b * d) d) What is the default data type of the literal value 0.46? (1) double 2
e) What is the default data type of the literal value 46? (1) int f) Identify and fix the errors in the following code: (6) public class Test{ public void main (String []args){ i = 100; double j j = 45.78; k = j + i; System.out.prtin( the value of k is k); Solution: public class Test{ int i = 100; double j; j = 45.78; double k = j + i; System.out.print( the value of k is + k); g) What is the output of the following code: (5) 11 50 111 12 51 System.out.println("1" + 1); System.out.println('1' + 1); System.out.println("1" + 1 + 1); System.out.println("1" + (1 + 1)); System.out.println('1' + 1 + 1); h) Declare a double variable named miles with initial value 100; (1) double miles = 100; 3
i) Declare a double constant named MILES_PER_KILOMETER with value 1.609; (1) final double MILES_PER_KILOMETER = 1.609; j) Consider the following code: public class test{ public static int i = 10; int m = 12 * i++; int j = 34; int n = 23 + --j; What are the values of the following variables after the execution of the above code? (4) i = 11 m = 120 j = 33 n = 56 k) Syntax errors are those errors that are detected by compiler. (1) l) Division by Zero is run time error. (1) m) Logical errors are done by the programmer and are hard to identify. (1) 4
Question 2: Programming (20) a) Write a Java program TicTacToeBoardPrinter that prints a Tic Tac Toe board like (10) public class TicTacToeBoardPrinter{ b) Finding runway length: Given an airplane s acceleration a and take-off speed v, you can compute the minimum runway length needed for an airplane to take off using the following formula: Write a Java program that prompts the user on console to enter v in meters/second (m/s) and the acceleration a in meters/second squared and displays the minimum runway length. (10) import java.util.scanner; public class RunwayLength{ double v, a, length; Scanner input = new Scanner(System.in); System.out.print ( Enter the value of Speed of Airplane in meter per second : ); v = input.nextdouble(); System.out.print ( Enter the value of Acceleration of Airplane in meter per second square : ); a = input.nextdouble(); length = (v * v)/(2 * a); System.out.print( The minimum length of the runway is + length); 5