Final Exam, Version 3 CSci 127: Introduction to Computer Science Hunter College, City University of New York

Similar documents
Final Exam, Version 3 CSci 127: Introduction to Computer Science Hunter College, City University of New York

Final Exam, Version 2 CSci 127: Introduction to Computer Science Hunter College, City University of New York

Final Exam, Version 1 CSci 127: Introduction to Computer Science Hunter College, City University of New York

Final Exam, Version 1 CSci 127: Introduction to Computer Science Hunter College, City University of New York

Final Exam, Version 2 CSci 127: Introduction to Computer Science Hunter College, City University of New York

Mock Final Exam CSci 127: Introduction to Computer Science Hunter College, City University of New York

Mock Final Exam CSci 127: Introduction to Computer Science Hunter College, City University of New York

Final Exam, Version 4 CSci 127: Introduction to Computer Science Hunter College, City University of New York

Mock Final Exam CSci 127: Introduction to Computer Science Hunter College, City University of New York

Sample Final Exam CSci 127: Introduction to Computer Science Hunter College, City University of New York

CSci 127: Introduction to Computer Science

The American University in Cairo Department of Computer Science & Engineeringt CSCI &09 Dr. KHALIL Exam-I Fall 2009

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, October 13, 2015

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

ARTIFICIAL INTELLIGENCE AND PYTHON

CS101 PLEDGED SPRING 2001

CSci 127: Introduction to Computer Science

Lecture 23: Pointer Arithmetic

CSci 127: Introduction to Computer Science

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Ch 6. Functions. Example: function calls function

Programming Language. Functions. Eng. Anis Nazer First Semester

2. For each of the regular expressions, give a string that will matches it:

Do not turn to the next page until the start of the exam.

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Increment and the While. Class 15

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

2. For each of the regular expressions, give a string that will matches it:

Name SECTION: 12:45 2:20. True or False (12 Points)

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Using the Matplotlib Library in Python 3

Name Section: M/W or T/TH. True or False (14 Points)

CSI33 Data Structures

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

In this chapter you will learn:

Skills Quiz - Python Edition Solutions

CSCI 111 Midterm 1, version A Exam Fall Solutions 09.00am 09.50am, Tuesday, November 24, 2015

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

INTERMEDIATE PYTHON FOR DATA SCIENCE. Comparison Operators

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

Introduction to Programming

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

Today in CS161. Lecture #7. Learn about. Rewrite our First Program. Create new Graphics Demos. If and else statements. Using if and else statements

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE Instructor: Final Exam Fall 2014

WARM UP LESSONS BARE BASICS

INTRODUCTION TO DATABASES IN PYTHON. Filtering and Targeting Data

Ch. 17: Linked Lists. Introduction to Linked Lists

Python Tutorial for CSE 446

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science

HW0 v3. October 2, CSE 252A Computer Vision I Fall Assignment 0

CSCI 111 Midterm 2 Exam Spring Solutions 09.00am 09.50am, Wednesday, May 04, 2016

EECS402 Lecture 02. Functions. Function Prototype

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Python Tutorial for CSE 446

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

1) You want to determine whether time has run out. The following code correctly implements this:

University of Dublin

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

CSCE 2014 Final Exam Spring Version A

CSci 127: Introduction to Computer Science

1 #include <iostream> 2 using namespace std; 3 4 // implementing the dynamic List ADT using Linked List 5 6 class Node{ 7 8 private: 9 int data; 10

Math 1MP3, final exam

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

CISC2000/ of 6

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

True or False (15 Points)

CPT101- Principles of Programming

Clustering to Reduce Spatial Data Set Size

Chapter 3 Problem Solving and the Computer

INTRODUCTION TO DATA VISUALIZATION WITH PYTHON. Working with 2D arrays

C++ Final Exam 2017/2018

INTRODUCTION TO COMPUTER SCIENCE - LAB

PS6-DCT-Soln-correction

CSCE 206: Structured Programming in C++

Computer Programming : C++

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Chapter 17 - Notes Recursion

Programming with Python

Shell / Python Tutorial. CS279 Autumn 2017 Rishi Bedi

VARIABLES & ASSIGNMENTS

Answer Key: CIS 166 Final Exam, Version 1, Spring 2015

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Transcription:

Final Exam, Version 3 CSci 127: Introduction to Computer Science Hunter College, City University of New York 22 May 2018 1. (a) What will the following Python code print: i. a = "one+two+three+four+five+six" print(a.count("+")) 5 ii. b = a.split("+") print(b[0]) one iii. num = b[-1].upper() print(num) SIX iv. for c in num: print(c.lower()) 0

s i x (b) Consider the following shell commands: $ ls nyc.txt p50.py p51.py p52.py i. What is the output for: $ ls *.py p50.py p51.py p52.py ii. What is the output for: $ ls *.py wc -l 3 iii. What is the output for: $ mkdir programs $ mv *.py programs $ ls wc -l 2 2. (a) After executing the Python code, write the name of the turtle: 1

i. which is purple: savannah ii. which is pink: import turtle turtle.colormode(255) brian = turtle.turtle() brian.color(255,0,0) savannah = turtle.turtle() savannah.color(1.0,0.0,1.0) calvin = turtle.turtle() calvin.color("#dddddd") olga = turtle.turtle() olga.color("#bb0000") olga iii. which is red: brian iv. which is gray: (b) Write the Python code for the following algorithm: calvin function decodemessage(numberlist) Create an empty message For each number in the list of numberlist code = 65 + ((number + 1) modulo 26) Convert the code to the corresponding Unicode character Concatenate the character to the beginning of the message Return the message #Shift message right by 1 and make upper case: def decodemessage(numberlist): mess = "" for number in numbers: code = 65 + ((numberlist + 1)%26) ch = chr(code) 2

