Вы находитесь на странице: 1из 8

Specimen Exam Question

TCC 121/05 PROGRAMMING FUNDAMENTALS WITH JAVA


Time: 3 Hours

Instructions to candidates:
1. Please check that this examination paper consists of eight (8) pages of printed material before you begin the examination. 2. Write in black or blue and answer all questions in the answer booklet provided. 3. No books, dictionaries, notes or any other written materials are allowed in this examination. 4. Non-programmable electronic calculator may be used.

Copyright 2012 WOU

...2/-

2
Part A

TCC 121/05

Answer the following questions in the answer booklet provided. Every question carries twenty marks. 1.(a) Define the term i Compiler ii Class iii Attributes of real world objects iv Static method (also called a se "static subroutine"). v Pseudocode [10 marks ] Answer [Award 1 mark for each terms definition] i. It takes a source file as input and produces an executable program as output. ii. A class is a set of similar kind of object with same attributes and operations. iii. Objects have identity, state, and behavior. iv. A static method is a method that belongs to the class in which it is defined, rather than to objects created using that class. A static method, when used outside the class where it is defined, is accessed using the name of the class, followed by a period, followed by the name of the method. v. A pseudocode algorithm is written in informal language, closer to English, rather than in the strict syntax of a programming language. It is possible to start with a brief outline of the algorithm, and then refine it step-by-step by expanding and filling in details.

(b) Draw a flowchart that sums up an unknown number of positive integers, and then prints the total. Use an appropriate sentinel value. [5 marks ] Answer 1 mark for the start and end symbol 1 mark for input 2 marks for the condition and consequence 1 mark for output
START

sum = 0

Input value Input

value != -1 Fals Print print sum sum

Tru

sum +=

END

TCC 121/05

(c) If F is a temperature measurement in degrees Fahrenheit, then the same temperature in degrees Celsius would be 5*(F-32)/9. Write a Java method that converts Fahrenheit to Celsius, where the Fahrenheit temperature is a parameter and the Celsius temperature is the return value. [5 marks ] Answer
public static double fahrenheitToCelsius(double degreesFahrenheit){ double degreesCelsius = 5*(degreesFahrenheit - 32)/9; return degreesCelsius; } (The "public static" here is optional, since it was not specified in the problem.) 2 marks 2 marks 1 mark

2. (a) Explain what the term "static" means when used as a modifier on a variable or method declaration in a class. [3 marks] Answer If a variable or method is declared to be static, than that variable or method really belongs to the class as a whole, rather than to any of the objects that might be created from that class [2 mark]. This means, for example, that the variable or method is shared by all such objects and that there is only one copy of the variable or method that exists the whole time a program is running [1 mark]. (b) Write the method header (or signature) for a public class method that takes as parameters an integer and a String and returns a boolean. (Note that you DO NOT need to write any code for the class, only give its header) [3 marks ] Answer Public static boolean method name(int n, String s) [3 marks] (c) What is the effect of giving a class member private access? [3 marks ] Answer When a member of a class is declared private it can be used only in methods that are members of that class [3 marks]. (d) Circle the syntax errors in the following code, and state briefly what is wrong in each case: int ct; ct = 0; while ct < 10 { ct++; if (ct % 2 = 0) System.out.println(ct); }

TCC 121/05 [3 marks ]

