2006SSExamQuestions

2006SSExamQuestions - COMPSCI 101 THE UNIVERSITY OF...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

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

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

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

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

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

View Full DocumentRight Arrow Icon
Background image of page 12
Background image of page 13

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

View Full DocumentRight Arrow Icon
Background image of page 14
Background image of page 15

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

View Full DocumentRight Arrow Icon
Background image of page 16
Background image of page 17

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

View Full DocumentRight Arrow Icon
Background image of page 18
Background image of page 19

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

View Full DocumentRight Arrow Icon
Background image of page 20
Background image of page 21

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

View Full DocumentRight Arrow Icon
Background image of page 22
Background image of page 23

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

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

Unformatted text preview: COMPSCI 101 THE UNIVERSITY OF AUCKLAND SUMMER SEMESTER, 2006 Campus: City COMPUTER SCIENCE Principles of Programming (Time allowed: TWO hours) ' NOTE: Attempt ALL 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 i _ i l i1 Login name: J l 01 Q4 (/20) ‘ (/8) (/12) (I6) (/4) (l8) (/9) (/10) (/5) (/8) (/10) {/100) CONTINUED - 2 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. Question 1 (20 marks) a) What is printed by the following? System.out.println(40 % 100); (2 marks) b) What is printed by the following? for (int i = 5; i >= 0; i = i—2) { System.out.println(i); } (2 marks) 0) What is printed by the following? int numbers = {1, 2, 3, 4}; System. out .println (numbers [numbers [1] ] ) ; (2 marks) CONTINUED - 3 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. d) What is printed by the following? String wordl new String(" bravo"); String word2 new String("BRAVO"); String word3 — word2; if ((wordl.trim()).equals(word2.toLowerCase())) { System.out.println("l" + wordl); } if (word3 :2 word2) { System.out.println("2" + word2); } (2 marks) e) What is printed by the following? String name = "Thought is free"; name = name.substring(8); String result = name.substring(3); System.out.println(result); (2 marks) 0 What is printed by the following? String word = "Art is long, life is short"; int positionl word.index0f("long"); int position2 — word.indexOf(','); word = word.sub tring(positionl, position2); System.out.println(“word: " + word); (2 marks) CONTINUED - 4 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. g) What is printed by the following? Point a = new Point(8, 9); Point b = new Point(8, 9); Point c = a; Point d = new Point(lO, 10); System.out.println(a.equals(d)); System.out.println(b == 0); System.out.println(c.equals(b)); (2 marks) h) What is printed by the following? boolean resultl, result2; int value = 7; resultl = value > 5 && !(value < O [I value%2 == ), result2 = (value I: 6 && value%2 != O) [I value > 3, System.out.println("resultl: " + resultl); System.out.println("result2: " + result2); (2 marks) CONTINUED Question/Answer Sheet i) The Number class is defined as follows: public class Number { private static int numberl = 0; private int number2; public Number(int num) { numberl numberl + num; numberZ num; II II } public String toString() { return "numberl: " + numberl + } } What is printed by the following? new Number(4) ; Number num2 new Number(2); Number num3 new Number(3) ; System. out.println (numl . toString () ) ,- System. out . println (num2 . toString () ) ; Number numl II II II j) What is printed by the following? String[l a = new String[lO]; System.out.println(a[5]); COMPSCI 101 . ID. ..................... ., ............................................ .. " number2: " + number2; (2 marks) (2 marks) CONTINUED - 6 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. Question 2 (4 marks) a) What is the output when the start ( ) method below is executed? public class Q2 { public void start() { lotsOfIfs(24, "HI"); System.out.println(); lotsOfIfs(2, "HI"); } private void lotsOfIfs(int value, String word) { if (value < 4 && !word.equals("HI")) { System.out.println("Line 1“); value = 12; word = word + "H0"; } else if (value > 10) { System.out.println("Line 2"); value = value/2; word = word + word + word; } if (value > 6) { System.out.println("Line 3"); value = —l; } else if (word.length() > 4) { System.out.println("Line 4"); } else { System.out.println("Line 5"); } System.out.println("Line 6 " + word); (4 marks) CONTINUED - 7 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. Question 3 (5 marks) The output of the following code segment: ll Point pts new Point[3]; pts[O] = new Point(lOO, 200); pts[l] = new Point(300, 400); pts[2] = new Point(500, 600); System.out.println(pts[O].x + " , " + pts[O].y); System.out.println(pts[l].x + " , “ + pts[l].y); System.out.println(pts[2].x + " , " + pts[2].y); is given below: 100 , 200 300 , 400 500 , 600 What would be the output if the following code is added after the statements above: ptSEll; templ; Point templ Point temp2 pts[O].y = temp2.x; templ.x = pts[2].x; templ = pts[2]; pts[2].x = temp2.y; pts[l] = pts[O]; pts[O] = templ; System.out.println(pts[0].x + “ , " + pts[O].y); System.out.println(pts[l].x + " , " + pts[l].y); System.out.println(pts[2].x + " , " + pts[2].y); (5 marks) CONTINUED - 8 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. Question 4 (8 marks) Write a method get2 4HourTime ( ) which accepts three parameters and returns a String. The three parameters are an int representing the hour value, an int representing the minute value and a String which is either “am” or “pm”. The method returns a String representing the time as a 24 hour time value. For example, 2:35pm is “14:35” in 24 hour time. Note: 12:0pm is “12:0” in 24 hour time and 12.0am is “0:0” in 24 hour time. For example, executing the Q4 program with the completed get2 4HourTime ( ) method produces the following output: > java Q4App 20:23 12:0 0:0 7:15 public class Q4 { public void start() { System.out.println(get24HourTime(8, 23, "p System.out.println(get24HourTime(12, 0, "p System.out.println(get24HourTime(12, O, "a System.out.println(get24HourTime(7, 15, "am" private get2 4HourTime ( (8 marks) CONTINUED - 9 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. Question 5 (8 marks) Write a method getRandomLetters ( ) which accepts two parameters and returns a String. The first parameter is a String of letters and the second parameter is an int value indicating how many random letters should be returned. The method should obtain the required number of random letters from the String parameter. The String which is returned is in upper case characters. Note: 0 A letter cannot be selected twice unless the letter occurs twice in the parameter String. 0 You can assume that the String parameter always contains enough letters and that the int parameter is always a positive value. For example, executing the Q5 program with the completed getRandomLetters ( ) method produces the following output: > java QSApp LEV OYE EVOL 0 OG public class Q5 { public void start() { System.out.println(getRandomLetters("LOVELY", 3 System.out.println(getRandomLetters("LOVELY", 3 System.out.println(getRandomLetters("LOVELY", 4 System.out.println(getRandomLetters("song", 1)) System.out.println(getRandomLetters("song", 2)) getRandomLetters (8 marks) CONTINUED - 10 - COMPSCI 101 Question/Answer Sheet 1]): ................................................................... .. Question 6 (8 marks) Draw the output produced by the following code in the grid given at the bottom of the page. The grid lines are not part of the output but are there to help you place the drawing in the correct position. The size of each square in the grid is 10 pixels by 10 pixels. import java.awt.*; import javax.swing.*; public class DranPanel extends JPanel { public void paintComponent(Graphics g){ super.paintComponent(g); draw(g, O, O, 10); draw(g, 80, 60, 20); } private void draw(Graphics g, int x, int y, int size){ g.drawOval(x, y, size*2, size*2); g.drawLine(x+size, y+size*2, x+size, y+size*4); g.fillRect(x+size/2, y+size*3, size, size/2 ); g.drawString("FUN",x+size*3/2, y+size*7/2); CONTINUED - 11 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. Question 7 (12 marks) The JPanel defined on the next page contains six components: A J TextField which contains an integer indicating the first number of hours, A J TextField which contains an integer indicating the first number of minutes, A J TextField which contains an integer indicating the second number of hours, A J TextField which contains an integer indicating the second number of minutes, A J TextField which displays a String indicating the total difference in minutes between the two times. 0 The "CALCULATE" JBut ton. Below is a screenshot Of the J Panel when it is first displayed. =5 A JFrame _._.......__ ,...._.___._.. HOURS 1 Lwdl MINUTES 1§__,___j' Emmi r - -m._.__m..u.._- L.....~._1 l EAEcULATE Whenever the user presses the "CALCULATE" button, the total difference in minutes between the two times is calculated and displayed in the "differenceT" J TextField. Note: the difference is always displayed as an absolute number i.e. a positive number. You are required to complete the J Panel definition on the next page so that the J Panel behaves as described above. You must use the variables given in the code. The screenshots below show the J Panel after the user has entered values in the top four J TextFields and pressed the "CALCULATE" button. HOURS 1 2- L“ _ HOURS 1513 "j MINUTES 156 ! ...__.L.J . _‘.__.-L.~J HOURS 25‘":' MINUTES 2:15 HOURS 2;; ____ MINUTES 2§1 i I itifeyence: 60 minutes i l fe re nce: 65 m infites CONTINUED - 12 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. HOURS 1 {12 jMINUTES 1 §48 .l"“ -lan 1 HOURS 2112 jMINUTE82§20 ‘ 3_._W.r-- I ifferencg;__28 minutes J import java.awt.*; import javax.swing.*; public class TimeJPanel extends JPanel private JTextField hourslT, minuteslT, hours2T, minutesZT; private JTextField differenceT; private JButton calculateB; public TimeJPanel() { JLabel hourle, minutele, hoursZL, minutes2L; hourslT = new JTextField(3); hours2T = new JTextField(3); minuteslT = new JTextField(3); minutesZT = new JTextField(3); differenceT = new JTextField(ZO); hourle hoursZL minutele minutesZL new JLabel("HOURS 1"); new JLabel("HOURS 2"); new JLabel("MINUTES 1"); new JLabel("MINUTES 2"); calculateB = new JButton("CALCULATE"); add(hourle); add(hourslT); add(minutele); add(minuteslT); add(hoursZL); add(hoursZT); add(minutesZL); add(minutes2T); add(differenceT); add(calculateB); CONTINUED - l3 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. public void (12 marks) CONTINUED - l4 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. Question 8 (9 marks) For this question you need to write a method called biggestArea ( ) which is passed an array of Rectangle objects, and returns the value of the area of the largest rectangle in the array. The area of a rectangle is the width multiplied by the height. As an example, to demonstrate how your method should work, consider the array of Rectangles created below: Rectangle rects = new Rectangle[5]; rects[O] = new Rectangle(lOO, 100, 10, 9); rects[l] = new Rectangle(20, O, 12, 2); rects[2] = new Rectangle(SO, 50, 10, ll); rects[3] = new Rectangle(lOO, 30, 5, 5); rects[4] = new Rectangle(40, 120, 7, 3); int largestArea = biggestArea(rects); System.out.println("Largest Area = " + largestArea); If you have defined your bigges tArea ( ) method correctly, the output from the above code segment should be: Largest Area = 110 You can assume that the array passed to the biggestArea ( ) method contains at least one element, and that no element in the array is null. Define the bigges tArea ( ) method in the space provided below: rects) { private int bi estArea(Rectangle (9Inarks) CONTINUED - 15 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. Question 9 (10 marks) For this question, you need to define a class called Square, which is used in the following program. When the program first starts, a square is drawn in outline in the middle of the window. The centre of the square is at position (100, 100) and the length of each side of the square is 40 pixels. The screenshot below illustrates this. The program responds to MouseEvents in the following way: 0 if the mouse is outside the square when the mouse button is pressed, then the square will be drawn in outline (this is illustrated in the diagram on the left below). 0 if the mouse is inside the square when the mouse button is pressed, then the square will be drawn filled in (this is illustrated in the diagram on the right below). CONTINUED - 16 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. The complete J Panel class has already been written: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ClickSquareJPanel extends JPanel implements MouseListener { private Square s; public RemoveRectsJPanel() { setBackground(Color.white); s = new Square(lOO, 100, 40); addMouseListener(this); } public void mousePressed(MouseEvent e) { s.processClick(e.getX(), e.getY()); repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); s.draw(g); public void mouseReleased(MouseEvent e) } public void mouseClicked(MouseEvent e) { public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { { } } CONTINUED - 17 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. Define the Square class in the space provided below, so that the program will behave correctly as described above. The instance variables for this class have been provided for you — you should not declare any other instance variables. import java.awt.*; public class Square { private Rectangle pos; private boolean filled; (10 marks) CONTINUED - 18 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. Question 10 (6 marks) a) Consider the array called nums, created as follows: int nums = { l, 2, 3, 4, 5 }; We can visualise this array as in the diagram below: nums Complete the diagram of the array given below after the following loop has executed: for (int i = 1; i < numbers.length; i++) { numbers[i—l] = numbers[i]; } (2 marks) CONTINUED - 19 - COMPSCI 101 Question/Answer Sheet II): ................................................................... .. b) Consider the following method in which some poor choices have been made for identifier names. private double xxx(int nums) { double a = 0.0; int b = O; for (int i = O; i < nums.length; i++) { a = a + nums[i]; b=b+l; } double 0 = a / b; return c; } What would be the output if this method is called as in the code segment below: int nums = {—3, 4, —l, 7, -2, 4}; System.out .println (xxx(nums) ) ; (2 marks) (1 mark) Suggest a more appropriate identifier than xxx as the name of the method. The name you choose should indicate what the method does. (I mark) CONTINUED - 20 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. Question 11 (10 marks) Consider the Car class, which represents a car, defined as follows: public class Car { private String colour; private double speed; private int gear; public Car() { colour = new String("red"); speed = 0.0; gear 2 O; } public boolean compare(Car other) { return (speed < other.speed); } public String getColour() { return colour; } public void paint(String newColour) { colour = newColour; } public void accelerate(double extraSpeed) { speed += extraSpeed; gear Math.min((int)(speed / 20.0) + l, 5); ll } public String toString() { return new String(colour + " car, "+ speed + "km/h, gear: " + gear); For this question you need to draw a diagram illustrating the data stored in the objects, and determine the output, when the following code segment is executed: new Car( new Car( Car cl Car c2 ); ); cl.paint("blue"); c2.accelerate(50); System.out.println(cl); System.out.println(c2); if ( cl.compare(02) ) { System.out.println(cl.getColour()); } else { System.out.println(c2.getColour()); } CONTINUED - 21 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. Complete the diagram below, accurately representing the information stored in each of the Car objects. Also clearly mark on the diagram any changes to the information as the code executes. c1B__. c2E—> (5 marks) In the space provided below, give the output when the code segment is executed: (5 marks) CONTINUED - 22 - COMPSCI 101 Question/Answer Sheet 1]): ................................................................... .. OVERFLOW PAGE (If you have used this page, please indicate clearly under the relevant question that you have overflowed to this page) CONTINUED - 23 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. ROUGH WORKING (WILL NOT BE MARKED) (You may use this page for rough working) CONTINUED - 24 - COMPSCI 101 Question/Answer Sheet ID: ................................................................... .. ROUGH WORKING (WILL NOT BE MARKED) (You may use this page for rough working) ...
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 / 24

2006SSExamQuestions - COMPSCI 101 THE UNIVERSITY OF...

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

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