Which of the following is a valid way to create a list in Python?
Correct answer:
C) my_list = [1, 2, 3]
Explanation: A list in Python is created using square brackets, like [1, 2, 3] .
Which of the following is a valid way to create a list in Python?
Correct answer:
C) my_list = [1, 2, 3]
Explanation: A list in Python is created using square brackets, like [1, 2, 3] .
What does the pop() method do in Python?
Correct answer:
B) Removes the last element from the list
Explanation: The pop() method removes and returns the last element from a list.
Which of the following statements about dictionaries in Python is true?
Correct answer:
A) Keys in dictionaries must be unique
Explanation: In a dictionary, keys must be unique, but values do not have to be.
What is the output of len([1, 2, 3, 4]) ?
Correct answer:
B) 4
Explanation: The len() function returns the number of elements in the list, which is 4 in this case.
What is a tuple in Python?
Correct answer:
D) An immutable ordered collection
Explanation: A tuple is an immutable sequence of values that cannot be changed once defined.
How can you check if a key exists in a dictionary?
Correct answer:
A) if key in dict:
Explanation: The in keyword checks whether a key exists in a dictionary.
What is the result of 5 % 3 ?
Correct answer:
B) 2
Explanation: The modulus operator % returns the remainder when dividing 5 by 3, which is 2.
How do you create an empty dictionary in Python?
Correct answer:
B) dict = {}
Explanation: An empty dictionary is created using curly braces {} .
What will be the result of print(3 * ‘abc’) ?
Correct answer:
A) abcabcabc
Explanation: The * operator repeats the string 'abc' three times, resulting in 'abcabcabc'.
What is the correct syntax for defining a set in Python?
Correct answer:
B) my_set = {1, 2, 3}
Explanation: A set in Python is created using curly braces, like {1, 2, 3} .
What is the purpose of the is operator in Python?
Correct answer:
B) To check if two variables point to the same object
Explanation: The is operator checks whether two variables point to the same object in memory.
How can you remove an element from a set in Python?
Correct answer:
B) set.remove()
Explanation: The remove() method removes a specified element from a set.
What is the purpose of the range() function in Python?
Correct answer:
B) To return a sequence of numbers
Explanation: The range() function generates a sequence of numbers, which is often used in loops.
Which of the following can be used to generate a random number in Python?
Correct answer:
A) random.randrange()
Explanation: The random module provides functions such as randrange() to generate random numbers.
How do you access the value associated with a key in a dictionary?
Correct answer:
C) dict[key]
Explanation: You can access the value associated with a key using the dict[key] syntax.
Which of the following methods can be used to split a string into a list?
Correct answer:
A) split()
Explanation: The split() method breaks a string into a list based on a delimiter.
Which function converts a string to a floating-point number in Python?
Correct answer:
C) float()
Explanation: The float() function converts a string or integer to a floating-point number.
What is the default return value of a function that has no return statement?
Correct answer:
B) None
Explanation: A function that does not specify a return value returns None by default.
How can you check if a list is empty in Python?
Correct answer:
B) if len(list) == 0
Explanation: The len() function returns the length of the list, and if it is 0, the list is empty.
What is the difference between a list and a tuple in Python?
Correct answer:
A) Lists are mutable, tuples are immutable
Explanation: Lists can be modified, while tuples cannot be changed once created.
How do you iterate over the keys in a dictionary in Python?
Correct answer:
D) for key in dict:
Explanation: Iterating directly over a dictionary in a for loop accesses the keys by default.
What is the result of 5 == ‘5’ in Python?
Correct answer:
B) False
Explanation: The == operator checks for value equality, and since an integer is not equal to a string, the result is False .
Which method can be used to add an element to a set?
Correct answer:
C) add()
Explanation: The add() method is used to insert elements into a set.
What is the result of 4 ** 2 in Python?
Correct answer:
B) 16
Explanation: The ** operator raises the number to the power, so 4 ** 2 equals 16.
How do you remove whitespace from both ends of a string in Python?
Correct answer:
A) strip()
Explanation: The strip() method removes leading and trailing whitespace from a string.
What does the max() function return?
Correct answer:
A) The maximum value in a list or sequence
Explanation: The max() function returns the largest item in a list or other iterable.
How can you convert a list into a string in Python?
Correct answer:
B) join()
Explanation: The join() method combines elements of a list into a single string.
Which of the following is a valid Python dictionary?
Correct answer:
B) {‘a’: 1, ‘b’: 2}
Explanation: A valid dictionary consists of key-value pairs enclosed in curly braces.
What is the output of print(2 + 3 * 4) ?
Correct answer:
A) 14
Explanation: In Python, multiplication has higher precedence than addition, so 3 * 4 is evaluated first, followed by 2 + 12 .
What is the correct syntax for defining a function in Python?
Correct answer:
B) def my_function():
Explanation: The def keyword is used to define a function in Python.
What will be the output of print(bool(0)) in Python?
Correct answer:
B) False
Explanation: In Python, 0 is considered False in a Boolean context
What does the lower() method do in Python?
Correct answer:
A) Converts a string to lowercase
Explanation: The lower() method converts all characters in a string to lowercase.
How do you check if a list contains an element in Python?
Correct answer:
A) if element in list:
Explanation: The in keyword is used to check for membership in a list.
What is the result of len(‘hello’) ?
Correct answer:
B) 5
Explanation: The len() function returns the number of characters in a string, including spaces.
What is the purpose of the elif statement in Python?
Correct answer:
B) To check another condition after an if statement
Explanation: The elif statement allows you to check additional conditions after an if statement.
How do you define a class in Python?
Correct answer:
C) class MyClass:
Explanation: The class keyword is used to define a class in Python.
Which method can be used to find the position of an element in a list?
Correct answer:
A) index()
Explanation: The index() method returns the position of the first occurrence of an element in a list.
What will be the result of 2 + 2 == 4 in Python?
Correct answer:
A) True
Explanation: The expression 2 + 2 == 4 evaluates to True because 2 + 2 equals 4.
What is the purpose of the for loop in Python?
Correct answer:
A) To execute code repeatedly
Explanation: The for loop is used to iterate over a sequence and execute a block of code multiple times
Which function converts a string to an integer in Python?
Correct answer:
B) int()
Explanation: The int() function converts a string or float to an integer.
What will be the output of print(‘hello’.upper()) ?
Correct answer:
A) HELLO
Explanation: The upper() method converts all characters in a string to uppercase.
What does the pass statement do in Python?
Correct answer:
B) Does nothing and continues to the next statement
Explanation: The pass statement is used as a placeholder and does not perform any action.
What is the result of print(7 // 3) ?
Correct answer:
A) 2
Explanation: The // operator performs floor division and returns the largest integer less than or equal to the result.
Which method is used to reverse a list in Python?
Correct answer:
A) reverse()
Explanation: The reverse() method reverses the elements of a list in place.
How do you delete an element from a dictionary in Python?
Correct answer:
A) del dict[key]
Explanation: The del statement removes the key-value pair from a dictionary.
What will be the result of print(10 % 3) ?
Correct answer:
B) 1
Explanation: The modulus operator % returns the remainder of a division, so 10 % 3 equals 1.
Which operator is used to check if two values are not equal in Python?
Correct answer:
A) !=
Explanation: The != operator checks if two values are not equal.
How do you create a multi-line comment in Python?
Correct answer:
B) Using “”” or ”’
Explanation: Multi-line comments are created using triple quotes, either """ or ''' .
What is the purpose of the break statement in a loop?
Correct answer:
B) To exit the loop immediately
Explanation: The break statement causes the loop to terminate immediately, regardless of its condition.
What is the result of 5 + 3 * 2 in Python?
Correct answer:
B) 11
Explanation: According to operator precedence, multiplication is performed before addition, so 3 * 2 is evaluated first, followed by 5 + 6 .
If this exam is giving you a hard time, let us be your helping hand. Contact us, and we'll provide the assistance you need to pass and move forward.