COMP 110 Practice Exercises for Midterm Solutions

Similar documents
CS159. Nathan Sprague. November 9, 2015

Scripting for Multimedia LECTURE 3: INTRODUCING JAVASCRIPT

CSE Winter 2015 Quiz 2 Solutions

Exercise 1: Basic HTML and JavaScript

cs1114 REVIEW of details test closed laptop period

An introduction to Scheme

Specifying, Creating, and Comparing Algorithms. And another look at JavaScript

c122mar413.notebook March 06, 2013

REPETITION CONTROL STRUCTURE LOGO

Sorting Pearson Education, Inc. All rights reserved.

JavaScript Basics. The Big Picture

COMS 469: Interactive Media II

Introduction to Programming in Turing. Input, Output, and Variables

Dublin Unified School District Suggested Pacing Guide for Grade 2 Text: Scott Foresman-Addison Wesley envision Math

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Boot Camp JavaScript Sioux, March 31, 2011

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

C/C++ Programming Lecture 18 Name:

Primitive Data, Variables, and Expressions; Simple Conditional Execution

CSE331 Winter 2014, Midterm Examination February 12, 2014

Data Structures and Algorithms Key to Homework 1

Excel 2010 Functions. 4/18/2011 Archdiocese of Chicago Mike Riley

Nested Loops. Chapter 11

Microsoft Excel. Part 2: Calculations & Functions. Department of Computer Science Faculty of Science Chiang Mai University

SQL CHEAT SHEET. created by Tomi Mester

1. What is the minimum number of bits needed to store a single piece of data representing: a. An integer between 0 and 100?

Factoring. Factor: Change an addition expression into a multiplication expression.

Discrete Structures Lecture 15

CS 115 Lecture 4. More Python; testing software. Neil Moore

CCBC Math 081 Order of Operations Section 1.7. Step 2: Exponents and Roots Simplify any numbers being raised to a power and any numbers under the

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

Prerequisites: Read all chapters through Chapter 4 in the textbook before attempting this lab. Read through this entire assignment before you begin.

8/2/10. Looking for something COMP 10 EXPLORING COMPUTER SCIENCE. Where is the book Modern Interiors? Lecture 7 Searching and Sorting TODAY'S OUTLINE

CMPT 120 Control Structures in Python. Summer 2012 Instructor: Hassan Khosravi

n! = 1 * 2 * 3 * 4 * * (n-1) * n

UNIVERSITY OF CAMBRIDGE INTERNATIONAL EXAMINATIONS General Certificate of Education Advanced Subsidiary Level and Advanced Level COMPUTING

CIS 110 Introduction to Computer Programming Summer 2016 Midterm. Recitation # (e.g., 201):

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

Introduction. two of the most fundamental concepts in computer science are, given an array of values:

CSC 160 Introduction to Programming for Non-majors. Lecture 4 - Looping Around Loops I: Counting Loops. Why loops?

Chapter 3. More Flow of Control. Copyright 2008 Pearson Addison-Wesley. All rights reserved.

CS 112: Intro to Comp Prog

8. Control statements

1.1 - Functions, Domain, and Range

Name Section: M/W T/TH Number Definition Matching (6 Points)

CSE331 Winter 2014, Midterm Examination February 12, 2014

Gateway Regional School District VERTICAL ARTICULATION OF MATHEMATICS STANDARDS Grades K-4

Name Section: M/W T/TH Number Definition Matching (8 Points)

(Refer Slide Time: 01.26)

Chapter 1 An Introduction to Computer Science. INVITATION TO Computer Science 1

COMP171 Data Structures and Algorithms Fall 2006 Midterm Examination

T H E I N T E R A C T I V E S H E L L

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

1.1 - Functions, Domain, and Range

Client-Side Web Technologies. JavaScript Part I

Assessment - Unit 3 lessons 16-21

PROGRESSION IS HIGHLIGHTED IN THE FOLLOWING DOCUMENT VIA BOLDED TEXT. MATHEMATICAL PROCESSES

Marking rubric for Assignment #2

SELECTION IDIOMS. Here is a summary of appropriate selection idioms: Selection Idioms. Action Condition Construct to Use. Sequential if statements

CSC 1351: Exam 2: Midterm

LABORATORY. 3 Representing Numbers OBJECTIVE REFERENCES. Learn how negative numbers and real numbers are encoded inside computers.

Lab 7: Change Calculation Engine

Full file at

Lab 6B Coin Collection

Iterative Searching and Sorting

Occupied versus Unoccupied

Variables and Typing

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Instructions PLEASE READ (notice bold and underlined phrases)

NO CALCULATOR ON ANYTHING EXCEPT WHERE NOTED

Review: Repetition Structure

The divide and conquer strategy has three basic parts. For a given problem of size n,

Matlab- Command Window Operations, Scalars and Arrays

AP Computer Science Unit 3. Programs

Welcome to the Primitives and Expressions Lab!

Chapter 6. Decision and Control Statements

7 Control Structures, Logical Statements

