Python Programming Basics Quiz
41 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What will be the output when the function divide(2, 1) is executed?

  • result is 2 executing finally clause (correct)
  • division by zero!executing finally clause
  • executing finally clause
  • result is 1 executing finally clause

What happens when divide(2, 0) is called?

  • result is 0 executing finally clause
  • result is 2 executing finally clause
  • TypeError: unsupported operand type(s) for /: 'int' and 'str'
  • division by zero!executing finally clause (correct)

What will occur if the function divide('2', '1') is executed?

  • division by zero!executing finally clause
  • TypeError: unsupported operand type(s) for /: 'str' and 'str' (correct)
  • result is 2 executing finally clause (correct)
  • executing finally clause

Which statement about files and their operations in Python is correct?

<p>Files must be opened before performing read or write operations. (D)</p> Signup and view all the answers

Which of the following is true about text files in Python?

<p>Each line in a text file ends with an End of Line (EOL) character. (A)</p> Signup and view all the answers

Which of the following operators is used to perform an implicit type conversion in Python?

<ul> <li>(B)</li> </ul> Signup and view all the answers

Which of the following is a valid augmented assignment operator in Python?

<p>/= (B), **= (C), //= (D)</p> Signup and view all the answers

What will be the value of the variable L after executing the statement L = [0.5 * x for x in range(0, 4)]?

<p>[0.0, 0.5, 1.0, 1.5] (B)</p> Signup and view all the answers

In Python, which of the following statements is true regarding decision-making control statements?

<p>They can include multiple if statements without else. (B)</p> Signup and view all the answers

Which of the following correctly describes the use of the 'break' statement in Python?

<p>It exits the nearest enclosing loop. (A)</p> Signup and view all the answers

How does the input() function behave in terms of type conversion?

<p>It returns the input as a string by default. (B)</p> Signup and view all the answers

Which of the following statements accurately describes membership operators in Python?

<p>They determine if a value exists in a sequence. (A)</p> Signup and view all the answers

What is the correct command to successfully change the book 'Crime' to 'Crime Thriller'?

<p>book[2] = 'Crime Thriller' (B)</p> Signup and view all the answers

How should Ramesh merge the dictionary 'book' with the dictionary 'library'?

<p>library.update(book) (D)</p> Signup and view all the answers

Which method adds an item at the end of a list?

<p>append() (C)</p> Signup and view all the answers

Which method would you use to find the index of the first occurrence of an element in a list?

<p>index() (B)</p> Signup and view all the answers

What method would you use to concatenate the contents of list2 to the end of list1?

<p>list1.extend(list2) (B)</p> Signup and view all the answers

What is the output of 'print(a.count(5))' if 'a' is defined as 'a = (5,(7,5,(1,2)),5,4)'?

<p>2 (C)</p> Signup and view all the answers

Which command will yield the length of the tuple 'a = (5,(7,5,(1,2)),5,4)'?

<p>len(a) (B)</p> Signup and view all the answers

In the context of reversing a list to check for symmetry, which statement correctly checks if a list is the same read forwards and backwards?

<p>if a == a[::-1]: (C)</p> Signup and view all the answers

What would the mid-point variable be given the list 'a = [1,2,3,3,2,1]'?

<p>2.5 (C)</p> Signup and view all the answers

If a list is checked for symmetry and the condition fails at an index, what would the appropriate action be?

<p>Print 'NO' and break out of the loop. (C)</p> Signup and view all the answers

What will be the output of the code snippet given that A = 10/2 and B = 10//3?

<p>5.0, 3 (A), 5.0, 3.3 (C)</p> Signup and view all the answers

Which method is correctly used to return the square root of a number in Python?

<p>sqrt() (D)</p> Signup and view all the answers

What is the result of the expression: 2 ** 3 ** 2?

<p>64 (A)</p> Signup and view all the answers

What is the result of evaluating: not 12 > 6 and 7 < 17 or not 12 < 4?

<p>True (B)</p> Signup and view all the answers

What will be printed by the code: String1="Coronavirus Disease"; print(String1.lstrip("Covid"));

<p>Coronavirus Disease (A)</p> Signup and view all the answers

What will be the output of the loop that converts the original string 'gmail@com' regarding case and non-alpha characters?

<p>GMAILBB (C)</p> Signup and view all the answers

What errors exist in the given function definition for func? Choose the correct statement about the code.

<p>All of the above (D)</p> Signup and view all the answers

