NATIONAL UNIVERSITY OF SINGAPORE

Similar documents
NATIONAL UNIVERSITY OF SINGAPORE

NATIONAL UNIVERSITY OF SINGAPORE

Do not start the test until instructed to do so!

Introduction to Computer Science Unit 2. Exercises

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

CS1083 Week 2: Arrays, ArrayList

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

Introduction to Computer Science Unit 2. Notes

The University of Nottingham

COE 211 Computer Programming. Welcome to Exam I Tuesday March 13, 2018

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Introduction to Computer Science Unit 2. Notes

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

CS 101 Spring 2007 Midterm 2 Name: ID:

Tutorial 7. Number Colour Black Brown Red Orange Yellow Green Blue Violet Gray White

CS1020 Data Structures and Algorithms I

AP Computer Science A Unit 2. Exercises

AP CS Unit 3: Control Structures Notes

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Introduction to Programming Written Examination

CS212 Midterm. 1. Read the following code fragments and answer the questions.

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Module 5: Review of Day 3

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Question: Total Points: Score:

Introduction to Programming (CS112): Sample

CS 110 Practice Final Exam originally from Winter, Instructions: closed books, closed notes, open minds, 3 hour time limit.

Module 5: Review of Day 3

AP COMPUTER SCIENCE A

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Data dependent execution order data dependent control flow

Second Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 11 November 2010

Faculty of Science COMP-202A - Foundations of Computing (Fall 2012) - All Sections Midterm Examination

Monday 9 June 2014 Afternoon

CS 180. Recitation 8 / {30, 31} / 2007

First Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 7 October 2010

Question: Total Points: Score:

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

NATIONAL UNIVERSITY OF SINGAPORE

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Problem Grade Total

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

CMPS 11 Introduction to Programming Midterm 1 Review Problems

NATIONAL UNIVERSITY OF SINGAPORE

CSCI 111 Midterm Spring 2014

EXAMINATION FOR THE BSC (HONS) INFORMATION TECHNOLOGY; BSC (HONS) INFORMATION SYSTEMS & BSC (HONS) COMPUTER SCIENCE; YEAR 1

This exam is open book. Each question is worth 3 points.

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

1 Short Answer (5 Points Each)

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Final Examination Semester 2 / Year 2011

INSTRUCTIONS TO CANDIDATES

CSI 1100 / 1500 Fall 2004 Introduction to Computer Science I Final Examination

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

CIS 110 Introduction to Computer Programming Summer 2014 Midterm. Name:

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 2 EXAMINATIONS 2014/2015 CI101/CI101H. Programming

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

CMPS 11 Intermediate Programming Midterm 2 Review Problems

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

COMPUTER APPLICATIONS

SAMPLE EXAM Second Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 11 November 2010

Exam Duration: 2hrs and 30min Software Design

COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

CMPS 12A Introduction to Programming Midterm 2 Review Problems

CIS Introduction to Computer Programming. 5 October 2012 Midterm

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ June Exam

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

COMPUTER APPLICATIONS

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Introduction to Software Development (ISD) Week 3

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

Full file at

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

COMP 110 Project 1 Programming Project Warm-Up Exercise

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 27 th March

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2014

Final Exam Practice. Partial credit will be awarded.

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

CSC 231 DYNAMIC PROGRAMMING HOMEWORK Find the optimal order, and its optimal cost, for evaluating the products A 1 A 2 A 3 A 4

JAVA Programming Language Homework I - OO concept

H212 Introduction to Software Systems Honors

COE 212 Engineering Programming. Welcome to Exam I Thursday June 21, Instructor: Dr. Wissam F. Fawaz

COE 212 Engineering Programming. Welcome to Exam II Tuesday November 28, 2018

The University of Nottingham

CIS 110 Introduction to Computer Programming. 17 December 2012 Final Exam

F I N A L E X A M I N A T I O N

Oct Decision Structures cont d

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

CSCI 1226 Sample Midterm Exam

CS 113 PRACTICE FINAL

