Python for Rookies. Example Examination Paper



Similar documents
CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Moving from CS 61A Scheme to CS 61B Java

north seattle community college

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Laboratory Assignments of OBJECT ORIENTED METHODOLOGY & PROGRAMMING (USING C++) [IT 553]

Welcome to Introduction to programming in Python

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

MATHEMATICS Y3 Using and applying mathematics 3810 Solve mathematical puzzles and investigate. Equipment MathSphere

Name Spaces. Introduction into Python Python 5: Classes, Exceptions, Generators and more. Classes: Example. Classes: Briefest Introduction

Turtle Power. Introduction: Python. In this project, you ll learn how to use a turtle to draw awesome shapes and patterns. Activity Checklist

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Introduction to Python

Exercise 1: Python Language Basics

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Pseudo code Tutorial and Exercises Teacher s Version

VISUAL ALGEBRA FOR COLLEGE STUDENTS. Laurie J. Burton Western Oregon University

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Programming Exercises

Data Structures and Algorithms Written Examination

Section IV.1: Recursive Algorithms and Recursion Trees

Informatica e Sistemi in Tempo Reale

Chapter 13 - Inheritance

Lecture 15: Inheritance

LINEAR INEQUALITIES. less than, < 2x + 5 x 3 less than or equal to, greater than, > 3x 2 x 6 greater than or equal to,

Python Lists and Loops

Exercise 4 Learning Python language fundamentals

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

Chapter 11 Number Theory

VHDL Test Bench Tutorial

61A Lecture 16. Friday, October 11

Goal: Practice writing pseudocode and understand how pseudocode translates to real code.

**Unedited Draft** Arithmetic Revisited Lesson 4: Part 3: Multiplying Mixed Numbers

Accentuate the Negative: Homework Examples from ACE

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Self-review 9.3 What is PyUnit? PyUnit is the unit testing framework that comes as standard issue with the Python system.

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

Homework 2. A 4*4 image with 16 pixels Borders unaltered. Color of B2 = Average color of (B1,A2,B3,C2) A1 A2 A3 A4 B1 B2 B3 B4 C1 C2 C3 C4 D1 D2 D3 D4

Introduction to programming moway

Obfuscation: know your enemy

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Playing with Numbers

50 Computer Science MI-SG-FLD050-02

Sources: On the Web: Slides will be available on:

Quosal Form Designer Training Documentation

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

PYTHON Basics

2667A - Introduction to Programming

J a v a Quiz (Unit 3, Test 0 Practice)

Java Application Developer Certificate Program Competencies

Positional Numbering System

PIC 10A. Lecture 7: Graphics II and intro to the if statement

PROG0101 Fundamentals of Programming PROG0101 FUNDAMENTALS OF PROGRAMMING. Chapter 3 Algorithms

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

2.6 Exponents and Order of Operations

Introduction to Python

Part 1 Foundations of object orientation

Visual Logic Instructions and Assignments

Introduction to Computer Science I Spring 2014 Mid-term exam Solutions

Advanced Programming with LEGO NXT MindStorms

Math Workshop October 2010 Fractions and Repeating Decimals

Basic Java Syntax. Program Structure

Curriculum Map. Discipline: Computer Science Course: C++

The 2010 British Informatics Olympiad

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

2 SYSTEM DESCRIPTION TECHNIQUES

Problem Solving Basics and Computer Programming

CS 100 Introduction to Computer Science Lab 1 - Playing with Python Due September 21/23, 11:55 PM

BCS2B02: OOP Concepts and Data Structures Using C++

Grade 6 Math Circles. Binary and Beyond

6. Standard Algorithms

CIS 192: Lecture 10 Web Development with Flask

Chapter 4 State Machines

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

Regular Expressions and Automata using Haskell

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CSCE 110 Programming I Basics of Python: Variables, Expressions, and Input/Output

Statements and Control Flow

Introduction to Python

Charles Dierbach. Wiley

Specimen 2015 am/pm Time allowed: 1hr 30mins

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Dynamic Programming Problem Set Partial Solution CMPSC 465

Analysis of Binary Search algorithm and Selection Sort algorithm

Python Objects. Charles Severance

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Java Interview Questions and Answers

The Smalltalk Programming Language. Beatrice Åkerblom

The C Programming Language course syllabus associate level

CS 111 Classes I 1. Software Organization View to this point:

Programming and Software Development CTAG Alignments

CS408 Animation Software Design Sample Exam Questions (Second Half)

Glossary of Object Oriented Terms

