Docsity

PYTHON PROGRAMMING: VARIABLES, DATA TYPES, AND EXPRESSIONS ACTUAL QUESTIONS AND CORRECT AN, Exams of Medicine

Toggle sidebar
bg1
1
PYTHON PROGRAMMING: VARIABLES, DATA TYPES, AND
EXPRESSIONS ACTUAL QUESTIONS AND CORRECT ANSWERS
1. Question: What is machine language?
Answer: CPU instructions represented in binary — sequences of 0's and 1's. The
ONLY
language a CPU can execute directly.
2. Question: What is assembly language?
Answer: CPU instructions written with mnemonics (e.g., add $r3, $r2, $r1). Easier
to read,
but equivalent to machine language.
3. Question: Name three high-level languages from lecture.
Answer: C, C++, Java, Python — much easier to use, but must be translated into
machine
language to run.
4. Question: Compiled language — when does translation happen?
Examples?
Answer: Translated ONCE, before running the code. Saves time. Examples: C,
C++, Java
(partially).
5. Question: Interpreted language — when does translation happen?
Examples?
Answer: Translated WHILE the code executes, every run. Can adapt to the
runtime
situation. Examples: Python, Basic, Java (partially).
6. Question: Is Python compiled or interpreted?
Answer: Interpreted — a high-level (scripting) language that needs an interpreter
to
execute.
7. Question: What is a Python interpreter?
Answer: A program that accepts Python commands and executes them.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download PYTHON PROGRAMMING: VARIABLES, DATA TYPES, AND EXPRESSIONS ACTUAL QUESTIONS AND CORRECT AN and more Exams Medicine in PDF only on Docsity!

PYTHON PROGRAMMING: VARIABLES, DATA TYPES, AND

EXPRESSIONS ACTUAL QUESTIONS AND CORRECT ANSWERS

  1. Question: What is machine language?

Answer: CPU instructions represented in binary — sequences of 0's and 1's. The ONLY

language a CPU can execute directly.

  1. Question: What is assembly language?

Answer: CPU instructions written with mnemonics (e.g., add $r3, $r2, $r1). Easier to read,

but equivalent to machine language.

  1. Question: Name three high-level languages from lecture.

Answer: C, C++, Java, Python — much easier to use, but must be translated into machine

language to run.

  1. Question: Compiled language — when does translation happen?

Examples?

Answer: Translated ONCE, before running the code. Saves time. Examples: C, C++, Java

(partially).

  1. Question: Interpreted language — when does translation happen?

Examples?

Answer: Translated WHILE the code executes, every run. Can adapt to the runtime

situation. Examples: Python, Basic, Java (partially).

  1. Question: Is Python compiled or interpreted?

Answer: Interpreted — a high-level (scripting) language that needs an interpreter to

execute.

  1. Question: What is a Python interpreter?

Answer: A program that accepts Python commands and executes them.

  1. Question: Interactive vs batch execution

Answer: Interactive: commands executed one at a time as they are typed into a shell.

Batch: many commands (a program) read from a file and executed in sequence.

  1. Question: What is IDLE?

Answer: The shell + text editor you get when installing Python from python.org (along

with the interpreter).

  1. Question: What does the >>> prompt mean?

Answer: The Python shell is waiting for you to type a command (interactive mode).

  1. Question: The two steps of =

Answer: 1) The expression is evaluated and its value is put into an object of the

appropriate type. 2) That object is assigned the name .

  1. Question: What happens if you use a variable before assigning it?

Answer: NameError: name '...' is not defined. A variable does not exist until it is assigned.

  1. Question: Legal characters in a variable name

Answer: a-z, A-Z, the underscore _, and digits 0-9. BUT the name cannot START with a

digit.

  1. Question: Why is 2x = 22 illegal? What error?

Answer: Names can't start with a digit — SyntaxError: invalid syntax.

  1. Question: Two conventions for multi-word variable names

Answer: Underscore delimiter (new_temp) or camelCase capitalization (newTemp).

  1. Question: Swap the values of a and b in ONE line

Answer: a, b = b, a

  1. Question: Swap a and b using a temp variable (3 lines)

Answer: tmp = b

b = a

a = tmp

  1. Question: min(3, 1, 8, -2, 5, -3, 0)

Answer: -

  1. Question: abs(-3.2)

Answer: 3.

  1. Question: In a mixed expression, which operator group is

evaluated first, next, last?

Answer: 1) Algebraic operators first, 2) comparison operators next, 3) Boolean

operators (and, or, not) last.

  1. Question: Full precedence chain (high to low)

Answer: ( ) > [ ] indexing > *

> unary +x, -x >

, /, //, % > +, - > in, not in > <, >, <=, >=, ==, != >

not > and > or

  1. Question: 2 + 4 == 2 * (9 / 3)

Answer: True — 6 == 6.0. An int and a float compare equal by VALUE.

  1. Question: How do you test 'x is divisible by n' in Python?

Answer: x % n == 0 (remainder is zero)

  1. Question: 284 % 2 == 0 and 284 % 3 == 0