Selected Questions from by Nageshwara Rao

CMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:

Transcription:

NATIONAL UNIVERSITY OF SINGAPORE SCHOOL OF COMPUTING EXAMINATION FOR Semester 1: 2007/8 CS1101X PROGRAMMING METHODOLOGY November 2007 Time allowed: 2 hours INSTRUCTIONS TO CANDIDATES 1. This examination paper consists of TWELVE (12) questions and comprises TWELVE (12) printed pages. 2. This is an OPEN BOOK examination. 3. Answer all questions. 4. Write your answers in the ANSWER SHEETS provided. 5. Fill in your Matriculation Number with a pen, clearly on every page of your ANSWER SHEETS. 6. You may use pencil to write your codes. Pen is preferred for other questions. 7. You must submit only the ANSWER SHEETS and no other document. - 1 of 12 -

SECTION A (5 Multiple Choice Questions: 20 Marks) Each question has one correct answer. Write your answer in the space provided on the ANSWER SHEETS. 4 marks are awarded for each correct answer and no penalty for wrong answer. Q1. What is the output of the following code? int sum[ ][ ] = new int[4][4]; for (int k = 0; k < 4; k++) sum[k][0] = 1; for (int k = 0; k < 4; k++) sum[0][k] = 1; for (int m = 1; m < 4; m++) for (int n = 1; n < 4; n++) sum[m][n] = sum[m-1][n-1] + sum[m][n-1]; for (int n = 1; n < 4; n++) System.out.print(sum[3][n] + " "); A. 2 4 6 B. 2 4 8 C. 2 4 10 D. 4 6 10 E. 4 10 20 Q2. What is the output of the following code? int a[ ] = {1, 1, 2, 3, 5, 8, 13, 21; for (int n = 0; n < 5; n++) { a[n] = a[n+1]; a[n+1] = a[n+2]; a[n+2] = a[n+3]; a[n+3] = a[n] + a[n+1] + a[n+2]; System.out.println(a[7]); A. 42 B. 44 C. 78 D. 82 E. 84-2 of 12 -

Q3. Given the following definitions of classes AA and BB. What is the output of the Test program? class AA { private void f() { System.out.print( "AA.f " ); protected void g() { System.out.print( "AA.g " ); public void h() { f(); g(); System.out.print( "AA.h " ); class BB extends AA { private void f() { System.out.print( "BB.f " ); public void g() { System.out.print( "BB.g " ); class Test { public static void main( String[] args ) { BB bb = new BB(); bb.h(); A. BB.f BB.g AA.h B. AA.f AA.g AA.h C. BB.f AA.g AA.h D. AA.f BB.g AA.h E. The program causes compile-time error. - 3 of 12 -

Q4. Which of the following code samples can be compiled successfully? Assume that all the classes are defined in separate files. class AA { public AA(int i) { System.out.print("AA"); Sample 1 class BB extends AA { public BB(int i) { System.out.print("BB"); class CC { public CC(int i) { System.out.print("CC"); Sample 2 class DD extends CC { public DD(int i) { super(i); System.out.print("DD"); class EE { // Empty Sample 3 class FF extends EE { public FF(int i) { System.out.print("FF"); A. Samples 1 and 2 only. B. Samples 1 and 3 only. C. Samples 2 and 3 only. D. Samples 1, 2 and 3. E. None of the above. - 4 of 12 -

Q5. In the program fragment below, which of the outputs is the best estimated value? int N = 1000000; int count = 0; double x, y; for (int i = 0; i < N; i++) { x = Math.random( ); y = Math.random( ); if (x*x + y*y <= 1.0 ) count++; System.out.println( count*1.0 / N ); A. 0.5 B. 0.785 C. 1.0 D. 3.142 E. There is no way to estimate. SECTION B (7 Questions: 80 Marks) Q6. Which company developed the Java programming language? [1 mark] Q7. Identifying errors [8 marks] Assuming that the classes Pen, Paper and Pencil have been defined, identify the error(s) in each of the following code fragments. (a) Identify the error(s) in the following code fragment. [4 marks] 1. public class TeachingAssistant { 2. // code omitted 3. public void gradepaper (Paper paper, Pen pen) { 4. // code omitted 5. 6. 7. // new class 8. public class Professor { 9. private TeachingAssistant ta; 10. private Pen _pen; 11. // code omitted 12. public void givetawork (Paper paper) { 13. ta.gradepaper (pen, paper); 14. 15. - 5 of 12 -

Q7. (continued ) (b) Identify the error(s) in the following code fragment. 1. public class Student { 2. private Pencil _pencil; [4 marks] 3. public Student() { 4. // code omitted 5. _pencil = new Pencil(); 6. 7. public void takenotes() { 8. this.write(pencil _pencil); 9. 10. Q8. Outputs of codes [19 marks] (a) What is the output of the following program fragment? [2 marks] int n = 13; switch (n) { case 1 : System.out.println ("Ace"); case 11: System.out.println ("Jack"); case 12: System.out.println ("Queen"); case 13: System.out.println ("King"); default: System.out.println (n); (b) What is the output of the following program fragment? [5 marks] String str = "Java_is_fun"; for (int i = 0; i < str.length(); i++) { if ("aeiou".indexof(str.charat(i)) > -1) str = str.substring(0, i) + "*" + str.substring(i+1); System.out.println(str); - 6 of 12 -

Q8. (continued ) (c) What is the output of the following program if the input n is 1729? [12 marks] import java.util.*; class Digits { public static void main( String[] args ) { Scanner sc = new Scanner(System.in); System.out.print("Enter an integer: "); int n = sc.nextint(); System.out.println("Method M1: " + M1(n)); System.out.println("Method M2: " + M2(n)); System.out.println("Method M3: " + Integer.parseInt(M3(n))); public static int M1 (int n) { int sum = 0; while (n > 0) { sum += n % 10; n /= 10; return sum; public static int M2 (int n) { int n1 = n; while (n1 >= 10) { n1 = 0; while (n > 0) { n1 += n % 10; n /= 10; n = n1; return n1; public static String M3 (int n) { String s = new Integer(n).toString(); String s1 = ""; int len = s.length(); for (int i = 0; i < len; i++) s1 += s.substring(len-i-1, len-i); return s1; - 7 of 12 -

Q9. Resistor [10 marks] A resistor is a circuit device designed to provide a specific resistance between its two ends. Resistance is expressed in ohms (Ω) or kilo-ohms (kω). Resistors are usually marked with coloured bands that encode their resistance, as shown in figure 1 below. The first two bands represent digits and the third is a power-of-ten multiplier. 1 st digit 2 nd digit multiplier Figure 1: Colour Bands of a resistor The table below shows the number value of each band colour. For example, if the first band is red (represents 2), the second is black (represents 0), and the third is orange (represents 3), the resistance is 20 10 3 Ω or 20 kω. Colour Number Value Black 0 Brown 1 Red 2 Orange 3 Yellow 4 Green 5 Blue 6 Violet 7 Grey 8 White 9 A Resistor class containing the default constructor is provided on the next page. You are to fill in the other constructor, which takes in three strings representing the three band colours, calculates and sets the resistance for the resistor. You may write other methods necessary to calculate the resistance. You do not need to write the main method. You may assume that the three colour strings are correct in spelling but may be a mixture of upper-case and lower-case letters. For example, a sample application code using the resistor class may be as follows: Resistor resistor1 = new Resistor( "black", "GREEN", "orange" ); System.out.println(resistor1.getResistance()); - 8 of 12 -

Complete the constructor Resistor(String b1, String b2, String b3). You may write additional methods to support the constructor. [10 marks] public class Resistor{ private static String[] colour = {"black","brown","red","orange","yellow", "green","blue","violet","grey","white"; // data members private String band1; private String band2; private String band3; private double value; // Resistor value (Ohms) // default constructor public Resistor(){ // Constructor taking three strings as parameters // and calculating the resistance public Resistor(String b1, String b2, String b3){ // *** to be completed *** // set the resistance value public void setresistance(double val){ this.value = val; // get the resistance public double getresistance(){ return this.value; - 9 of 12 -

Q10. Overlapping Rectangles [27 marks] The code below defines a MyRectangle class with four public data members representing the x- and y-coordinates of the bottom-left vertex and top-right vertex of a rectangle, whose sides are parallel to the x- or y-axis. class MyRectangle { // Data members public int vertex1x; // x-coordinate of bottom-left vertex public int vertex1y; // y-coordinate of bottom-left vertex public int vertex2x; // x-coordinate of top-right vertex public int vertex2y; // y-coordinate of top-right vertex // Constructor public MyRectangle(int v1x, int v1y, int v2x, int v2y) { vertex1x = v1x; vertex1y = v1y; vertex2x = v2x; vertex2y = v2y; // Returns area of rectangle public int area() { /****** to be completed refer to part (a) ******/ // Returns the overlap region of this and rect public MyRectangle overlap(myrectangle rect) { /****** to be completed refer to part (b) ******/ // Returns the overlap region of all rectangles // in the array public static MyRectangle overlapall(myrectangle[] rectangles) { /****** to be completed refer to part (c) ******/ // Returns string representation public String tostring() { /****** to be completed refer to part (e) ******/ - 10 of 12 -

Q10. (continued ) For example, the statement new MyRectangle(20,80,30,90) creates a rectangle with bottom-left vertex at position (20,80), and top-right vertex at (30,90). (a) Complete the area() method, which computes the area of a rectangle.[3 marks] (b) Complete the overlap(myrectangle rect) method. This method returns a rectangle which is the overlapped region of two rectangles. In the event that there is no overlap, it should return a rectangle with both bottom-left vertex and top-right vertex at position (0,0). [9 marks] (c) Using the overlap(myrectangle rect) method written above, complete the overlapall(myrectangle[] rectangles) method which returns the overlapped region of all the rectangles in the array. You may assume that there is at least one element in the array. Your method should be efficient in that the moment it finds that the overlapped region is empty, it should return a rectangle with both vertices at (0,0) immediately. [9 marks] (d) In the following code MySqaure.java, MySquare extends MyRectangle. import java.util.*; class MySquare extends MyRectangle { public MySquare(int xcoord, int ycoord, int length) { /****** to be completed refer to part (d) ******/ super(... ); public static void main(string[] args) { Scanner sc = new Scanner(System.in); int x = sc.nextint(); // x-coord of bottom-left corner int y = sc.nextint(); // y-coord of bottom-left corner int len = sc.nextint(); // length of side MySquare square = new MySquare(x, y, len); System.out.println(square); System.out.println("Area = " + square.area()); A square is defined by its bottom-left vertex and the length of its side. Complete the super(... ) statement in the constructor. [3 marks] (e) Below is output of MySquare.java program when the user enters: 10 30 5. Class MySquare: [(10,30); (15,35)] Area = 25 Complete the tostring() method in MyRectangle in order to get such output. [3 marks] - 11 of 12 -

Q11. Recursion [10 marks] The Conway s recursive sequence is defined by the following recurrence relation for positive integer n. a ( n) = a 1 if n { 1,2 ( a( n 1) ) + a( n a( n 1) ) otherwise (a) Write a recursive method for the sequence. (b) What is the value of a(5)? [4 marks] [2 marks] (c) What is the total number of recursive methods activated when evaluating a(5)? [2 marks] (d) What is the maximum number of activations of the recursive method at any instance of time when evaluating a(5)? [2 marks] Q12. Recursion [5 marks] You are given the following methods: static String r(int m) { if (m > 0) return "+" + r(m-1); else return ""; static String s(int m, int n) { if (n > 0) return "-" + s(m,n-1) + "-"; else return r(2*m); Write the output for the following statement: System.out.println( s(3,2) ); [5 marks] === END OF PAPER === - 12 of 12 -