3. Mathematical Induction

Transcription:

Python for Rookies Example Examination Paper Instructions to Students: Time Allowed: 2 hours. This is Open Book Examination. All questions carry 25 marks. There are 5 questions in this exam. You should answer any 4 questions.

Question 1. This question tests your understanding of Object Oriented Programming. The following code defines the start of a class to represent bank accounts: class BankAccount(object): interest_rate = 0.3 def init (self, name, number, balance): self.name = name self.number = number self.balance = balance return a) Name the class variables and the instance variables in the given code. [5 marks] b) Add instance methods called deposit() and withdraw() which increase and decrease the balance of the account. Make sure the withdraw() method doesn't allow the account to go into overdraft. Add a third method called add_interest() which adds interest to the balance (the interest should be the interest rate multiplied by the current balance). [10 marks] c) Create a subclass of BankAccount called StudentAccount. Every StudentAccount should have an overdraft limit of 1000. Write a constructor for the new class. Override the withdraw() method to make sure that students can withdraw money up to their overdraft limits. [10 marks]

Question 2. This question tests your ability to manipulate strings. A palindromic word is one that reads the same backwards as forwards. Hence the words hello and peel are not palindromes, but the words peep, deed and dad are palindromes. a) Create a class called Palindrome. [2 marks] b) In your Palindrome class, create a method called reverse() which takes a string argument. Your method should return the reverse of the argument as a string. For example, if the argument is Foobar then your method should return raboof. [8 marks] c) Create a second method in Palindrome called ispalindrome() which takes a string argument. This method should return True if the argument is a palindrome and False otherwise. [5 marks] HINT: Probably the easiest way to do this is to use the result of your reverse() method and compare the string against its reverse. d) Write some code to test your new Palindrome class and print out results of your testing to the user. Give some consideration to what sort of strings you might want to use for your testing. [10 marks]

Question 3. This question tests your understanding of iteration (e.g. loops and generators). Remember, you will not be marked down for small errors in syntax or typos, so long as you demonstrate that you understand how iteration works in programming languages. a) A program to sum the number of integers from 1 to a given number n. [4 marks] b) A program which sums the contents of a integer list or array. [5 marks] c) Translate the following for loop into a while loop (which does the same thing as the for loop): for i in range(1,10): print "i = ", i [8 marks] d) Translate the following while loop into a for loop (which does the same thing as the while loop): i = 20 while (i > 0): print "i = ", i i =- 1 [8 marks]

Question 4. This question tests your ability to construct algorithms. Your answers can be in English, pseudo-code or Python. You may have already used a language like Logo or the Python turtle module, which allows you to program a simple robot which can move forwards and backwards or rotate left and right. The remainder of this question requires you to consider how you might design algorithms to instruct the turtle to perform useful tasks. a) How would you tell the turtle to move in a complete circle? [5 marks] b) Imagine that your turtle is now equipped with sensors which can tell if they are over a white pixel or a black pixel (see the screenshot below). Your mission is to move around the screen and find a black pixel. How would you tell the turtle to do this? More marks will be awarded for answers that cope well with difficult test cases. For example, a terrain that is all-white except for a single black pixel in a corner. [5 marks] c) The screenshot above shows that the turtle is placed on a track. Each of the three red dots on the turtle is one of it's sensors. In the picture, one sensor is over a black pixel and two are over white pixels (see the diagram on the bottom right of the screenshot). The turtle is currently at the start of the track. Describe (in English) how you would instruct the turtle to move around the track, given that it can move forwards and backwards, rotate left and right and tell whether it's sensors are over black or white pixels. [15 marks]

Question 5. This questions tests your understanding of recursion, which is a fundamental concept in programming. a) What is a recursive function? [2 marks] b) The following iterative function returns the sum of all elements in a list. For example, given the list [1, 2, 3] the function will return 1+2+3 or 6. Re-write the function as a recursive function. def sum(arg): result = 0 for i in arg: result += i return result [5 marks] c) The following code implements a recursive function in Python called foobar. What does the foobar function do? Write a line of code which calls the foobar function with a suitable argument and state what the return value will be. def foobar(arg): if arg == []: return arg else: return foobar(arg[1:]) + [ arg[0] ] [8 marks] d) The following code implements two mutually recursive functions functions which call each other: def ise(n): if n==0: return True else: return iso(n-1) def iso(n): if n==0: return False else: return ise(n-1) What will the following function calls return: iso(3) iso(2) ise(3) ise(2) What do the functions iso and ise do? [10 marks]