Python Code Snippets MCQ
30 Questions
74 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</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</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)</p> Signup and view all the answers

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

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

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

    <p>1</p> Signup and view all the answers

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

    <p>in</p> Signup and view all the answers

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

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

    Which of the following statements is invalid in Python?

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

    Which operator has the highest precedence in a Python expression?

    <p>Exponential</p> Signup and view all the answers

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

    <p>list</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</p> Signup and view all the answers

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

    <p>'”That’s okay”'</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</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</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</p> Signup and view all the answers

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

    <p>4</p> Signup and view all the answers

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

    <p>5</p> Signup and view all the answers

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

    <p>False</p> Signup and view all the answers

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

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

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

    <p>'hEllOwOrlD'</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']</p> Signup and view all the answers

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

    <p>3</p> Signup and view all the answers

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

    <p>[6, 8]</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]</p> Signup and view all the answers

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

    <p>list1.remove(5)</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()</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]</p> Signup and view all the answers

    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