Recent tagged python

Ask a Question
Loading…

Description : What will be the output of the following Python code? >>> a={4,5,6}>>> b={2,8,6}>>> a+b a) {4,5,6,2,8} b) {4,5,6,2,8,6} c) Error as unsupported operand type for sets d) Error as the duplicate item 6 is present in both sets

Answer : Answer: c Explanation: Execute in python shell to verify.

Description : If a={5,6,7}, what happens when a.add(5) is executed? a) a={5,5,6,7} b) a={5,6,7} c) Error as there is no add function for set data type d) Error as 5 already exists in the set

Answer : Answer: b Explanation: There exists add method for set data type. However 5 isn’t added again as set consists of only non-duplicate elements and 5 already exists in the set. Execute in python shell to verify.

Description : If a={5,6,7,8}, which of the following statements is false? a) print(len(a)) b) print(min(a)) c) a.remove(5) d) a[2]=45

Answer : Answer: d Explanation: The members of a set can be accessed by their index values since the elements of the set are unordered.

Description : What will be the output of the following Python code? >>> a={5,4}>>> b={1,2,4,5}>>> a

Answer : Answer: b Explanation: a

Description : Which of the following statements is used to create an empty set? a) { } b) set() c) [ ] d) ( )

Answer : Answer: b Explanation: { } creates a dictionary not a set. Only set() creates an empty set.

Description : What will be the output of the following Python code? nums = set([1,1,2,3,3,3,4,4])print(len(nums)) a) 7 b) Error, invalid syntax for formation of set c) 4 d) 8

Answer : Answer: c Explanation: A set doesn’t have duplicate items.

Description : Which of the following is not the correct syntax for creating a set? a) set([[1,2],[3,4]]) b) set([1,2,2,3,4]) c) set((1,2,3,4)) d) {1,2,3,4}

Answer : Answer: a Explanation: The argument given for the set must be an iterable.

Description : Which of these about a set is not true? a) Mutable data type b) Allows duplicate values c) Data type with unordered values d) Immutable data type

Answer : Answer: d Explanation: A set is a mutable data type with non-duplicate, unordered values, providing the usual mathematical set operations.

Description : What will be the output of the following Python code? >>>t = (1, 2) >>>2 * t a) (1, 2, 1, 2) b) [1, 2, 1, 2] c) (1, 1, 2, 2) d) [1, 1, 2, 2]

Answer : Answer: a Explanation: * operator concatenates tuple.

Description : What will be the output of the following Python code? d = {"john":40, "peter":45} d["john"] a) 40 b) 45 c) “john” d) “peter”

Answer : Answer: a Explanation: Execute in the shell to verify.

Description : What will be the output of the following Python code? >>>t = (1, 2, 4, 3, 8, 9) >>>[t[i] for i in range(0, len(t), 2)] a) [2, 3, 9] b) [1, 2, 4, 3, 8, 9] c) [1, 4, 8] d) (1, 4, 8)

Answer : Answer: c Explanation: Execute in the shell to verify.

Description : What will be the output of the following Python code? >>>t=(1,2,4,3) >>>t[1:-1] a) (1, 2) b) (1, 2, 4) c) (2, 4) d) (2, 4, 3)

Answer : Answer: c Explanation: Slicing in tuples takes place just as it does in strings.

Description : What will be the output of the following Python code? >>>t=(1,2,4,3) >>>t[1:3] a) (1, 2) b) (1, 2, 4) c) (2, 4) d) (2, 4, 3)

Answer : Answer: c Explanation: Slicing in tuples takes place just as it does in strings.

Description : Suppose t = (1, 2, 4, 3), which of the following is incorrect? a) print(t[3]) b) t[3] = 45 c) print(max(t)) d) print(len(t))

Answer : Answer: b Explanation: Values cannot be modified in the case of tuple, that is, tuple is immutable.

Description : Which of the following is a Python tuple? a) [1, 2, 3] b) (1, 2, 3) c) {1, 2, 3} d) {}

Answer : Answer: b Explanation: Tuples are represented with round brackets.

Description : What will be the output of the following Python code snippet? d1 = {"john":40, "peter":45} d2 = {"john":466, "peter":45} d1 > d2 a) True b) False c) Error d) None

Answer : Answer: c Explanation: Arithmetic > operator cannot be used with dictionaries.

Description : What will be the output of the following Python code snippet? d1 = {"john":40, "peter":45} d2 = {"john":466, "peter":45} d1 == d2 a) True b) False c) None d) Error

Answer : Answer: b Explanation: If d2 was initialized as d2 = d1 the answer would be true.

Description : What will be the output of the following Python code snippet? d = {"john":40, "peter":45} "john" in d a) True b) False c) None d) Error

Answer : Answer: a Explanation: In can be used to check if the key is int dictionary.

Description : What will be the output of the following Python code snippet? d = {"john":40, "peter":45} a) “john”, 40, 45, and “peter” b) “john” and “peter” c) 40 and 45 d) d = (40:”john”, 45:”peter”)

Answer : Answer: b Explanation: Dictionaries appear in the form of keys and values.

Description : Which of the following statements create a dictionary? a) d = {} b) d = {“john”:40, “peter”:45} c) d = {40:”john”, 45:”peter”} d) All of the mentioned

Answer : Answer: d Explanation: Dictionaries are created by specifying keys and values.