








































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









































































DIFFICULTY: Mixed (Foundational to Intermediate) SECTION 1: PYTHON BASICS, VARIABLES, AND DATA TYPES --- Q1. Which symbol is used to begin a single-line comment in Python? A) // B) # C) /* D) Rationale: Numbers containing a decimal point are of the float (floating-point) data type. Integers (int) do not have a decimal point. Q3. Which of the following is a valid variable name in Python? A) 2nd_place B) my-var C) _myVariable D) my var Correct ANSWER: C Rationale: Variable names can start with a letter or underscore, cannot start with a digit, and cannot contain hyphens or spaces. _myVariable is the only valid option. Q4. What is the output of the following code? print(type(10 / 2)) A) B) C) D) Correct ANSWER: B Rationale: In Python 3, the / operator performs true division and always returns a float, even if the result is a whole number (e.g., 10 / 2 = 5.0). Q5. What is the output of bool([]) (an empty list)? A) True B) False C) None D) Error Correct ANSWER: B
Correct ANSWER: B Rationale: The floor division operator // returns the integer quotient, discarding any remainder. 5 // 2 equals 2. Q9. What is the result of 17 % 5 in Python? A) 3. B) 2 C) 3 D) 3. Correct ANSWER: B Rationale: The modulo operator % returns the remainder of a division operation. 17 divided by 5 is 3 with a remainder of 2. Q10. Which operator is used to check if two values are equal in Python? A) = B) == C) != D) === Correct ANSWER: B Rationale: == is the equality comparison operator. = is the assignment operator. != means "not equal". === is not a valid operator in Python. Q11. Which function is used to convert a string to an integer in Python? A) str() B) int() C) float() D) convert()
Correct ANSWER: B Rationale: The int() function converts a value (like a string containing a number) to an integer data type. Q12. What is the result of int("3.14")? A) 3 B) 3. C) Error D) 4 Correct ANSWER: C Rationale: int() cannot directly convert a string containing a decimal point to an integer. It will raise a ValueError. To do this, you must first convert to a float (e.g., int(float("3.14"))). Q13. What is the output of print(2 ** 3)? A) 6 B) 8 C) 9 D) 5 Correct ANSWER: B Rationale: The ** operator performs exponentiation. 2 ** 3 means 2 raised to the power of 3, which equals 8. Q14. What will the following code output? print(3 + 2 * 4) A) 20 B) 14 C) 11
Correct ANSWER: B Rationale: _class is valid. class is a reserved keyword and cannot be used. Variable names cannot start with a number or contain hyphens. Q18. What will print(5 + 5) output? A) 10 B) 55 C) Error D) 5+ Correct ANSWER: A Rationale: The + operator performs arithmetic addition on integers, resulting in 10. Q19. What will print("5" + "5") output? A) 10 B) 55 C) Error D) 5+ Correct ANSWER: B Rationale: The + operator performs string concatenation when used with strings, joining them together to form "55". Q20. What is the output of len("Python")? A) 5 B) 6 C) 7 D) 4 Correct ANSWER: B
Rationale: The len() function returns the number of characters in a string. "Python" has 6 characters. Q21. Which method is used to remove whitespace from the beginning and end of a string? A) strip() B) trim() C) remove() D) cut() Correct ANSWER: A Rationale: The strip() method removes leading and trailing whitespace from a string. Q22. What is the output of "HELLO".lower()? A) "HELLO" B) "hello" C) "Hello" D) "hELLO" Correct ANSWER: B Rationale: The lower() method converts all characters in a string to lowercase. Q23. What is the output of "hello".upper()? A) "hello" B) "HELLO" C) "Hello" D) "hELLO" Correct ANSWER: B Rationale: The upper() method converts all characters in a string to uppercase.
Q27. Which escape sequence is used for a newline in Python? A) \t B) \n C) \r D) \b Correct ANSWER: B Rationale: The newline escape sequence is \n. \t is for a tab, \r is for a carriage return, and \b is for backspace. Q28. What is the output of print("Hello\nWorld")? A) Hello\nWorld B) Hello World C) Hello World D) "Hello" "World" Correct ANSWER: C Rationale: The \n escape sequence inserts a newline, so "Hello" and "World" are printed on separate lines. Q29. What is the output of print("Hello\tWorld")? A) Hello\tWorld B) Hello World C) Hello World D) HelloWorld Correct ANSWER: C
Rationale: The \t escape sequence inserts a tab character, creating horizontal spacing between "Hello" and "World". Q30. What is the output of print("Hello\\nWorld")? A) Hello\nWorld B) Hello World C) Hello World D) Error Correct ANSWER: A Rationale: The double backslash \\ escapes the backslash character itself, printing a literal backslash followed by "n". So Hello\nWorld is printed literally. Q31. What is the output of print("He said, \"Hello\"")? A) He said, "Hello" B) He said, "Hello" C) He said, Hello D) Error Correct ANSWER: B Rationale: The backslash \" escapes the double quote, allowing it to be included as a literal character within the string. Q32. Which function is used to get user input in Python? A) input() B) get() C) read() D) scan()
Rationale: The int() function converts the string returned by input() to an integer, so the result is 42 (an int). Q36. What happens if a user enters "abc" and the code tries int(input())? A) The program runs without error. B) The program raises a ValueError. C) The program raises a TypeError. D) The program returns 0. Correct ANSWER: B Rationale: int() cannot convert a non-numeric string to an integer, so it raises a ValueError. Q37. What is the output of float(input("Enter a number: ")) if the user enters 3.14? A) 3. B) '3.14' C) Error D) 3 Correct ANSWER: A Rationale: The float() function converts the string to a floating-point number. Q38. What is the result of eval("2 + 3")? A) 5 B) '2 + 3' C) Error D) None Correct ANSWER: A
Rationale: The eval() function evaluates the expression in the string and returns the result (5). Q39. What is the result of eval("'Hello' + 'World'")? A) HelloWorld B) 'HelloWorld' C) Error D) None Correct ANSWER: A Rationale: eval() evaluates the string expression, performing string concatenation. Q40. What is the output of str(42)? A) 42 B) '42' C) Error D) None Correct ANSWER: B Rationale: str() converts the integer to a string representation. Q41. What is the output of float(5)? A) 5 B) 5. C) '5' D) Error Correct ANSWER: B Rationale: float() converts an integer to a floating-point number.
Q45. What is the output of abs(-5)? A) - 5 B) 0 C) 5 D) Error Correct ANSWER: C Rationale: The abs() function returns the absolute value of a number. Q46. What is the output of min(3, 7, 1, 9)? A) 9 B) 7 C) 3 D) 1 Correct ANSWER: D Rationale: min() returns the smallest value among its arguments. Q47. What is the output of max(3, 7, 1, 9)? A) 9 B) 7 C) 3 D) 1 Correct ANSWER: A Rationale: max() returns the largest value among its arguments. Q48. What is the output of sum([1, 2, 3, 4])? A) 6
Correct ANSWER: B Rationale: sum() returns the sum of all elements in the iterable. Q49. What is the output of pow(2, 3)? A) 6 B) 8 C) 9 D) 5 Correct ANSWER: B Rationale: pow(2, 3) calculates 2 raised to the power of 3, which is 8. Q50. What is the output of pow(2, 3, 3)? A) 8 B) 2 C) 3 D) Error Correct ANSWER: B Rationale: pow(base, exp, mod) calculates (base ** exp) % mod. (2 ** 3) % 3 = 8 % 3 = 2. Q51. What is the output of divmod(17, 5)? A) (3, 2) B) (2, 3) C) 3.
Correct ANSWER: B Rationale: 10.0 is a floating-point number, so its type is float. Q55. What is the output of type("10")? A) B) C) D) Correct ANSWER: C Rationale: "10" is a string, so its type is str. Q56. What is the output of type(None)? A) B) C) D) Correct ANSWER: B Rationale: None is of type NoneType, representing the absence of a value. Q57. What is the output of isinstance(10, int)? A) True B) False C) Error D) None Correct ANSWER: A
Rationale: isinstance() checks if an object is an instance of a specified class. 10 is an instance of int, so it returns True. Q58. What is the output of isinstance("hello", int)? A) True B) False C) Error D) None Correct ANSWER: B Rationale: "hello" is a string, not an integer, so it returns False. Q59. What is the output of isinstance(3.14, float)? A) True B) False C) Error D) None Correct ANSWER: A Rationale: 3.14 is a float, so it returns True. Q60. What is the output of isinstance(True, bool)? A) True B) False C) Error D) None Correct ANSWER: A Rationale: True is a Boolean value, so it returns True.