Answer There are two errors [1.5 marks each]. The first error is in the third line: The condition in a while loop must be enclosed in parentheses, so it should read: while ( ct < 10 ) { The second error is in the fifth line. The operator for testing equality is "==", not "=", so that it should read: if ( ct % 2 == 0 ) (e) What are the possible values expressions? (int)(2 + 4*Math.random()) [3 marks ] Answer The value of 4*Math.random() is a real number in the range from 0.0 to 4.0, but not including 4.0 exactly [1 mark]. (Numbers such as 3.9 and 3.995 are included as possibilities.) Adding 2 gives a number in the range 2.0 to 6.0, not including 6 [1 mark]. The type case operator (int) throws away the fractional part of the number, leaving an integer value that can be either 2, 3, 4, or 5 [1 mark]. (Note: In the expression (int)(M + N*Math.random()), N is the number of possible values and M is the first possible value.) (f) Examine the following code: i. What does this code print on the monitor? int count = -2 ; while ( count < 3 ) { System.out.print( count + " " ); count = count + 1; } System.out.println( ); [3 marks ] Answer: -2 -1 0 1 2 ii. What is written to the monitor by the following section of code: String strA = new String("Roasted "); String strB = new String("Acorns "); strA = strB; if ( strA == strB ) system.out.println("Two copies of a reference."); else system.out.println("Two different references."); [3 marks ] Answer: Two copies of a reference

5 3.

TCC 121/05

(a) The following program is supposed to write out the integers 1, 2, 3, and 4. Decide what should go in the blanks. class Counter { public static void main (String[] args) int count ; count =________ ; while ( count______ 4 ) { System.out.println( count ); count = count + ____ ; } } } {

[ 3 marks ] Answer: 1, <=, 1 (b) The following program is supposed to write out the integers 10, 11, 12, 13, 14, and 15. Decide what should go in the blanks. class Counter2 { public static void main (String[] args) int j ; j = ____ ; while ( j <_____ ) { System.out.println( j ); j = j + ______ ; } } } Answer: 10, 16, 1 (c) The following program is supposed to write out the values 1!, 2!, 3!, 4!, up to 10! . Fill in the blanks. class Factorial { public static void main (String[] args) { int fact =___ ; // current factorial value int count = ___ ;// loop control while ( count ___ 10 ) { fact = fact *_______ ; System.out.println( count +"! is " + fact ); count = count + 1 ; } } } [ 4 marks ] {

[ 3 marks ]

6 Answer: 1, 1, <=, count (d) Convert the while loop into for loop : int x = 0; while ( x < 500 ){ System.out.println( x ); x = x + 5; }

TCC 121/05

[5 marks ] Answer
for ( int x = 0; x < 500; x+=5 ) System.out.println( x );

(e) Write a function public static double minimum(double array[]) that returns the smallest of all the numbers in the array. You may assume that the array is non-empty. [5 marks ] Answer public static double minimum(double array[]) { double min = array[0]; for (int i = 1; i < array.length; i++) if (array[i] < min) min = array[i]; return min; }
A while loop is equally acceptable.

Mark 0.5 0.5 1.5 1 1 0.5

4. (a) Implement the following class into Java codes. The attributes must have types (int, String and double in this case). You have to write a complete program without main() method and you are not required to initialise the class. You should define the class and declare the data variables and contents for the methods with return statement if required. Given: The three most important numbers describing a telescope are: 1) the diameter of the main lens (the one in front), 2) the focal length of the main lens, and 3) the focal length of the eyepiece. From these values other

TCC 121/05

charactersitics of the telescope such as its magnification and the f-number of the main lens are calculated. The formula to use is: magnification = mainLength/eyeLength The formula to use is: fNumber = mainLength/diameter
Telescope
diameter: double mainLength: double eyeLength: double

getDiameter(double dmeter): void getMainLength(double mLength):void getEyeLength(double eLength):void magnification():double fNumber():double

[10 marks ]
Answer
class Telescope { // Instance Variables double diameter; double mainLength; double eyeLength; // Methods void getDiameter(double dmeter) { diameter = dmeter ; } void getMainLength(double mLength) { mainLength = mLength ; } void getEyeLength(double eLength) { eyeLength = eLength ; } double magnification() { return mainLength / eyeLength ; } double fNumber() { return mainLength/diameter ; } }

Description 1 mark for correct class definition 0.5 mark for each variable with correct data type

Mark 1 1.5

1.5 x 5 =7.5 0.5 mark for each correct method name with the correct parameter 0.5 mark for correct data type or return type. 0.5 mark for the correct operation in the method

TCC 121/05

(b) For this problem, you should write a very simple but complete class. The class represents a counter that counts 0, 1, 2, 3, 4,.... The name of the class should be Counter. It has one private instance variable representing the value of the counter. It has two instance methods: increment() adds one to the counter value, and getValue() returns the current counter value. Write a complete definition for the class, Counter. [10 marks ] Answer: Here is a possible answer. (Note that the initialization of the instance variable, value, to zero is not really necessary, since it would be initialized to zero anyway if no explicite initialization were provided.)
public class Counter { // points[1] // An object of this class represents a counter that // counts up from zero. // current value of the counter private int value = 0; // points[2] public void increment() // points[1] { // add one to the value of the counter value++; // points[1] } public int getValue() // points[2] { // get the current value of the counter return value; // points[1] } } // end of class Counter

points[1] for proper curly braces points[1] bonus for additional works

End of question paper

Вам также может понравиться