2011S2ExamAnswers

2011S2ExamAnswers - CompSci 101 CONTINUED THE UNIVERSITY OF...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CompSci 101 CONTINUED THE UNIVERSITY OF AUCKLAND SECOND SEMESTER, 2011 Campus: City COMPUTER SCIENCE Principles of Programming (Time Allowed: TWO hours) 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) Q1 (/10) Q4 (/4) Q7 (/8) Q10 (/10) Q2 (/6) Q5 (/15) Q8 (/7) Q11 (/10) Q3 (/6) Q6 (/15) Q9 (/9) Total /100 Question/Answer Sheet - 2 - CompSci 101 ID: ........................................................... CONTINUED Question 1 (10 marks) a) Complete the output produced when the following code is executed. String a = "3"; String b = "2"; int y = 5; int z = 4; System.out.print("1. "); System.out.println(a + b + y + z + (y + z)); 1. 32549 (2 marks) b) Complete the output produced when the following code is executed. int a = 2; int b = a; a++; b = a + b; a = b + a; b = a + b; System.out.println("a: "+ a + " b: "+ b); a: 8 b: 13 (2 marks) Question/Answer Sheet - 2 - CompSci 101 ID: ........................................................... CONTINUED Question 1 (10 marks) a) Complete the output produced when the following code is executed. String a = "3"; String b = "2"; int y = 5; int z = 4; System.out.print("1. "); System.out.println(a + b + y + z + (y + z)); 1. 32549 (2 marks) b) Complete the output produced when the following code is executed. int a = 2; int b = a; a++; b = a + b; a = b + a; b = a + b; System.out.println("a: "+ a + " b: "+ b); a: 8 b: 13 (2 marks) Question/Answer Sheet - 3 - CompSci 101 ID: ........................................................... CONTINUED c) Complete the statement below which assigns a random number between 1 and 12 inclusive to the variable, monthNumber . int monthNumber = (int)(Math.random() * 12 + 1); (2 marks) d) The testCompile() method below does not compile. Give the line number where the compile error occurs (the line numbers are shown on the left hand side). 1 private void testCompile(String value) { 2 int result1 = value + 1; 3 String result2 = result1 + "2"; 4 System.out.println(result2); 5 } Compile error occurs at line: 2 (2 marks) e) Give the output produced when the following code is executed. String a = "56"; int x = (Integer.parseInt(a) / 7 + 2) * 6 - 4; String b = "" + x; System.out.println(b.equals(a)); true (2 marks) Question/Answer Sheet - 4 - CompSci 101 ID: ........................................................... CONTINUED Question 2 (6 marks) a) Complete the definition of the Rectangle , r2 , so that the following code prints the letter B. Rectangle r1 = new Rectangle(20, 40, 100, 50); r1.x = r1.x + 10; (2 marks) if (r1.intersects(r2)) { System.out.println("A"); } else { System.out.println("B"); } b) Complete the definition of the Point , p , so that the following code prints the letter A....
View Full Document

This note was uploaded on 03/01/2012 for the course COMPSCI 101 taught by Professor Adrianaferraro during the Summer '12 term at Auckland.

Page1 / 34

2011S2ExamAnswers - CompSci 101 CONTINUED THE UNIVERSITY OF...

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

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