SECOND SEMESTER, 2007
Campus: City
COMPUTER SCIENCE TEST
Principles of Programming
(Time allowed: 75 MINUTES)
NOTE: AttemptALL questions
Write your answers in the space provided
There is space at the back for answers that overflow the allotted space No calculators are permitted
Surname:
Forenames:
Student ID number:
Login name:
CompSci 101 Test Results
Question Marks Out of
Question 1 10
Question 2 5
Question 3 5
Question 4 10
Question 5 20
Question 6 10
TOTAL 60
What is displayed by each of the following pieces of Java program?
a) System.out.println("Hello world");
(1 mark) and 9 more like this.
Question 2 (5 marks)
Add the return type to the following Java methods:
a)
private same(double d) { return d;
}
(1 mark) and 4 more like this.
Question 3 (5 marks)
a) Write a Javaboolean expression which tests if the value of theint variable, apple, is not zero.
(1 mark) and 4 more like this.
Question 4 (10 marks)
(These all have quite-short answers.)
Complete each of the methods below as specified in the comment preceding each method.
a)
// method to find the longer of two Strings private String longer(String s1, String s2){
}
(2 marks)
Question 5 (20 marks)
(This question has longer answers but all of the methods are very similar to those that you have seen in assignments and lectures.)
a. Complete the methodisAllVowels() which accepts aString parameters and returns the boolean valuetrue if everychar ins is a vowel,false otherwise. You may make use of the methodisVowel() that returnstrue orfalse depending on whether itschar parameter is a vowel.
// method to find whether a String s is all vowels private boolean isAllVowels( String s) {
}
(5 marks) and 3 more like this.
Question 6 (10 marks)
a) What is the output when the following program is executed? (This one involves understanding for- loops and Strings.)
// mystery program public void start() {
String s = "abcde";
for (int i=0; i<s.length(); i++){
System.out.println(s.substring(0,i+1));
} } }
Show the output here:
(5 marks) b) What is the output when the following program is executed? (This one involves for-loops, arrays, and methods with array parameters.)
// mystery program Show the output here:
(5 marks)