Vous êtes sur la page 1sur 21

MARKING SCHEME

DIPLOMA IN COMPUTER SCIENCE

FINAL EXAMINATION
SESSION II, ACADEMIC YEAR 2008/2009

TCP 2023
FUNDAMENTALS OF PROGRAMMING

INSTRUCTION TO CANDIDATES:

1. The time allotted for this paper is 3 HOURS.


2. This question paper consists of THREE sections.
SECTION A: 30 multiple choice questions
SECTION B: 6 structured questions
SECTION C: 3 program questions
3. Answer ALL questions on the Answer Sheet Provided.
4. This examination pack consists of:
i) The Question Paper
ii) An Answer Booklet
iii) An Objective Answer Sheet

THIS QUESTIONS PAPER CONSISTS OF 17 PRINTED PAGES.


DO NOT OPEN UNTIL YOU ARE TOLD TO DO SO.

CONFIDENTIAL
TCP 2023 FEBRUARY 2009

SECTION A
Marks: 30

Answer ALL questions in the objective answer sheet provided.

1. Input to a compiler is called ______________________.

A. object code.
B. source code.
C. a byte-code file.
D. an executable file.

2. Which of the following is a variable declaration?

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.

if (score >= 90) grade = ‘A’;


if (score >= 80) grade = ‘B’;
if (score >= 70) grade = ‘C’;
if (score >= 60) grade = ‘D’;
else grade = ‘F’;

A. This code will work correctly in all cases.


B. This code will work correctly only if grade < 60.
C. This code will work correctly only if grade < 70.
D. This code will work correctly only if grade >= 60.

2 CONFIDENTIAL
TCP 2023 FEBRUARY 2009

Answer Questions 4 and 5 based on the Figure 1 below.

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.

4. What is the value of y if x = 5?

A. 1.
B. 2.
C. 3.
D. 4.

5. Based on the code above, what is the value of y if x = 6?

A. 1.
B. 2.
C. 3.
D. 4.

3 CONFIDENTIAL
TCP 2023 FEBRUARY 2009

6. Consider the program segment in Figure 2.

for (int i = 1; i <= 10; i++)


for (int j = 1; j <= 10; j++)
System.out.println(“1”);

Figure 2.

How many lines of output will be printed?


A. 1.
B. 10.
C. 20.
D. 100.

7. Which of the following is not an advantage of using methods?

A. Using methods makes program run faster.


B. Using methods makes reusing code easier.
C. Using methods makes programs easier to read.
D. Using methods hides detailed implementation from the clients.

8. Which of the following is the syntax for a method declaration?

A. <return type> <modifiers> <method name> ( <parameters>) {


<method body>
}
B. <modifiers> <method name> ( <parameters> ) {
<method body>
}
C. <return type> <method name> ( <parameters> ) {
<method body>
}
D. <modifiers> <return type> <method name> ( <parameters> ) {
<method body>
}

4 CONFIDENTIAL
TCP 2023 FEBRUARY 2009

Answer Questions 9 - 11 based on the Figure 3 below.

public static int minimum(int x, int y)


{
int (x < y)
smaller = x;
else
smaller = y;
return smaller;
}

Figure 3.

9. What is the return type of the method?

A. int.
B. void.
C. public.
D. Nothing is returned.

10. What is the name of the method?

A. x.
B. y.
C. minimum.
D. smaller.

11. Which of the following is a valid call to the method?

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.

13. Which of the following is not true about return statement?

A. A method can have more than one return statement.


B. return statements can be used in void methods to return values.
C. A value-returning method returns its value via the return statement.
D. Whenever a return statement executes in a method, the remaining
statements are skipped and the method exits.

14. Information is passed to a method in

A. the method body.


B. the method name.
C. that method’s return.
D. the arguments to the method.

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

16. Which of the following declares an array of int named hits?

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?

(i) int[] alpha = new [25];


(ii) int[] alpha = new int[25];

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 };

19. In the following Figure 4, what is the output for list2?

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;

for (int i = list2.length – 1; i >= 0; i--)


System.out.print(list2[i] + “ ”);

