NATIONAL UNIVERSITY OF SINGAPORE

Similar documents
AP CS Unit 3: Control Structures Notes

Introduction to Computer Science Unit 2. Notes

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

NATIONAL UNIVERSITY OF SINGAPORE

Introduction to Computer Science Unit 2. Notes

Final Exam. COMP Summer I June 26, points

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

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

Software Practice 1 Basic Grammar

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

NATIONAL UNIVERSITY OF SINGAPORE

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

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

Unit 4: Classes and Objects Notes

Section 2.2 Your First Program in Java: Printing a Line of Text

Do not start the test until instructed to do so!

Course Outline. Introduction to java

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

Sample Examination Paper Programming and Software Development

NATIONAL UNIVERSITY OF SINGAPORE

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

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

Computational Expression

Practice Midterm 1. Problem Points Score TOTAL 50

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Programming with Java

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

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

CS260 Intro to Java & Android 03.Java Language Basics

Unit 4: Classes and Objects Exercises

Problem Grade Total

CSE 1223: Exam II Autumn 2016

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Supplementary Test 1

University of Palestine. Mid Exam Total Grade: 100

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

CSCI 1226 A Test #1. Wednesday, 10 October, 2018 Name: Student #: General Instructions Read and follow all directions carefully.

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

Introduction to Programming Using Java (98-388)

CSCI 135 Final Fall Problem Points Score Total 100

COMPUTER APPLICATIONS

Practice Midterm 1 Answer Key

Final Examination Semester 2 / Year 2011

Fall 2002 Page 1 of 9 READ THIS NOW!

APCS Semester #1 Final Exam Practice Problems

Sri Vidya College of Engineering & Technology

Introduction to Computer Science Unit 2. Exercises

Array. Prepared By - Rifat Shahriyar

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

CSCI 355 Lab #2 Spring 2007

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

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

INSTRUCTIONS TO CANDIDATES

Final Exam Practice. Partial credit will be awarded.

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013

1 Short Answer (10 Points Each)

AP Computer Science A

COMPUTER APPLICATIONS

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Logic & program control part 2: Simple selection structures

Part I. Multiple Choice Questions (2 points each):

PROGRAMMING FUNDAMENTALS

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Review for Test 1 (Chapter 1-5)

Object-oriented Programming and Software Engineering CITS1001. Multiple-choice Mid-semester Test

Using Java Classes Fall 2018 Margaret Reid-Miller

Exam 1 - (20 points)

CIS Introduction to Computer Programming. 5 October 2012 Midterm

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Getting started with Java

Full file at

DM550 Introduction to Programming part 2. Jan Baumbach.

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

NATIONAL UNIVERSITY OF SINGAPORE CS2100 COMPUTER ORGANISATION

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Question: Total Points: Score:

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

CS 101 Exam 1 Spring 200 Id Name

DM503 Programming B. Peter Schneider-Kamp.

Section 2.2 Your First Program in Java: Printing a Line of Text

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

CS111: PROGRAMMING LANGUAGE II

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Selected Questions from by Nageshwara Rao

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

More on methods and variables. Fundamentals of Computer Science Keith Vertanen

YOU ARE ALLOWED TO HAVE ONLY THE FOLLOWING ON YOUR DESK OR WORKTABLE:

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

CS Week 5. Jim Williams, PhD

AP COMPUTER SCIENCE A DIAGNOSTIC EXAM. Multiple Choice Section Time - 1 hour and 15 minutes Number of questions - 40 Percent of total grade - 50

CSCI 355 LAB #2 Spring 2004

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

Introduction to Java

CSE 114 Midterm 1. Please leave one seat between yourself and each of your neighbors.

Crash Course Review Only. Please use online Jasmit Singh 2

Object-Oriented Programming and Software Engineering CITS1001 MID-SEMESTER TEST

Chapter 2: Basic Elements of Java

Transcription:

NATIONAL UNIVERSITY OF SINGAPORE SCHOOL OF COMPUTING TERM TEST #1 Semester 1 AY2006/2007 CS1101X/Y/Z PROGRAMMING METHODOLOGY 16 September 2006 Time Allowed: 60 Minutes INSTRUCTIONS 1. This question paper contains THRITY (30) questions and comprises ELEVEN (11) printed pages, including this page. 2. An ANSWER SHEET is provided for you to write the answers. It comprises TWO (2) printed pages. 3. Answer ALL questions within the space provided on the Answer Sheet. 4. Maximum score is 35 marks. 5. This is an OPEN BOOK test. 6. Write legibly with a pen or pencil. 7. Calculators are allowed, but not laptops, PDAs or other computing devices. 8. Submit only the Answer Sheet at the end of the test. You may keep the question paper. 9. Write your MATRICULATION NUMBER on the Answer Sheet using A PEN. END OF INSTRUCTIONS

