Python EXAM 1 - Spring 2024

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What symbol is used in Python to start a comment?

#

Which operator is used for floor division in Python?

//

What does the append() method do in Python lists?

Adds a new element to the end of the list

What will happen if the condition in an if statement evaluates to False?

Any subsequent else or elif blocks may be considered for execution.

How can infinite loops be prevented in a while loop?

By ensuring the loop condition becomes False at some point

T/F: A function in python can only have one parameter or argument.

False.

T/F: In Python, private member variables can be accessed directly from outside the class without any restrictions.

False.

T/F: Instance variables are shared across all instances of a class.

False.

T/F: Its impossible to create an infinite loop with a while loop in Python.

False.

T/F: Method overloading in python can be directly achieved by defining multiple methods with the same name but different parameters.

False.

T/F: Method overriding is not supported in Python.

False.

T/F: Objects in Python cannot have properties and behaviors.

False.

T/F: Operator overloading is limited to arithmetic operators in Python.

False.

T/F: Protected members in python are enforced by the language and cannot be accessed from outside the class.

False.

T/F: The __init__ method cannot be considered a form of polymorphism when it involves default parameter values.

False.

T/F: The open() function in Python can only create new files, not open existing ones.

False.

T/F: The self parameter in class methods is optional.

False.

T/F: You cannot check if a file exists before attempting to delete it using the os module.

False.

T/F: A for loop can only iterate over lists in python.

False. A for loop can iterate over any iterable object, including lists, tuples, dictionaries, sets, an strings.

T/F: A python module can only contain function definitions.

False. A module can contain function definitions, class definitions, variables, and executable statements.

T/F: Python packages and modules refer to the same concept.

False. A package is a collection of modules organized in a directory hierarchy.

T/F: In Python, if statements cannot be nested within another if statement.

False. If statements can be nested within another if statement.

T/F: In Python, a while loop can only execute a single statement as long as the conditions remain true.

False. It can execute multiple statements in a block.

T/F: The readlines() method returns the entire content of the file as a single string.

False. It returns a list of lines.

T/F: Python lists are immutable.

False. Lists are mutable, meaning their elements can be changed.

T/F: Lists in python can only contain elements of the same data type.

False. Lists contain elements of different data types

T/F:Nested for loops are not allowed in Python.

False. Nested for loops are allowed and commonly used in Python.

T/F: Python requires parentheses around the condition in an if statement.

False. Parentheses are not required around the condition in an if statement in python, though they can be used for clarity.

T/F: In python, variables must be declared before they are assigned a value.

False. Python does not require explicit variable declarations before assignment

T/F: Python is a low-level programming language focusing primarily on system operations and memory management.

False. Python is a high-level programming language

Python is a proprietary software, and obtaining it requires a purchase.

False. Python is open-source

T/F: Python is only suitable for web development and cannot be used in data science

False. Python is widely used in web development, data science, machine learning, and more.

T/F: In Python, arguments passed to functions are always passed by value.

False. Python uses "Call by Object Reference" or "Call by Assignment".

T/F: Python allows using reserved words as variable names.

False. Reserved words cannot be used as variables names

T/F: The ** operator in python is used for multiplication.

False. The ** operator is used for exponentiation.

T/F: In python, the += operator is used to subtract a value from a variable

False. The += operator is used to add a value to a variable.

T/F: Python uses the = operator for both assignment and equality check.

False. The = is used for assignment, while == is used for equality check.

T/F: The == operator is used to assign a value to a variable in an if statement's condition.

False. The == operator is used for comparison, not assignment. The = operator is used for assignment.

T/F: The assignment operator in Python is ==

False. The assignment operator in Python is =

T/F: The elif statement can only be used after an else statement.

False. The elif statement is used after an if statement and before an else statement

T/F: The else statement is executed only if the if statement's condition is true.

False. The else statement is executed when the if statement's condition is False.

T/F: The first element in a Python list is 1.

False. The index of the first element is 0.

T/F: The range() function in python includes the end value specified in its arguments.

False. The range() function generates numbers up to, but not including, the end value.

T/F: Variables defined inside a function are accessible from anywhere in the program.

False. They are only accessible within the function.

T/F: The while loop cannot be used for iterating over a sequence of numbers generated by range().

False. While not its primary use, a while loop can iterate over a sequence generated by range() when properly coded.

Who created Python?

Guido van Rossum.

Which of the following about Python is true?

It is considered easy for beginners to learn.

