Final Examination Semester 3 / Year 2012 COURSE : INTRODUCTION TO PROGRAMMING COURSE CODE : PROG1013 TIME : 2 1/2 HOURS DEPARTMENT : COMPUTER SCIENCE LECTURER : LIM PEI GEOK Student s ID : Batch No. : Notes to candidates: 1) The question paper consists of 6 pages includes this page. Section A: 10 questions (20 marks) Section B: 4 questions (20 marks) Section C: 3 questions (60 marks) 2) Answer all questions. 3) Write your answer for Section A and Section B in answer booklet. 4) Save your answer for question in Section C and submit your answer in softcopy to Southern College E-Learning System.
SECTION A: MULTIPLE CHOICE QUESTION (10 X 2 = 20 MARKS) Please choose most appropriate answer. State the answer in the answer booklet. 1. Which of the following is INVALID statement to cast a variable to another data type? a. int x = (int) ABC b. int x = (int) 10.5 c. double x = (double)10 d. char x = (char) 65 2. Which of the following expressions explain Math.pow(a,5)? a. 5 a b. a 5 c. a*5 d. 3. Which of the following operator is used to ensure that the two conditions must be true to return a true result? a. && b. c. ^^ d. ^ 4. int sum = 0, x = 0; while(x<10){ if(x > 5 && x < 9) x = x + 1; else x = x + 2; sum += x; System.out.println(sum); Consider the codes above. What is the value of sum variable? a. 10 b. 12 c. 17 d. 18 5. Which of the following is NOT Java keyword? a. class b. double c. scanner d. final 6. What is num after evaluation? int num = 10; num = (true ^ false)? 1: 2; a. 11 b. 1 c. 12 d. 2 1/5
7. A program contains a method with the header: public static double calculatesalary(double amount, String name) Which of the following methods can coexist in the same program with no possible ambiguity? a. public static double calculatesalary(double annualamount, String taxpayerid) b. public static String calculatesalary(double amount, String taxpayername) c. public static double calculatesalary(string name, double amount) d. All of these can coexist without ambiguity. 8. Which of the following is true about methods? a. A program can call one method at most. b. A program can contain a method that calls another method. c. A method can contain one or more other method. d. All of these are true. 9. Each element in a five-element array can hold value(s)? a. one b. five c. at least five d. an unlimited number of 10. Consider the following statement, int[] number = {0,0,0,0; Which of the following expression is VALID? a. number[2] = 10 b. int length = number.length c. number[1] = number[0] + 2 d. All of the above 2/5
SECTION B: SHORT QUESTIONS (20 MARKS) Question 1 (5 Marks) Convert the following for loop into while loop. int x; for(x=0; x<50; x+=5){ System.out.println(x); Question 2 (5 Marks) Convert the multiple if-else statements to switch case. char grade = A ; if(grade == A grade == B ){ System.out.println( Passed ); else if (grade == C ){ System.out.println( Failed ); else{ System.out.println( Invalid input ); Question 3 (5 Marks) There is one error in the following code fragment. Identified the error and correct it. 1 public class Test{ 2 public static void main(string[] args){ 3 final double number = 10; 4 number = 11.5; 5 6 Question 4 (5 Marks) Show the output for the following program. public class DisplayOutput{ public static void main (String[] args) { int x = 10; int y = addvalue(x); System.out.println("x = "+x+"; y = "+y); public static int addvalue(int z){ return (++z); 3/5
SECTION C: CODING QUESTIONS (60 MARKS) Submit your answer in softcopy and upload it to Southern E-Learning System. Question 1 (25 Marks) Ahmad is a carpenter who wants an application to compute the price of any desk a customer orders based on the following: desk length and width in inches, type of wood and number of drawers. The price is compute as follows: The minimum charge for all desks is RM 200. If the surface (length * width) is over 750 square inches, add RM 50. If the wood is mahogany, add RM 150; for oak, add RM 125. No charge is added for pine. For every drawer in the desk, there is an additional RM 30 charge. Write a program to accept data for customer name, length and width of the desk ordered, type of wood, and number of drawers. Your program is then display the all the entered data and the final price for the desk. Sample output: Welcome to Ahmad Furniture Company Please enter your order here! Enter your name: Christine Enter the length of desk (inches): 100 Enter the width of desk (inches): 100 Enter the type of wood (1:Mahogany 2:Osk 3:Pine): 2 Enter the number of drawers: 2 Customer name: Christine Information of Desk: Length: 100.0 Width: 100.0 Type of wood: Osk Number of drawer: 2 Price: RM435.0 Thanks You! Question 2 (18 Marks) Write a java program to ask user to enter 10 numbers. The number should between the ranges 0 to 100. If the user enters a number that out of the range then your program should ask user to enter a number again. All the valid number should store into an array called numberlist. Your program should able to display the number in reverse order. 4/5
Sample output: Enter number 1: 10 Enter number 2: 20 Enter number 3: 300 Enter number 3: 30 Enter number 4: 40 Enter number 5: 50 Enter number 6: 60 Enter number 7: 70 Enter number 8: 80 Enter number 9: 90 Enter number 10: 100 Display the numberlist in reverse order: 100 90 80 70 60 50 40 30 20 10 INTRODUCTION TO PROGRAMMING Question 3(17 Marks) Write a java program that calculates the volume of a sphere. Your program will prompt user to enter radius of the sphere. Write a method that returns the volume of the sphere using the following method header: public static double calculatevolume (double r) The formula for computing the volume is Your program will then display the volume. The value of radius and volume should be format to two decimal digits after the decimal point. Sample output: Enter radius : 5 Radius : 5.00 Volume : 392.70 000 5/5