SECTION A (25 Multiple Choice Questions. 25 Marks) Each question has one correct answer. Write your answer in the space provided on the Answer Sheet. 1 mark for each correct answer and no penalty for wrong answer. 1. What is the difference between a method and a message? A. A method is a sequence of instructions that a class or object uses to perform a task, and a message is a signal that tells the object to perform a task. B. A method is strictly concerned with programming and a message is strictly concerned with e-mail. C. A method is a prototype of how a computer should carry out a task, and a message implements the method. D. A method is a set of instructions to tell the object what to do, and a message is a packet of information used to communicate between two users on a network. E. A message is a sequence of instructions that a class or object uses to perform a task, and a method is a signal that tells the object to perform a task. 2. What are the four kinds of data members? A. Class variables, class constants, class method variables, and instance variables. B. Class variables, class constants, instance variables, and instance constants. C. Class variables, method variables, data variables, and instance variables. D. There are actually only two kinds: class constants, and instance variables. E. There are actually only three kinds: class variables, class constants, and instance variables. 3. What is the merit of inheritance? Pick the best answer. A. Inheritance makes a Java program more efficient and thus run faster. B. Inheritance makes a program more portable across different platforms. C. Inheritance allows information hiding so that internal details of objects are protected from unauthorized access. D. Inheritance can be used to efficiently design two or more entities that are different but share many common features. E. Inheritance allows objects to share common data values and therefore reduces computer memory usage. Page 2 of 11

4. Which of the followings would be the best choice to make a class constant of the class Tortoise? A. weight B. number of legs C. age D. average age E. longest recorded oldest age 5. Jacky has a set of integer data values ranging from 128 to 128. Which of the following Java primitive types is the most suitable and most space-efficient for storing each data value in the computer memory. A. byte B. short C. int D. long E. double 6. Which of the following Boolean expressions is equivalent to the Boolean expression!(x < 0 && y < 0)? A.!(x < 0) &&!(y < 0) B.!(x >= 0)!(y >= 0) C. x >= 0 && y >= 0 D. x > 0 && y > 0 E. x >= 0 y >= 0 7. How many of the followings are valid Java identifiers? 78class Class87 sixdogs User$ID Jump_Up_ DEFAULT_VAL False Private Average-Age Hello! First One String A. 5 B. 6 C. 7 D. 8 E. 9 Page 3 of 11

