# exam04s1 - CS1101 NATIONAL UNIVERSITY OF SINGAPORE SCHOOL...

This preview shows pages 1–4. Sign up to view the full content.

CS1101 NATIONAL UNIVERSITY OF SINGAPORE SCHOOL OF COMPUTING EXAMINATION FOR Semester 1: 2004/5 CS1101 – PROGRAMMING METHODOLOGY November 2004 Time allowed: 2 hours INSTRUCTIONS TO CANDIDATES 1. This examination paper consists of FIVE (5) questions and comprises TEN (10) printed pages. 2. This is an OPEN BOOK examination. 3. Answer all questions. You may use pencil to write your codes. Pen is preferred for other questions. 4. Fill in your Matriculation Number on the cover page of the Answer Book. 5. Do NOT fill in the question numbers on the cover page of the Answer Book. 6. Do not tie your Answer Book if you use only one Answer Book. 7. If you use more than one Answer Book, tie them together with the strings provided. 8. Do not submit any unused Answer Book.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS1101 1. Dice [15 marks] The following program Dice.java simulates 6000 rolls of a die to show that each face of the die shows up with approximately equal probability. That is, the output will show that each face (1 through 6) of the die appears approximately 1000 times. The program uses an integer array freq to store the frequency of each face of a die. Seven elements are declared for the array, but we use only freq[1] to freq[6] for the six faces and leave freq[0] unused. The program contains errors (syntax errors, logic errors and omissions). Study the program and identify all the errors. You should indicate the line number where the error occurs and provide the corrected version. For omission, you should indicate what the omission is and where it should appear. 1: public class dice { 2: 3: int [] freq; 4: 5: public Dice() { 6: freq = new int[7]; // freq[0] is not used 7: } 8: 9: public void getFreq( int face) { 10: return freq[face]; 11: } 12: 13: public void setFreq( int face, int value) { 14: freq[face] = value; 15: } 16: 17: public static void main(String[] args) { 18: Dice die = new Dice(); 19: initFreq(); 20: printFreq(); 21: } 22: 23: public void initFreq() { 24: Random num = new Random(); 25: int face = 0; 26: for ( int roll = 1; roll == 6000; roll++) { 27: int face = 1 + num.nextInt() * 6; 28: setFreq(face, getFreq(face)); 29: } 30: } 31: 32: public void printFreq() { 33: for ( int i = 1; i <= 6; ++i) { 34: System.out.println ("Face " + i + 35: " has frequency " + getFreq(face)); 36: } 37: } 38: } - 2 of 10 -
CS1101 2. Output [15 marks] Study the following classes carefully, and give the output of class useS .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/19/2012 for the course SOC 1101 taught by Professor A.tan during the Fall '08 term at National University of Singapore.

### Page1 / 10

exam04s1 - CS1101 NATIONAL UNIVERSITY OF SINGAPORE SCHOOL...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online