2000FCExamQuestions

2000FCExamQuestions - 415.101FC THE UNIVERSITY OF AUCKLAND...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
415.101FC THE UNIVERSITY OF AUCKLAND ______________________________________________ EXAMINATION FOR BSc ETC 2000 ______________________________________________ COMPUTER SCIENCE Principles of Programming (Time allowed: TWO hours) NOTE: Attempt ALL questions. Write your answers in the answer book provided at the end of the exam paper. You may detach the answer book from the questions. Marks for each question are shown and total 100 . Page 9 of this book includes a number of useful methods and instance variables. There are blank pages at the back of the book to use, if required, for any rough working. Program style will be taken into account in marking; you should follow the style guidelines taught throughout the course. You will get partial marks if you express parts of a program in precise pseudocode rather than Java. Unless otherwise stated your programs do NOT need to deal with invalid inputs. When you are asked to write code you do NOT have to include Javadoc comments.
Background image of page 1

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

View Full DocumentRight Arrow Icon
- 2 - 415.101FC 1. Fixing logic errors in a program. (5 marks) This program is supposed to compute the sum of the first five integers – that is 1+2+3+4+5=15. The program compiles and runs without error, however it produces the wrong answer. Instead of printing: The sum of the numbers from 1 to 5 is: 15 it prints: The sum of the numbers from 1 to 5 is: 11 You have to fix the program so that it correctly calculates the sum of the first 5 integers and prints the correct result. There are two errors in the program, and they are both in the sum() method. Circle the errors in the answer book and write the correct line underneath the mistake. public class Q1 { public static void main(String[] args) { System.out.print("The sum of the numbers from 1 to 5 is: "); System.out.println(sum(1, 5)); } public static int sum(int start, int end) { int result, i; i = start; result = start; while (i < end) { result = result + i; i++; } return result; } } 2. Method call output. (10 marks) What is the output of this program? (As this is an Applet the print statements would be seen in the Java console window.) The program compiles and runs without error. import java.awt.*;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

2000FCExamQuestions - 415.101FC THE UNIVERSITY OF AUCKLAND...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online