Uploaded by Nicole Wang

hw1

advertisement
hw1.md
2023-09-10
Part 1
1. What early computing device uses beads on sticks to represent numeric values?
O Difference Engine O Napier's bones O slide rule ✔ abacus
2. Who designed the early computing machines known as the Difference Engine and the Analytical
Engine?
✔ Charles Babbage O George Boole O John Napier O Ada Lovelace
3. Who wrote the "GOTO statement considered harmful" letter in 1968 that ushered in the age of
structured programming?
O Bill Gates ✔ Edsger Dijkstra O Alan Turing O OGrace Murray Hopper
4. Which of the following programming languages was developed first?
✔ FORTRAN O C O COBOL O Java
FORTRAN, which stands for FORmula TRANslation, was created by John Backus and his team at
IBM in 1957. C was created by Dennis Ritchie at Bell Labs in 19723, COBOL was created by a
committee of computer experts in 19594, and Java was created by James Gosling at Sun
Microsystems in 1995.
5. What IBM computer system, which can answer guestions posed in Enalish, plaved Jeopardy (and won)
acainst two pastchampions?
O Android ✔ Watson O Deep Blue O Eliza
Part 2
Reorder the terms on the left to match the descriptions on the right. Once you start reordering, be sure to
finish beforechecking your answers.
syntax -> grammar rules
semantics -> meaning
programming language -> notational system used to write a program
program -> sequence of instructions
abstraction -> a simpler way to describe a complex idea
imperative -> uses step-by-step procedures
logic -> uses axioms, rules and queries
object-oriented -> uses models and encapsulation
functional -> uses mathematics and recursion
interpreter -> runs code as it translates it
compiler -> translates source code to target code
cross-compiler -> translates from one language to another
1 / 11
hw1.md
2023-09-10
Part 3
1. A programming paradigm is a how a programming language can be grouped with other languages
based on similarcharacteristics.
✔ True O False
Some of the most common programming paradigms are:
Imperative:
This paradigm uses step-by-step instructions to manipulate data and
control the flow of the program. Examples of imperative languages are C,
Java, and Python.
Functional:
This paradigm uses mathematical functions to transform data and avoid
side effects. Examples of functional languages are Haskell, Lisp, and Scala.
Logic:
This paradigm uses facts, rules, and queries to express logic and
reasoning. Examples of logic languages are Prolog, Datalog, and ASP.
Object-oriented:
This paradigm uses objects, classes, and inheritance to model data and
behavior. Examples of object-oriented languages are C++, Ruby, and
Smalltalk.
2. It doesn't matter which general-purpose language you use since they all can do the same things
O True ✔ False
Different general-purpose languages have different strengths and weaknesses, and they may not
be equally suitable for every task.
For example, some languages may be faster, more expressive, more portable,
more readable, or more compatible than others. Some languages may also have
more support, libraries, frameworks, or tools available for specific domains
or platforms. Therefore, choosing a language depends on various factors,
such as the requirements, preferences, and goals of the project and the
programmer.
3. There are only four programming paradigms you really need to consider.
O True ✔ False
https://en.wikipedia.org/wiki/Programming_paradigm https://www.freecodecamp.org/news/anintroduction-to-programming-paradigms/
4. Many object-oriented languages are also imperative languages
2 / 11
hw1.md
2023-09-10
✔ True O False
Many object-oriented languages are also imperative languages, meaning that they use step-bystep instructions to manipulate data and control the flow of the program. For example, C++,
Java, and Python are all object-oriented languages that support imperative programming.
However, not all object-oriented languages are imperative, and not all imperative languages are
object-oriented. For example, Smalltalk is an object-oriented language that is more declarative
than imperative, and C is an imperative language that is not object-oriented.
Object-oriented programming (OOP) is a programming paradigm that uses objects, classes, and
inheritance to model data and behavior. Objects are instances of classes that have attributes
(data) and methods (functions) associated with them. Classes are blueprints that define the
common properties and behaviors of a group of objects. Inheritance is a mechanism that allows
classes to inherit attributes and methods from other classes, forming a hierarchy of classes.
Imperative programming is a programming paradigm that uses statements to change the state
of the program and variables to store data. Statements are commands that tell the computer
what to do, such as assignments, conditionals, loops, and function calls. Variables are names that
refer to values stored in memory.
5. What are two good choices of programming language to create mobile applications?
O JavaScript and C++ ✔ Java and Swift O Fortran and SQL O Batman and Robin O They are all
appropriate for mobile app development
Part 4
1. Python variables are created using an assignment statement.
✔ True O False
2. The value of a variable can change throughout a program.
✔ True O False
3. Which of the following is NOT a valid Python identifier?
O place1 O first_place O FIRST_PLACE ✔ 1stPlace
4. Which of the following identifiers follows the convention for naming Python variables?
O totalValue ✔ total_value O TOTAL_VALUE O Total_Value
This identifier follows the convention for naming Python variables, which is to use snake case.
Snake case has all words lowercase separated by underscores.
This makes the variable name more readable and consistent with the Python style guide. The
other identifiers are either using camel case, pascal case, or upper case with underscores, which
are not recommended for Python variables.
5. In dynamically typed languages, variables are declared to store a specific type of data.
3 / 11
hw1.md
2023-09-10
O True ✔ False
6. Python variables that represent integers are NOT object reference variables
O True ✔ False
Python variables that represent integers are also object reference variables. In Python, everything
is an object, including integers, strings, lists, functions, and classes.
A Python variable is a symbolic name that is a reference or pointer to an object. When you
assign a value to a variable, you are actually creating an object that holds the value and then
making the variable point to that object. For example, when you write x = 10, you are creating
an int object with the value 10 and making x refer to it.
You can verify this by using the id() function, which returns the unique identifier of an object in
memory.
Part 5
1. Which of the following identifiers follows the naming convention for a Python constant?
O maxPenalties ✔ MAX_PENALTIES O MAXPENALTIES O Max_Penalties
2. Python constants cannot be created for floating-point values.
O True ✔ False
3. Constants automatically convert between units of measure.
O True ✔ False
4. Which of the following is NOT a reason to use a constant?
O Constants prevent inadvertent programming errors O Constants convey more meaning than literals.
O Constants make maintenance tasks easier and safer. ✔ Constants minimize the need for input
validation.
This is not a reason to use a constant, because input validation is the process of checking if the
user's input is valid, correct, and secure¹. Constants have nothing to do with input validation, as
they are fixed values that are defined by the programmer and cannot be changed by the user.
Input validation is still necessary regardless of whether constants are used or not.
The other options are valid reasons to use a constant, according to the web search results.
Constants prevent inadvertent programming errors, because they prevent the programmer from
accidentally changing the value of a constant or using an incorrect value. Constants convey more
meaning than literals, because they give a name to a value that describes its purpose or role in
the program. Constants make maintenance tasks easier and safer, because they allow the
programmer to change the value of a constant in one place and have it reflected throughout the
program, without having to search and replace every occurrence of the literal value.
5. Which of the following is NOT a constant from the Python Standard Library?
4 / 11
hw1.md
2023-09-10
O tkinter.NE O math.pi ✔ conversions.KILOMETERS_PER_MILE O math.e
conversions.KILOMETERS_PER_MILE is not a constant from the Python Standard Library, but
from a third-party library called conversions, which provides functions and constants for
converting between different units of measure¹. The other options are valid constants from the
Python Standard Library, according to the web search results.
tkinter.NE is a constant that represents the northeast direction for placing widgets in a grid
layout using the tkinter module, which is a standard interface to the Tk GUI toolkit²³.
math.pi is a constant that holds the value of pi, which is the ratio of the circumference of a circle
to its diameter, using the math module, which provides access to the mathematical functions
defined by the C standard⁴ .
math.e is a constant that holds the value of Euler's number, which is the base of natural
logarithms, using the math module⁴ .
Part 6
1. For Python, the term data type can be used interchangeably with what other term?
✔ class O index O sequence O object
For Python, the term data type can be used interchangeably with the term class, because
everything in Python is an object, and data types are actually classes that define the properties
and behaviors of the objects. For example, int, str, list, and dict are all data types and classes in
Python, and variables are instances or objects of these classes. Python also allows users to create
their own custom data types or classes using the class keyword.
2. Which category of data types represents an ordered collection of values?
O numeric O boolean O dictionary ✔ sequence
Some common characteristics of sequences are:
They can be indexed and sliced using square brackets [] and a colon :. For example, seq[0] returns the
first element of the sequence, and seq[1:3] returns a subsequence from the second to the third
element.
They can be iterated over using a for loop or a comprehension. For example, for x in seq: print(x) prints
each element of the sequence, and [x.upper() for x in seq] returns a new sequence with each element
converted to uppercase.
They can be concatenated using the + operator and repeated using the * operator. For example, seq1 +
seq2 returns a new sequence that contains the elements of seq1 followed by the elements of seq2, and
seq * 3 returns a new sequence that repeats the elements of seq three times.
They support membership testing using the in and not in operators. For example, x in seq returns True
if x is an element of seq, and False otherwise.
They have some built-in functions that can be applied to them, such as len(), min(), max(), sum(),
sorted(), reversed(), and any(). For example, len(seq) returns the number of elements in seq, and
sorted(seq) returns a new sequence that contains the elements of seq in ascending order.
Some differences between sequences are:
5 / 11
hw1.md
2023-09-10
Strings, bytes, and tuples are immutable, which means they cannot be modified after they are created.
Lists and byte arrays are mutable, which means they can be changed by adding, removing, or replacing
elements. Range objects are immutable, but they do not store their elements explicitly; instead, they
generate them on demand.
Strings and bytes can only store values of a certain type: Unicode characters for strings and integers
from 0 to 255 for bytes. Lists, tuples, and byte arrays can store values of any type, including other
sequences. Range objects can only store integers that follow a certain pattern: start, stop, and step.
Strings have some methods that are specific to them, such as lower(), upper(), find(), replace(), split(),
join(), and format(). Bytes and byte arrays have some methods that are specific to them, such as
decode(), encode(), hex(), fromhex(), and count(). Lists have some methods that are specific to them,
such as append(), insert(), remove(), pop(), sort(), and reverse().
3. All Python data types are immutable.
O True ✔ False
That is incorrect. Not all Python data types are immutable. Immutable data types are those that
cannot be changed after they are created, while mutable data types are those that can be
modified in place. According to the web search results, Python has both immutable and mutable
data types.
Some examples of immutable data types in Python are:
Numbers (Integer, Float, Complex, Decimal, Rational & Booleans)
Tuples
Strings
Frozen Sets
Some examples of mutable data types in Python are:
Lists
Dictionaries
Sets
Byte Arrays
4. Python is dynamically typed, meaning each variable can only store one type of value.
O True ✔ False
That is incorrect. Python is dynamically typed, meaning that the type of a variable can change
over its lifetime, depending on the value assigned to it. For example, a variable that initially
stores a string can later store an integer, a list, or any other type of object. Python does not
require explicit type declarations for variables, and it determines the type of a variable at runtime
based on the value assigned to it. This is different from statically typed languages, where
variables have fixed types that cannot change and must be declared before use.
5. The Python type conversion functions are actually what kind of functions
O predicate functions O user-defined types ✔ object constructors O iterators
6 / 11
hw1.md
2023-09-10
6. Reorder the terms on the left to match the descriptions on the right, Once you start reorderinq, be sure
tochecking your answers.
set->An unordered collection of unique values
list->A list of values.
dict->An unordered collection of key/value pairs
bool->A true or false value
int->A whole number.
str->A character string.
tuple->An immutable list of values.
Part 7
1. Python code can be run from interactively or from a file
✔ True O False
2. IDLE is a development environment for Python programs
✔ True O False
3. The Python shell is a window in which you can experiment with Python commands.
✔ True O False
4. You must use IDLE to run a Python program.
O True ✔ False
Part 8
1. An IDE combines the tools needed to write, edit, and run code
✔ True O False
2. IDLE provides an interactive shell in which commands can be entered
✔ True O False
3. What is syntax highlighting?
O Displaying program output in particular colors O Wrapping program output in colored boxes ✔
Showing certain elements of program code in different colors O Displaying each function of a program
in a different color
4. A debugger lets you to evaluate the value of variables during program execution.
✔ True O False
Part 9
7 / 11
hw1.md
2023-09-10
1. Thonny is best described as which of the following?
O Debugger O Package manager O Text editor ✔ Integrated Development Environment (IDE) O
Command shell
An IDE is a software that provides a set of tools for writing, editing, running, and debugging
code. Thonny is a free and open-source IDE that is designed for beginners who want to learn
Python. It has many features, such as a built-in debugger, a variable explorer, a package
manager, and syntax highlighting.
2. The output of a Python program run in Thonny appears in the shell window.
✔ True O False
The shell window is the part of the Thonny user interface where you can see the results of your
code execution, as well as enter Python commands interactively. The shell window is also known
as the interactive interpreter, because it allows you to interact with the Python language in real
time.
3. Thonny displays code using syntax highlighting.
✔ True O False
Syntax highlighting is the feature of showing certain elements of code in different colors, such as
keywords, variables, strings, and comments. Syntax highlighting can help programmers to read,
write, and debug code more easily, as it can highlight the structure, syntax, and semantics of the
code. Syntax highlighting can also help to spot errors, such as missing parentheses, quotes, or
brackets. Syntax highlighting is supported by most IDEs, including Thonny.
4. Running a program that contains errors will cause the Thonny development environment to terminate
O True ✔ False
Running a program that contains errors will not cause Thonny to terminate, but it will display the
error messages in the shell window and highlight the line where the error occurred in the code
editor window. Thonny also provides a simple debugger that can help you to find and fix errors
in your code. You can use the debugger to run your code step by step, inspect the values of
variables, and set breakpoints.
5. Thonny has a package manager that lets you install and update external Python packages.
✔ True O False
A package is a collection of modules that provide additional functionality for Python programs.
For example, you can use packages to create graphical user interfaces, access databases,
perform scientific computations, and more. Thonny has a built-in package manager that allows
you to install and update packages from PyPI (the Python Package Index), which is the official
repository of Python packages. You can access the package manager from the Tools menu in
Thonny's user interface.
Part 10
8 / 11
hw1.md
2023-09-10
1. What is the Python shell?
O An integrated development environment for developing Python programs O An alternative to IDLE or
Thonny. ✔ A window in which Python statements and expressions are executed immediately. O A help
system for Python programming.
The Python shell is also known as the interactive interpreter, because it allows you to interact
with the Python language in real time. You can use the Python shell to test your code, explore
new features, or learn new concepts. The Python shell is one of the features of IDLE, but you can
also access it from the command line or from other IDEs. To start the Python shell, you just need
to type python in the command prompt or terminal, and then press enter. You will see a prompt
that looks like this: >>>. This means that the Python shell is ready to accept your input. You can
type any valid Python expression or statement after the prompt, and then press enter to execute
it. The Python shell will display the output or any error messages below your input. To exit the
Python shell, you can type exit() or press Ctrl+D.
2. The Python shell is great for exploring Python language features
✔ True O False
The Python shell is a convenient and powerful tool for learning and experimenting with Python.
You can use the Python shell to try out different syntax, operators, functions, modules, data
types, and more. You can also use the Python shell to perform calculations, manipulate strings,
create lists, dictionaries, and other data structures, and even write simple programs. The Python
shell gives you immediate feedback on your code, so you can see how it works and what it does.
The Python shell is also useful for debugging and testing your code, as you can isolate and
examine specific parts of your program without running the whole file.
3. You must use a print statement to display the result of an expression in the Python shell.
O True ✔ False
You do not need to use a print statement to display the result of an expression in the Python
shell. The Python shell will automatically print the value of any expression that you enter, unless
it is None or an assignment statement. For example, if you type 2 + 3 in the Python shell and
press enter, you will see 5 as the output. However, if you type x = 2 + 3 in the Python shell and
press enter, you will not see any output, because this is an assignment statement that stores the
value of 2 + 3 in the variable x. If you want to see the value of x, you can simply type x in the
next line and press enter.
4. You can store a value in a variable in the Python shell.
✔ True O False
You can store a value in a variable in the Python shell using the assignment operator (=). A
variable is a name that refers to a value in memory. You can use variables to store and
manipulate data in your program. For example, if you type name = "Alice" in the Python shell
and press enter, you will create a variable called name that refers to the string "Alice". You can
then use the variable name in other expressions or statements, such as print("Hello, " +
9 / 11
hw1.md
2023-09-10
name) or len(name). Variables that you create in the Python shell are stored in a special
namespace called __main__, which is accessible until you exit the shell or restart it.
5. The Python shell has an integrated help system.
✔ True O False
The Python shell has an integrated help system that provides documentation on various aspects
of the Python language and its standard library modules. You can access the help system by
typing help() in the Python shell and pressing enter. This will open an interactive help session
that allows you to enter topics or keywords to search for information. For example, if you type
help(print) in the help session and press enter, you will see a detailed description of the builtin function print, including its syntax, parameters, return value, and examples. You can also type
help("topics") in the help session and press enter to see a list of available topics that cover
various concepts and features of Python. To exit the help session, you can type quit or press
Ctrl+D.
Part 11
1. The code you write is called Python bytecode.
O True ✔ False
The code you write is called Python source code, which is a human-readable text file that follows
the syntax and semantics of the Python language. Python bytecode is the intermediate
representation of your source code that is generated by the Python compiler when you run your
program. Python bytecode is a binary file that contains a sequence of instructions for the Python
virtual machine, which is an abstract machine that executes your code. You can use the dis
module to inspect the bytecode of your source code.
2. To run a Python program, the interpreter combines your code with any library code your program uses.
O True ✔ False
To run a Python program, the interpreter first compiles your code and any library code your
program imports into bytecode files, which have the extension .pyc or .pyo. Then, the interpreter
loads these bytecode files into memory and executes them one by one using the Python virtual
machine. The interpreter does not merge or concatenate your code and library code into a
single file or object.
3. Python bytecode is not associated with any particular type of computer processor
✔ True O False
Python bytecode is designed to be portable and independent of any specific hardware or
operating system. This means that you can run the same bytecode file on any machine that has a
compatible version of the Python interpreter installed, regardless of the underlying architecture
or platform. This is one of the advantages of using a virtual machine, as it abstracts away the
details of the physical machine and provides a uniform execution environment for your code.
10 / 11
hw1.md
2023-09-10
4. The Python package installer is called pip
✔ True O False
pip is the recommended tool for installing Python packages from the Python Package Index
(PyPI) and other sources. A package is a bundle of software that provides additional functionality
for Python programs, such as modules, libraries, frameworks, etc. You can use pip to search,
download, install, update, and uninstall packages using simple commands. pip is included with
most recent versions of Python, or you can install it separately if needed.
11 / 11
Download