




























































































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!





























































































Exam Coverage: All 12 Chapters — Python Fundamentals · Variables & Data Types · Operators & Expressions · Control Flow (Conditionals) · Loops & Iteration · Functions & Scope · Data Structures (Lists, Tuples, Dictionaries, Sets) · String Manipulation · File I/O & Exception Handling · Modules & Imports · Object-Oriented Programming Basics · Debugging & Testing Instructions: Each question is followed immediately by the correct answer and a detailed rationale. Questions are numbered sequentially from 1 to 1,000+ across all chapters.
Question 1: Which symbol begins a single-line comment in Python? A) // B) # C) -- D) /* Answer: B
Explanation: In Python, the hash symbol (#) begins a single-line comment. Everything after # on that line is ignored by the interpreter. The // symbol is used for integer division in Python, not comments. Question 2: What is the correct file extension for a Python source file? A) .pt B) .pyt C) .py D) .python Answer: C Explanation: Python source files use the .py extension. For example, hello.py is a valid Python file name. Question 3: Which of the following is the correct way to output text to the console in Python? A) console.log("Hello") B) print("Hello") C) echo "Hello" D) printf("Hello") Answer: B
Explanation: Variable names can contain letters, digits, and underscores, but cannot start with a digit, cannot contain hyphens, and cannot be reserved keywords like class. Question 6: What will the following code output? python print("Hello" + " " + "World") A) Hello World B) HelloWorld C) Hello+World D) Error Answer: A Explanation: The + operator concatenates strings, including spaces. The output is Hello World. Question 7: Which function is used to get user input in Python 3? A) raw_input() B) input() C) scanf() D) read() Answer: B
Explanation: In Python 3, input() reads a line from the user and returns it as a string. raw_input() was used in Python 2 and no longer exists. Question 8: What is the output of print(type(42))? A) B) C) D) Answer: A Explanation: 42 is an integer literal, so type() returns . Question 9: Which keyword is used to define a function in Python? A) function B) def C) func D) define Answer: B Explanation: The def keyword begins a function definition in Python, e.g., def my_function():.
C) n D) Error Answer: A Explanation: String indexing starts at 0 in Python, so [0] returns the first character, "P". Question 13: Which statement about Python is true? A) Python is a compiled language only B) Python is an interpreted language C) Python requires semicolons at the end of each line D) Python code must be written in uppercase Answer: B Explanation: Python is an interpreted language, meaning code is executed line by line by the Python interpreter. Semicolons are optional and rarely used. Question 14: What does the following code do? python name = input("Enter your name: ") print("Hello, " + name) A) Prints "Hello, name" B) Asks for input and prints a personalized greeting
C) Causes an error D) Prints only "Hello," Answer: B Explanation: input() prompts the user, stores their response in the variable name, then print() concatenates "Hello, " with the user's input. Question 15: Which of the following is the correct way to write a multi-line string? A) "This is a string" B) 'This is a string' C) """This is a multi-line string""" D) This is a string Answer: C Explanation: Triple quotes (""" or ''') allow strings to span multiple lines in Python. Question 16: What is the output of print(2 ** 3)? A) 6 B) 8 C) 9 D) 5
Answer: C Explanation: The % operator returns the remainder of division. 5 divided by 2 leaves a remainder of 1. Question 19: Which of the following correctly assigns a value to a variable? A) 10 = x B) x := 10 C) x = 10 D) let x = 10 Answer: C Explanation: In Python, assignment uses a single equals sign: variable = value. The walrus operator (:=) exists in Python 3.8+ but is used for assignment expressions within larger expressions, not simple assignment. Question 20: What is the output of print(bool(0))? A) True B) False C) 0 D) Error Answer: B
Explanation: In Python, 0 evaluates to False when converted to a boolean. Most other numbers evaluate to True. Question 21: Which method is used to convert a string to all uppercase? A) toUpper() B) uppercase() C) upper() D) toUpperCase() Answer: C Explanation: The .upper() method returns a copy of the string converted to uppercase, e.g., "hello".upper() returns "HELLO". Question 22: What will the following code print? python print(len("Python")) A) 5 B) 6 C) 7 D) Error Answer: B Explanation: The len() function returns the number of characters in a string. "Python" has 6 characters.
Question 25: Which of the following statements about Python indentation is correct? A) Indentation must be exactly 4 spaces B) Indentation must be consistent within a block C) Indentation is optional D) Indentation must use tabs only Answer: B Explanation: Python requires consistent indentation within a code block, but the number of spaces can vary (commonly 4). Mixing tabs and spaces can cause errors. Question 26: What is the output of the following code? python x = "10" y = int(x) print(y + 5 ) A) 105 B) 15 C) 10 + 5 D) Error Answer: B Explanation: int(x) converts the string "10" to the integer 10. Adding 5 gives 15.
Question 27: Which of the following is used to define a block of code that runs only when a condition is true? A) for B) while C) if D) def Answer: C Explanation: The if statement executes a block of code only when its condition evaluates to True. Question 28: What does the following code do? python for i in range( 3 ): print(i) A) Prints 1, 2, 3 B) Prints 0, 1, 2 C) Prints 0, 1, 2, 3 D) Prints 3 three times Answer: B Explanation: range(3) generates the sequence 0, 1, 2. The loop prints each value.
python a = 10 b = 3 print(a / b) A) 3 B) 3. C) 3. D) Error Answer: C Explanation: The / operator performs true division and returns a float. 10 / 3 = 3.3333333333333335. Question 32: Which of the following is NOT a valid Python data type? A) int B) float C) char D) str Answer: C Explanation: Python does not have a separate char type. Single characters are represented as strings of length 1. Question 33: What is the output of print("Hello"[1:4])?
A) Hel B) ell C) llo D) Hell Answer: B Explanation: Slicing [1:4] returns characters at indices 1, 2, and 3, which are "e", "l", "l" — "ell". Question 34: Which keyword is used to exit a loop prematurely? A) exit B) stop C) break D) return Answer: C Explanation: The break statement immediately terminates the innermost loop and transfers control to the statement after the loop. Question 35: What does the following code output? python print("5" + "5") A) 10 B) 55
Explanation: round() rounds to the nearest integer. 3.7 rounds to 4. Question 38: Which of the following correctly checks if a value is of type int? A) if type(x) == int: B) if x is int: C) if x.type() == int: D) if isinstance(x, int): Answer: D Explanation: isinstance(x, int) is the preferred way to check if x is an integer. It also returns True for subclasses. Question 39: What is the output of the following code? python x = 5 x += 3 print(x) A) 5 B) 3 C) 8 D) 53 Answer: C
Explanation: x += 3 is shorthand for x = x + 3. Starting at 5, adding 3 gives 8. Question 40: Which of the following is a correct way to write a docstring? A) # This is a docstring B) /* This is a docstring */ C) """This is a docstring""" D) // This is a docstring Answer: C Explanation: Docstrings are triple-quoted strings placed at the beginning of a module, function, class, or method to document it. Question 41: What is the output of print(10 > 5 and 3 < 1)? A) True B) False C) Error D) None Answer: B Explanation: 10 > 5 is True, but 3 < 1 is False. The and operator returns True only if both operands are True.