During which operation would a TypeError most likely occur in the provided code examples?

<p>Trying to add x and y without defining y (D)</p> Signup and view all the answers

In the code provided, what would be the output of 'print(String1.rstrip("sea"))'?

<p>Coronavirus Dise (C)</p> Signup and view all the answers

What is a key difference between parameters and arguments in a function?

<p>Parameters are the variable names within functions, and arguments are the actual values passed to these parameters. (A)</p> Signup and view all the answers

Which of the following statements accurately describes local variables?

<p>Local variables exist only within the scope of the function or block they are declared in. (A)</p> Signup and view all the answers

What is the significance of using functions in programming?

<p>Functions help in modularity, making programs easier to manage and understand. (D)</p> Signup and view all the answers

In the provided code snippet, which type of variable is 'n' when declared inside the 'prime' function?

<p>Local variable (A)</p> Signup and view all the answers

What would happen if the syntax 'if n%i=0:' were used instead of 'if n%i==0:'?

<p>An error will be raised since '=' cannot be used in this context. (B)</p> Signup and view all the answers

Why is it important to distinguish between local and global variables?

<p>To prevent variables from being accidentally overwritten in different parts of the program. (A)</p> Signup and view all the answers

Which statement correctly identifies a flaw in the 'prime' function's implementation?

<p>The range function for checking factor divisibility has been incorrectly defined. (D)</p> Signup and view all the answers

Which of the following defines a global variable?

<p>A variable declared outside of any function that can be accessed from anywhere in the program. (C)</p> Signup and view all the answers

What issue is evident with the print statements in the 'prime' function?

<p>The quotes in the print statements use incorrect syntax. (B)</p> Signup and view all the answers

What is the primary disadvantage of having redundant code in a program?

<p>It increases the complexity and the likelihood of errors. (A)</p> Signup and view all the answers

Flashcards

