Page 1 of 6 Template no.: A Course Name: Computer Programming1 Course ID: Exam Duration: 2 Hours Exam Time: Exam Date: Final Exam 1'st Semester Student no. in the list: Exam pages: Student's Name: Student ID: Group ID: 101 Lecturer name: Mahmoud R. Alfarra Final marks Marks First Question Q1 Q2 Q3 Q Q Total 1 * 7 1 * 20 * 0. * 2 17 0 No. of Branches (1) (7/0) (1 min.) (7/7) Identify and correct the errors in each of the following segments of code (note the type of errors): 1 2 3 6 The following segment should return the result of factorial x. public long fact (long x){ if (x <= 1) return fact(x) else return x * fact(x-1); } The following segment should sum the numbers from 0 to 100. while ( x <= 100 ) total += x; ++x; The following code should print the odd integers from 19 to 1: for ( i = 19; i >= 1; i += 2 ) System.out.println( i ); int gender; if ( gender == 1 ) System.out.println( "Woman" ); else; System.out.println( "Man" ); int sum( int x, int y ) { int result; result = x + y;} int y = 9; if (y<=0); system.out.println("y is less"+ than zero"); else System.out.println("fail");
Page 2 of 6 Assume int b[] = new int [ 10 ]; 7 for ( int i = 0; i <= b.length; i++ ) b[ i ] = 1; Second Question A No. of Branches (1) (2 min.) Convert the following English sentences into a Java sentences to accomplish each of the following tasks: Write the header of Method smallest, which takes three integers x, y and z and returns an integer. (/0) (/) B Using Math class, compare the double x and double y to determine the maximum one of them. C Write two overloaded methods to return the summation of integer x with integer y and float x with float y. D Declare a method ArraySum which takes the array called numbers and returns its summation. And then write the calling statements. E Declare a String array which has 30 cells and then using loop to give the value "Ali" as the initialization of the array's cells. Third Question No. of Branches (2) (10/0) (10 min.) Branch One (/10) State whether each of the following is true [T] or false [F]. If false, explain why or correct? 1 The scope of a parameter declaration is the Header of the method in which the declaration appears.
Page 3 of 6 2 Arrays are data structures consisting of related data items many types. 3 Arrays are allocated with new because arrays are static. Using a value of type long as an array index results in a logical error. The while loop is considered a Post test loop Third Question No. of Branches (2) (1 min.) Branch Two Choose the most correct answer for the following statements: (10/0) (/10) 1 Iteration and recursion each involve a. a Integer counter b termination test c selection d ( b ) + ( c ) 2 The result of Math.floor(-3.7) is. a - 3.0 b - 3 c -.0 d - 3 Motivation of modularizing in java programming is avoid a reusability b Divide & conquer c repeating code d ( a ) + ( b ) A method which performs a task that does not depend on the contents of any object is called.. a Recursive b static c class d ( b ) + ( c ) Counter-controlled repetition requires: a Control variable b initial value c continuation condition d ( a ) + ( b ) + ( c ) 6 A program can test multiple floating cases by placing.. statements a if...else b switch c if d ( a ) + ( b ) 7 Set a semi colon after the condition results a a syntax error b logic error c compilation error d ( a ) + ( d ) 8 Java provides statements and.. to alter the flow of control. a break b continue c switch d ( a ) + ( b ) 9 Comparing strings is doing by using.. method. a ComparTo b == c Ceil d Math 10 A set of statements contained within a pair of braces is called a a segment b loop c block d Class
Page of 6 Fourth Question (10/0) (1 min.) (10/10) Trace the following java's code segments and print the expected results int even [ ]= new int[10]; for (in i = 0 ; i<=7; i++ ) {if(i%2==0) even [i]=i*2; 1 else even[i]= (i-1)*2; System.out.println( even[i]+"\n" );} System.out.println( "Finished" ); counter = 2; do { 2 System.out.println( counter ); counter += 2 ; } While ( counter < 100 ) Suppose x = Start No. of Branches (1) Read X Counter = 1 No Counter <=12 Yes Print (counter *X) Counter = counter +1
Page of 6 Fifth Question No. of Branches () (17/0) (3 min.) (17/17) Answer all the following questions: A Using recursion method concepts, Write a method to calculate the Fibonacci series and then use it to calculate the Fibonacci of with drawing. 3 marks 7 min [Hint]: Fibonacci (n) = Fibonacci (n-1)+ Fibonacci (n-2) Consider a two-by-three integer array t. 1. Write a statement that declares and creates t. 2. Write the names of all the elements in the second row of t. B 2 marks 8 min 3. Write the names of all the elements in the third column of t.. Write a single statement that sets the element of t in row 1 and column 2 to zero.
Page 6 of 6 C marks 20 min (Duplicate Elimination) Use a one-dimensional array to solve the following problem: Write an application that inputs five numbers, each of which is between 10 and 100, inclusive. As each number is read, display it only if it is not a duplicate of a number already read. Provide for the "worst case," in which all five numbers are different. Use the smallest possible array to solve this problem. Display the complete set of unique values input after the user inputs each new value. With My Best Wishes Mahmoud Rafeek Alfarra