Open In App

Output of Python Programs | Set 24 (Sets)

Last Updated : 29 Dec, 2017
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Python-Sets

1. What is the output of the code shown below?




sets = {1, 2, 3, 4, 4}
print(sets)


Options:

  1. {1, 2, 3}
  2. {1, 2, 3, 4}
  3. {1, 2, 3, 4, 4}
  4. Error
Output:
2. {1, 2, 3, 4}

Explanation : Duplicate values are not allowed in sets. Hence, the output of the code shown above will be a set containing the duplicate value only once. Hence output will be {1, 2, 3, 4}.

2. What is the output of the code shown below?




sets = {3, 4, 5}
sets.update([1, 2, 3])
print(sets)


Options:

  1. {1, 2, 3, 4, 5}
  2. {3, 4, 5, 1, 2, 3}
  3. {1, 2, 3, 3, 4, 5}
  4. Error
Output:
1. {1, 2, 3, 4, 5}

Explanation: The method update adds elements to a set.

3. What is the output of the code shown below?




set1 = {1, 2, 3}
set2 = set1.copy()
set2.add(4)
print(set1)


Options:

  1. {1, 2, 3, 4}
  2. {1, 2, 3}
  3. Invalid Syntax
  4. Error
Output:
2. {1, 2, 3}

Explanation: In the above piece of code, set2 is barely a copy and not an alias of set1. Hence any change made in set2 isn’t reflected in set1.

4. What is the output of the code shown below?




set1 = {1, 2, 3}
set2 = set1.add(4)
print(set2)


Options:

  1. {1, 2, 3, 4}
  2. {1, 2, 3}
  3. Invalid Syntax
  4. None
Output:
4. None

Explanation: add method doesn’t return anything. Hence there will be no output.

5. What is the output of the code shown below?




set1 = {1, 2, 3}
set2 = {4, 5, 6}
print(len(set1 + set2))


Options:

  1. 3
  2. 6
  3. Unexpected
  4. Error
Output:
4. Error

Explanation: unsupported operand type(s) for +: ‘set’ and ‘set’.



Similar Reads

Output of Python Programs | Set 21 (Bool)
Prerequisite : Boolean Note: Output of all these programs is tested on Python31. What is the output of the code: C/C++ Code print(bool('False')) print(bool()) False, TrueNone, NoneTrue, TrueTrue, False Output: 4. True, False Explanation: If the argument passed to the bool function does not amount to zero then the Boolean function returns true else
2 min read
Output of Python Programs | Set 22 (Loops)
Prerequisite: Loops Note: Output of all these programs is tested on Python3 1. What is the output of the following? mylist = ['geeks', 'forgeeks'] for i in mylist: i.upper() print(mylist) [‘GEEKS’, ‘FORGEEKS’]. [‘geeks’, ‘forgeeks’]. [None, None]. Unexpected Output: 2. [‘geeks’, ‘forgeeks’] Explanation: The function upper() does not modify a string
2 min read
Output of Python Programs | Set 23 (String in loops)
Prerequisite: Loops and String Note: Output of all these programs is tested on Python3 1. What is the output of the following? my_string = "geeksforgeeks" i = "i" while i in my_string: print(i, end =" ") None geeksforgeeks i i i i i i … g e e k s f o r g e e k s Output: 1. None Explanation: 'i' is not present in string
2 min read
Output of Python Programs | Set 18 (List and Tuples)
1) What is the output of the following program? C/C++ Code L = list('123456') L[0] = L[5] = 0 L[3] = L[-2] print(L) a) [0, '2', '3', '4', '5', 0] b) ['6', '2', '3', '5', '5', '6'] c) ['0', '2', '3', '5', '5', '0'] d) [0, '2', '3', '5', '5', 0] Ans. (d) Explanation: L[0] is '1' and L[5] is '6', both of these elements will be replaced by 0 in the Lis
3 min read
Output of Python Programs | Set 19 (Strings)
1) What is the output of the following program? C/C++ Code str1 = '{2}, {1} and {0}'.format('a', 'b', 'c') str2 = '{0}{1}{0}'.format('abra', 'cad') print(str1, str2) a) c, b and a abracad0 b) a, b and c abracadabra c) a, b and c abracadcad d) c, b and a abracadabra Ans. (d) Explanation: String function format takes a format string and an arbitrary
3 min read
Output of Python Programs | Set 20 (Tuples)
Prerequisite: Tuples Note: The output of all these programs is tested on Python3 1. What will be the output of the following program? [GFGTABS] Python3 tuple = (1, 2, 3, 4) tuple.append( (5, 6, 7) ) print(len(tuple)) [/GFGTABS]Options: 125Error Output: 4. ErrorExplanation: In this case an exception will be thrown as tuples are immutable and don’t h
2 min read
Output of C Programs | Set 1
Predict the output of below programs. Question 1 C/C++ Code #include<stdio.h> int main() { int n; for(n = 7; n!=0; n--) printf("n = %d", n--); getchar(); return 0; } Output:Above program goes in infinite loop because n is never zero when loop condition (n != 0) is checked.Question 2 C/C++ Code #include<stdio.h> int main() { pr
2 min read
Output of C Programs | Set 2
Predict the output of below programs. Question 1 C/C++ Code #include<stdio.h> char *getString() { char str[] = "Will I be printed?"; return str; } int main() { printf("%s", getString()); getchar(); } Output: Some garbage value The above program doesn't work because array variables are stored in Stack Section. So, when getS
2 min read
Output of C Programs | Set 3
Predict the output of the below program. Question 1 C/C++ Code #include <stdio.h> int main() { printf("%p", main); getchar(); return 0; } Output: Address of function main. Explanation: Name of the function is actually a pointer variable to the function and prints the address of the function. Symbol table is implemented like this. st
3 min read
Output of C Programs | Set 4
Predict the output of below programs Question 1 C/C++ Code #include‹stdio.h› int main() { struct site { char name[] = "GeeksforGeeks"; int no_of_pages = 200; }; struct site *ptr; printf("%d",ptr->no_of_pages); printf("%s",ptr->name); getchar(); return 0; } Output: Compiler errorExp
2 min read
Practice Tags :