What will be the Output of the Following Output Code?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

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 090-9. Therefore, values from 00 to 99 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 00 and ends before 00, 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 00 to 99 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 00, 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 22 to 99.

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 22 and ends at 99 with each step being 22.

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 1+12=01 + 1 - 2 = 0.

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 ii is 1010. A while loop is run which breaks if i%007=i%7=0i\; \%\; 007 = i\; \%\; 7 = 0, till then, the value of ii is printed and incremented by 11 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 10+11=1010 + 1 - 1 = 10.

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 ii '3' to an integer value (using typecasting) and then printing it after adding 22 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 44.

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 TrueTrue 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 1122/2211 * 22 / 22 which is 11.011.0 in Python, as 112211 * 22 is 242242 and 242/22242 / 22 is 11.011.0 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 1-1.

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 55, 66, 77 as separate elements but upon looking closely we can see that [5,6,7][5, 6, 7] 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 xx but not in set yy. In this case, 11 and 22 are present in set xx but not in set yy 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 dd (dictionary) means iterating on its keys. Thus, printing ii 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 dd (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 dd) 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 ss 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 00, 11, and 22 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:
VV (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 LL will be appended by the corresponding key to the value of dictionary DD.

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 (33) 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 ii from ss in each case. The values of ii will be 10,9,8,...,210, 9, 8, ..., 2.

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. ii, a variable cannot be assigned oo 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 [10,15,20,25,30][10, 15, 20, 25, 30].
List after inserting 44 at the 3rd3rd position: [10,15,20,25,4,30][10, 15, 20, 25, 4, 30]
List after inserting 33 at the 2nd2nd position: [10,15,3,20,25,4,30][10, 15, 3, 20, 25, 4, 30]
The 5th5th last element of the list will be 33.

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 aa in each iteration will be 1,3,5,7,91, 3, 5, 7, 9 and bb is incremented by a+2a+2 in each iteration, therefore, the final value of bb will be 3636.

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 xx and incrementing it by 11 till x<9x<9.

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 2.02.0 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.