Sign Up Free

Python Programming: True or False

True/False 22 questions Computer Science & Technology > Python by steven marone
Study this material interactively with flashcards, quizzes, and games on GabaBrain.
Study on GabaBrain

True/False (22)

Question 1
Python variables are statically typed, meaning their type is fixed once assigned and cannot be changed.
Correct Answer
False
Python uses dynamic typing, meaning the type of a variable is determined at runtime and can change if a new value of a different type is assigned to it.
Question 2
Strings in Python are immutable, meaning that once a string is created, its individual characters cannot be changed in place.
Correct Answer
True
Strings are immutable sequence types in Python. Operations that appear to modify a string actually create a new string object.
Question 3
The boolean value True evaluates to 1 when used in an arithmetic expression.
Correct Answer
True
In Python, True is an instance of the int subclass bool, and its integer value is 1. False has an integer value of 0.
Question 4
An 'elif' block can be used on its own in a conditional statement without a preceding 'if' block.
Correct Answer
False
An 'elif' block must always follow an 'if' block (or another 'elif' block) within the same conditional statement. It cannot stand alone.
Question 5
The 'and' logical operator in Python short-circuits, meaning it evaluates the second operand only if the first operand is true.
Correct Answer
True
If the first operand of 'and' is false, the expression is already determined to be false, so the second operand is not evaluated.
Question 6
Variables defined inside a function are always accessible from outside that function after the function has completed execution.
Correct Answer
False
Variables defined inside a function have local scope and are not accessible from outside the function once the function finishes, unless explicitly returned or declared global.
Question 7
A Python function can return multiple values, which are packed into a tuple automatically.
Correct Answer
True
While a function technically returns a single object, multiple values can be returned by packaging them into a tuple (or list, dictionary, etc.), which is then treated as a single return value.
Question 8
Lists in Python are immutable sequences, meaning their elements cannot be changed, added, or removed after the list is created.
Correct Answer
False
Lists are mutable sequences. Their elements can be changed, added, or removed after creation using methods like 'append()', 'pop()', or direct assignment.
Question 9
Dictionary keys must always be of the string data type.
Correct Answer
False
Dictionary keys can be any immutable data type, such as integers, floats, strings, or tuples. Mutable types like lists or other dictionaries cannot be keys.
Question 10
The 'append()' method for lists adds a new element to a specified index within the list.
Correct Answer
False
The 'append()' method always adds a new element to the end of the list. To insert an element at a specific index, the 'insert()' method should be used.
Question 11
In Python 3.7 and later, dictionaries preserve the order in which items are inserted.
Correct Answer
True
As of Python 3.7 (and CPython 3.6 implementation detail), dictionaries are guaranteed to maintain insertion order.
Question 12
The 'break' statement immediately terminates the current iteration of a loop and moves to the next iteration.
Correct Answer
False
The 'break' statement immediately terminates the entire loop, exiting it completely. The 'continue' statement terminates only the current iteration and proceeds to the next one.
Question 13
A 'for' loop in Python can directly iterate over the elements of a list without needing to use 'range()' to generate indices.
Correct Answer
True
'for' loops are designed to iterate directly over the elements of any iterable, such as lists, strings, and tuples.
Question 14
When opening a file in 'w' (write) mode, if the file already exists, Python will raise an error to prevent accidental overwriting.
Correct Answer
False
Opening a file in 'w' mode will truncate (empty) the file if it exists, or create a new file if it does not. It does not raise an error for existing files.
Question 15
It is considered good practice to use a 'with' statement when handling files because it automatically ensures the file is closed, even if errors occur.
Correct Answer
True
The 'with' statement creates a context manager that guarantees the file's 'close()' method is called automatically when the block is exited, regardless of whether an exception occurred.
Question 16
The 'read()' method, when called without any arguments on a file object, reads a single line from the file.
Correct Answer
False
When called without arguments, the 'read()' method reads the entire content of the file as a single string. The 'readline()' method reads a single line.
Question 17
A 'finally' block in a 'try-except' statement will always execute, regardless of whether an exception occurred or was handled.
Correct Answer
True
The 'finally' block is guaranteed to execute, making it suitable for cleanup actions like closing files or releasing resources.
Question 18
The 'except' block in a 'try-except' statement can catch multiple types of exceptions by listing them as a tuple.
Correct Answer
True
You can specify multiple exception types in a single 'except' clause by enclosing them in parentheses as a tuple, for example: 'except (ValueError, TypeError):'.
Question 19
Raising an exception using the 'raise' keyword immediately stops the program's execution if there is no corresponding 'try-except' block to handle it.
Correct Answer
True
If an exception is raised and not caught by an appropriate 'except' block higher up the call stack, the program will terminate and print a traceback.
Question 20
The 'len()' built-in function can be used to determine the number of digits in an integer.
Correct Answer
False
The 'len()' function works on sequences (like strings, lists, tuples) and collections (like dictionaries, sets) to return their number of items. It cannot be directly applied to an integer.
Question 21
The 'range(1, 5)' built-in function generates a sequence of numbers that includes both 1 and 5.
Correct Answer
False
The 'range()' function generates numbers up to, but not including, the stop value. So, 'range(1, 5)' generates 1, 2, 3, 4.
Question 22
The 'input()' built-in function always returns user input as a string, even if the user types numbers.
Correct Answer
True
The 'input()' function reads a line from input, converts it to a string (stripping the trailing newline), and returns that string. Type conversion (e.g., using 'int()') is necessary for numerical input.

Ready to study Python Programming: True or False?

Study with flashcards, play quiz games, challenge your friends, and track your progress.

Start Studying Free