Floor Division (//)

In Python, the double slash operator (//) performs floor division. It divides two numbers and rounds the result down to the nearest whole number, discarding any fractional part.

Square Root Function (sqrt())

The sqrt() function in Python's math module calculates the square root of a given number. You need to import the math module before using the sqrt() function.

Exponent Operator (**)

The exponent operator (**) in Python calculates the power of a number. It raises the base number to the power of the exponent.

Logical NOT Operator (not)

The not operator in Python inverts the truth value of a Boolean expression. If the expression is True, not makes it False, and vice versa.

Signup and view all the flashcards

Local vs. Global Variables

A local variable is declared inside a function and can only be accessed within that function. A global variable is declared outside of any function and can be accessed from anywhere in the program.

Signup and view all the flashcards

String Methods: lstrip() and rstrip()

The lstrip() method removes leading characters from a string based on the specified characters. The rstrip() method removes trailing characters.

Signup and view all the flashcards

String Methods: isupper() and lower()

The isupper() method checks if all characters in a string are uppercase. The lower() method converts a string to lowercase.

Signup and view all the flashcards

Nested Lists

In Python, a list can contain elements of different data types, including other lists. This allows for nested lists, where a list can contain sub-lists.

Signup and view all the flashcards

for and while Loops

In Python, a loop iterates through a sequence of items. A while loop continues to execute as long as a given condition remains True. A for loop iterates through each item in a sequence.

Signup and view all the flashcards

What does the index() method do?

The index() method returns the index of the first occurrence of a specified element in a list.

Signup and view all the flashcards

Explain the append() method.

The append() method adds a new element to the end of a list.

Signup and view all the flashcards

What's the purpose of the extend() method?

The extend() method adds the contents of one list to the end of another list.

Signup and view all the flashcards

How do you arrange a list in descending order?

The sort(reverse=True) method sorts a list in descending order.

Signup and view all the flashcards

What does the count() method do for tuples?

The count() method returns the number of times a specified value appears in a tuple.

Signup and view all the flashcards

How do you merge two dictionaries using a simple method?

The update() method merges the contents of one dictionary into another dictionary.

Signup and view all the flashcards

What does the len() function do?

The len() function returns the number of elements in a tuple.

Signup and view all the flashcards

How does the loop know when to stop in the given code?

The loop iterates until the index variable i reaches the middle of the list.

Signup and view all the flashcards

What is the condition being checked in the if statement?

The if statement checks whether the element at index i is the same as the element at index len(a) - i - 1.

Signup and view all the flashcards

What is the condition that controls the loop in the given code?

The loop iterates until the index variable i reaches half the length of the list.

Signup and view all the flashcards

Text File

A sequence of characters (usually ASCII) stored on a permanent storage media. Each line is terminated by a special character called End of Line (EOL). Python, while using ASCII by default, supports Unicode as well.

Signup and view all the flashcards

End of Line (EOL)

A special character that marks the end of a line in a text file, used to indicate where the line break should be.

Signup and view all the flashcards

File Operation

A process in Python that involves opening a file for reading, writing, or performing other operations, and then eventually closing it.

Signup and view all the flashcards

RAM (Random Access Memory)

Volatile memory, meaning that it loses its data when the power is turned off. Ideal for temporary data storage.

Signup and view all the flashcards

Non-Volatile Memory

Non-volatile memory that retains data even when the power is off. Best suited for permanent data storage.

Signup and view all the flashcards

Assignment operator

In Python, the assignment operator (=) assigns values to variables. It places the value on the right of the operator into the variable on the left. For example, x = 5 assigns the value 5 to the variable x.

Signup and view all the flashcards

Augmented assignment operators

Augmented assignment operators combine an arithmetic operation with the assignment operator in a single step. This streamlines code when you need to modify a variable's value by adding, subtracting, multiplying, dividing, or applying other operations. Examples: += Add and assign: x += 5 is equivalent to x = x + 5 -= Subtract and assign: x -= 2 is equivalent to x = x - 2 *=, /= - Multiply and assign, Divide and assign, //= Floor divide and assign, %= Modulo and assign, **= Exponentiation and assign

Signup and view all the flashcards

Identity Operators (is, is not)

Identity operators check if two variables point to the same memory location, meaning they hold exactly the same value, not just a similar value.
is returns True if the objects refer to the same memory location, and False otherwise. is not returns True if the objects refer to different memory locations, and False otherwise.

Signup and view all the flashcards

Membership operators (in, not in)

Membership operators determine whether a specific value is contained within a sequence (like lists, tuples, or strings).
in returns True if the specified value is present in the sequence, and False otherwise.
not in returns True if the specified value is NOT present in the sequence, and False otherwise.

Signup and view all the flashcards

Type Conversion

Type conversion, or type casting, transforms a value from one data type to another. Python offers two types of conversion:

  1. Implicit: Automatic conversion done by Python without explicit action, often for arithmetic.
  2. Explicit: You explicitly instruct Python to change the data type using functions like int(), str(), float(), etc.
Signup and view all the flashcards

Control statements

Control statements are like traffic signals for code execution. They guide the order in which instructions are run, creating different program flows based on conditions. Python uses three types:

  1. Decision Making (if-elif-else): They choose what code to run based on conditions being True or False.
  2. Iteration (while and for loops): Repeat code blocks until a condition is met or a specific range is exhausted.
  3. Jump Statements (break, continue, pass): These alter the flow within loops or conditional blocks.
Signup and view all the flashcards

Mutable and immutable data types

Mutable data types can be changed or modified after creation. Think of them as flexible containers. Lists are the classic example, as you can add, remove, or change elements within them. Immutable data types are unchangeable after creation, like a sealed package. Common examples include: integers, strings, and tuples.

Signup and view all the flashcards

Parameter

A temporary variable named inside a function.

Signup and view all the flashcards

Argument

A specific value passed to a function when it's called.

Signup and view all the flashcards

Local Variable

A variable declared inside a function, only accessible within that function.

Signup and view all the flashcards

Global Variable

A variable declared outside any function, accessible from anywhere in the program.

Signup and view all the flashcards

Function

A block of reusable code designed to perform a specific task.

Signup and view all the flashcards

Loop

A code block that repeats a set of instructions a specific number of times.

Signup and view all the flashcards

While Loop

A loop that continues until a specific condition is met.

Signup and view all the flashcards

For Loop

A loop that runs a set number of times, based on an integer.

Signup and view all the flashcards

Modulo Operator (%)

The remainder after integer division.

Signup and view all the flashcards

Divisible By (n % m == 0)

Determines if a number is divisible by another number.

Signup and view all the flashcards

Study Notes

No specific text provided. Please provide the text or questions for which you would like study notes.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Description

Test your knowledge of Python programming basics with this quiz. It covers functions, file operations, type conversions, and control statements. Perfect for beginners looking to assess their understanding of Python syntax and behavior.

Use Quizgecko on...
Browser
Browser