1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.

Slides:



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

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Introduction to Computing Science and Programming I
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Structured programming
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
Chapter 2 Writing Simple Programs
Intro to Python Paul Martin. History Designed by Guido van Rossum Goal: “Combine remarkable power with very clear syntax” Very popular in science labs.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
More Functions CS303E: Elements of Computers and Programming.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
If statements while loop for loop
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Variables and Expressions CMSC 201 Chang (rev )
Announcements Project1 Due Wednesday September 21 st 4:30pm We will post instructions on how to turnin from home Note: you can always turn in from the.
CS101 Computer Programming I Chapter 4 Extra Examples.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Python Arithmetic Operators OperatorOperationDescription +AdditionAdd values on either side of the operator -SubtractionSubtract right hand operand from.
Lecture 14 – lists, for in loops to iterate through the elements of a list COMPSCI 1 1 Principles of Programming.
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Chapter 2 Writing Simple Programs
G. Pullaiah College of Engineering and Technology
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Introduction to Programming
Bill Tucker Austin Community College COSC 1315
Python: Control Structures
Basic operators - strings
User input We’ve seen how to use the standard output buffer
Arithmetic operations, decisions and looping
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Consecutive Integers: Numbers are one apart
CS1100 Computational Engineering
Suggested Layout ** Designed to be printed on white A3 paper.
Introduction to Programming Using Python PART 2
Summary Two basic concepts: variables and assignments Basic types:
Nate Brunelle Today: Conditional Decision Statements
Introduction to Programming
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Class 13 function example unstring if if else if elif else
Python Basics with Jupyter Notebook
Relational Operators.
COMPUTER PROGRAMMING SKILLS
CS 101 First Exam Review.
Peer Instruction 4 Control Loops.
Class code for pythonroom.com cchsp2cs
REPETITION Why Repetition?
COMPUTING.
Control 9 / 25 / 19.
Presentation transcript:

1 CS 177 Week 6 Recitation Slides Review for Midterm Exam

2 Announcements

Question 1 Q1. Which numbers are printed by the following loop: (A) 22 (B) 21 (C) 0 (D) 20 sum = 0 x = 10 while x > 1 : sum = sum + x x = x - 3 print (sum)

Question 1 How many iterations in total?  After 1 st iteration, x=7  After 2 nd, x=4  After 3 rd, x=1  Then exit the loop What is the value of sum in each iteration?  After 1 st, sum=0+(x=10)=10  After 2 nd, sum=10+(x=7)=17  After 3 rd, sum=17+(x=4)=21 4

Question 2 Q2. Consider the following code snippet. What is the output of calling: fun(10, 20, “BOB”)? (A) -10 (B) 30 (C) -1 (D) 0 5 def fun(a, b, c): if (a < b or c[0] == "A"): if(a < 0 and c[0] == "B"): return a + b else: print (a - b ) else: return -1

Question 2 After the function call, input arguments are initialized as follows:  a= 10, b=20, c=“BOB”  For 1 st if statement, since a<b is True, the expression is True  Then it evaluates if(a < 0 and c[0] == "B"):  It is False since a<0 is False  Then print a-b will be executed  Result is -10 6

Question 3 Lab 5 solution 7 def integerDecision(number): if number <=0: print("Input number is not positive.") elif number 0: print("Input number is less than 50.") elif number >=50 and number<=100: if number == 60: print("I FOUND 60.") else: print("Input number ",number, " is >= 50 and <=100") elif number > 100: print("Input number is greater than 100.") else: print("It's a strange number.")

Question 3 #TODO 1.01 #Test if the input number is less than or equal to 0 #TODO 1.02 #Test if the input number is less than 50 8 if number <=0: print("Input number is not positive.") elif number 0: print("Input number is less than 50.")

Question 3 #TODO 1.03 #Test if the input number is greater than or equal to 50, and less than or equal to 100, but not equal to 60 #1.04 #Inside the condition of 1.03, test if the number is equal to 60 9 elif number >=50 and number<=100: if number == 60: print("I FOUND 60.") else: print("Input number ",number, " is >= 50 and <=100")

Question 3 #TODO 1.05 #Test the input number is greater than elif number > 100: print("Input number is greater than 100.") else: print("It's a strange number.")

Question 4 Q8. Which of the following statements produce the same output? 1) print (11 + (13.0/2) + 2.5) 2) print ( // ) 3) print ( / ) 4) print (( )/ ) (A) 1), 2) and 3) (B) 1) and 2) (C) 1) and 3) (D) None of the above. 11

Question 4 First equation evaluates to 20 Second equation evaluates to 19.5 (13/2=6) Third equation evaluates to 20 Fourth equation evaluates to 14.5 Python will cast an int to double if the int is doing arithmetic operations (+,/,*,-) with another double value. Integer division will not round 12

Question 5 Q9. Which of the following statements (1-4 below) generate an error? (A) 1), 2) and 4) (B) 1) and 2) (C) 1) and 4) (D) 1), 2), 3) and 4) 13 myList = [0,1,2,3,4] myString = “ hello” 1) mylist[len(mylist)] 2) b + 6 = 17 3) c = "Hello" + "World" 4) myString[2] = ‘j’

Question 5 mylist[len(mylist)] will throw an error, since the valid index will be 0 to len(mylist)-1 b + 6 = 17 is wrong since there cannot be an expression on the assignment left hand side myString[2] = ‘j’ is wrong since you cannot change a python string 14

Question 6 Q14. What is the output of the following code? (A) good (B) It will produce no output (C) ok (D) It will produce an error 15 a = 15 b = 13 c = 12 d = 6 e = 10 if (a>b and d>4) or (e a+b): print ("good" ) else: print ("ok“)

Question 6 a>b is True (15>13) d>4 is True (6>4) The rest will not be evaluated good will be printed 16

Question 7 Q15. Consider the following five loops (i-v). Which loops will generate the same output? (i) (ii) 17 i = 0 while(i < 11): print (i) for i in range(0,10): print (i)

Question 7 (iii) (iv) (v) (A) i, ii, iii, iv (B) i, ii, iv, v (C) i, iii, iv (D) i, iii, v 18 i = -1 while(i < 10): i = i + 1 print (i) for i in range(10%10,10%11) : print (i) for i in range(10) : print (i)

Question 7 (i) will print 0~9 (ii) will print 0~10 (iii) will print 0~9 (iv) 10%10=0, 10%11=10, will print 0~9 (v) will print 0~10 19

Question 8 Q17. Considering the following code snippet, what sequence of numbers is printed? (A) All even numbers greater than 0 and less than 10 (B) All odd numbers greater than 0 and less than 10 (C) Every other odd number greater than 0 and less than 10 (D) Nothing 20 for i in range(1,10,2): if i % 2 == 0: print (i)

Question 8 for i in range(1,10,2): will loop through 1,3,5,7,9 None of them satisfy the condition i%2==0 Nothing will be printed 21

Question 9 Q19. Considering the following code snippet, what would the output be if you ran the function t4()? 22 x = 0 y = 10 def t1(): x = 1 def t2(): x = 2 def t3(): x = y

Question 9 (A) 4 (B) 2 (C) 1 (D) 10 Within each functions, x is just a local variable. Calling t1(), t2(), t3() will not change the local variable x in t4(). (A) is correct. Then how to change a global variable within a function? 23 def t4(): x = 4 t1() t2() t3() print (x)