What will be the Output of the Following Output Code?
Overview
Multiple Choice Questions (MCQ) on Python regarding loops, functions, variables, value assignment, if else conditions, control statements, dictionary, lists, strings, etc.
This Quiz is mainly for beginners in Python, but anyone can give this to revise their knowledge in the Python language.
What will be the Output of the Following Python Code Snippets?
Q1. What will be the Output of the Following Python Code? (Variables and Strings)
- Hello world
- text
- 'text'
- None of the above
Output:
Explanation:
In the above code snippet, we are printing a string 'text' and not the text variable.
Q2. What will be the Output of the Following Python Code? (Variables and Strings)
- Hello world
- text
- null
- None of the above
Output:
Explanation:
In the above code snippet, we are printing the text variable that holds the value 'Hello world'.
Q3. What will be the Output of the Following Python Code? (Variables and Strings)
- name
- coder
- Throws error
- None of the above
Output:
Explanation:
In the above code snippet, we are printing the name variable that holds the value 'coder'.
Q4. What will be the Output of the Following Python Code? (Variables and Strings)
- Jericho
- Jerry
- JerichoJerry
- None of the above
Output:
Explanation:
In the above code snippet, we are assigning the newname variable to the name variable. Then we are printing the variable name that now holds the value 'Jerry'.
Q5. What will be the Output of the Following Python Code? (Variables and Strings)
- programmer
- coder
- Throws error
- None of the above
Output:
Explanation:
In the above code snippet, we are reassigning the value of the variable name from 'coder' to 'programmer'.
Q6. What will be the Output of the Following Python Code? (Variables and Strings)
- Hello {name}
- f Hello {name}
- Hello Jericho
- None of the above
Output:
Explanation:
In the above code snippet, we are using an f-string to insert the variable of the value 'Jericho' inside the string.
Q7. What will be the Output of the Following Python Code? (For Loop and Range)
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- 1, 2, 3, 4, 5, 6, 7, 8, 9
Output:
Explanation:
In the above code snippet, range(10) means . Therefore, values from to are printed using a for loop.
Q8. What will be Output of the Following Python Code? (For Loop and Range)
- 2
- 12
- Throws error
- None of the above
Output:
Throws an Error
Explanation:
The Indentation Error occurs when the spaces and tabs are not placed properly. In the above code snippet, we can see that the second line should be indented inside the for loop.
Q9. What will be the Output of Following Python Code? (For Loop and Range)
- 0
- Infinity
- -1
- Nothing
Output:
Explanation:
In the above code snippet, the range starts from and ends before , which means there is nothing inside the range. Therefore, the output is empty / nothing.
10. What will be Output of the Following Python Code? (For Loop and Range)
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- 10
- Throws Error
- None of the above
Output:
Explanation:
In the above code snippet, we are printing numbers from the range to as range(y) means range(10).
Q11. What will be the Output of the Following Python Code? (For Loop and Indentation)
- 0
- -1
- Throws error
- No output
Output:
Throws an Error
Explanation:
This code snippet may first look like it will output nothing as the range is , but on looking closesly, we can see that the second line is not indented, resulting in an IndentationError. The Indentation Error occurs when the spaces and tabs are not placed properly. In the above code snippet, we can see that the second line should be indented inside the for loop.
Q12. What will be the Output of the Following Python Code? (For Loop and Range)
- 0, 1, 10
- 2, 3, 4, 5, 6, 7, 8, 9
- 2, 4, 6, 8
- 2, 4, 6, 8, 10
Output:
Explanation:
In the above code snippet, we are printing values inside the range(2,10), which means numbers from to .
Q13. What will be the Output of the Following Python Code? (For Loop and Range)
- 2, 2, 2, 2, 2, 2, 2, 2
- 2, 3, 4, 5, 6, 7, 8, 9
- 2, 4, 6, 8
- 2, 4, 6, 8, 10
Output:
Explanation:
The third parameter of the range() function is the incremental value (value by which each step will be incremented). The range starts from and ends at with each step being .
Q14. What will be the Output of the Following Python Code? (Arithmetic Operators)
- -1
- 0
- Throws Error
- 2
Output:
Explanation:
In the above code snippet, we are printing the value of sum variable which is .
Q15. What will be the Output of the Following Python Code? (While Loop, if Condition and Break Statement)
- 1, 2, 3, 4, 5, 6
- 1, 2, 3, 4, 5, 6, 7
- 1
- 0
Output:
Explanation:
In the above code snippet, the initial value of is . A while loop is run which breaks if , till then, the value of is printed and incremented by in each iteration.
Q16. What will be the Output of the Following Python Code? (Arithmetic Operators)
- 12
- 10
- 9
- 11
Output:
Explanation:
In the above code snippet, we are printing the value of result variable which is .
Q17. What will be the Output of the Following Python Code? (String to Int Typecasting)
- 2
- 5
- Throws Error
- None of the above
Output:
Explanation:
Here we are converting a string '3' to an integer value (using typecasting) and then printing it after adding to it.
Q18. What will be Output of the Following Python Code? (Operand Types)
- 7
- 43
- 4'3'
- Throws Error
Output:
Throws an Error
Explanation:
We cannot add a string and an integer value, both the operands should be of the same type for any type of calculation.
Q19. What will be the Output of the Following Python Code? (String to Int Typecasting)
- 7
- 43
- 4'3'
- Throws Error
Output:
Explanation:
Here we are typecasting the string '3' to an integer to perform addition with .
Q20. What will be the Output of the Following Python Code? (String Multiplication)
- 24
- 1212
- 122
- Throws Error
Output:
Explanation:
Multiplying a string with an integer results in a multiplied string without spaces.
Q21. What will be the Output of Fllowing Python Code? (Assigning Keywords)
- True
- False
- No output
- Throws Error
Output:
Throws an Error
Explanation:
This code snippet throws a Syntax error because is a keyword and it cannot be assigned a value.
Q22. What will be the Output of the Following Python Code? (Artihmetic Operators)
- 22.0
- 11.0
- 345
- Throws an error
Output:
Explanation:
Here we are simply printing the value of which is in Python, as is and is because diving an integer by a floating point number results in a floating point number.
Q23. What will be the Output of the Following Python Code? (find() Function)
- 0
- 1
- -1
- Throws error
Output:
Explanation:
The find() function returns the index of a substring in a given string and if the substring is not found, it returns .
Q24. What will be the Output of the Following Python Code? (Appending in a List)
- 1, 2, 3, 4, 5, 6, 7
- [1, 2, 3, 4, [5, 6, 7]]
- [1, 2, 3, 4, 5, 6, 7]
- Throws an error
Output:
Explanation:
In this code snippet, it might look like we are appending , , as separate elements but upon looking closely we can see that is a single list element.
Q25. What will be the Output of the Following Python Code? (difference() Function)
- {1, 2}
- {1, 2, 3, 4, 5}
- {1, 2, 3, 4, {3, 4, 5}}
- Throws an error
Output:
Explanation:
The difference() function returns the values which are present in set but not in set . In this case, and are present in set but not in set so they are returned.
Q26. What will be the Output of the Following Python Code? (For Loop)
- 1 2 3
- 123
- Throws an error
- None of the above
Output:
Throws an Error
Explanation:
An int object is not iterable, to iterate in a number, we will have to use a range() function.
Q27. What will be the Output of the Following Python Code? (For Loop and Dictionary)
- 0 1 2
- a b c
- 0 a 1 b 2 c
- None of the above
Output:
Explanation:
Iterating in (dictionary) means iterating on its keys. Thus, printing in each iteration means printing the keys of the dictionary.
Q28. What will be the Output of the Following Python Code? (For Loop and Dictionary)
- 0 1 2
- a b c
- 0 a 1 b 2 c
- Throws an error
Output:
Throws an Error
Explanation:
Iterating in (dictionary) only gives its keys (a single value). We can only use two variables if we use d.items() which returns both the keys and its values.
Q29. What will be the Output of the Following Python Code? (For Loop and Dictionary)
- 0 1 2
- a b c
- 0 a 1 b 2 c
- None of the above
Output:
Explanation:
Here we are iterating on d.items() (items of dictionary ) which returns both the keys and values of the dictionary.
Q30. What will be the Output of the Following Python Code? (For Loop and Dictionary)
- 0 1 2
- a b c
- 0 a 1 b 2 c
- None of the above
Output:
Explanation:
Iterating in d.keys() (keys of the dictionary) only gives its keys (a single value).
Q31. What will be the Output of the Following Python Code? (For Loop and Dictionary)
- 0 1 2
- a b c
- 0 a 1 b 2 c
- None of the above
Output:
Explanation:
Iterating in d.values() (values of the dictionary) only gives its values (a single value).
Q32. What will be Output of the Following Python Code? (For Loop and Dictionary)
- 0 1 2
- a b c
- 0 a 1 b 2 c
- None of the above
Output:
None of the Above
Explanation:
Here we are trying to get the value of dictionary[value] which returns a KeyError as we can only use keys for this.
Q33. What will be the Output of the Following Python Code? (For Loop and Set)
- 0 1 2
- None None None
- Throws an error
- none of the mentioned
Output:
Throws an Error
Explanation:
A set data structure doesn't have multiple attributes such as key and value.
Q34. What will be the Output of the Following Python Code? (For Loop and Set)
- 0 1 2
- {0, 1, 2} {0, 1, 2} {0, 1, 2}
- Throws an error
- None of the above
Output:
Explanation:
We are printing the values of the set by iterating on it using a for loop.
Q35. What will be the Output of the Following Python Code? (For Loop, Set and add() Function)
- 0 1 2
- None None None
- 0 1 2 0 1 2 0 1 2...
- None of the above
Output:
Explanation:
The add() function returns nothing which is why the output is None in each iteration.
Q36. What will be the output of the following Python code? (While loop, if condition and Break Statement)
- 0 1 2 0
- 0 1 2
- Throws an error
- None of the above
Output:
Explanation:
There is a break control statement inside in the while loop in the above code snippet, which is why, the else statement won't be executed. Therefore, only values , , and will be printed before the loop breaks.
Q37. What will be the Output of the Following Python Code? (For Loop, Dictionary, and append() Function)
- [1, 2, 3]
- ['One', 'Two', 'Three']
- [2, 3]
- ['Two', 'Three']
Output:
Explanation:
(a string) is the value of dictionary in each iteration. V[0] means the first character of the string, so if the first character of a value is equal to 'T', the empty list will be appended by the corresponding key to the value of dictionary .
Q38. What will be the Output of the Following Python Code? (For Loop, String)
- None
- Nothing is printed
- Throws an error
- None of the above
Output:
Explanation:
String is empty, doesn't have characters to loop over.
Q39. What will be the Output of the Following Python Code? (Function, Pass Statement, and type() Function)
- <class 'function'>
- <class 'type'>
- <class 'NoneType'>
- None of the above
Output:
Explanation:
The type of a function is determined by its return value, a function which returns nothing is of class NoneType.
Q40. What will be the Output of the Following Python Code? (Dictionary and pop() Function)
- {1: 2}
- {1: 2, 3: 4}
- {1: 2, 5: 6}
- Throws an error
Output:
Explanation:
The second parameter of the dict.pop() function is a default parameter if the first parameter is not present in the dictionary. In this case, the first paramter () is present in the dictionary so it got popped out with its value.
Q41. What will be the Output of the Following Python Code? (List and sort() Function)
- [2.1, 2, '4']
- [2.1, 3, '4']
- [2.1, 3]
- Throws an error
Output:
Throws an Error
Explanation:
We can only sort a list if it has same type of data structure as its elements. In this case, the list contains strings as well as numbers so there is no criteria for sorting in this case.
Q42. What will be the Output of the Following Python Code? (For Loop, and range() Function)
- 55
- -55
- 54
- -54
Output
Explanation:
In this case, we are subtracting the value of from in each case. The values of will be .
Q43. What will be the Output of the Following Python Code?
- 1, 2, 3, 4
- 0, 1, 2
- 0, 1, 2, 3
- Throws an error
Output:
Throws an error
Explanation:
This is a tricky question as there is no need to read beyond the first line which contains a NameError. , a variable cannot be assigned which is not defined in the program.
Q44. What will be the Output of the Following Python Code? (insert() Function)
- 2
- 3
- 4
- 20
Output:
Explanation:
The parameters of the index() function are position and element.
Initally the list is .
List after inserting at the position:
List after inserting at the position:
The last element of the list will be .
Q45. What will be the Output of the Following Python Code? (For Loop, range() Function, and += Operator)
- 31
- 33
- 36
- 39
Output:
Explanation:
The values of in each iteration will be and is incremented by in each iteration, therefore, the final value of will be .
Q46. What will be the Output of the Following Python Code? (While Loop)
- 2 3 4 5 6 7 8
- 1 2 3 4 5 6 7 8
- 1 2 3 4 5 6 7 8 9
- 2 3 4 5 6 7 8 9
Output:
Explanation:
We are printing the value of and incrementing it by till .
Q47. What will be the Output of the Following Python Code? (For Loop)
- 0.0 1.0
- 0 1
- Throws an error
- None of the above
Output:
Throws an Error
Explanation:
The range() function only accepts integers as it parameter, using a floating point number with the range() function throws an error (TypeError).
Q48. What will be the Output of the Following Python Code? (For Loop, range() Function, and '+=' Operator)
- 0.0 1.0
- 0 1
- Throws an error
- None of the above
Output:
Explanation:
Here, we are typecasting the floating point number to an integer and then passing it to the range() function.
Q49. What will be the Output of the Following Python Code? (For Loop, range() Function, and inf)
- 0.0 1.0 2.0 3.0...
- 0 1 2 3
- 0.0 0.1 0.2 0.3 0.4
- Throws an error
Output:
Throws an Error
Explanation:
The range() function only accepts integers as it parameter, using a floating point number with the range() function throws an error (TypeError).
Q50. What will be the Output of the Following Python Code? (For Loop, range() Function, '+=' Operator, Typecasting)
- 0.0 1.0 2.0 3.0...
- 0 1 2 3
- 0.0 0.1 0.2 0.3 0.4
- Throws an error
Output:
Throws an Error
Explanation:
Infinity cannot be used as a range in Python as it will never end resulting in an Overflow error.
Conclusion
- To initialize a variable in Python, just name it and assign a value to it.
- Strings in Python are written inside single inverted commas () or double inverted commas ().
- Lists are linear data structure that can contain any data type. They support indexing and are mutable.
- Tuples are just like lists, except for the fact that they are immutable.
- Dictionaries in Python are like Hashmaps as they contain key-value pairs.
- If statement checks if a condition is true or not, if true, it runs the block of code indented inside it, if not true, elif blocks are used to add more conditions, and if even they are not satisfied, an else block is used.
- The for loops and while loops are used to loop over something until the condition is not satisfied.
- A function Python is created using the def keyword.
- The range() function is used to return a sequence of numbers.
- The append() function is used to add a single item to the existing list.
To learn more about Python, click here.