Figure 4.

A. 0 1 2.
B. 1 2 3.
C. 2 1 0.
D. 3 2 1.

20. Every Java application is composed of at least one

A. local variable.
B. imported class.
C. instance variable.
D. public class declaration.

21. In the UML, public visibility is indicated by placing a ____________ before an


operation or an attribute, whereas a ____________ indicates private visibility.

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

22. Assume a program uses a Class, FurnitureStore, which contains a method


called “getSize” that is used as follows:

FurnitureStore fs = new FurnitureStore(30, “LazyBoy”);


int size = fs.getSize();

Which of the following is the prototype for the getSize method?

A. public int getSize();


B. public int getSize(int);
C. public void getSize(int);
D. public static void getSize();

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();

24. Relative paths normally start from which directory?

A. The root directory.


B. The directory in which the Java interpreter is installed.
C. The directory in which the application began executing.
D. None of the above.

25. Which of the following statements is not equivalent to Figure 6

9 CONFIDENTIAL
TCP 2023 FEBRUARY 2009

File name = new File( “c:\\books\\2004\\files.txt” );

Figure 6.

Assume we are currently in the directory c:\books.

A. File name = new File( “files.txt” );


B. File name = new File( “2004”, “files.txt” );
C. File name = new File( “c:\\books\\2004”, “files.txt” );
D. All of the above are equivalent to the statement in the question.

26. Which of the following is not an application of a File object?

A. Open or edit a file.


B. Determine if a file exists.
C. Determine whether a file is writable.
D. Determine whether a file is readable.

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?

A. The method returns 0.


B. The method returns -1.
C. The method returns null.
D. An EOFException is thrown.

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

Answer ALL questions in the answer sheet provided.

Question 1

Write Java statements that accomplish each of the following tasks:

a. Display the message “Enter an integer: ”, leaving the cursor on the


same line.
System.out.print( “Enter an integer: ” );
b. Assign the product of variables b and c to variable a.
a = b * c;
c. State that a program performs a sample payroll calculation (i.e., use text that
helps to document a program).
// This program performs a simple payroll calculation.
(3 marks)

Question 2

Show the output produced by the following code segment.

