FINAL EXAMINATION
SESSION II, ACADEMIC YEAR 2008/2009
TCP 2023
FUNDAMENTALS OF PROGRAMMING
INSTRUCTION TO CANDIDATES:
CONFIDENTIAL
TCP 2023 FEBRUARY 2009
SECTION A
Marks: 30
A. object code.
B. source code.
C. a byte-code file.
D. an executable file.
A. int appleCount;
B. System.exit(0);
C. “Enter number of apples:”
D. totalFruitCount = appleCount + orangeCount;
3. Consider the following code that will assign a letter grade of ‘A’, ‘B’, ‘C’, ‘D’, or
‘F’ depending on a student’s test score.
2 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
int x, y;
if (x > 5)
y = 1;
else if (x < 5)
{
if (x < 3)
y = 2;
else
y = 3;
}
else
y = 4;
Figure 1.
A. 1.
B. 2.
C. 3.
D. 4.
A. 1.
B. 2.
C. 3.
D. 4.
3 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
Figure 2.
4 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
Figure 3.
A. int.
B. void.
C. public.
D. Nothing is returned.
A. x.
B. y.
C. minimum.
D. smaller.
A. minimum(5, 4);
B. minimum(int 5, int 4);
C. minimum(int x, int y);
D. public static int minimum(5, 4);
5 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
12. The parameter list in the method header and the arguments in the method call
must agree in:
A. type.
B. order.
C. number.
D. all of the above.
15. Which modifier is used to specify that a method cannot be used outside a
class?
A. public.
B. static.
C. private.
D. abstract.
6 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
A. int hits;
B. int[] hits;
C. new int hits[];
D. int hits = int[];
17. Which of the following creates an array of 25 components of the type int?
A. Only (i).
B. Only (ii).
C. None of these.
D. Both (i) and (ii).
18. Which of the following initializer lists would correctly set the elements of array
n?
A. int n[] = { 1, 2, 3, 4, 5 };
B. int n[ 5 ] = { 1; 2; 3; 4; 5 );
C. int n = new int( 1, 2, 3, 4, 5 );
D. array n[ int ] = { 1, 2, 3, 4, 5 };
7 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
class test {
public static void main(String[] args) {
int[] list1 = {3, 2, 1};
int[] list2 = {1, 2, 3};
list2 = list1;
list1[0] = 0; list1[1] = 1; list2[2] = 2;
Figure 4.
A. 0 1 2.
B. 1 2 3.
C. 2 1 0.
D. 3 2 1.
A. local variable.
B. imported class.
C. instance variable.
D. public class declaration.
A. letter n, letter p.
B. letter p, letter n.
C. minus sign (-), plus sign (+).
D. plus sign (+), minus sign (-).
8 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
23. Assume getAge is a method of the Person class and Katie is a Person
object. Which of the following represents a valid method call?
A. katie.getAge();
B. katie.getAge(Person);
C. katie = Person.getAge();
D. Person = katie.getAge();
9 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
Figure 6.
27. Which class of the following list of classes provides platform independent
methods that enable a program to determine whether a String value corresponds to
the name of a file in an indicated directory?
A. File.
B. Pipe.
C. Buffer.
D. Stream.
28. What will the readLine() method of the BufferedReader class return when
attempting to read the end-of-file character of a text file?
10 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
A. 0.
B. -1.
C. null.
D. \eof.
29. What happens when the readInt() method of the DataInputStream class
attempts to read past the end of a binary file?
30. Which class of the following list of classes provides methods which are most
appropriate for writing text to a text file?
A. Writer.
B. FileWriter.
C. OutputStream.
D. DataOutputStream.
SECTION B
Marks: 40
11 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
Question 1
Question 2
System.out.println(i * j);
}
System.out.println(i);
}
1
2
1
2
12 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
2
3
(6 marks)
Question 3
Question 4
13 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
6
7 public static int xMethod(int n)
8 if (n > 0) return 1;
9 else if (n == 0) return 0;
10 else if (n < 0) return -1;
11 }
12 }
Line 2: method1 is not defined correctly. It does not have a return type or void.
Line 2: type int should be declared for parameter m.
Line 8: parameter type for n should be double to match xMethod(3.4).
Line 11: if (n<0) should be removed in xMethod, otherwise the a compilation
error is reported.
(4 marks)
Question 5
Question 6
14 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
The memory diagram below shows the variables and objects just before the
statement in Line 5 executes. Draw the memory diagram of variables and objects
after the statement in Line 8 executes.
(6 marks)
15 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
Question 7
Complete the following tasks, assuming that each applies to the same program:
16 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
Question 1
Write a test program that invokes these methods to display the following tables:
17 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
System.out.println(“--------------------------------------------
-”); →
1
double celsius = 40; double fahrenheit = 120; →
1
for (int i = 1; i <= 10; celsius--, fahrenheit -= 10, i++) { → 1
System.out.println(celsius + “\t\t” +
celsiusToFahrenheit(celsius) + “\t|\t” + fahrenheit + “\t\t” +
fahrenheitToCelsius(fahrenheit)); →2
}
}
Question 2
Write a program that reads ten integers into an array, shuffles the array, and display
the number from the array.
(10 marks)
import javax.swing.JOptionPane; →
1
18 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
num[i] = Integer.parseInt(dataString); →
1
}
System.exit(0); →
1
}
}
Question 3
// FileDemonstration.java
// Demonstrating the File class.
19 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
import java.io.File; →
1
20 CONFIDENTIAL
TCP 2023 FEBRUARY 2009
} // end outer if
else // not file or directory, output error message →
1
{
System.out.printf( “%s %s”, path, “does not exist.” );
} // end else
} // end method analyzePath
} // end class FileDemonstration
(10 marks)
21 CONFIDENTIAL