Python Code Snippets MCQ
30 Questions
105 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 is the output of $23+(5+6)(1+1)?$

  • 17 (correct)
  • 121
  • 8
  • 129

What will be the data type of 'var' after the following code snippet?

  • str and str
  • int and int
  • str and int (correct)
  • int and str

What will be the output of the following code snippet?

a = [1, 2, 3] 
a = tuple(a) 
a = 2 
print(a) 

  • (2, 2, 3)
  • Error. (correct)
  • [2, 2, 3]
  • (1, 2, 3)

What is the output of $5/2$ and $5//2$?

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

What will be the output of the following code snippet?

a = [1, 2, 3, 4, 5] 
sum = 0 
for ele in a: 
    sum += ele 
print(sum) 

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

What will be the output of the following code snippet?

a=3 
b=1 
print(a, b) 
a, b = b, a 
print(a, b) 

<p>(13, 31) (B)</p> Signup and view all the answers

Which of the following is an invalid variable name in Python?

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

What is the result of $22 ext{ % }3$ in Python?

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

Which of the following CANNOT be a valid variable name in Python?

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

What is the correct operator for power($x^y$) in Python?

<p>$X**y$ (A)</p> Signup and view all the answers

Which of the following statements is invalid in Python?

<p>a b c = 1000 2000 3000 (D)</p> Signup and view all the answers

Which operator has the highest precedence in a Python expression?

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

What data type is the object L = [1, 23, 'hello', 1]?

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

What is the output of the following Python code? i=1 while True: if i%3 == 0: break print(i) i+=1

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

Which of the following Python code snippets results in a SyntaxError?

<p>'”That’s okay”' (D)</p> Signup and view all the answers

What will be the average value of the following Python code snippet? >>>grade1 = 80 >>>grade2 = 90 >>>average = (grade1 + grade2) / 2

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

What will be the output of the following Python code? i=1 while True: if i%2 == 0: break print(i) i += 2

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

What will be the output of the following Python code? i=1 while False: if i%2 == 0: break print(i) i += 2

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

What will be the output of the following Python code? print('xyyzxyzxzxyy'.count('xy'))

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

What will be the output of the following Python code? print('abcdef'.index('f'))

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

What will be the output of the following Python code? print('123abc'.isdigit())

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

What will be the output of the following Python code? print('Hello'.replace('l', 'x'))

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

What will be the output of the following Python code? print('HeLLoWoRLd'.swapcase())

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

What will be the output of the following Python code? print('Python is fun'.split())

<p>['Python', 'is', 'fun'] (C)</p> Signup and view all the answers

What is the output of len(list1) if list1 is ['apple', 'banana', 'cherry']?

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

In list1 = [2, 4, 6, 8, 10], what does list1[2:4] return?

<p>[6, 8] (A)</p> Signup and view all the answers

If list1 = [1, 2, 3] and list2 = [4, 5], what is the result of list1 + list2?

<p>[1, 2, 3, 4, 5] (C)</p> Signup and view all the answers

How can an element be removed by value from a list in Python?

<p>list1.remove(5) (D)</p> Signup and view all the answers

What is the correct way to copy the contents of list1 into a new list called list2?

<p>list2 = list1.copy() (D)</p> Signup and view all the answers

If list1 = [10, 20, 30] and list2 = list1, what happens if we modify list2[0] to 5?

<p>list1 becomes [5, 20, 30] (B)</p> Signup and view all the answers

Flashcards

Output of 23 + (5+6)(1+1)

The result of the calculation 2 multiplied by 3 plus the product of (5 plus 6) and (1 plus 1).

Data type of 'var' after code snippet

The variable "var" becomes a string and an integer.

Python list to tuple conversion

Converts a list to a tuple in Python using the tuple() function.

Output of 5/2

The result is the floating-point division of 5 by 2. It's a decimal.

Signup and view all the flashcards

Output of 5//2

The integer division of 5 by 2 using the floor division operator //, discarding the remainder.

Signup and view all the flashcards

Summing elements in a list

Iterating over a list and accumulating the sum of its elements

Signup and view all the flashcards

Output of a=3,b=1, swapping

The code initially prints (3, 1), then it swaps the values assigned to a and b to (1, 3).

Signup and view all the flashcards

Invalid variable name

