You are on page 1of 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/-

and behavior. followed by a period. closer to English. 1. v Pseudocode [10 marks ] Answer [Award 1 mark for each term’s definition] i. A static method is a method that belongs to the class in which it is defined. A pseudocode algorithm is written in informal language.(a) Define the term i Compiler ii Class iii Attributes of real world objects iv Static method (also called a se "static subroutine"). is accessed using the name of the class. A static method. (b) Draw a flowchart that sums up an unknown number of positive integers. Objects have identity. followed by the name of the method. v. [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 . ii. iii. rather than to objects created using that class. Every question carries twenty marks. and then prints the total. and then refine it step-by-step by expanding and filling in details.2 Part A TCC 121/05 Answer the following questions in the answer booklet provided. Use an appropriate sentinel value. It takes a source file as input and produces an executable program as output. rather than in the strict syntax of a programming language. when used outside the class where it is defined. state. iv. It is possible to start with a brief outline of the algorithm. A class is a set of similar kind of object with same attributes and operations.

rather than to any of the objects that might be created from that class [2 mark]. } . if (ct % 2 = 0) System. return degreesCelsius. while ct < 10 { ct++. since it was not specified in the problem. (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. } (The "public static" here is optional. and state briefly what is wrong in each case: int ct. [5 marks ] Answer public static double fahrenheitToCelsius(double degreesFahrenheit){ double degreesCelsius = 5*(degreesFahrenheit . 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].out. (Note that you DO NOT need to write any code for the class. then the same temperature in degrees Celsius would be 5*(F-32)/9.32)/9. 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].println(ct). (a) Explain what the term "static" means when used as a modifier on a variable or method declaration in a class. (d) Circle the syntax errors in the following code.) 2 marks 2 marks 1 mark 2. where the Fahrenheit temperature is a parameter and the Celsius temperature is the return value. only give its header) [3 marks ] Answer Public static boolean method name(int n. ct = 0.3 TCC 121/05 (c) If F is a temperature measurement in degrees Fahrenheit. This means. [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. Write a Java method that converts Fahrenheit to Celsius.

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

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

} TCC 121/05 [5 marks ] Answer for ( int x = 0. while ( x < 500 ){ System. for (int i = 1. Mark 0. 2) the focal length of the main lens.println( x ). i < array. (a) Implement the following class into Java codes.5 0. You should define the class and declare the data variables and contents for the methods with return statement if required. count (d) Convert the while loop into for loop : int x = 0.5 1. String and double in this case). return min. 1. You may assume that the array is non-empty.5 1 1 0. x+=5 ) System.length. You have to write a complete program without main() method and you are not required to initialise the class. Given: The three most important numbers describing a telescope are: 1) the diameter of the main lens (the one in front). } A while loop is equally acceptable. and 3) the focal length of the eyepiece. [5 marks ] Answer public static double minimum(double array[]) { double min = array[0]. (e) Write a function public static double minimum(double array[]) that returns the smallest of all the numbers in the array. x < 500.out. i++) if (array[i] < min) min = array[i].6 Answer: 1. <=. x = x + 5.5 4. The attributes must have types (int. From these values other .out.println( x ).

double mainLength.5 0. double eyeLength.5 mark for each correct method name with the correct parameter 0. } } Description 1 mark for correct class definition 0.5 1.5 mark for the correct operation in the method .5 x 5 =7.5 mark for correct data type or return type. } double magnification() { return mainLength / eyeLength . } void getMainLength(double mLength) { mainLength = mLength .7 TCC 121/05 charactersitics of the telescope such as its magnification and the f-number of the main lens are calculated. 0. 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. } void getEyeLength(double eLength) { eyeLength = eLength . // Methods void getDiameter(double dmeter) { diameter = dmeter .5 mark for each variable with correct data type Mark 1 1. } double fNumber() { return mainLength/diameter .

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