Algorithm Analysis. Algorithm Analysis 1

CMPS 11 Introduction to Programming Midterm 1 Review Problems

Creating the Fee Schedule

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

Lab Exercise 1 Using EGit and JUnit

COMP284 Practical 6 JavaScript (1)

Objective A Identify the numerator and the denominator of a fraction. When the numerator is less than the denominator, it is a(n) fraction.

Introduction to TURING

Section 1: True / False (2 points each, 30 pts total)

PIC 40A. Lecture 10: JS: Wrapper objects, Input and Output, Control structures, random numbers. Copyright 2011 Jukka Virtanen UCLA 1 04/24/17

What is Java Script? Writing to The HTML Document. What Can JavaScript do? CMPT 165: Java Script

19 Much that I bound, I could not free; Much that I freed returned to me. Lee Wilson Dodd

CS1073 Exam 3, Fall 2009 page 1. Important test instructions code fragments

Announcements. Project 5 is on the street. Second part is essay questions for CoS teaming requirements.

COSC 122 Computer Fluency. Iteration and Arrays. Dr. Ramon Lawrence University of British Columbia Okanagan

CSE341 Spring 2017, Final Examination June 8, 2017

Selections. CSE 114, Computer Science 1 Stony Brook University

Odd-Numbered Answers to Exercise Set 1.1: Numbers

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

CARLETON UNIVERSITY. Laboratory 2.0

Transcription:

1 COMP 110 Practice Exercises for Midterm Solutions Answers are highlighted in blue color. Evaluate each of the following Javascript expressions and show the value. 1. 9*3 27 2. value is + 50 value is 50 3. 17 % 5 2 4. 5 % 17 5 5. 5/10 0.5 6. (4 == 4) true 7. (4!= 5) true 8. (7 <= 8) true 9. Math.ceil(x) Math.floor(x) 1 Convert these binary numbers into decimal numbers. 10. 110111 55 11. 101011 43 12. 10100 20 Convert these decimal values into binary numbers. 13. 55 110111 14. 43 101011 15. 20 10100 Write javascript code to do the following. 16. Alert "Hello world." alert("hello world"); 17. Read a number (using prompt) and display it using alert. var n = prompt("please enter a number."); alert("your number is "+ n); 18. Read two numbers and display their product. var n1 = prompt("please enter a number.");

2 var n2 = prompt("please enter another number."); alert("the product of "+n1+" and "+n2+" is "+ n1*n2); 19. Read two numbers and display their sum. What problem did you encounter? var n1 = 1*prompt("Please enter a number."); var n2 = 1*prompt("Please enter another number."); alert("the sum of "+n1+" and "+n2+" is "+ n1+n2); If you leave out multiplying the numbers by 1, you will get concatenation instead of addition since prompt returns a string. 20. Read in two numbers and display the larger. var n1 = 1*prompt("Please enter a number."); var n2 = 1*prompt("Please enter another number."); if (n1 > n2) else alert(n1+" is the larger."); alert(n2+" is the larger."); If you leave out the multiplication by 1, you will get strange answers since prompt returns a string. For example, while the number 100 is greater than the number 99, the string "99" is greater than the string "100". 21. Read in two numbers and display them in ascending order. var n1 = 1*prompt("Please enter a number."); var n2 = 1*prompt("Please enter another number."); if (n1 > n2) else alert(n2+" "+n1); alert(n1+" "+n2); 22. Use a loop to display the numbers 0 through 5, each in a separate alert window. for (var i=0; i<=5; i++) alert(i); 23. Use a loop to display the numbers 0 through 5 in a single alert window.

3 var s=""; // Create empty string. for (var i=0; i<=5; i++) alert(s); s = s + i + " "; // Add next integer plus a space. 24. Use a loop to display the numbers in the range 0 20 that are multiples of 3. // Solution 1: Generate all numbers and test each. for (var i=0; i<=20; i++) if (i%3==0) alert(i); // Display those that are // divisible by 3. // Solution 2: Generate only multiples of 3. for (var i=0; i<=20; i=i+3) alert(i); // Only multiples of 3 are generated. 25. Use a loop to display the integers 9 through 0 in descending order. for (var i=9; i>=0; i--) alert(i); 26. Prompt the user for a number in the range 0 100. If the number is out of range, display an error message and prompt again until a valid number is entered. Assume the user enters a number each time. // Solution 1: Put the test in the while statement. var n = prompt("please enter a number in the range 0 100"); while (n<0 n>100) alert(n +" is out of range. Try again."); n = prompt("please enter a number in the range 0 100");