8. Which of the following three if statements are equivalent? I. if (a == b) { if (c == d) a = 1; else b = 1; II. if (a == b) { if (c == d) a = 1; else b = 1; III. if (a == b) if (c == d) a = 1; else { b = 1; A. None B. I and II C. II and III D. I and III E. I, II and III 9. What is the output of the following program fragment? int a = 2, b = 4, c = 6; if ( (++a) > 2 (b++) > 3 ) c++; System.out.println( a + ", " + b + ", " + c ); A. 2, 5, 7 B. 3, 5, 6 C. 3, 4, 6 D. 3, 5, 7 E. 3, 4, 7 10. What is the output of the following program fragment? int a = (int) Math.floor( 2.6 ); int b = (int) ( 2.6 ); int c = (int) Math.floor( -1.7 ); int d = (int) ( -1.7 ); System.out.println( a + ", " + b + ", " + c + ", " + d ); A. 2, 2, -2, -1 B. 2, 2, -1, -1 C. 2, 2, -1, -2 D. 3, 2, -2, -1 E. 2, 3, -1, -1 Page 4 of 11

11. What is the number of distinct values that can be represented with 14 bits? A. 10 14 B. 32768 C. 16384 D. 8192 E. 14 2 12. Which of the following statements is true? A. All methods defined in a class must be declared public. B. A public instance method of a class cannot access private instance data members of the same class. C. A local variable in a method can be declared as private but not public. D. Class constants must always be declared public. E. None of the above. 13. Given the following class definitions, what is the output of the main method? class CX { public int x = 0; class A { public static void f1( int x ) { x = 1; public static void f2( CX cx ) { cx.x = 2; public static void main( String[] args ) { int x = 0; CX c1 = new CX(); CX c2 = new CX(); f1(x); f2(c1); f1(c2.x); System.out.println( x + ", " + c1.x + ", " + c2.x ); // main // A A. 0, 2, 0 B. 1, 2, 1 C. 0, 0, 0 D. 0, 2, 1 E. 1, 0, 1 Page 5 of 11

14. What is the output of the following program fragment? String s1 = new String( "Hello" ); String s2 = new String( "Hello" ); String s3 = s1; System.out.print( (s1 == s2) + ", " ); System.out.print( (s1 == s3) + ", " ); System.out.print( s1.equals(s2) + ", " ); System.out.print( s1.equals(s3) ); A. true, true, true, true B. false, true, false, true C. false, true, true, false D. false, true, true, true E. false, false, true, true 15. What is the output of the following program fragment? int x; x = (int)( 3 * 3 + (int) 3.5 / 0.5 ); System.out.println( "x + 2 = " + x + 2 ); A. x + 2 = 17 B. x + 2 = 152 C. x + 2 = 16 D. x + 2 = 18 E. x + 2 = 162 16. Given the mystery method: public static boolean mystery( String s ) { int i = s.indexof( "XX" ); if ( i < 0 ) return false; s = s.substring( i + 2, s.length() ); return ( s.indexof( "X" )!= 0 ); Which of the followings evaluates to false? A. mystery( "abxx" ); B. mystery( "abxxxdx" ); C. mystery( "abxxdx" ); D. mystery( "abxxdex" ); E. mystery( "abxdexx" ); Page 6 of 11

17. What is the output of this program? class Kool { public static int foo( int x ) { System.out.print( "A" ); return x; System.out.print( "B" ); return x + 1; System.out.print( "C" ); public static void main( String[] args ) { System.out.println( foo(3) ); // main // Kool A. ABC4 B. AB4 C. A3 D. 3A E. 4ABC Note: Question 17 voided as it contains compilation error. 18. What is the decimal value of the binary number 100101.011? A. 37.375 B. 37.03 C. 37.3 D. 21.03 E. 21.375 19. Assuming that all variables that appear in the following have not been declared before. Which of the following will generate a compile-time error? A. double x = (int) 20.5; B. int a = 1, b; C. double y = 0; z = 1.0; D. final int C = 12 * 34 99; E. None of the above. Page 7 of 11

20. Given the following program: import java.util.*; class Inputs { public static void main( String[] args ) { Scanner sc = new Scanner( System.in ); System.out.print( "Enter two numbers: " ); int a = sc.nextint(); double b = sc.nextdouble(); System.out.print( "Enter two more numbers: " ); int c = sc.nextint(); double d = sc.nextdouble(); System.out.println( a + ", " + b + ", " + c + ", " + d ); // main // Inputs What is the output produced by the last statement if the user s inputs are 12 45 87<ENTER> 76 99<ENTER> where <ENTER> indicates where the user presses the Enter key on the keyboard? A. Program causes run-time error because there are too many inputs. B. Program causes run-time error because some inputs are of the wrong numerical data type. C. 45, 87.0, 76, 99.0 D. 12, 45.0, 87, 76.0 E. 12, 45.0, 76, 99.0 21. Which of the following statements is true? A. A default constructor can accept any number of arguments. B. A constructor can be called directly in a client class like calling any other ordinary instance methods. C. A class can have at most one constructor. D. A constructor can call any public or private method in the same class. E. If a constructor is defined, it must be defined before any other methods in the class, otherwise it will be a compile-time error. Page 8 of 11

22. Which of the following expressions generates a random odd integer in the range [7, 21] (inclusive of 7 and 21)? A. (int)( Math.random()*(21-7+1) ) + 7 B. (int)( Math.random()*(10-3+1) ) * 2 + 7 C. (int)math.random()*(10-3+1) * 2 + 7 D. (int)( Math.random()* 2 ) * (10-3+1) + 7 E. None of the above. 23. Given the following class definition: class ABC { public int kool; public ABC() { kool = 5; public void foo( int kool ) { System.out.print( kool + ", " ); kool = kool; System.out.print( kool + ", " ); // ABC What is the output of the following code fragment? ABC abc = new ABC(); System.out.print( abc.kool + ", " ); abc.foo( 10 ); System.out.println( abc.kool ); A. 5, 5, 10, 10 B. 5, 10, 10, 10 C. 5, 10, 10, 5 D. The class definition causes run-time error because there is a name conflict. E. The class definition causes compile-time error because there is a name conflict. Page 9 of 11

24. Given the following method: public static void foo( int x ) { switch( x ) { case 1: System.out.print( "A" ); case 2: System.out.print( "B" ); break; case 3: System.out.print( "C" ); System.out.print( "D" ); case 4: break; default: System.out.print( "E" ); What is the output of the following code fragment? foo(0); foo(1); foo(2); foo(3); foo(4); A. ABCD B. EABCD C. EABCDE D. EABBCDE E. EABBCD 25. Given the following expression, what is the order of evaluation of the operations (from first to last)? x!a == b > c && y A. &&,, ==,!, > B.!, ==, >,, && C. ==,!, >, &&, D.!, >, ==, &&, E. The expression always causes compile-time error. Page 10 of 11

SECTION B (5 Questions. 10 Marks) Write your answer in the space provided on the Answer Sheet. 2 marks for each correct answer. 26. Draw a class hierarchy for the classes Insect, Mammal, Animal, Human, Fish, Rat and Mosquito. 27. Write a public instance method, ismultiple, that returns true if and only if the first integer parameter is a multiple of the second integer parameter. 28. Write a public instance method, headtail, that returns a string made up of the first and last character of the string parameter. You may assume that the length of the input string is at least 2. 29. Write a public instance method, somemethod, that returns the product of the two positive integer parameters if they are both even or both odd, otherwise it returns the sum of the parameters. You must use the?: operator, and the method body must have only one statement. You must not use any if or if-else statement. 30. Complete the given program fragment (on Answer Sheet) so that the values of variables a and b are exchanged after your code is executed. END OF PAPER Page 11 of 11