Python Coding Practice - 1 (1 - 15 Questions)

Read each question carefully. Fill in missing code or select the correct option. Click Check to verify your answer.

Q1. Print Hello World.
print("")
Use exact text: Hello World.
Answer: Hello World.
Explanation: The print function outputs the string inside quotes.
Q2. Take name input from the user.
name = ("Enter name: ")
print(name)
Built-in function for user input is input().
Answer: input
Explanation: input(prompt) reads text from the user and returns it as a string.
Q3. Check if number is positive.
x = 10
x > 0:
  print("Positive")
Use the conditional keyword.
Answer: if
Explanation: if condition: executes the block when condition is True.
Q4. Print numbers from 1 to 5.
for i in (1, 6):
  print(i)
range(start, stop) excludes stop.
Answer: range
Explanation: range(1, 6) yields 1,2,3,4,5.
Q5. Which statement correctly prints the following pattern?
*
* *
* * *
for i in range(3): print( ? )
Answer: "* " * (i + 1)
Explanation: String multiplication repeats the pattern. We need 1, 2, 3 stars with spaces.
Q6. Print first character of string.
text = "Python"
print(text[])
Indexing is zero-based.
Answer: 0
Explanation: text[0] is 'P'.
Q7. Reverse the string using slicing.
text = "abc"
print(text[])
Use a negative step in the slice.
Answer: ::-1
Explanation: Full slice with step -1 reverses the string.
Q8. Add 40 to the list.
nums = [10, 20, 30]
nums.(40)
Use the method to add one element at the end.
Answer: append
Explanation: append adds a single item to the list.
Q9. Remove duplicates from list (keep as list).
lst = [1,2,2,3]
unique = (lst)
Convert to a set for uniqueness, then back to a list.
Answer: list(set(lst))
Explanation: set removes duplicates; list converts back to list (order not preserved).
Q10. Add value to set.
s = {1,2}
s.(3)
Use the method that inserts a single element into a set.
Answer: add
Explanation: add inserts one element; update expects an iterable.
Q11. Create dictionary with key "age".
student = {"name": "A", ____ : 20}
Answer: "age"
Explanation: Dictionary string keys must be quoted. Either double or single quotes are okay in Python, but we match the expected answer here.
Q12. Access value using key.
student = {"name": "A", "age": 20}
print(student[ ____ ])
Answer: "name"
Explanation: Use bracket access with the string key; dot access doesn't work for dicts.
Q13. Count characters using dictionary (safe access method).
freq[ch] = freq.(ch, 0) + 1
Method returns value if key exists, else default.
Answer: get
Explanation: get(ch, 0) avoids KeyError and provides default 0.
Q14. Loop through dictionary keys.
for k in student.(): print(k)
Method returns a view of keys.
Answer: keys
Explanation: keys() iterates over all dictionary keys.
Q15. Check membership efficiently.
my_set = {1, 2, 3, 4}
x = 3
if x in : print("Found")
Use a set for O(1) average-time membership tests.
Answer: my_set
Explanation: Sets provide fast membership checks compared to lists.
Your Score: 0 / 15

Post a Comment

0 Comments