{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

2011S2TestAnswers

# 2011S2TestAnswers - CompSci 101 THE UNIVERSITY OF AUCKLAND...

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

CompSci 101 THE UNIVERSITY OF AUCKLAND SECOND SEMESTER, 2011 Campus: City COMPUTER SCIENCE TEST Principles of Programming (Time Allowed: 75 minutes) NOTE: You must answer all questions in this test. No calculators are permitted Answer in the spaces provided in this booklet. There is space at the back for answers that overflow the allotted space. Surname Forenames Student ID Login (UPI) Lab Time Q1 (/10) Q4 (/9) Q7 (/13) Q10 (/7) Q2 (/8) Q5 (/15) Q8 (/10) Total /100 Q3 (/10) Q6 (/6) Q9 (/12)

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

View Full Document
Question/Answer Sheet - Page 2 - CompSci 101 ID: ........................................................... continued Question 1 (10 marks) a) What output is produced when the following code is executed? System.out.println(4 + 3 * (5 / 3) + " * " + (4 - 3) + 1); 7 * 11 (2 marks) b) Complete the output produced when the following code is executed. int a = 4; int b = a; a++; b = a; a = a - 2; b = 10 * a - b; System.out.println("a: " + a + " b: " + b); a: 3 b: 25 (2 marks)
Question/Answer Sheet - Page 3 - CompSci 101 ID: ........................................................... continued c) Complete the output produced when the following code is executed. int a = 15; int b = 7; int c = a % a; int d = b % (b + 2); int e = a % b; int sum = c + d + e; System.out.println("Sum: " + sum); Sum: 8 (2 marks) d) Complete the output produced when the following code is executed. double num1 = 27.6; int num2 = (int) num1; double num3 = 5 / 10; System.out.println("num2: " + num2); System.out.println("num3: " + num3); num2: 27 num3: 0.0 (2 marks)

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

View Full Document
Question/Answer Sheet - Page 4 - CompSci 101 ID: ........................................................... continued e) Complete the output produced when the following code is executed. int a = 3; int b = 12; int c = 6; int d = 1; System.out.println("1: " + (c + 2 * a)); System.out.println("2: " + (d - b / c)); 1: 12 2: -1 (2 marks)
Question/Answer Sheet - Page 5 - CompSci 101 ID: ........................................................... continued Question 2 (8 marks) Parts a), b) and c) refer to the following code. int value = ... if ((value < 10 || value > 30) && !(value == 0)) { System.out.println("A"); } else if (value > 15 && value < 25) { System.out.println("B"); } else if (value < 16) { System.out.println("C"); } else if (value > 25) { System.out.println("D"); } else { System.out.println("E"); } a) What is the output if the variable, value , is assigned 14? C (2 marks) b) What is the output if the variable, value , is assigned 0? C (2 marks) c) What is the output if the variable, value , is assigned 25? E (2 marks)

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

View Full Document
Question/Answer Sheet - Page 6 - CompSci 101 ID: ........................................................... continued d) Write a boolean expression which tests whether the int variable, score , contains a value between 10 and 20 (both inclusive). score >= 10 && score <= 20 (2 marks)
Question/Answer Sheet - Page 7 - CompSci 101 ID: ........................................................... continued Question 3 (10 marks) a) The program below contains the display() method. In the start() method make a call to the display() method. Your call to the display() method can produce any output but it must compile. public class CallDisplay () { public void start() { display("Miss", 'A', false); (2 marks) } private void display(String title, char initial, boolean inShortForm) { int pos = title.length() - 1; if (inShortForm) { System.out.println(""+ title.charAt(0) + title.charAt(pos) + ". "+ initial + ""); } else { System.out.println(title + initial + ""); } } } b) Give two different numbers which could be output by the following code: int number = (int)(Math.random() * 2) - 2; System.out.print(number); -2, -1 (2 marks)

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.

{[ snackBarMessage ]}

### Page1 / 26

2011S2TestAnswers - CompSci 101 THE UNIVERSITY OF AUCKLAND...

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

View Full Document
Ask a homework question - tutors are online