Here's the breakdown of the questions and their correct answers:
-
Identify the error in the syntax of the following code:
def is_year(year): if year % 4 == 0: print('Year is divisible by 4') else year % 4 != 0: print('Year is not divisible by 4')
- Correct Answer: You cannot add a conditional statement after the else keyword.
- Explanation: The
else
keyword should not have a condition; it should simply beelse:
.
-
How can we filter out numbers less than 2 in a number sequence to find prime numbers?
- Correct Answer: We can have a conditional check to filter out numbers less than 2.
- Explanation: Prime numbers are defined as being greater than 1, so a simple conditional check is enough to filter them.
-
Consider a list [a, b, c, d]. Which element exists at index 4?
- Correct Answer: There exists no element at index 4.
- Explanation: In Python (and most programming languages), indexing starts at 0. Therefore, a four-element list (
[a, b, c, d]
) has valid indices 0, 1, 2, and 3.
-
Which of the following is the method of list traversal?
- Correct Answer: for loop.
- Explanation: A
for loop
is commonly used to traverse through lists in programming.
-
Which of the following data structures consists of a root node from which all the other elements can be accessed?
- Correct Answer: tree.
- Explanation: A tree data structure has a root node, and other nodes are connected to it, forming a hierarchical structure.
If you have any further questions or need clarification, feel free to ask!