mess = mess + ch return(mess) 3. (a) What is the value (True/False): in1 = True i. in2 = True out = in1 and in2 out = True ii. in1 = True in2 = True out = not in1 or (in2 and not in1) out = False iii. in1 = False in2 = True or not in1 in3 = in1 and in2 out = in1 and not in3 out = False iv. in1 = False in2 = False out = True (b) Design a circuit that implements the logical expression: ((in1 or in2) and (in1 or in3)) and (in2 or not in3) 3

4. (a) Draw the output for the function calls: i. ramble(tess,0) import turtle tess = turtle.turtle() tess.shape("turtle") def ramble(t,side): if side == 0: t.stamp() else: for i in range(side): t.forward(50) t.left(360/side) ii. ramble(tess,4) (b) For the following code: def v3(panda, lily): def start(): if panda + lily > 10: xiaojie = 5 return lily karoline = 11 else: nicky = v3(xiaojie,karoline) return -1 return nicky i. What are the formal parameters for v3(): panda, lily 4

ii. What are the formal parameters for start(): None iii. What does start() return: 11 5. Write a complete Python program that asks the user for numbers (separated by spaces) and prints how many end in 0. For example: If the user entered: 101 15 50 640 Your program should print: 2 #Counting numbers ending in 0 numbers = input( Enter numbers: ) num = numbers.count( 0 ) if numbers[-1] == 0 : num = num+1 print("number of numbers ending in 0 is", num) 6. Write a complete Python program that asks the user for the name of a.png (image) file and displays the lower right quarter of the image. For example if the image is hunterlogo.png (left), the displayed image would be (right): #Name: CSci 127 Teaching Staff #Date: Fall 2017 #This program loads an image, displays it, and then creates and displays # a new image that is only the lower right corner. #Import the packages for images and arrays: import matplotlib.pyplot as plt 5

import numpy as np inf = input( Enter file name: ) img = plt.imread(inf) #Read in image from inf height = img.shape[0] width = img.shape[1] print(height,width) img2 = img[height/2:, width/2:] plt.imshow(img2) plt.show() #Get height #Get width #Crop to lower right corner #Load our new image into pyplot #Show the image (waits until closed to continue) 7. Fill in the following functions that creates a graph of the fraction of population over time: getdata(): asks the user for the name of the CSV and returns a DataFrame of the contents, makefraction(): creates a column of the fraction of the two columns, and makegraph(): makes a plot of the x versus y columns specified. import pandas as pd import matplotlib.pyplot as plt def getdata(): Asks the user for the name of the CSV. Returns a DataFrame of the contents. fname = input( Enter current latitude: ) df = pd.read_csv(fname) return(df) def makefraction(df,top,total,frac): Makes a new column, frac, of df that is df[top]/df[total] Returns the DataFrame, df df[frac] = df[top]/df[total] return df def makegraph(df,xcol,ycol): Makes a pyplot plot of x versus y column in DataFrame df df.plot(x = xcol, y = ycol) 6

8. (a) What are the values of register, $s0 for the run of this MIPS program: #Sample program that loops from 20 down to 5 ADDI $s0, $zero, 20 #set s0 to 20 ADDI $s1, $zero, 5 #use to decrement counter, $s0 ADDI $s2, $zero, 5 AGAIN: SUB $s0, $s0, $s1 BEQ $s0, $s2, DONE J AGAIN DONE: #To break out of the loop Values of $s0: #use to compare for branching 20 15 10 5 (b) Write a MIPS program where the register, $s0 loops through the values: 3,6,9,12 #Program that loops from 3 up to 12, by threes ADDI $s0, $zero, 3 #set s0 to 2 ADDI $s1, $zero, 3 #use to increment counter, s0 ADDI $s2, $zero, 12 #set s2 to use for comparison AGAIN: ADD $s0, $s0, $s1 BEQ $s0, $s2, DONE J AGAIN DONE: #To break out of the loop 9. What is the output of the following C++ programs? (a) //Walt Whitman #include <iostream> using namespace std; int main() { cout << "I exist as\ni am,"; cout << "\nthat is enough" << endl; cout << "--W. Whitman"; I exist as I am, 7

that is enough --W. Whitman //Greetings! #include <iostream> using namespace std; int main() { cout << "Begin" << endl; int x = 2; (b) while (x > 0) { cout <<"Again\n"; x--; cout << "End" (c) Begin Again Again End //Pluses and minuses #include <iostream> using namespace std; int main() { int i, j; for (i = -2; i < 2; i++) { for (j = 0; j < 3; j++) if (i < 0) cout << "+"; else cout << "-"; cout << endl; +++ +++ --- --- 10. (a) Write a complete Python program that asks the user for their graduation year. If they enter a number before 1920, the program continues to ask for their graduation year. The 8

program then prints the year that was entered. #Input checking: year = int(input( Enter your graduation year: )) while year < 1920: year = int(input( Enter your graduation year: )) print("you entered:,year) (b) Write a complete C++ program that prints the change in population of the the United States: p = p + Bp Dp where p is the population, B is the birth rate of 12.4 births for every 1000 people ( 12.4 1000 ) each year, and D is the death rate of 8.4 for every 1000 people ( 8.4 1000 ). In 2017, the population of United States was 325.7 million. Your program should print expected population over the 10 years from 2017 to 2026. Each line should have: the year and the population (in millions). //NY State Population Growth #include <iostream> using namespace std; int main() { float p = 325.7; int year; cout << "Year\tPopulation (in Millions)\n"; for (year = 2017; year < 2027; year++) { cout << year << "\t" << p << "\n"; p = p + p*(12.4/1000) - p*(8.4/1000); return 0; 9