COMP102: Test July, 2006

Similar documents
COMP102: Test 1 Model Solutions

COMP102: Test 2: Part B Model Solutions

Model Solutions. COMP 102: Test May, 2016

COMP 102: Test , September 25 ** WITH SOLUTIONS **

Model Solutions. COMP 102: Test May, 2015

COMP102: Test. 26 April, 2006

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Midterm Examination (MTA)

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

COMP 102: Test , December?? ** WITH SOLUTIONS **

Model Solutions. COMP 102: Test May, 2014

Introduction to Programming: Test 2

Model Solutions. COMP 102: Test 2. 5 May, 2011

COMP102: Test Model Solutions

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

Model Solutions. COMP 102: Test May, 2012

Loops. CSE 114, Computer Science 1 Stony Brook University

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

5) (4 points) What is the value of the boolean variable equals after the following statement?

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

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

COMP 102: Test. 2017, May 15 ** WITH SOLUTIONS **

AP Computer Science Unit 1. Writing Programs Using BlueJ

CS 161. SAMPLE FINAL Fall 2014

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

EXAMINATIONS 2010 END-OF-YEAR COMP 102 INTRODUCTION TO COMPUTER PROGRAM DESIGN

Tutorial about Arrays

St. Edmund Preparatory High School Brooklyn, NY

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

COMP102: Test 1. Model Solutions

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

COMP 102: Test August, 2017

COMP102: Test. 31 August, 2005

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

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

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

Java Coding 3. Over & over again!

IST 297D Introduction to Application Programming Chapter 4 Problem Set. Name:

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

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

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Handout 4 Conditionals. Boolean Expressions.

Chapter 3. Selections

Model Solutions. COMP 102: Test March, 2015

COMP 102 : Test. 2017, Apr 3

COMP 102: Test 2 Model Solutions

Model Solutions. COMP 102: Test 1. 6 April, 2016