4 // Solution 2: Put the test inside the loop. while (true) var n = prompt("please enter a number in the range 0 100"); if (n>=0 && n<=100) break; alert(n +" is out of range. Try again."); 27. Repeat previous exercise, but this time allow for the possibility that the user enters something that is not a number. Hint: the built in function isnan(x) returns true if x is not a number. It returns false if x is a number. Sort of backwards, but that's what's available. // Solution 1: put the test in the while statement. var n = prompt("please enter a number in the range 0 100"); while (isnan(n) n<0 n>100) alert(n +" is out of range or not a number. Try again."); n = prompt("please enter a number in the range 0 100"); // Solution 2: put the test inside the loop. while (true) var n = prompt("please enter a number in the range 0 100"); if (!isnan(n) && n>=0 && n<=100) break; alert(n +" is out of range or not a number. Try again."); 28. Prompt for an integer, then display the sum of the integers from 0 through the number entered. For example, if you enter 10, then display 55 which is the sum of 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10. // Get the upper bound. var max = 1*prompt ("Please enter an integer."); var sum = 0; for (var i=0; i<=max; i++)

5 sum = sum + i; // Add i to the running sum. alert("the sum of 0 through "+max+" is "+sum); 29. Prompt for an integer, then display the average of the integers from 0 through the number entered. For example, if you enter 10, then display 5 which is the average of (0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10)/11. // Get the upper bound. var max = 1*prompt ("Please enter an integer."); var sum = 0; for (var i=0; i<=max; i++) sum = sum + i; // Add i to the running sum. alert("the average of 0 through "+max+" is "+(sum/(max+1))); Write a function to do each of the following. 30. greet(); displays "Hello world" function greet() alert("hello world"); 31. sum(n); displays the sum of 0+1+2+ +n. // Sum the integers 0 n. function sum(n) var tot=0; for (var i=0;i<=n;i++) tot=tot+i; alert("the sum of 0 through "+n+" is "+tot); 32. isvalid(n) returns true if n is a number 0 100. // Is the parameter between 0 and 100? function isvalid(n) // Restriction: none // Errors checked for: n not a number;

6 // n out of range. if (isnan(n)) return false; // Not a number. return n>=0 && n<=100; // Range test. 33. isinteger(n) returns true is n is an integer 0 100. Hint: use isvalid. // Is n an integer in 0... 100? function isinteger(n) // Restriction: none // Errors checked for: n not a number; n out of range; // n not an integer. if (!isvalid(n)) return false // Not a number or out of range. // If we get to this point, n must be a number in range. return (Math.floor(n)==n) // Integer test. Code tracing: 34. Trace the following code by showing the values of the 3 variables in the table on the right, for each line of code that is executed (after the line is executed): x y z // Start of execution var x = 5; var y = 10; var z = 7; 5 5 5 Undefined 10 10 Undefined Undefined 7 x = (y+z)/2; y = 8; z = (x-y)/2; 8.5 10 7 8.5 8 7 8.5 8 0.25 35. Trace the following program by showing the text of the alert messages that are displayed when it runs. There are no errors. // Start of execution

7 var x =5; var y =1; while (x > 0) 4 4 3 12 2 24 1 24 0 0 var i; x = x-1; y = y*x; alert(x + " " + y); var count =0; for (i =0; i < 11; i++) if (i < 3 7 <i) count++; alert(count + " i " + i + " range 1"); else if (i == 5) count++; alert(count + " i " + i + " range 2"); if ( 2<= i && i < 7) 1 i 0 range 1 2 i 1 range 1 3 i 2 range 1 4 i 2 range 3 5 i 3 range 3 count++; alert(count + " i " + i + " range 3");

8 6 i 4 range 3 7 i 5 range 2 8 i 5 range 3 9 i 6 range 3 10 i 8 range 1 11 i 9 range 1 12 i 10 range 1 36. What is the output displayed by each of the following code fragments? for (var i=0; i<10; i++) alert(i); 10 alerts with the values 0 9. for (var i=10; i<10; i++) alert(i); Nothing. The loop condition fails immediately. for (var i=10; i>=0; i--) alert(i) 11 alerts with the values 10 down to 0. for (var i=0; i<5; i++) for (var j=0; j<3; j++) alert(i +" "+ j); 15 alerts with the following values.

9 0 0 0 1 0 2 1 0 1 1 1 2 2 0 2 1 2 2 3 0 3 1 3 2 4 0 4 1 4 2 Remember, when one loop is inside another, the inner loop runs to completion for every step of the outer loop. for (var i=0; i<5; i++) for (var j=i; j<5; j++) alert(i +" "+j); 15 alerts with the following values. Note that the inner loop starts at a different value each time. 0 0 0 1 0 2 0 3 0 4

10 1 1 1 2 1 3 1 4 2 2 2 3 2 4 3 3 3 4 4 4 37. What does this code do? var count=0; for (var half=0; half<=2; half++) for (var qtr=0; qtr<=4; qtr++) for (var dime=0; dime<=10; dime++) for (var nick=0; nick<=20; nick++) for (penny=0; penny<=100; penny++) if (50*half + 25*qtr + 10*dime + 5*nick + penny == 100) alert(count); count++; It counts and displays the number of different way to make $1.00 using half dollars, quarters, dimes, nickels, and pennies. It tests 349,965 different combinations to find the 292 combinations that add up to exactly $1.00.