What does the != operator in python signify?

Not equal

Which of the following is a key feature of Python?

Objected-oriented

How can you confirm the value assigned to a variable?

Printing the variable's value

Python's design philosophy emphasizes:

Readability and simplicity

What does the continue statement do inside a for loop?

Skips the rest of the code inside the loop for the current iteration

What does the continue statement do in a while loop?

Skips the rest of the code inside the loop for the current iteration and moves to the next iteration

What is the result of using the list.insert(index, element) method?

The element is inserted at the specified index, moving other elements to the right.

Which statement about while loops is correct?

The loop condition must be updated within the loop to avoid infinite loops.

T/F: Python variables are case-sensitive.

True

T/F: Python was created by Guido van Rossum in the late 1980s

True

T/F: Pythons syntax allows programmers to write programs with fewer lines than some other programming languages

True

T/F: The condition for a while loop must be True for the loop to execute.

True

T/F: The input() function in python always returns a value of the string type.

True

T/F: When opening a file in append mode ('a'), if the file does not exist, Python will create it.

True

T/F: A function defined without a return statement automatically returns None.

True.

T/F: Encapsulation can be used to hide the implementation details of a class from the users of that class.

True.

T/F: Encapsulation in Python restricts access to methods and variables from outside the class.

True.

T/F: Encapsulation prevents the direct access of class properties from outside the class.

True.

T/F: Every class in Python must have a constructor method defined.

True.

T/F: In Python, a class is a blueprint for creating objects.

True.

T/F: Inheritance allows a child class to inherit methods and properties from a parent class.

True.

T/F: List comprehension can replace most for loops that are used to create or modify lists.

True.

T/F: Polymorphism in Python cannot be achieved through method overloading.

True.

T/F: Polymorphism in Python refers to the ability to redefine methods for derived classes.

True.

T/F: Python supports operator overloading, allowing operators to be redefined for custom behaviors in classes.

True.

T/F: Static methods in Python cannot access instance variables.

True.

T/F: The __add__ method in python can be overridden to change the behavior of the + operator for objects of a class.

True.

T/F: The break statement can be used to exit a while loop before the condition becomes False.

True.

T/F: The global keyword allows a function to modify a variable defined outside of it.

True.

T/F: The len() function can be used to find the number of elements in a list.

True.

T/F: Type casting can change a variable from an int type to a string type

True.

T/F: Using the with statement to open files automatically closes the file when the block is exited.

True.

T/F: You can import specific functions from a module without importing the entire module.

True.

T/F: You can us negative indexing to access elements from the end of a list in Python.

True.

T/F: You can use the break statement inside a for loop to exit the loop.

True.

T/F: NumPy arrays can only hold elements of the same data type.

True. NumPy arrays are homogenous.

T/F: The alias of a module can be used interchangeably with its original name after importing.

True. Once an alias is defined, it is used to refer to the module.

How can you iterate over a list in reverse order using a for loop?

Use the reversed() function in the loop declaration

How can you execute multiple conditions sequentially in Python?

Using elif statements following an if statement

Python can be used for:

Web development, machine learning, data science (all of the above)

For an if-elif-else chain, when is the else block executed?

When all the if and elif conditions are false

Which of the following creates a list of squares of numbers from 0 to 4?

[x**2 for x in range(5)]

Which statement is used to execute a block of code only if a specified condition is True?

if

Which of the following is the correct way to write an if statement in Python?

if x > 10: print("Greater than 10.")

To convert a string to an integer, which fucntion is used in Python?

int ()

Which operator checks if two variables are the same object?

is

Which function would you use to combine the elements of another list to the end of the current list?

list.extend(list2)

How can you remove the first occurrence of an element in a list?

list.remove(element)

Which of the following is a logical operator in Python?

not

What is the correct way to assign the integer value 5 to the variable num?

num = 5

Which of the following will create a sublist of the first three elements of a list named numbers?

numbers[0:3] and numbers[:3]

Which of the following is correct about the range() function?

range(5) generates the sequence 0,1,2,3,4

Which of the following is allowed as a Python variable name?

thing2


Set pelajaran terkait

Biology: Transcription and Translation

View Set

Meg-Surg Week 9 Ch. 62, Care of Patients with Pituitary and Adrenal Gland Problems

View Set

Chapter 6 - Federal Procedures and Laws Governing Real Estate

View Set

Public Policy and the Black Community-Michael Coard

View Set