Example Program. public class ComputeArea {

COMP102: Test 2 Model Solutions

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

COMP102: Test. 18 April, 2005

Building Java Programs

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

ITERATION WEEK 4: EXMAPLES IN CLASS

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

CS111: PROGRAMMING LANGUAGE II

File class in Java. Scanner reminder. File methods 10/28/14. File Input and Output (Savitch, Chapter 10)

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

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

COMP 102: Test August, 2017

Object Oriented Programming. Java-Lecture 1

Over and Over Again GEEN163

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

4 WORKING WITH DATA TYPES AND OPERATIONS

Console Input and Output

Nested Loops. A loop can be nested inside another loop.

Introduction to Computer Science Unit 2. Exercises

Building Java Programs

Mr. Monroe s Guide to Mastering Java Syntax

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Model Solutions. COMP 102: Test March, 2014

COMP 202 Java in one week

Full file at

Dynamic Programming. See p of the text

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

There are some situations in which recursion can be massively inefficient. For example, the standard Fibonacci recursion Fib(n) = Fib(n-1) + Fib(n-2)

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2009

Peer Instruction 1. Elementary Programming

Date: Dr. Essam Halim

CS 101 Spring 2007 Midterm 2 Name: ID:

Model Solutions. COMP 103: Test May, 2013

BİLGE KÖROĞLU. Lecture Notes (May 2 4, 2007) METHOD DECOMPOSITION and ARRAYS

COMP 202. Java in one week

Solution Notes. COMP 151: Terms Test

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CS111: PROGRAMMING LANGUAGE II

CMPT 125: Lecture 4 Conditionals and Loops

Introduction to Java Applications

Conditional Programming

1. Boolean algebra. [6] 2. Constructing a circuit. [4] 3. Number representation [4] 4. Adders [4] 5. ALU [2] 6. Software [4]

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

Topic 12 more if/else, cumulative algorithms, printf

Full file at

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

Welcome! COMP s1. Programming Fundamentals

Chapter 5 Methods. public class FirstMethod { public static void main(string[] args) { double x= -2.0, y; for (int i = 1; i <= 5; i++ ) { y = f( x );

Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program.

Transcription:

Name:.................................. ID Number:............................ COMP102: Test 1 26 July, 2006 Instructions Time allowed: 45 minutes. Answer all the questions. There are 45 marks in total. Write your answers in the boxes in this test paper and hand in all sheets. If you think some question is unclear, ask for clarification. There is some Java documentation at the end of the test paper. Model solutions for part of Assignment 2 are also included at the end of the test paper. This test will contribute 5% of your final grade, but only if it helps your grade. Non-electronic translation dictionaries and calculators without a full set of alphabet keys are permitted. Questions Marks 1. Basic Java [13] 2. Defining a method [10] 3. Using Scanner for input [15] 4. Loops and Conditionals [7] TOTAL:

Question 1. Basic Java [13 marks] (a) [8 marks] For each of the following nine terms, find a corresponding element of the program below, and draw a labelled circle around the element. The first one is done as an example. 1. Class name: Question1 2. Method definition from public void doqn... to 3. Assignment statement a = 20.0; or b = n; or a = b; or b = (a + 15)/5; 4. Method call System.out.println(... n); or System.out.printf(... b) 5. Parameter declaration (int n) 6. Variable declaration double a or double b 7. Type int or double 8. Expression 20.0 or n or b or (a+15)/5 or n is now + n or... 9. String n is now or first is now %.2f second is now %.2f\n public class Question1{ public void doqn(int n){ double a = 20.0; double b = n; a = b; b = (a + 15)/ 5; System.out.println("n is now "+ n); System.out.printf("first is now %.2f second is now %.2f\n", a, b); (b) [5 marks] Suppose the doqn method above is called with an argument of 10, (eg, you call the method using BlueJ and enter 10 in the dialog box asking for the value of n). What will it print out? n is now 10 first is now 10.00 second is now 5.00 COMP 102 (Terms Test) 2 continued...

Question 2. Defining a Method [10 marks] Complete the following computeordercost method so that it prints out the total cost of an order of some number of CD s. The method should have one parameter an integer specifying the number of CD s in the order. The CD s cost $4.95 each. They are shipped in boxes that hold up to 10 CD s each. The shipping cost of each box is $3.40. The method should compute the number of boxes required, and the total cost of the order including both the cost of the CD s and the shipping cost. It should print out the result in a form such as: Total cost of 16 CDs = $86.00 public void computecost( int numitems){ double costcd = numitems 4.95; int numboxes = ((numitems 1) / 10) + 1; double shipping = numboxes 3.40; System.out.printf("Total cost of %d items = $%.2f\n", numitems, (cost+shipping)); // OR public void computecost( int numitems){ System.out.printf("Total cost of %d items = $%.2f\n", numitems, (numitems 4.95 + (((numitems 1) / 10) + 1) 3.40); // OR... COMP 102 (Terms Test) 3 continued...

Question 3. Using a Scanner [15 marks] (a) [6 marks] The following method will prompt the user for some values. What will the method print out if the user types the following line 3 6 2006 14 Wellington Jane in response to the first prompt public void doit (){ Scanner scan = new Scanner(System.in); System.out.print("Enter values: "); int n = scan.nextint (); n = n + scan.nextint (); System.out.println("answer1 = " + n); String y = scan.next(); int w = scan.nextint (); y = y + w; System.out.println("answer2 = " + y); String name = scan.next(); String city = scan.next(); System.out.println(name + " commutes to " + city); Enter values: 3 6 2006 14 Wellington Jane answer1 = 9 answer2 = 200614 Wellington commutes to Jane () (Question 3 continued on next page) COMP 102 (Terms Test) 4 continued...

(Question 3 continued) (b) [9 marks] Complete the following printtag method so that it asks the user to enter their name and then prints out a name tag like the one shown below. It should use a Scanner to read the name from the user. If the user typed the name Peter Smith, the nametag should look something like: +---------------------------+ HELLO, My name is Peter Smith +---------------------------+ public void printtag(){ Scanner scan = new Scanner(System.in); System.out.print("Enter name: "); String name = scan.nextline(); // watch out for names with two tokens! System.out.println(" +----------------------+"); System.out.println(" HELLO, My name is "); System.out.println(" "); System.out.printf (" %15s \n", name); System.out.println(" "); System.out.println(" +----------------------+"); COMP 102 (Terms Test) 5 continued...

Question 4. Loops and Conditionals (harder) [7 marks] (a) [5 marks] What will the following donums method print out if it is called with the arguments 23 and 7? public void donums(int n, int m){ System.out.println("doNums:"); int x = n; int y = m; int z = 0; while (x > 0){ if (x % 2 == 1){ System.out.println(y); z = z + y; x = x 1; else if (x % 2 == 0) { x = x / 2; y = y + y; System.out.printf("%d, %d => %d", n, m, z); donums: 7 14 28 112 23, 7 => 161 (b) [2 marks] Explain what the method computes and how. The method computes the product of its two arguments. Effectively, it converts the first argument to a binary number and multiplies the second number by all the 1 s in the first number as it goes. It is called the Russian Peasant method of multiplication The advantage is that you can multiply any two numbers if know how to add, to double, and to halve you don t need to know your times tables ******************************** COMP 102 (Terms Test) 6