50 multiple-choice questions with answers and detailed explanations. Untimed — choose an answer to check it instantly.
0/50 answered0 correct
Question 1 of 50
Which Python function is used to add data to a file?
Correct answer: A) write()
Explanation: The write() function is used to add data to a file in Python. The read() function is for reading files, append() adds to the end, and load() is for loading content in some specific formats.
Question 2 of 50
Which of the following creates a default greeting function in Python?
Correct answer: C) def greet(name, greeting = “Welcome to WGU”):
Explanation: The code defines a Python function with a default greeting parameter of "Welcome to WGU".
Question 3 of 50
How do you resolve the syntax error in this Python function?
def multiply_numbers(num1, num2)
result = num1 * num2
return result
Correct answer: B) Add a colon after the function definition
Explanation: A colon : is required after the function definition for proper syntax.
Question 4 of 50
Which Python data structure is used to store the following?
print(f”The processing speed of {current_speed} GHz is fast.”)
else:
print(f”The processing speed of {current_speed} GHz is not fast enough. Consider an upgrade.”)
Correct answer: A) Increase the indentation of the print statement inside the else block
Explanation: The else block should be indented correctly to align with Python's indentation rules.
Question 7 of 50
Which data structure is best suited for storing immutable network interface records?
Correct answer: D) Tuple
Explanation: A tuple is an immutable sequence type in Python, making it ideal for storing records that should not be changed.
Question 8 of 50
What is the index of the element 40 in the list?
numbers = [10, 20, 30, 40]
Correct answer: D) 3
Explanation: In Python, the list index starts at 0, so 40 is at index 3.
Question 9 of 50
What are the characteristics of tuples in Python?
Correct answer: C) Ordered and unchangeable
Explanation: Tuples in Python are ordered collections of items and are immutable (unchangeable).
Question 10 of 50
What is the error in the following Python code?
port = input(“Secure Web access? Y/N: “)
if port == “Y”
print(“Use port 443.”)
else
print(“Use port 80.”)
Correct answer: B) Missing colons at the end of if and else lines
Explanation: Colons are required after the if and else statements.
Question 11 of 50
What will be the result of this Python expression?
result = 5 + 3 * 2
Correct answer: C) 11
Explanation: According to the order of operations, multiplication is done before addition, so 3 * 2 = 6 , and then 5 + 6 = 11 .
Question 12 of 50
What is the purpose of the is operator in Python?
Correct answer: B) To check if two variables refer to the same object in memory
Explanation: The is operator checks if two variables point to the same object, not if their values are equal.
Question 13 of 50
What is the result of this Python expression?
bool(0)
Correct answer: B) False
Explanation: In Python, 0 is considered False , while any non-zero number is considered True .
Question 14 of 50
Which method is used to return the number of elements in a list?
Correct answer: C) len()
Explanation: The len() function is used to return the number of elements in a list or any iterable object.
Question 15 of 50
Which of the following is used to declare a function in Python?
Correct answer: C) def
Explanation: The def keyword is used to define a function in Python.
Question 16 of 50
What is the output of the following code?
print(3 ** 2)
Correct answer: C) 9
Explanation: The ** operator is used for exponentiation in Python, so 3 ** 2 is 3 raised to the power of 2 , which equals 9 .
Question 17 of 50
How can you convert the string “123” into an integer in Python?
Correct answer: B) int(“123”)
Explanation: The int() function converts a string or float into an integer, provided that the string represents a valid integer.
Question 18 of 50
What is the default value of the step parameter in Python’s string slicing?
Correct answer: B) 1
Explanation: The step parameter in Python slicing defaults to 1 , meaning that the slicing proceeds through the string one character at a time.
Question 19 of 50
Which of the following methods removes whitespaces from the beginning and the end of a string in Python?
Correct answer: A) strip()
Explanation: The strip() method removes leading and trailing whitespaces from a string
Question 20 of 50
What will be the result of this Python code?
fruits = [‘apple’, ‘banana’, ‘cherry’]
print(fruits[-1])
Correct answer: C) cherry
Explanation: Negative indexing in Python starts from the end of the list. So -1 refers to the last element, which is "cherry" .
Question 21 of 50
Which of the following creates a dictionary in Python?
Correct answer: C) {key: value}
Explanation: Dictionaries in Python are created using curly braces {} with key-value pairs inside, separated by colons.
Question 22 of 50
Which Python method is used to add an item to the end of a list?
Correct answer: B) append()
Explanation: The append() method is used to add an element to the end of a list.
Question 23 of 50
Which operator is used to check if an element exists in a list?
Correct answer: B) in
Explanation: The in operator is used to check if an element exists in a list or any other iterable.
Question 24 of 50
What is the output of the following code?
print(“Hello” + “World”)
Correct answer: A) HelloWorld
Explanation: The + operator concatenates strings, so "Hello" + "World" results in "HelloWorld" .
Question 25 of 50
How would you access the value associated with the key “name” in the dictionary below?
person = {“name”: “John”, “age”: 30}
Correct answer: B) person[“name”]
Explanation: In Python dictionaries, you access values using the key inside square brackets.
Question 26 of 50
Which Python method is used to combine two lists?
Correct answer: C) extend()
Explanation: The extend() method adds the elements of one list to the end of another list.
Question 27 of 50
What is the output of this Python code?
numbers = [1, 2, 3]
numbers[0] = 10
print(numbers)
Correct answer: B) [10, 2, 3]
Explanation: The first element of the list (at index 0) is reassigned to 10, so the list becomes [10, 2, 3] .
Question 28 of 50
What will this code return?
my_str = “Hello World”
print(my_str[0:5])
Correct answer: B) Hello
Explanation: The slicing my_str[0:5] returns the first 5 characters of the string, which is "Hello" .
Question 29 of 50
What does this Python code output?
print(2 * 3 ** 2)
Correct answer: B) 18
Explanation: According to the order of operations, 3 ** 2 = 9 , and then 2 * 9 = 18 .
Question 30 of 50
How can you create a tuple in Python?
Correct answer: B) (1, 2, 3)
Explanation: A tuple is created by using parentheses () with comma-separated values inside.
Question 31 of 50
Which of the following is not a valid Python data type?
Correct answer: C) array
Explanation: Arrays are not a built-in data type in Python. Python has lists, sets, and dictionaries as built-in data structures.
Question 32 of 50
Which keyword is used to define a class in Python?
Correct answer: A) class
Explanation: The class keyword is used to define a class in Python.
Question 33 of 50
What does the following Python expression return?
len([10, 20, 30])
Correct answer: C) 3
Explanation: The len() function returns the number of items in the list, which is 3.
Question 34 of 50
Which of the following Python keywords is used to create a loop?
Correct answer: C) for
Explanation: The for keyword is used to create a loop that iterates over a sequence.
Question 35 of 50
How do you remove an element from a list in Python?
Correct answer: B) remove()
Explanation: The remove() method removes the first occurrence of a specified element from the list.
Question 36 of 50
What does the continue statement do in Python loops?
Correct answer: B) Skips the current iteration and moves to the next
Explanation: The continue statement skips the current iteration and proceeds with the next iteration of the loop.
Question 37 of 50
What is the output of this code?
my_str = “Hello”
print(my_str[-1])
Correct answer: C) o
Explanation: Negative indexing starts from the end, so my_str[-1] returns the last character, which is "o" .
Question 38 of 50
What does this Python code return?
x = 7
y = 5
print(x % y)
Correct answer: A) 2
Explanation: The modulus operator % returns the remainder when 7 is divided by 5 , which is 2 .
Question 39 of 50
How do you declare an empty set in Python?
Correct answer: C) set()
Explanation: set() is used to create an empty set in Python. {} is used for dictionaries.
Question 40 of 50
What is the output of the following Python code?
x = 5
y = 10
print(x == y)
Correct answer: B) False
Explanation: The == operator checks if x is equal to y . Since 5 is not equal to 10 , the result is False .
Question 41 of 50
Which method is used to get the index of an element in a list?
Correct answer: B) index()
Explanation: The index() method returns the index of the first occurrence of an element in a list.
Question 42 of 50
What is the result of this Python expression?
not True and False
Correct answer: B) False
Explanation: According to Boolean logic, not True is False , and False and False is False .
Question 43 of 50
Which keyword is used to handle exceptions in Python?
Correct answer: C) try
Explanation: The try keyword is used to start a block of code that might raise an exception. If an exception occurs, it is handled by the except block.
Question 44 of 50
What is the output of this Python code?
print(len(“Python”))
Correct answer: B) 6
Explanation: The string "Python" contains 6 characters, so len("Python") returns 6 .
Question 45 of 50
What does the pop() method do in Python?
Correct answer: B) Removes the last element of the list
Explanation: The pop() method removes and returns the last element of the list.
Question 46 of 50
Which of the following is a valid function definition in Python?
Correct answer: C) def myFunc():
Explanation: A function in Python is defined using the def keyword followed by the function name and parentheses, with a colon at the end.
Question 47 of 50
Which of the following will create an empty list?
Correct answer: D) Both a and b
Explanation: Both list() and [] can be used to create an empty list in Python.
Question 48 of 50
How can you concatenate two tuples in Python?
Correct answer: A) +
Explanation: You can concatenate two tuples using the + operator.
Question 49 of 50
What will be the output of the following Python code?
x = [1, 2, 3]
y = x
y.append(4)
print(x)
Correct answer: B) [1, 2, 3, 4]
Explanation: Since y refers to the same list as x , appending to y will also modify x .
Question 50 of 50
What is the result of this Python expression?
5 // 2
Correct answer: A) 2
Explanation: The // operator performs floor division, which divides and rounds down to the nearest integer. 5 // 2 equals 2 .
Set 1 complete
You scored 0%
0 correct out of 0 answered (50 questions in this set).