for (int i = 1; i < 4; i++) {


for (int j = 1; j < 4; j++)
if (i * j > 2)
break;

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

Write method headers for the following methods:


a. Computing a sales commission, given the sales amount and the commission
rate.
public static double getCommission(double salesAmount, double
commissionRate)
b. Printing the calendar for a month, given the month and year.
public static void printCalendar(int month, int year)
c. Computing a square root.
public static double sqrt(double value)
d. Testing whether a number is even, and returning true if it is.
public static boolean isEven(int value)
e. Printing a message a specified number of times.
public static void printMessage(String message, int times)
f. Computing the monthly payment, given the loan amount, number of years,
and annual interest rate.
public static double monthlyPayment(double loan, int
numberOfYears, double annualInterestRate)
g. Finding the corresponding uppercase letter, given a lowercase letter.
public static char getUpperCase(char letter)
(7 marks)

Question 4

Identify and correct the errors in the following program:


1 public class Test {
2 public static method1(int n, m) {
3 n += m;
4 xMethod(3.4);
5 }

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

State whether the following array declarations are valid or invalid.

a. int i = new int(30);


Invalid
b. double d[] = new double[30];
Valid
c. char[] r = new char(1..30);
Invalid
d. int i[] = (3, 4, 3, 2);
Invalid
e. float f[] = {2.3, 4.5, 6.6};
Valid
f. char[] c = new char();
Invalid
(6 marks)

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.

public class Question6


{
public static void main(String[] args)
{
int num1; // Line 1
IntClass num2 = new IntClass(); // Line 2
char ch; // Line 3
StringBuffer str; // Line 4
num1 = 10; // Line 5
num2.setNum(15); // Line 6
ch = ‘A’; // Line 7
str = new StringBuffer(“Sunny”); // Line 8
}
}

(6 marks)

15 CONFIDENTIAL
TCP 2023 FEBRUARY 2009

Question 7

Complete the following tasks, assuming that each applies to the same program:

a. Write a statement that opens file “oldmast.txt” for input-use Scanner


variable inOldMaster.
Scanner inOldMaster = new Scanner( new File
( “oldmast.txt” ) );
b. Write a statement that opens file “trans.txt” for input-use Scanner variable
inTransaction.
Scanner inTransaction = new Scanner( new
File( “trans.txt” ) );
c. Write a statement that opens file “newmast.txt” for output (and creation)-
use formatter variable outNewMaster.
Formatter outNewMaster = new Formatter( “newmast.txt” );
d. Write the statements needed to read a record from the file “oldmast.txt”.
The data read should be used to create an object of class AccountRecord-
use Scanner variable inOldMaster.
AccountRecord account = new AccountRecord();
account.setAccount( inOldMaster.nextInt() );
account.setFirstName( inOldMaster.next() );
account.setLastName( inOldMaster.next() );
account.setBalance( inOldMaster.next() );
(8 marks)
SECTION C
Marks: 30

16 CONFIDENTIAL
TCP 2023 FEBRUARY 2009

Question 1

Write a class that contains the following two methods:

/** Converts from Celsius to Fahrenheit */


public static double celsiusToFahrenheit(double celsius)

/** Converts from Fahrenheit to Celsius */


public static double fahrenheitToCelsius(double Fahrenheit)

The formula for conversion is:

fahrenheit = (9.0 / 5) * celsius + 32

Write a test program that invokes these methods to display the following tables:

Celsius Fahrenheit Fahrenheit Celsius

40.0 104.0 120.0 48.89


39.0 102.2 110.0 43.33
...
32.0 89.6 40.0 4.44
31.0 87.8 30.0 -1.11
(10 marks)

public class Question1 { →


1
public static void main(String[] args) { →
1
System.out.println(“Celsius\t\tFahrenheit\t|\tFahrenheit\tCelsiu
s”);
→1

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
}
}

public static double celsiusToFahrenheit(double celsius) {


return (9.0 / 5.0) * celsius + 32; →
1
}

public static double fahrenheitToCelsius(double fahrenheit) {


return (5.0 / 9) * (fahrenheit – 32); →
1
}
}

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

public class Question2 { →


1

18 CONFIDENTIAL
TCP 2023 FEBRUARY 2009

public static void main (String[] args) { →


1
int[] num = new int[10]; →
1

for (int i = 0; i < 10; i++) →


1
// Read a number
String dataString = JOptionPane.showInputDialog(null, “Read a
number:”, “Question2”, JOptionPane.QUESTION_MESSAGE); →
1

num[i] = Integer.parseInt(dataString); →
1
}

// Display the array


for (int i = 9; i >= 0; i--) { →
1
System.out.println(num[i]); →
1
}

System.exit(0); →
1
}
}

Question 3

Complete the following program by writing the missing statements,

// FileDemonstration.java
// Demonstrating the File class.

19 CONFIDENTIAL
TCP 2023 FEBRUARY 2009

import java.io.File; →
1

public class FileDemonstration →


1
{
// display information about file user specifies
public void analyzePath( String path ) →
1
{
// create File object based on user input
File name = new File( path ); →
1

if ( name.exists() ) // if name exists, output information


about it →
1
{
// display file (or directory) information
System.out.printf( “%s%s\n%s\n%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n
%s%s”, name.getName(), “exists”, ( name.isFile() ? “is a file” :
“is not a file” ), ( name.isDirectory() ? “is a directory” : “is
not a directory” ), ( name.isAbsolute() ? “is absolute path” : “is
not absolute path” ), “Last modified: ”, namelastModified(),
“Length: ”, name.length(), “Path: ”, name.getPath(), “Absolute
path: ”, namegetAbsolutePath(), “Parent: ”, name.getParent() );
if ( name.isDirectory() ) // output directory listing → 1
{
String directory[] = name.list(); →
1
System.out.println( “\n\nDirectory contents:\n” );
→1
for ( String directoryName : directory )
System.out.printf( “%s\n”, directoryName ); →1
} // end else

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