Docsity

Python Programming Basics: Variables, Strings, and Error Handling | Objective Assessment |, Exams of Public Health

Toggle sidebar
bg1
A+
Python Programming Basics: Variables, Strings, and Error
Handling | Objective Assessment | Questions and Answers
with Verified Solutions |
Question: Answer:
What is a computer program? A sequence of instructions that a computer can run.
Question: Answer:
Name two examples of computer
programs in everyday life.
Online music streaming and mobile apps.
Question: Answer:
What is the primary goal of the book? To teach introductory programming and problem
solving.
Question: Answer:
What does the statement print('Good
morning') do?
It instructs Python to output the message 'Good
morning' to the user.
Question: Answer:
What does the statement count = 0
do?
It assigns the integer 0 to the variable count.
Question: Answer:
Why is Python a popular programming
language?
It has many libraries for real work and its syntax is
concise and straightforward.
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Python Programming Basics: Variables, Strings, and Error Handling | Objective Assessment | and more Exams Public Health in PDF only on Docsity!

A+

Python Programming Basics: Variables, Strings, and Error

Handling | Objective Assessment | Questions and Answers

with Verified Solutions |

Question: Answer:

What is a computer program? A sequence of instructions that a computer can run.

Question: Answer:

Name two examples of computer programs in everyday life. Online music streaming and mobile apps.

Question: Answer:

What is the primary goal of the book? To teach introductory programming and problem solving.

Question: Answer:

What does the statement print('Good morning') do? It instructs Python to output the message 'Good morning' to the user.

Question: Answer:

What does the statement count = 0 do? It assigns the integer 0 to the variable count.

Question: Answer:

Why is Python a popular programming language? It has many libraries for real work and its syntax is concise and straightforward.

What is a library in programming? A collection of code that can be used in other programs.

Question: Answer:

What is the purpose of the print() function? To display output to the user.

Question: Answer:

What does the input() function do? It obtains user input.

Question: Answer:

What is the default separator for the print() function? A space character.

Question: Answer:

What does the end option in the print() function do? It customizes what is printed at the end of the output, defaulting to a newline character.

Question: Answer:

What is the output of print('555', '0123', sep='-')?

Question: Answer:

How does Python syntax compare to Java syntax? Python syntax is generally more concise and straightforward than Java syntax.

Question: Answer:

What is the first program traditionally written when learning a new language? Hello World program.

What is the role of error messages in programming? To help understand and debug mistakes in the code.

Question: Answer:

What is the importance of precision in programming? Being precise is essential for writing correct and functional code.

Question: Answer:

What does the statement song = input() do? It prompts the user to enter their favorite song.

Question: Answer:

What is the output of print('I like string beans.', end='')? It prints 'I like string beans.' and stays on the same line.

Question: Answer:

What is one of the learning objectives of section 1.2? To display output using the print() function.

Question: Answer:

What type of devices are considered computers? Smartphones, tablets, laptops, desktops, and servers.

Question: Answer:

What is the purpose of the input() function in Python? It reads one line of input from the user.

Question: Answer:

What does the assignment operator (=) do in Python? It sets a variable to a value.

What is a variable in programming? A name that refers to a value stored in memory.

Question: Answer:

What is a prompt in the context of the input() function? A short message indicating the program is waiting for input.

Question: Answer:

How do you correctly obtain and store user input in Python? By using the syntax: variable_name = input()

Question: Answer:

What will be displayed by print('You entered:', name) if name is assigned 'Sophia'? 'You entered: Sophia'

Question: Answer:

What is the output if the user enters 'six' as input for the variable number? 'Value = six'

Question: Answer:

What are the rules for naming variables in Python? Variable names can consist of letters, digits, and underscores, cannot start with a digit, and are case- sensitive.

Question: Answer:

Which of the following is a valid variable name: 'median', 'class', or 'import'? 'median' is valid; 'class' and 'import' are keywords.

What is the correct way to concatenate strings in Python? Using the + operator.

Question: Answer:

What is a common mistake when defining a string? Not enclosing the string in quotes.

Question: Answer:

Which of the following is an invalid string: 'Hello!', 29, or '7 days'? 29 is invalid because it is not enclosed in quotes.

Question: Answer:

What does Python's style guide recommend for variable naming? Using snake case (all lowercase with underscores).

Question: Answer:

What is the output of print('You say "Please" to be polite')? 'You say "Please" to be polite'

Question: Answer:

What is the output of print('I hope you aren't sad.')? 'I hope you aren't sad.'

Question: Answer:

How do you create a variable in Python? By using an assignment statement, e.g., variable_name = value.

Question: Answer:

What will be the output of print('Hello'

  • ' World')? 'Hello World'

What is the significance of keywords in Python? Keywords have special functions and cannot be used as variable names.

Question: Answer:

What is the output of the expression 1 + 0?

Question: Answer:

What does the concatenation operator (+) do in Python? It combines two or more strings sequentially.

Question: Answer:

What will be the result of the expression '1' + '0'?

Question: Answer:

What is the output of the code print('Your favorite color is ' + color + '!') if color is 'red'? 'Your favorite color is red!'

Question: Answer:

How do you assign the string 'one- sided' to a variable named holiday? holiday = 'one-' + 'sided'

Question: Answer:

What are the two basic number formats supported by Python? Integer and floating-point.

Question: Answer:

What is the output of print(x, type(x)) if x = 1? 1

What will be the output of print(z * 1.5) if z = 2?

Question: Answer:

What is the result of the expression (1 +

  1. ** 2 / 4?

Question: Answer:

What is the output of the expression 3.5 - 1.5?

Question: Answer:

What is the output of print(x / z) if x = 7 and z = 2?

Question: Answer:

What does the term 'traceback' refer to in Python error messages? It is a report of the location and type of error.

Question: Answer:

What will be the output of the code c = x - z; c = c + 1; print(c) if x = 7 and z = 2?

Question: Answer:

What will the program output if the user inputs 'Alan' and 'Turing'? 'Your first name is 4 letters long. Your last name is 6 letters long.'

Question: Answer:

What is the output of print('Hello' + ' World')? 'Hello World'

How do you define a string variable in Python? By assigning a value enclosed in quotes, e.g., string_a = 'value'.

Question: Answer:

What is the output of print('10' + '10')? '1010'

Question: Answer:

What is the output of the expression - ** 2?

Question: Answer:

What type of error occurs when a closing parenthesis is missing? SyntaxError: unexpected EOF while parsing

Question: Answer:

What does a SyntaxError: EOL while scanning string literal indicate? The closing quote marks are missing.

Question: Answer:

What is a NameError? It occurs when a variable name is not defined, often due to a spelling mistake.

Question: Answer:

What does an IndentationError: unexpected indent mean? It indicates that there is an unexpected space at the start of the line.

Question: Answer:

What is the purpose of comments in Python code? To explain what the code is doing and improve code readability.

What is the significance of the blank line in code? It helps to separate different sections of the code for better readability.

Question: Answer:

What is an example of a well- formatted comment?

Get the user's preferences

Question: Answer:

What should a blank line in code indicate? A separation between different logical sections of the code.

Question: Answer:

What does the error message 'NameError: name 'wird' is not defined' suggest? The spelling of the variable 'word' is incorrect.

Question: Answer:

What is the purpose of using a docstring in a program? To summarize the program's purpose or usage.