Xin Liu Feb 11, 2013. * Part 1 1. What value does s have, after this code is run? s = '*' s = s + s s = s + s + s (A) '**' (B) '***' (C) '****' (D) '*****'

Slides:



Advertisements
Similar presentations
What type of data can a variable hold?
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
Python  By: Ben Blake, Andrew Dzambo, Paul Flanagan.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Introduction to: Python and OpenSesame Part I. Python A high-level programming language It can do a lot of things We will use python in this course in.
Lecture 20 – Test revision COMPSCI 101 Principles of Programming.
Programming Training Main Points: - Problems with repetitions. - Discuss some important algorithms.
If statements while loop for loop
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Xin Liu Jan 30, * By default, input() read a string from keyboard * This can be changed with an indirection operator ‘
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
Xin Liu Feb 4, * Use midterm questions for previous 231/217 midterms for practices * ams
Python Conditionals chapter 5
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
Decision Structures, String Comparison, Nested Structures
Boolean Expressions and Logical Operators CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington Credits:
Week 4 : Function with parameters and return values.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
Loops ! We've seen variables change in-place before: [ x*6 for x in range(8) ] [ 0, 6, 12, 18, 24, 30, 36, 42 ] remember range ?
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
Programming Seminar 2009 Night 0. Why we’re holding this seminar You’re probably not a computer science major – Or even anything remotely close (e.g.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
Decision Making CMSC 201 Chang (rev ).
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Control flow Ruth Anderson UW CSE 160 Spring
Introduction to Computing Using Python Repetition: the for loop  Execution control structures  for loop – iterating over a sequence  range() function.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Control flow Ruth Anderson UW CSE 160 Winter
Python Flow of Control CS 4320, SPRING Iteration The ‘for’ loop is good for stepping through lists The code below will print each element in the.
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
Scientific Programming in Python -- Cheat Sheet
Fundamentals of Programming I Overview of Programming
Introduction to Python
Python unit_4 review Tue/Wed, Dec 1-2
Making Choices with if Statements
Ruth Anderson UW CSE 160 Winter 2017
Selection CIS 40 – Introduction to Programming in Python
Types, Truth, and Expressions (Part 2)
Multiple Selections (ELIF Statements)
Ruth Anderson UW CSE 140 Winter 2014
Michael Ernst UW CSE 140 Winter 2013
Control flow : if statements
Programming Training Main Points: - Problems with repetitions.
To Start Create C:\100 folder
Flow Control Presented by: Md Fashiar Rahman.
Suggested Layout ** Designed to be printed on white A3 paper.
Introduction to Programming Using Python PART 2
Suppose I want to add all the even integers from 1 to 100 (inclusive)
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Python ~ Control Structure
Python Basics with Jupyter Notebook
CS 101 First Exam Review.
Types, Truth, and Expressions
Data Types and Maths Programming Guides.
While Loops in Python.
Class code for pythonroom.com cchsp2cs
COMPUTING.
Presentation transcript:

Xin Liu Feb 11, 2013

*

Part 1 1. What value does s have, after this code is run? s = '*' s = s + s s = s + s + s (A) '**' (B) '***' (C) '****' (D) '*****' (E) '******'

2. What does this code print when run? sum = 0 for i in range(3): for j in range(2): sum = sum + j print(sum) (A) 1 (B) 2 (C) 3 (D) 4 (E) 5

3. What does this code print when run? sum = 0 for i in range(3): for j in range(i): sum = sum + j print(sum) (A) 0 (B) 1 (C) 2 (D) 3 (E) 4

4. What does this code print when run? print( * 3) (A) 17 (B) 18 (C) 21 (D) 39 (E) What does this code print when run? print('1' * 2) (A) 1 (B) 2 (C) 11 (D) 12 (E) Nothing; there is an error

6. What is the value of x after this code is run? x = 1 / 2 (A) 0 (B) 1 (C) 0.0 (D) 0.5 (E) What is the value of x after this code is run? x = 1 // 2 (A) 0 (B) 1 (C) 0.0 (D) 0.5 (E) 1.0

8. Which of the following are valid variable names? _ foo if 4ever MO0S3 (A) _ and MO0S3 (B) All except 4ever (C) All except _ (D) foo and if (E) foo, if, and 4ever

9. Real numbers are the same as floating point numbers. (A) True (B) False 10. How many of the following statements evaluate to False ? 'ab' not in 'abc' 'ab' in 'abc' '9' < '10' 5.0 > -2 (A) 0 (B) 1 (C) 2 (D) 3 (E) 4

11. How many of the following statements evaluate to True ? 2 < 4 3 >= 5 42 == 42 'foo' != 'bar’ (A) 0 (B) 1 (C) 2 (D) 3 (E) 4

12. How many times is 1 printed when this code is run? for i in range(2): for j in range(4): x = 1 print(x) (A) 0 (B) 2 (C) 4 (D) 6 (E) 8

13. How many times is 1 printed when this code is run? for i in range(2): for j in range(4): x = 1 print(x) (A) 0 (B) 2 (C) 4 (D) 6 (E) 8

14. How many times is X printed when this code is run? for i in range(5): if i != 2: print('X') (A) 0 (B) 1 (C) 2 (D) 3 (E) How many times is X printed when this code is run? for i in range(5): if (i == 2) or (i-1 < 1): print('X') (A) 0 (B) 1 (C) 2 (D) 3 (E) 4

16. What input would cause this program to print X ? s = input() b = 'e' not in s if not b: print('X') (A) Batman (B) hobbit (C) orc (D) elf (E) Gandalf

17. Under what conditions is baz printed? Assume A and B are Boolean variables that have been assigned a value. if A: print('foo') elif B: print('bar') else: print('baz') (A) One of A or B is True (B) A is True (C) B is True (D) Both A and B are True (E) Neither A nor B are True

18. What does this code draw when run? import turtle turtle.lt(90) turtle.fd(75) turtle.rt(45) turtle.bk(100) turtle.fd(50) turtle.rt(45) turtle.fd(40) (A) Nothing listed here (B) A triangle (C) The letter “N” (D) The letter “A” (E) Nothing; there is an error when it is run

19. Just as dividing by zero in math is somewhat problematic, an error is caused if a Python program tries to divide by zero. Does this program cause an error when it is run? b = True or 1 / 0 (A) Yes (B) No

Part 2 There is a Python module called foo. In that module is a function named bar that you can call. bar takes one integer as an argument. Starting with the following: AAA BBB s = '42' CCC 20. What should AAA be replaced with? (A) import math (B) import turtle (C) Nothing 21. What should BBB be replaced with? (A) import foo (B) import time (C) Nothing

22. What should CCC be replaced with? (A) foo.bar(s) (B) bar(s) (C) foo.bar(int(s)) (D) foo.bar(integer(s)) (E) bar(int(s))

Part 3 Write a loop to print the numbers 1 through 10, inclusive. Starting with the following: n = 10 for i in AAA: BBB 23. What should AAA be replaced with? (A) range(n) (B) n (C) range(len(n)) (D) range(1, n) (E) range(1, n+1) 24. What should BBB be replaced with? (A) print i (B) print n (C) print(i) (D) print(n)

Part 4 Write code to draw a horizontal row of six boxes. Starting with the following: AAA BBB for i in CCC: turtle.pu() turtle.fd(75) turtle.pd() for j in DDD: turtle.fd(50) turtle.lt(90) 25. What should AAA be replaced with? (A) import math (B) import turtle (C) Nothing 26. What should BBB be replaced with? (A) n = 5 (B) n = 6 (C) Nothing

27. What should CCC be replaced with? (A)4 (B)(B) n (C) n-1 (D) range(n) (E) range(n-1) 28. What should DDD be replaced with? (A) 4 (B) n (C) range(4) (D) range(n)

Part 5 Convert a 2 - 2ab + c π-1 to Python code. Starting with the following: import math AAA - BBB + CCC 29. What should AAA be replaced with? (A) a * 2 (B) a ^ 2 (C) a ** What should BBB be replaced with? (A) 2ab (B) 2 * ab (C) 2a * b (D) 2 * a * b 31. What should CCC be replaced with? (A) c * pi-1 (B) c * math.pi-1 (C) c ** (pi-1) (D) c ^ (math.pi-1) (E) c ** (math.pi-1)

Part 6 The input to the programs in this section is the following: What does the following program print when it is run with the above input? input() a = float(input()) input() b = int(input()) print(a * b) (A) 8.0 (B) 14 (C) 14.0 (D) 18 (E) 18.0

33. What does the following program print when it is run with the above input? x = 0 for i in range(3): n = int(input()) x = x + n print(x) (A) 11 (B) 15 (C) 22 (D) Nothing; there is an error 34. What does the following program print when it is run with the above input? x = 0 for i in range(4): n = int(input()) x = x * n print(x) (A) 0 (B) 18 (C) 72 (D) 504 (E) Nothing; there is an error