1. What will be the output of the following Python code snippet?
k = [print(i) for i in my_string if i not in "aeiou"] A) prints all the vowels in my_string B) prints all the consonants in my_string C) prints all characters of my_string that aren’t vowels D) prints only on executing print(k) Answer: prints all characters of my_string that aren’t vowels Explanation: print(i) is executed if the given character is not a vowel. |
2. What will be the output of the following Python code snippet?
k = [print(i) for i in my_string if i not in "aeiou"] print(k) A) all characters of my_string that aren’t vowels B) a list of Nones C) List of Trues D) List of Falses Answer: a list of Nones Explanation: print() returns None. |
3. What will be the output of the following Python code snippet?
my_string = "hello world" k = [(i.upper(), len(i)) for i in my_string] print(k) A) [(‘HELLO’, 5), (‘WORLD’, 5)] B) [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1), (‘ ‘, 1), (‘W’, 1), (‘O’, 1), (‘R’, 1), (‘L’, 1), (‘D’, 1)] C) [(‘HELLO WORLD’, 11)] D) none of the mentioned Answer: [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1), (‘ ‘, 1), (‘W’, 1), (‘O’, 1), (‘R’, 1), (‘L’, 1), (‘D’, 1)] Explanation: We are iterating over each letter in the string. |
4. What will be the output of the following Python code snippet?
x = [i**+1 for i in range(3)]; print(x); A) [0, 1, 2] B) [1, 2, 5] C) error, **+ is not a valid operator D) error, ‘;’ is not allowed Answer: [0, 1, 2] Explanation: **+1 is evaluated as (i)**(+1). |
5. What will be the output of the following Python code snippet?
print([i.lower() for i in "HELLO"]) A) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’] B) ‘hello’ C) [‘hello’] D) hello Answer: [‘h’, ‘e’, ‘l’, ‘l’, ‘o’] Explanation: We are iterating over each letter in the string. |
6. What will be the output of the following Python code snippet?
print([i+j for i in "abc" for j in "def"]) A) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’] B) [[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]] C) [[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]] D) [‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’] Answer: [‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’] Explanation: Explanation: If it were to be executed as a nested for loop, i would be the outer loop and j the inner loop. |
7. What will be the output of the following Python code snippet?
print([[i+j for i in "abc"] for j in "def"]) A) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’] B) [[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]] C) [[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]] D) [‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’] Answer: [[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]] Explanation: The inner list is generated once for each value of j. |
8. What will be the output of the following Python code snippet?
print([if i%2==0: i; else: i+1; for i in range(4)]) A) [0, 2, 2, 4] B) [1, 1, 3, 3] C) error D) none of the mentioned Answer: error Explanation: Syntax error. |
9. Which of the following is the same as list(map(lambda x: x**-1, [1, 2, 3]))?
A) [x**-1 for x in [(1, 2, 3)]] B) [1/x for x in [(1, 2, 3)]] C) error D) [1/x for x in (1, 2, 3)] Answer: [1/x for x in (1, 2, 3)] Explanation: x**-1 is evaluated as (x)**(-1). |