




























































































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!





























































































Question 1. What is the correct file extension for a standard Python script file? A) .js B) .py C) .java D) .exe Answer: B Explanation: Python script files use the .py extension to indicate they contain Python code. Question 2. Which of the following is a mutable data type in Python? A) Tuple B) String
C) List D) Integer Answer: C Explanation: Lists in Python can be modified after creation, making them mutable, while tuples and strings are immutable. Question 3. What is the output of the following code? print(type(3.5)) A) <class 'int'> B) <class 'float'> C) <class 'str'> D) <class 'complex'> Answer: B Explanation: 3.5 is a floating-point number, so the type is float.
C) insert() D) extend() Answer: B Explanation: The append() method adds a single element to the end of a list. Question 6. How can you access the value associated with the key 'a' in the dictionary d = {'a':1, 'b':2}? A) d[a] B) d.'a' C) d['a'] D) d[a,1] Answer: C
Explanation: Dictionary values are accessed using their keys in square brackets. Question 7. What does the 'break' statement do in a loop? A) Skips to the next iteration B) Exits the loop immediately C) Restarts the loop D) Pauses the loop Answer: B Explanation: The break statement terminates the nearest enclosing loop. Question 8. Which of the following is the correct way to define a function called foo with no parameters?
Answer: B Explanation: '**' is the exponentiation operator, so 2 ** 3 equals 8. Question 10. Which statement correctly creates a tuple? A) t = [1, 2, 3] B) t = (1, 2, 3) C) t = {1, 2, 3} D) t = <1, 2, 3> Answer: B Explanation: Tuples are defined using parentheses. Question 11. If s = "python", what does s[1:4] return? A) pyt
B) yth C) ytho D) pyth Answer: B Explanation: Slicing from index 1 up to, but not including, 4 returns 'yth'. Question 12. Which of the following is NOT a valid variable name in Python? A) var B) 2var C) var D) varTwo Answer: B
B) Exits the loop C) Does nothing D) Returns a value Answer: C Explanation: The pass statement is a null operation; it is used as a placeholder. Question 15. What will be printed by: print("5" + "6")? A) 11 B) 56 C) 5 6 D) Error Answer: B
Explanation: Both operands are strings, so they are concatenated as '56'. Question 16. Which operator is used for integer division in Python 3? A) / B) % C) // D)
Answer: C Explanation: '//' performs integer (floor) division. Question 17. What is the result of bool([])? A) True
Question 19. What does the following code print? print(type(b'hello')) A) <class 'str'> B) <class 'bytes'> C) <class 'bytearray'> D) <class 'list'> Answer: B Explanation: The prefix 'b' creates a bytes object. Question 20. Which function converts an object to a string? A) int() B) str() C) float()
D) chr() Answer: B Explanation: str() converts its argument to a string. Question 21. What is the output of: print(list("abc"))? A) ['abc'] B) ['a', 'b', 'c'] C) ('a', 'b', 'c') D) {'a', 'b', 'c'} Answer: B Explanation: list() on a string creates a list of its characters. Question 22. Which statement is used to handle exceptions in Python?
Explanation: % is the modulus operator; 10 % 3 is 1. Question 24. How do you open a file named 'test.txt' for reading? A) open('test.txt', 'w') B) open('test.txt', 'r') C) open('test.txt', 'rw') D) open('test.txt', 'a') Answer: B Explanation: Mode 'r' opens the file for reading. Question 25. Which method reads all lines of a file into a list? A) read() B) readline()
C) readlines() D) readall() Answer: C Explanation: readlines() reads the entire file and returns a list of lines. Question 26. Which keyword is used to define a class? A) object B) class C) def D) struct Answer: B Explanation: The 'class' keyword is used to define a class in Python.
Answer: B Explanation: init is the constructor method, called when an object is instantiated. Question 29. How do you comment a single line in Python? A) // comment B) /* comment */ C) # comment D) Answer: C Explanation: The hash symbol (#) indicates a comment in Python. Question 30. What is the difference between '==' and 'is' in Python?
A) No difference B) '==' compares values, 'is' compares identities C) 'is' compares values, '==' compares identities D) Both compare types only Answer: B Explanation: '==' checks value equality; 'is' checks object identity. Question 31. Which of the following is a valid way to import the math module? A) import math B) import Math C) #import math D) include math