Answer: False — 284 is even, but 284 % 3 is 2. (With 'or' instead, it's True.)

  1. Question: The six comparison operators

Answer: < > == != <= >= — they evaluate to True or False.

  1. Question: The three Boolean operators

Answer: and, or, not — evaluated LAST in a mixed expression.

  1. Question: 2 < 3 and 3 < 4

Answer: True (both sides True)

  1. Question: 4 == 5 or 3 < 4

Answer: True — 'or' needs only one True side.

  1. Question: not(3 < 4)

Answer: False

  1. Question: True + 0 and True + True

Answer: 1 and 2 — bools convert to ints (True = 1) in arithmetic.

  1. Question: What is a string value?

Answer: A sequence of characters enclosed in quotes (single or double).

  1. Question: Are strings mutable? What does pet = 'cod'; pet[2] = 'w'

raise?

Answer: NO — strings are IMMUTABLE. Item assignment raises TypeError: 'str' object

does not support item assignment.

  1. Question: 'rock' + ' ' + 'climbing'

Answer: 'rock climbing' — + concatenates strings.

  1. Question: 5 * 'ab'

Answer: 'ababababab' — * repeats a string.

  1. Question: 'o' in 'climbing'

Answer: False — no letter o in climbing. On STRINGS, 'in' checks substrings.

  1. Question: 'bi' in 'climbing'

Answer: True — 'bi' is a substring.

  1. Question: 'ant' in 'Anteater'

Answer: False — case matters! The string contains 'Ant', not 'ant'.

  1. Question: 'rock' > 'climbing'

Answer: True — string comparison is alphabetical, and 'r' comes after 'c'.

  1. Question: len('climbing')

Answer: 8

  1. Question: What index does the FIRST item of a sequence have?

Answer: 0 — the second has index 1, the third has index 2, ...

  1. Question: What does a NEGATIVE index mean?

Answer: Position from the end: the last item is -1, second to last is -2, etc.

  1. Question: s = 'Apple' — give s[0], s[4], s[-1], s[-5]

Answer: 'A', 'e', 'e', 'A'

  1. Question: 4 in [1, 2, 3]

Answer: False

  1. Question: 'do' in ['cat', 'dog']

Answer: False — on a LIST, 'in' matches WHOLE items, not substrings. ('dog' in the list is

True.)

  1. Question: len([1, [2, 3], 4])

Answer: 3 — the nested list counts as ONE item.

  1. Question: lst = [0, 1, 'two', 'three', [4, 'five']] — what are lst[4] and

lst[4][1]?

Answer: lst[4] is [4, 'five'] (the whole inner list); lst[4][1] is 'five'.

  1. Question: min([1,2,3]), max([1,2,3]), sum([1,2,3])

Answer: 1, 3, 6

  1. Question: lst.append(x)

Answer: Adds x to the END of lst. Modifies the list; RETURNS None.

  1. Question: lst.count(x)

Answer: RETURNS the number of times x occurs in lst. Does not modify the list.

  1. Question: lst.index(x)

Answer: RETURNS the index of the FIRST occurrence of x.

  1. Question: lst.pop()

Answer: Removes AND RETURNS the last item — the only mutator that gives back a

value.

  1. Question: lst.remove(x)

Answer: Removes the FIRST occurrence of the VALUE x (not index!). Returns None.

Raises ValueError if x isn't in the list.

  1. Question: lst.reverse()

Answer: Reverses the order of items IN PLACE. Returns None.

  1. Question: lst.sort()

Answer: Sorts items in increasing order IN PLACE. Returns None.

  1. Question: print(lst.sort()) displays what?

Answer: None — sort() modifies the list but returns nothing. Same for append, remove,

reverse.

  1. Question: Of append, count, index, pop, remove, reverse, sort —

which RETURN a value?

Answer: count, index, and pop. The others (append, remove, reverse, sort) return None

but modify the list. pop BOTH modifies and returns.

  1. Question: Function vs method — definition + one example of each

Answer: A function is called on its own: len(lst), sum(lst). A method must be called ON

an object with a dot: lst.append(7) — append() can't be called independently.

  1. Question: lst = [1, 2, 3]

lst.append(7)

lst.append(3)

What is lst and lst.count(3)?

Answer: [1, 2, 3, 7, 3] and count(3) is 2.

  1. Question: for loop — what does it do and what is the syntax?

Answer: Executes a code block once for every item of a sequence:

for in :

  1. Question: Looping for char in 'Apple': print(char) outputs what?

Answer: A, p, p, l, e — one character per line (items of a string are its characters).

  1. Question: range(5) produces which numbers?

Answer: 0, 1, 2, 3, 4 — it stops BEFORE 5. range(n) gives n numbers starting at 0.

  1. Question: age = input('age: ') # user types 17

if age < 18: ...

What error and why?

Answer: TypeError — '17' < 18 compares a str with an int, which isn't supported. Fix: wrap

input() with int().

  1. Question: Build the string: Marie, you will be 18 next year!

(name='Marie', age=17)

Answer: name + ', you will be ' + str(age + 1) + ' next year!' — str() is required; you can't

concatenate str + int.

  1. Question: One-way if statement structure

Answer: if :

— the non-indented line ALWAYS runs.

  1. Question: Two-way if statement structure

Answer: if :

else:

  1. Question: In an if/elif/else chain, how many branches run?

Answer: Exactly one — the FIRST condition that is True (top-down); else runs if none are

True.

  1. Question: Why must if/elif conditions be mutually exclusive

(explicitly or implicitly)?

Answer: Because only the first True branch runs. Test the most extreme condition FIRST

(t > 86 before t > 32), or make ranges explicit (86 >= t > 32).

  1. Question: def temperature(t):

Answer: if t > 32:

print('It is cool')

elif t > 86:

print('It is hot')

temperature(90) prints what? Why is it wrong? 'It is cool' — 90 > 32 is True so

the first branch fires; the elif is never checked. Fix: test t > 86 first.

  1. Question: Why is indentation critical after if/for?

Answer: Indentation determines which statements belong to the block. Indent

'Goodbye' and it only runs when the condition is True; unindent it and it always

runs.

  1. Question: What error does a missing colon after if x > 5 cause?

Answer: SyntaxError

  1. Question: General format of a function definition

Answer: def (<0 or more variables>):

  1. Question: print() vs return inside a function

Answer: return sends a value back so it can be used in expressions. print just displays

text — the function then returns None.