What is the output of print(5 // 2) in Python?
Correct answer:
A) 2
Explanation: The // operator performs floor division, which returns the largest integer less than or equal to the result of the division.
What is the output of print(5 // 2) in Python?
Correct answer:
A) 2
Explanation: The // operator performs floor division, which returns the largest integer less than or equal to the result of the division.
How do you convert a string to a list of characters in Python?
Correct answer:
B) list(str)
Explanation: The list() function converts a string into a list of characters.
What is the purpose of the pass statement in Python?
Correct answer:
C) Does nothing and acts as a placeholder
Explanation: The pass statement is used as a placeholder where code is syntactically required but not yet implemented.
Which function is used to create a dictionary from two lists in Python?
Correct answer:
B) dict()
Explanation: The dict() function can be used in conjunction with zip() to create a dictionary from two lists (keys and values).
How do you declare a multi-line string in Python?
Correct answer:
A) ''' or """
Explanation: Triple quotes, either ''' or """, are used to create multi-line strings in Python.
What does print(10 % 3) return?
Correct answer:
A) 1
Explanation: The % operator returns the remainder of the division, so 10 % 3 equals 1.
Which of the following is a valid Python variable name?
Correct answer:
B) var_name
Explanation: Variable names in Python cannot start with a number and must not contain spaces or hyphens.
How do you add an element to the beginning of a list?
Correct answer:
B) list.insert(0, value)
Explanation: The insert() method inserts an element at a specified position, and 0 refers to the first position in the list.
Which method is used to remove and return the last element from a list?
Correct answer:
A) pop()
Explanation: The pop() method removes and returns the last element of a list.
What is the output of len([1, 2, [3, 4], 5])?
Correct answer:
B) 4
Explanation: The len() function counts the top-level elements, and [3, 4] is considered one element.
How do you concatenate two tuples in Python?
Correct answer:
B) tuple1 + tuple2
Explanation: The + operator is used to concatenate two tuples in Python.
What does list[::-1] do?
Correct answer:
A) Reverses the list
Explanation: The slice notation [::-1] is used to reverse a list.
How do you declare a set in Python?
Correct answer:
D) set()
Explanation: An empty set is declared using set() because {} is used to declare an empty dictionary.
Which method removes all elements from a set?
Correct answer:
B) clear()
Explanation: The clear() method removes all elements from a set, leaving it empty.
What is the output of print(3 == 3.0)?
Correct answer:
A) True
Explanation: In Python, == compares values, and 3 is considered equal to 3.0 since their numeric values are the same.
How do you convert a list to a set in Python?
Correct answer:
A) set(list)
Explanation: The set() function converts a list into a set, removing any duplicates in the process.
Which of the following is immutable in Python?
Correct answer:
C) Tuple
Explanation: A tuple is immutable, meaning that once it is created, its elements cannot be changed.
What is the purpose of the lambda keyword in Python?
Correct answer:
B) To create anonymous functions
Explanation: The lambda keyword is used to create small anonymous functions in Python.
What does the isinstance() function check?
Correct answer:
B) If an object belongs to a specified class or type
Explanation: The isinstance() function checks if an object is an instance or subclass of a class or type.
What is the output of print('python'.capitalize())?
Correct answer:
A) Python
Explanation: The capitalize() method converts the first character of a string to uppercase and the rest to lowercase.
How do you check the length of a set in Python?
Correct answer:
C) len(set)
Explanation: The len() function returns the number of elements in a set.
Which operator is used for logical AND in Python?
Correct answer:
C) and
Explanation: The and operator is used for logical AND in Python.
How do you create a dictionary with keys 'a', 'b', and 'c' and corresponding values 1, 2, and 3?
Correct answer:
D) Both a and c
Explanation: Both methods are valid ways to create a dictionary in Python.
What is the output of print("Hello".replace("l", "x"))?
Correct answer:
A) Hexxo
Explanation: The replace() method replaces all occurrences of "l" with "x" in the string "Hello".
How do you remove duplicates from a list in Python?
Correct answer:
C) list(set(list))
Explanation: Converting a list to a set removes duplicates, and then converting it back to a list restores the original structure.
What is the correct way to check if a key exists in a dictionary?
Correct answer:
A) if key in dict
Explanation: The in operator checks if a key exists in a dictionary.
What is the result of type(3.14) in Python?
Correct answer:
B) float
Explanation: The number 3.14 is of type float.
How do you iterate over the keys and values of a dictionary simultaneously?
Correct answer:
B) for key, value in dict.items():
Explanation: The items() method returns both keys and values, allowing iteration over both.
What is the difference between remove() and pop() for lists in Python?
Correct answer:
A) remove() removes by value, pop() removes by index
Explanation: remove() removes the first matching value, while pop() removes an element at a specific index.
What does the zip() function do in Python?
Correct answer:
B) Combines elements from multiple iterables into tuples
Explanation: The zip() function pairs elements from two or more iterables into tuples.
How do you stop an infinite loop in Python?
Correct answer:
B) break
Explanation: The break statement is used to exit a loop prematurely, preventing infinite execution.
What will be the output of print(bool([]))?
Correct answer:
B) False
Explanation: An empty list evaluates to False in a Boolean context.
What is the function of the with statement in Python?
Correct answer:
D) To ensure proper cleanup of resources
Explanation: The with statement is used to wrap the execution of a block of code and ensures resources are properly released, such as when working with file handling.
What does the del keyword do in Python?
Correct answer:
D) Deletes a variable or object
Explanation: The del keyword is used to delete variables or objects in Python.
Which operator is used for exponentiation in Python?
Correct answer:
B) **
Explanation: The ** operator is used for exponentiation, raising a number to the power of another.
What will be the result of print(3 + 2 * 2)?
Correct answer:
B) 7
Explanation: The multiplication is performed first, so the expression becomes 3 + 4, which equals 7.
What is the default value of the step parameter in the range() function?
Correct answer:
B) 1
Explanation: The default value of the step parameter in range() is 1, meaning the sequence increments by 1 unless otherwise specified.
How do you check if a string contains only digits in Python?
Correct answer:
B) isdigit()
Explanation: The isdigit() method returns True if all characters in the string are digits, otherwise False.
What is the output of print("ABCD"[1:3])?
Correct answer:
C) BC
Explanation: The slice [1:3] returns characters from index 1 up to, but not including, index 3.
Which of the following is not a valid way to create a string in Python?
Correct answer:
D) "Hello'
Explanation: Strings in Python must start and end with the same type of quotation marks.
What is the output of print(type({}))?
Correct answer:
B) dict
Explanation: An empty pair of curly braces {} creates an empty dictionary in Python.
What is the function of the ord() method?
Correct answer:
A) Returns the Unicode point for a character
Explanation: The ord() method returns the Unicode code point of a single character.
Which of the following is not a Python built-in function?
Correct answer:
D) clear()
Explanation: clear() is a method for certain data types like dictionaries and sets, but not a built-in function.
How do you convert an integer to a string in Python?
Correct answer:
A) str()
Explanation: The str() function converts any data type to a string.
What is the result of print(4 / 2)?
Correct answer:
B) 2.0
Explanation: Division / in Python always returns a float, even if the result is a whole number.
Which of the following functions can be used to create a new tuple?
Correct answer:
C) tuple()
Explanation: The tuple() function is used to create a new tuple in Python.
What is the default value of the start parameter in the range() function?
Correct answer:
B) 0
Explanation: The start parameter in range() defaults to 0, meaning the sequence starts at 0 unless otherwise specified.
Which of the following is true about Python lists?
Correct answer:
B) Lists are ordered collections
Explanation: Lists in Python are ordered, meaning the elements are indexed and maintain the order in which they were added.
How do you access the last element of a list in Python?
Correct answer:
A) list[-1]
Explanation: The index -1 refers to the last element in a list in Python.
Which of the following is not a valid Python loop control structure?
Correct answer:
C) do-while
Explanation: Python does not have a do-while loop; it only supports for and while loops.
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.