NIELIT O Level Exam

<p>What will be the output of the following Python code snippet?</p> <pre><font color="#333333">total={}<br></font><font color="#333333">def insert(items):<br></font><font color="#333333">&nbsp; &nbsp; if items in total:<br></font><font color="#333333">&nbsp; &nbsp; &nbsp; &nbsp; total[items] += 1<br></font><font color="#333333">&nbsp; &nbsp; else:<br></font><font color="#333333">&nbsp; &nbsp; &nbsp; &nbsp; total[items] = 1<br></font><font color="#333333">insert('Apple')<br></font><font color="#333333">insert('Ball')<br></font><font color="#333333">insert('Apple')&nbsp;<br></font><font color="#333333">print (len(total))</font></pre><div><br></div>

What will be the output of the following Python code snippet?

total={}
def insert(items):
    if items in total:
        total[items] += 1
    else:
        total[items] = 1
insert('Apple')
insert('Ball')
insert('Apple') 
print (len(total))


निम्नलिखित Python कोड स्निपेट का आउटपुट क्या होगा?

total={}
def insert(items):
    if items in total:
        total[items] += 1
    else:
        total[items] = 1
insert('Apple')
insert('Ball')
insert('Apple') 
print (len(total))

A. 3

B. 1

C. 2

D. 0




Correct Answer is :

C. 2



Explanation
The insert() function counts the number of occurrences of the item being inserted into the dictionary. There are only 2 keys present since the key ‘Apple’ is repeated. Thus, the length of the dictionary is 2.
Latest Current Affairs 2025 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad