Python · Advanced 7
Working Like a Professional
Name
Class
Date
Mark / 30
Section A
Circle the letter of the correct answer. One mark each.
- 1Which naming style does PEP 8 recommend for a function?1
- AUPPER_CASE
- BcamelCase
- Csnake_case
- DPascalCase
- 2Why replace a 'magic number' such as 0.2 with a named constant?1
- AThe name explains what it means, and it can be changed in one place
- BConstants run faster
- CIt saves memory
- DPython requires it
- 3What does a doctest do?1
- ADocuments the tests
- BRuns the examples in a docstring and checks their output
- CTests the docs for spelling mistakes
- DCounts the lines in a file
- 4What does
if __name__ == '__main__':achieve?1- AIt stops the file being imported
- BThe code in it runs when the file is run directly, but not when it is imported
- CIt makes the file run twice
- DIt marks the main function for the compiler
- 5What is the purpose of a virtual environment?1
- ATo keep each project's libraries separate from others
- BTo encrypt the code
- CTo run Python in a browser
- DTo make programs run faster
- 6What is a commit in Git?1
- AA conflict between two branches
- BA request for a review
- CA copy of the project on a server
- DA saved snapshot of the project with a message
- 7What is a branch used for?1
- ADeleting old code
- BChecking spelling
- CSending code to a server
- DA separate line of work, so a feature can be developed without disturbing the working version
- 8Which should you do first when a program is too slow?1
- AMeasure it, to find which part is the bottleneck
- BBuy a faster computer
- CRemove the comments
- DRewrite it in a different language
- 9What does an exit code of 0 mean?1
- AThe program crashed
- BThe program finished successfully
- CThe program found no input
- DThe program did nothing
- 10In the version number 2.5.1, which number changes for a bug fix that adds no features?1
- AThe middle one, 5
- BThe first one, 2
- CThe last one, 1
- DNone of them
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1DescribeDescribe two features of readable program code and explain why readable code is important.4
- 2ExplainA student is developing a coursework project in a team of three. Explain two benefits of the team using version control.4
- 3ExplainExplain what the statement if __name__ == '__main__': does in a Python file, and why it is used.3
- 4ExplainA program is running too slowly. Explain why the programmer should use a profiler before changing any of the code.3
- 5DiscussA programmer is deciding whether to use a third party library to handle a complex task or to write the code themselves. Discuss the advantages and disadvantages of using the library.6
Mark scheme
Section A
Section B
1 Describe two features of readable program code and explain why readable code is important. 4
One mark for each of the following, to a maximum of 4.
- Meaningful names for variables, functions and classes
- Comments or docstrings that explain what the code does or why
- Other features such as consistent layout (indentation), short subprograms, and constants instead of magic numbers
- Readable code is easier to understand, test, debug and maintain, especially by someone other than the original programmer
Model answer. One feature is the use of meaningful names for variables, subprograms and classes, so that a reader can tell what each one holds or does. Another is comments and docstrings, which explain what the code does or why it was written that way, together with consistent indentation and layout and short subprograms that each do one job. Readable code is important because it is easier to understand, test, debug and maintain, especially for someone other than the original programmer, or for the same programmer returning months later.
2 A student is developing a coursework project in a team of three. Explain two benefits of the team using version control. 4
One mark for each of the following, to a maximum of 4.
- It keeps a history of every change, so an earlier working version can be restored if something goes wrong
- It shows who made each change and when, and why (the commit message)
- Team members can work on separate branches at the same time without overwriting each other's work
- Changes can then be merged, and conflicts identified, so that the work is combined safely
Model answer. Version control keeps a history of every change made to the project, so that an earlier working version can be restored if a change introduces a fault. It also records who made each change, when, and why, using the commit message. In a team, each member can work on a separate branch at the same time without overwriting anyone else's work, and the changes can later be merged, with any conflicts being identified, so the work is combined safely.
3 Explain what the statement if __name__ == '__main__': does in a Python file, and why it is used. 3
One mark for each of the following, to a maximum of 3.
- The code inside it only runs when the file is run directly as a program
- It does not run when the file is imported as a module into another program
- This allows the same file to be used both as a program and as a library of reusable functions
Model answer. The code inside the statement only runs when the file is run directly as a program. It does not run when the file is imported as a module by another program, because then the name of the module is not '__main__'. This allows the same file to be used both as a program and as a library of functions that other programs can import and reuse.
4 A program is running too slowly. Explain why the programmer should use a profiler before changing any of the code. 3
One mark for each of the following, to a maximum of 3.
- A profiler measures how long each part of the program takes and how often it is called
- This shows where the bottleneck is, because programmers are often wrong when they guess
- So effort is spent on the part that will make a real difference, instead of making other code harder to read for no benefit
Model answer. A profiler measures how long each part of the program takes and how many times each subprogram is called. This shows where the bottleneck really is, because programmers are often wrong when they guess which part is slow. The programmer's effort can then be spent on the part that will make a real difference, instead of wasting it on, and perhaps making harder to read, code that makes no noticeable difference to the speed.
5 A programmer is deciding whether to use a third party library to handle a complex task or to write the code themselves. Discuss the advantages and disadvantages of using the library. 6
One mark for each of the following, to a maximum of 6.
- It saves development time, because the code already exists
- A widely used library has been tested by many users, so it is likely to contain fewer faults than new code
- It may include expert knowledge, such as security or efficiency, that the programmer does not have
- Disadvantage: the project depends on code written by others, which might be abandoned, change or contain security flaws
- Disadvantage: it may add size and complexity, and the licence may limit how the program can be used
- A reasoned conclusion: use the library for a complex task, checking its licence and keeping it up to date
Model answer. Using the library saves development time because the code already exists. A widely used library has been tested by many users, so it is likely to contain fewer faults than new code written by the programmer, and it may include expert knowledge, for example about security or efficiency, which the programmer does not have. However, the project then depends on code written by other people, which might be abandoned, change in ways that break the program, or contain security flaws, and the library may add size and complexity, while its licence may limit how the program can be used. For a complex task, the programmer should use the library, as long as its licence is checked and it is kept up to date.