Class - 12 CBSE Computer Science Important Output Questions 2025
Predict the output of following code if the input is :
(i) 12, 3, 4, 5, 7, 12, 8, 23, 12
(ii) 8, 9, 2, 3, 7, 8
Code :
s = eval(input("Enter a list : "))
n = len(s)
t = s[1:n-1]
print(s[0] == s[n-1] and
t.count(s[0]) == 0)
Python
Linear Lists
2 Likes
Answer
Enter a list : [12, 3, 4, 5, 7, 12, 8, 23, 12]
False
Enter a list : [8, 9, 2, 3, 7, 8]
True
Working
s = eval(input("Enter a list : "))
— This line prompts the user to enter a list.n = len(s)
— This line finds the length of the lists
.t = s[1:n-1]
— This line slices the lists
starting from index 1 up to index n-1, which effectively removes the first and last elements from the lists
and stores it in listt
.print(s[0] == s[n-1] and t.count(s[0]) == 0)
— The conditions[0] == s[n-1] and t.count(s[0]) == 0
checks if the first and the last element of the lists
are same [s[0] == s[n-1]
] and that element does not appear anywhere else in lists
apart from the first and last position [t.count(s[0]) == 0
]. For case (i), 12 is the first and the last element of list but as it also occurs at the 5th index hence the output isFalse
. For case (ii), 8 is the first and the last element of list and it doesn't appear anywhere else hence the output isTrue
.
Answered By
1 Like