A variable name starting with a number.

Signup and view all the flashcards

Modulo operation (22 % 3)

The remainder of 22 divided by 3.

Signup and view all the flashcards

Invalid Python variable

Keywords like "in" are reserved words and cannot be used as variable names.

Signup and view all the flashcards

Power operator in Python

The exponentiation operator (**).

Signup and view all the flashcards

Invalid Python statement

Multiple values assigned to a single variable.

Signup and view all the flashcards

Highest precedence operator

The exponential operator (**).

Signup and view all the flashcards

Data type of L = [1,23,'hello',1]

A list containing integers and a string

Signup and view all the flashcards

Output of while loop i % 3 ==0

Prints numbers from 1 up to the number divisible by 3 then breaks

Signup and view all the flashcards

Syntax error (example)

Invalid Python code.

Signup and view all the flashcards

Average calculation (grade1+grade2)/2

Calculates the average of two grades, grade1 and grade2.

Signup and view all the flashcards

Output of while loop i % 2 ==0

Prints odd numbers then breaks when it encounters an even number.

Signup and view all the flashcards

Output while loop with false condition

No output as the loop does not execute.

Signup and view all the flashcards

Count occurrences

The count() method is used to count the number of times a substring occurs in a string

Signup and view all the flashcards

Index of substring

Finds the index position of a substring in a string

Signup and view all the flashcards

Check digits only

Checks whether a string contains only digits.

Signup and view all the flashcards

String replacement

Replaces a substring with another in a string

Signup and view all the flashcards

String case conversion

Converts the case of a string (upper to lower or vice-versa)

Signup and view all the flashcards

String splitting

Splits a string into a list of substrings based on a delimiter.

Signup and view all the flashcards

Length of a list

The number of elements in a list; use len() function

Signup and view all the flashcards

List slicing

Extracting a portion of a list using start and stop indices

Signup and view all the flashcards

List concatenation

Combining two lists into a single list

Signup and view all the flashcards

Removing element from a list (by value)

Removes the first occurrence of a specific value from a list in Python

Signup and view all the flashcards

Copying a list

Creating a new list containing the same elements as an existing list without modifying the original list

Signup and view all the flashcards

Modifications affect original list

If two lists are linked (not copied), changing one list affects the other.

Signup and view all the flashcards

Study Notes

Python Basics

  • In Python, 2**3 + (5 + 6)**(1 + 1) evaluates to 129
  • print(type(var)) will output str or int depending on the type of var
  • a = [1, 2, 3]; a = tuple(a) converts a list to a tuple
  • print(type(5 / 2)) outputs float and print(type(5 // 2)) outputs int
  • a = [1, 2, 3, 4, 5]; sum = 0; for ele in a: sum += ele; print(sum) outputs 15
  • a, b = b, a swaps the values of a and b

Loops and Control Structures

  • while True: creates an infinite loop
  • break statement exits a loop
  • if i%3 == 0: break exits a loop when i is a multiple of 3
  • if i%2 == 0: break exits a loop when i is even

Data Types and Operations

  • L = [1, 23, 'hello', 1] is a list
  • dict is used to store values in key-value pairs
  • average = (grade1 + grade2) / 2 calculates the average of two numbers
  • str.endswith("xyy") checks if a string ends with "xyy"
  • str.find("cd") finds the index of the first occurrence of "cd" in a string
  • str.format is used to format strings

Functions and Operators

  • % is the modulus operator
  • ** is the exponentiation operator
  • int(x) converts a value to an integer
  • str() converts a value to a string
  • print("xyyzxyzxzxyy".endswith("xyy")) checks if a string ends with "xyy"
  • print("abcdef".find("cd")) finds the index of the first occurrence of "cd" in a string

Lists and Tuples

  • list[index] accesses an element in a list
  • list[index:] slices a list from index to the end
  • list1 * 2 concatenates a list with itself
  • list1.append(5) adds an element to the end of a list
  • list1.insert(2, 5) inserts an element at a specific position in a list

Studying That Suits You

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

Quiz Team

Description

Test your knowledge of Python with these multiple-choice questions covering topics such as arithmetic operations, data types, and data structures. See if you can predict the output of various code snippets and identify the data types of variables.

Use Quizgecko on...
Browser
Browser