


























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!



























1. What is the correct way to output the string "Hello, World!" in Python?
A) printf("Hello, World!") B) console.log("Hello, World!") C) print("Hello, World!") D) echo "Hello, World!"
Correct Answer: C) print("Hello, World!") Rationale: Python’s built-in print() function outputs text to the console. The other options are used in other languages (C, JavaScript, PHP). Understanding the correct syntax for output is foundational to Python programming.
2. Which of the following is a valid variable name in Python?
A) 2nd_place B) my-var C) my_var D) class
Correct Answer: C) my_var Rationale: Variable names must start with a letter or underscore, cannot contain hyphens, and cannot be a reserved keyword (class is a keyword). Underscores are allowed and commonly used.
3. What is the data type of the value 3.14 in Python?
A) int B) float C) string D) boolean
Correct Answer: B) float Rationale: Numbers with a decimal point are of type float (floating-point). Integers have no decimal point.
4. Given x = 5 and y = 2, what is the result of x // y?
A) 2. B) 2 C) 2. D) 1
Correct Answer: B) 2 Rationale: The floor division operator // returns the integer quotient (discards the remainder). 5 // 2 = 2. This is distinct from true division (/), which always returns a float.
5. 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 valid in Python.
6. What is the output of print(type(10))?
A) B) C) 'int' D)
Correct Answer: A) Rationale: The type() function returns the type object; printing it displays for an integer.
7. Which of the following is NOT a valid way to create a string in Python?
A) 'hello' B) "hello" C) """hello""" D) hello
C) boolean D) float
Correct Answer: D) float Rationale: Python has several built-in data types including int, float, str, bool, and complex. While "string," "boolean," and "number" are conceptual terms, they are not the actual Python data type names. "Float" is the correct Python data type for decimal numbers.
12. What is the correct way to declare a variable that stores the number 42?
A) var x = 42 B) x == 42 C) x = 42 D) declare x = 42
Correct Answer: C) x = 42 Rationale: In Python, variables are assigned values using the single equals sign (=). The other options show syntax from other programming languages or use incorrect Python syntax.
13. What does the input() function return?
A) An integer B) A float C) A string D) A boolean
Correct Answer: C) A string Rationale: The input() function always returns a string, even if the user enters digits. Conversion (e.g., int(input())) is needed for numeric operations.
14. Which of the following is a valid integer literal in Python?
A) 3. B) "42" C) 42 D) True
Correct Answer: C) 42 Rationale: An integer literal is a whole number without a decimal point or quotes. 3.0 is a float, "42" is a string, and True is a boolean.
15. What is the output of print(5 + 3 * 2)?
A) 16 B) 11 C) 13 D) 10
Correct Answer: B) 11 Rationale: Python follows standard operator precedence: multiplication before addition. 3 * 2 = 6, then 5 + 6 = 11.
16. Which of the following is a reserved keyword in Python?
A) variable B) define C) class D) value
Correct Answer: C) class Rationale: class is a reserved keyword used to define classes. variable, define, and value are valid identifiers (not keywords).
17. What is the output of print("5" + "3")?
A) 8 B) 53 C) "53" D) Error
Correct Answer: B) 53 Rationale: The + operator concatenates strings. "5" + "3" produces the string "53", which print() displays without quotes as 53.
18. Which of the following correctly converts the string "10" to an integer?
A) int("10") B) str(10) C) float("10") D) "10".int()
Correct Answer: A) int("10") Rationale: The int() function converts a string to an integer. str() converts to string, float() converts to float, and "10".int() is not valid syntax.
Correct Answer: C) = Rationale: = is the assignment operator, not a comparison operator. ==, !=, and <= are comparison operators.
23. What is the output of print(True and False)?
A) True B) False C) Error D) None
Correct Answer: B) False Rationale: The and operator returns True only if both operands are True. True and False evaluates to False.
24. Which of the following is a valid float literal?
A) 3 B) 3. C) "3.0" D) 3,
Correct Answer: B) 3. Rationale: A float literal contains a decimal point. 3 is an int, "3.0" is a string, and 3,0 is invalid syntax.
25. What is the result of "Hello".upper()?
A) "hello" B) "HELLO" C) "Hello" D) Error
Correct Answer: B) "HELLO" Rationale: The upper() method returns a copy of the string with all characters converted to uppercase.
26. Which function is used to get the length of a list?
A) size() B) length()
C) len() D) count()
Correct Answer: C) len() Rationale: The built-in len() function returns the number of items in a list (or other sequence).
27. What is the output of print(3 == 3.0)?
A) True B) False C) Error D) None
Correct Answer: A) True Rationale: In Python, 3 (int) and 3.0 (float) compare equal because they represent the same numeric value. == compares value, not type.
28. Which of the following is the correct way to import the math module?
A) include math B) import math C) using math D) require math
Correct Answer: B) import math Rationale: The import statement is used to bring a module into the current namespace. import math makes math functions available (e.g., math.sqrt()).
29. What is the output of print(bool(0))?
A) True B) False C) 0 D) Error
Correct Answer: B) False Rationale: bool(0) converts the integer 0 to False. In Python, 0, 0.0, "", [], {}, and None are falsy; most other values are truthy.
30. Which of the following is a valid way to concatenate two strings a and b?
C) To define a new variable D) To import a module
Correct Answer: B) To exit the function and send a value back to the caller Rationale: return ends the function’s execution and provides a value to the code that called the function. Without return, a function returns None by default.
34. What is the output of the following code?
python
def square(x):
return x * x
result = square(4)
print(result)
A) 4 B) 8 C) 16 D) Error
Correct Answer: C) 16 Rationale: The function square returns x * x. square(4) returns 16, which is stored in result and printed.
35. What is the output of the following code?
python
for i in range(3):
print(i)
A) 1 2 3 B) 0 1 2 C) 0 1 2 3 D) 1 2
Correct Answer: B) 0 1 2 Rationale: range(3) generates 0, 1, 2. The loop prints each value on a separate line.
36. What is the output of the following code?
python
x = 10
if x > 5:
print("A")
else:
print("B")
A) A B) B C) AB D) Error
Correct Answer: A) A Rationale: The condition x > 5 is True (10 > 5), so the if block executes and prints A. The else block is skipped.
37. What is the output of the following code?
python
i = 0
while i < 3:
print(i)
i += 1
A) 0 1 2 B) 1 2 3 C) 0 1 2 3 D) Infinite loop
Correct Answer: A) 0 1 2 Rationale: The loop starts at i = 0 and increments by 1 while i < 3. It prints 0, 1, and 2, then exits.
38. What does the break statement do inside a loop?
A) Skips the current iteration and continues with the next B) Exits the loop immediately C) Restarts the loop D) Ends the program
C) Error D) None
Correct Answer: B) 6 Rationale: b has a default value of 2. multiply(3) uses a=3 and b=2, returning 6.
42. What is the scope of a variable defined inside a function?
A) Global B) Local C) Module-level D) Built-in
Correct Answer: B) Local Rationale: Variables defined inside a function are local to that function and are not accessible outside unless returned or declared global.
43. What is the output of the following code?
python
def outer():
x = 10
def inner():
print(x)
inner()
outer()
A) 10 B) Error C) None D) 0
Correct Answer: A) 10 Rationale: The inner function can access the enclosing function’s variable x (closure). It prints 10.
44. What is the output of the following code?
python
def f(x):
if x > 0:
return "Positive"
elif x < 0:
return "Negative" else:
return "Zero"
print(f(0))
A) Positive B) Negative C) Zero D) Error
Correct Answer: C) Zero Rationale: The condition x > 0 is False, x < 0 is False, so the else branch executes and returns "Zero".
45. What is the output of the following code?
python
total = 0
for i in range(1, 4):
total += i
print(total)
A) 6 B) 10 C) 3 D) Error
Correct Answer: A) 6 Rationale: range(1, 4) yields 1, 2, 3. total accumulates 1 + 2 + 3 = 6.
46. What is the output of the following code?
Correct Answer: A) 2 4 6 8 Rationale: range(2, 10, 2) starts at 2, stops before 10, and steps by 2: 2, 4, 6, 8.
49. What is the output of the following code?
python
x = 5
if x > 3:
if x < 10:
print("A")
else:
print("B")
else:
print("C")
A) A B) B C) C D) Error
Correct Answer: A) A Rationale: x > 3 is True, and x < 10 is also True, so the inner if block executes and prints A.
50. What is the output of the following code?
python
def f():
return
print(f())
A) None B) 0 C) Error D) No output
Correct Answer: A) None Rationale: A function without a return value (or with a bare return) returns None by default. print(f()) displays None.
51. What is the output of the following code?
python
def f(x):
if x % 2 == 0:
return "Even"
return "Odd"
print(f(7))
A) Even B) Odd C) Error D) None
Correct Answer: B) Odd Rationale: 7 % 2 equals 1, which is not 0, so the if condition is False. The function proceeds to return "Odd".
52. What is the output of the following code?
python
count = 0
while count < 5:
count += 1
if count == 3: break
print(count)
A) 3 B) 5 C) 0 D) Error
def f():
x = 10
x = 5
f()
print(x)
A) 10 B) 5 C) Error D) None
Correct Answer: B) 5 Rationale: The variable x inside f() is local and does not affect the global x. The global x remains 5, which is printed.
56. What is the output of the following code?
python
def f(x):
return x * 2
result = f(3) + f(4)
print(result)
A) 14 B) 12 C) 7 D) 24
Correct Answer: A) 14 Rationale: f(3) returns 6, f(4) returns 8. 6 + 8 = 14.
57. What is the output of the following code?
python
for i in range(3):
for j in range(2):
print(i, j)
Correct Answer: A) 0 0 0 1 1 0 1 1 2 0 2 1 Rationale: The nested loops iterate i from 0 to 2 and j from 0 to 1, printing all combinations: (0,0), (0,1), (1,0), (1,1), (2,0), (2,1).
58. What is the output of the following code?
python
def f(x):
if x > 10:
return "Large"
elif x > 5:
return "Medium" else:
return "Small"
print(f(7))
A) Large B) Medium C) Small D) Error
Correct Answer: B) Medium Rationale: 7 is not greater than 10, but it is greater than 5, so the elif branch returns "Medium".
59. What is the output of the following code?
python
def f(n):
if n <= 1:
return 1
return n * f(n - 1)