Python Question and Answers – Part 2

Python Lists – Quiz

1. How do you create a list in Python?

  • A. {}
  • B. []
  • C. ()
  • D. <>

2. What is the first index of a Python list?

  • A. 0
  • B. 1
  • C. -1
  • D. None

3. Which method adds an item to the end of a list?

  • A. add()
  • B. insert()
  • C. append()
  • D. extend()

4. How would you access the last item in a list?

  • A. list[0]
  • B. list[-1]
  • C. list[1]
  • D. list[2]

5. What does fruits[1:3] return for fruits = [“apple”, “banana”, “cherry”, “date”]?

  • A. [“apple”, “banana”]
  • B. [“banana”, “cherry”]
  • C. [“cherry”, “date”]
  • D. [“banana”]

6. Which method removes an item by its index?

  • A. delete()
  • B. discard()
  • C. remove()
  • D. pop()

7. What does list.reverse() do?

  • A. Sorts the list
  • B. Adds items to the list
  • C. Reverses order
  • D. Removes the last item

8. How do you delete an item by its value?

  • A. list.pop()
  • B. list.remove()
  • C. list.del()
  • D. list.pop(value)

9. What does fruits.sort() do for fruits = [“cherry”, “apple”, “banana”]?

  • A. Arranges alphabetically
  • B. Reverses the list
  • C. Does nothing
  • D. Adds “apple”

10. How would you add “pear” at the start of a fruits list?

  • A. fruits[0] = “pear”
  • B. fruits.append(“pear”)
  • C. fruits.insert(0, “pear”)
  • D. fruits.push(“pear”)

Python Dictionaries – Quiz

1. How do you create a dictionary in Python?

  • A. {}
  • B. []
  • C. ()
  • D. <>

2. What data structure is a collection of key-value pairs?

  • A. List
  • B. Set
  • C. Tuple
  • D. Dictionary

3. How would you access the value of “name” in person[“name”]?

  • A. person.name
  • B. person{“name”}
  • C. person[“name”]
  • D. person{[“name”]}

4. How do you add a new key-value pair to a dictionary?

  • A. add()
  • B. update()
  • C. append()
  • D. dictionary[key] = value

5. Which method removes a key-value pair and returns the value?

  • A. pop()
  • B. remove()
  • C. delete()
  • D. discard()

6. What happens if you access a key that doesn’t exist?

  • A. Error
  • B. Returns None
  • C. Adds key with None
  • D. Creates empty dictionary

7. How do you loop through all keys and values?

  • A. .items()
  • B. .pairs()
  • C. .all()
  • D. .keys()

8. Which method returns only the values in a dictionary?

  • A. keys()
  • B. items()
  • C. values()
  • D. pairs()

9. How do you delete a key-value pair by key?

  • A. del
  • B. remove()
  • C. delete()
  • D. discard()

10. Which is a valid dictionary?

  • A. {“a”, “b”, “c”}
  • B. {1, 2, 3}
  • C. {“key”: value}
  • D. [“key”: “value”]

Python Tuples – Quiz

Question 1: What is a tuple in Python?

  • A. A mutable collection
  • B. An immutable collection
  • C. A method
  • D. A variable type

Question 2: How do you create a tuple in Python?

  • A. []
  • B. {}
  • C. ()
  • D. <>

Question 3: What is the main difference between a list and a tuple?

  • A. Tuples can be changed, lists cannot
  • B. Tuples are immutable, lists are mutable
  • C. Lists store strings only, tuples store integers only
  • D. Tuples are slower than lists

Question 4: Which of the following is a valid tuple?

  • A. [1, 2, 3]
  • B. {1, 2, 3}
  • C. (1, 2, 3)
  • D. tuple[1, 2, 3]

Question 5: What will be the output of the following code?

t = (1, 2, 3)
print(t[1])
        
  • A. 1
  • B. 2
  • C. 3
  • D. Error

Question 6: How do you find the length of a tuple in Python?

  • A. size(t)
  • B. len(t)
  • C. length(t)
  • D. count(t)

Question 7: What does the following code do?

t = (1, 2, 3)
t[1] = 5
        
  • A. Updates the tuple
  • B. Throws an error
  • C. Creates a new tuple
  • D. Deletes the tuple

Question 8: Which method is used to count occurrences of an element in a tuple?

  • A. index()
  • B. find()
  • C. count()
  • D. occurrences()

Question 9: What will t = (1, 2, 3, 2); print(t.count(2)) return?

  • A. 0
  • B. 1
  • C. 2
  • D. Error

Question 10: Which method is used to find the index of an element in a tuple?

  • A. locate()
  • B. find()
  • C. position()
  • D. index()

Python Functions – Quiz

Question 1: What is a function in Python?

  • A. A type of loop
  • B. A block of code that performs a specific task
  • C. A variable declaration
  • D. A type of data structure

Question 2: Which keyword is used to define a function in Python?

  • A. func
  • B. define
  • C. def
  • D. function

Question 3: What will the following code output?

def add(a, b):  
    return a + b  

print(add(2, 3))
        
  • A. 23
  • B. 5
  • C. 6
  • D. Error

Question 4: What does a function without a return statement return?

  • A. None
  • B. 0
  • C. False
  • D. Error

Question 5: What is a parameter in Python?

  • A. The name of the function
  • B. The output of the function
  • C. Input to the function
  • D. A type of variable

Question 6: Which of the following is NOT a valid function name?

  • A. calculateSum
  • B. 1stFunction
  • C. my_function
  • D. _helper

Question 7: What is the correct way to call the following function?

def greet(name):  
    print(f"Hello, {name}!")  
        
  • A. greet()
  • B. greet(“John”)
  • C. print(greet(“John”))
  • D. Hello(name)

Question 8: What does the return keyword do in a function?

  • A. Stops the function execution
  • B. Specifies what value a function gives back
  • C. Defines a parameter
  • D. None of the above

Question 9: How can you pass multiple arguments to a function?

  • A. Using a list
  • B. Using multiple variables separated by commas
  • C. Using a string
  • D. Only one argument can be passed

Question 10: Which of these is an example of a function with default parameters?

def greet(name="User"):  
    print(f"Hello, {name}")  

greet()  
        
  • A. def greet(name=”User”): print(f”Hello, {name}”) greet()
  • B. def greet(): print(“Hello, User”) greet()
  • C. def greet(name): print(f”Hello, {name}”) greet(“User”)
  • D. def greet(): return “Hello, User”

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *