Python Quiz For Beginners

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Catherine Halcomb
Catherine Halcomb
Community Contributor
Quizzes Created: 1382 | Total Attempts: 6,148,923
Questions: 34 | Attempts: 4,845

SettingsSettingsSettings
Python Quiz For Beginners - Quiz

Hey, how much do you know about Python? Will you be able to solve this python quiz that we've created below? Let's find out today. Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation. Try solving the questions of this quiz and get to test your understanding of Python.
So, are you ready? Let's start then.
Good luck!


Questions and Answers
  • 1. 

    Given the numeric variable myNumber, which lines of code properly prints the value? (Select 2)

    • A.

      Print(“%d”)

    • B.

      Print(“%d”, myNumber)

    • C.

      Print(myNumber)

    • D.

      Print(“%d” % myNumber)

    Correct Answer(s)
    C. Print(myNumber)
    D. Print(“%d” % myNumber)
    Explanation
    The first line of code, "print(%d)", does not include any variable or value to be printed, so it will not print the value of myNumber. The second line of code, "print(%d, myNumber)", uses the incorrect syntax for formatting the variable myNumber, so it will not print the value correctly. The third line of code, "print(myNumber)", correctly prints the value of myNumber. The fourth line of code, "print(%d % myNumber)", uses the correct syntax for formatting the variable myNumber, so it will print the value correctly.

    Rate this question:

  • 2. 

     You have the following code segment: testValue = 2 testValue = 10 + (testValue * 2) What is the value of testValue?

    • A.

      10

    • B.

      12

    • C.

      14

    • D.

      2

    Correct Answer
    C. 14
    Explanation
    The code segment assigns the value 2 to the variable testValue. Then, it calculates the value of testValue as 10 plus the product of testValue (which is 2) and 2. Therefore, the value of testValue is 14.

    Rate this question:

  • 3. 

    Which piece of code shows a new class Spam inheriting from Egg?

    • A.

      Class Egg(Spam):

    • B.

      Class (Spam)Egg:

    • C.

      Class Spam(Egg):

    Correct Answer
    C. Class Spam(Egg):
    Explanation
    The correct answer is "class Spam(Egg):". This code snippet demonstrates the creation of a new class called "Spam" that inherits from the existing class "Egg". This is indicated by the use of parentheses and the name of the parent class "Egg" following the class name "Spam".

    Rate this question:

  • 4. 

    Which two sections of code print 'Hello World' to the console?

    • A.

      MyValue = “Hello World” if myValue == “Hello World” : print(“Hello World”)

    • B.

      MyValue = “Hello World” if myValue == “Hello World” : print(myValue)

    • C.

      MyValue = “Hello World” if myValue != “Hello World” : print(myValue)

    • D.

      MyValue = “Hello World” if myValue != “Hello World” : print(“Hello World”)

    Correct Answer(s)
    A. MyValue = “Hello World” if myValue == “Hello World” : print(“Hello World”)
    B. MyValue = “Hello World” if myValue == “Hello World” : print(myValue)
    Explanation
    The two sections of code that print 'Hello World' to the console are:
    1. myValue = "Hello World"
    if myValue == "Hello World":
    print("Hello World")

    2. myValue = "Hello World"
    if myValue == "Hello World":
    print(myValue)

    In both sections, the variable myValue is assigned the value "Hello World" and then the condition if myValue == "Hello World" is checked. Since the condition is true, the print statement is executed and "Hello World" is printed to the console.

    Rate this question:

  • 5. 

    You have the following code segment: myValue = “yes” if myValue != “yes” :     print(“Hello”) print(“World”) What is the output from this code?

    • A.

      World Hello

    • B.

      Hello World

    • C.

      Hello

    • D.

      World

    Correct Answer
    D. World
    Explanation
    The output from this code is "World". The code assigns the value "yes" to the variable myValue. The if statement checks if myValue is not equal to "yes", which is false. Therefore, the code inside the if statement, which is to print "Hello", is not executed. After the if statement, the code prints "World".

    Rate this question:

  • 6. 

    You have the following code segment: currentAmount = 100 itemCost = 100 if currentAmount >= itemCost :     print(“Item Purchased”) else :     print(“Not Enough Funds”) What is the output from this code?

    • A.

      Not Enough Funds

    • B.

      Item Purchased Not Enough Funds

    • C.

      No output is displayed

    • D.

      Item Puchased

    Correct Answer
    D. Item Puchased
    Explanation
    The output from this code is "Item Purchased". This is because the condition in the if statement is true (currentAmount is greater than or equal to itemCost), so the code inside the if block is executed and "Item Purchased" is printed.

    Rate this question:

  • 7. 

    What is the result of this code? def print_double(x):      print(2 * x) print_double(3)

    • A.

      8

    • B.

      6

    • C.

      4

    • D.

      10

    Correct Answer
    B. 6
    Explanation
    The code defines a function called print_double that takes a parameter x. Inside the function, it multiplies x by 2 and prints the result. Then, the function is called with the argument 3. Therefore, the output of the code is the result of 2 multiplied by 3, which is 6.

    Rate this question:

  • 8. 

    Which symbol is used to check whether two variables are the same?

    • A.

      -

    • B.

      ==

    • C.

      |

    • D.

      =

    Correct Answer
    B. ==
    Explanation
    The symbol "==" is used to check whether two variables are the same. It is an equality operator that compares the values of two variables and returns true if they are equal, and false if they are not. This symbol is commonly used in programming languages to perform conditional statements and comparisons.

    Rate this question:

  • 9. 

    Which code segment will NOT reach its print() function?

    • A.

      If ‘yes’ != ‘no’ : print(“condition met”)

    • B.

      If ‘yes’ != ‘yes’ : print(“condition met”)

    • C.

      If not ‘yes’ == ‘no’ : print(“condition met”)

    • D.

      If ‘yes’ == ‘yes’ : print(“condition met”)

    Correct Answer
    B. If ‘yes’ != ‘yes’ : print(“condition met”)
    Explanation
    The code segment "if 'yes' != 'yes' : print("condition met")" will not reach its print() function because the condition 'yes' != 'yes' is False, so the code inside the if statement will not be executed.

    Rate this question:

  • 10. 

    Which two lines of code are valid strings in Python?  

    • A.

      This is a string

    • B.

      ‘This is a string’

    • C.

      (This is a string)

    • D.

      “This is a string”

    Correct Answer(s)
    B. ‘This is a string’
    D. “This is a string”
    Explanation
    The two valid strings in Python are 'This is a string' and "This is a string". In Python, strings can be enclosed in either single quotes or double quotes, as long as the opening and closing quotes match. Therefore, both 'This is a string' and "This is a string" are valid strings in Python.

    Rate this question:

  • 11. 

    True or False: A string can be surrounded by three sets of single quotation marks or by three sets of double quotation marks.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    A string can indeed be surrounded by three sets of single quotation marks or by three sets of double quotation marks. This is known as triple quoting and is a feature in some programming languages, such as Python. Triple quoting allows for the inclusion of multiline strings without the need for escape characters. By using three sets of either single or double quotation marks, a string can span multiple lines while maintaining its integrity.

    Rate this question:

  • 12. 

    Which line of code has the proper syntax for the print statement?

    • A.

      Print( Its’ a rainy day )

    • B.

      Print( ‘it’s a rainy day’ )

    • C.

      Print( ‘it’s a rainy day” )

    • D.

      Print( ‘it\’s a rainy day’ )

    Correct Answer
    D. Print( ‘it\’s a rainy day’ )
    Explanation
    The correct line of code is "print( ‘it\’s a rainy day’ )" because it uses single quotes to enclose the string and includes an escape character (\) before the apostrophe in "it's" to indicate that it is part of the string and not the end of the string.

    Rate this question:

  • 13. 

    You have the following code segment: print(“Here we have a line of text \n and \n we can do newlines!”) What is the output of this code?

    • A.

      Here we have a line of text and we can do newlines!

    • B.

      Here we have a line of text and we can do newlines!

    • C.

      Here we have a line of text and we can do newlines!

    • D.

      Here we have a line of text and we can do newlines!

    Correct Answer
    A. Here we have a line of text and we can do newlines!
    Explanation
    The output of this code will be "Here we have a line of text and we can do newlines!". The code uses the print() function to display the given string, which contains line breaks indicated by the "" tags. When the code is executed, the line breaks are interpreted as newlines, resulting in the text being displayed with the line breaks intact. Therefore, the output will show the string exactly as it is written in the code, with the line breaks included.

    Rate this question:

  • 14. 

    Which line of code produces an error?

    • A.

      "7" + 'eight'

    • B.

      3 + 4

    • C.

      "one" + "2"

    • D.

      '5' + 6

    Correct Answer
    D. '5' + 6
    Explanation
    The line of code '5' + 6 produces an error because it is trying to concatenate a string ('5') with a number (6). In most programming languages, you cannot directly concatenate a string and a number without converting one of them to the other data type first.

    Rate this question:

  • 15. 

    What is the output of this code? >>> print(3 * 'TCS') ANS = '_______'  

    Correct Answer
    TCSTCSTCS
    Explanation
    The code is using the multiplication operator (*) to repeat the string 'TCS' three times. Therefore, the output of the code would be 'TCSTCSTCS'.

    Rate this question:

  • 16. 

    What is the output of this code? >>> int("3" + "4")

    • A.

      "7"

    • B.

      "34"

    • C.

      34

    Correct Answer
    C. 34
    Explanation
    The output of this code is 34 because the code is concatenating the strings "3" and "4" using the + operator. Since the strings are being concatenated and not added as numbers, the result is a string "34".

    Rate this question:

  • 17. 

    What is the output of this code? >>> float("210" * int(input("Enter a number:" ))) Enter a number: 2

    • A.

      "420.0"

    • B.

      "210210"

    • C.

      420

    • D.

      210210.0

    Correct Answer
    D. 210210.0
    Explanation
    The code takes an input from the user and multiplies it by the string "210" which is converted to an integer. The result is then converted to a float. In this case, if the user enters the number 2, the code will multiply 2 by 210, resulting in 420.0. Therefore, the output of this code is "420.0".

    Rate this question:

  • 18. 

    What is the output of this code? num = 7 if num > 3:    print("3")    if num < 5:       print("5")       if num ==7:         print("7")

    Correct Answer
    3
    Explanation
    The output of the code is "3". This is because the condition "num > 3" is true, so the code inside the if statement is executed. The next if statement is nested inside the first if statement, so it will only be executed if the first if statement is true. Since "num < 5" is false, the code inside the second if statement is not executed. Therefore, only the first print statement is executed, resulting in the output "3".

    Rate this question:

  • 19. 

    A shorter option for an "else if" statement is:

    Correct Answer
    elif
    Explanation
    The correct answer is "elif" because it is a shorter and more concise way of writing "else if" in many programming languages. It is commonly used to add additional conditions to an if statement, allowing for multiple branches of code to be executed based on different conditions. Using "elif" can help to improve the readability and maintainability of code by reducing the amount of nesting and making the code more streamlined.

    Rate this question:

  • 20. 

    What is the result of this code? if 1 + 1 * 3 == 6:    print("Yes") else:    print("No")

    • A.

      No

    • B.

      Yes

    Correct Answer
    A. No
    Explanation
    The result of this code is "No". This is because the condition in the if statement (1 + 1 * 3 == 6) evaluates to False, so the code inside the else block is executed, which prints "No".

    Rate this question:

  • 21. 

    Which value type does input() return?

    • A.

      Boolean

    • B.

      String

    • C.

      Int

    • D.

      Float

    Correct Answer
    B. String
    Explanation
    The input() function in Python returns a string value. When the input() function is used to prompt the user for input, it always returns the input as a string, regardless of whether the user enters a number or any other type of value. If the input needs to be treated as a different data type, such as an integer or a float, it needs to be explicitly converted using type casting.

    Rate this question:

  • 22. 

    Which number is not printed by this code? try:   print(1)   print(20 / 0)   print(2) except ZeroDivisionError:   print(3) finally:   print(4)

    • A.

      2

    • B.

      3

    • C.

      4

    Correct Answer
    A. 2
    Explanation
    The code prints the numbers 1, 3, and 4. However, the line "print(20 / 0)" will raise a ZeroDivisionError, causing the code to skip printing the number 2. Therefore, the number that is not printed by this code is 2.

    Rate this question:

  • 23. 

    True or False: Variables can be assigned only once.

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    Variables can be assigned multiple times in programming. Once a variable is assigned a value, it can be changed or updated with a new value at any point in the program. This allows for dynamic and flexible manipulation of data within the program. Therefore, the statement "Variables can be assigned only once" is false.

    Rate this question:

  • 24. 

    You have the following code segment: myString01 = “my“ myString02 = “string” print(myString01 , myString02) What is the output of this code?

    • A.

      My string

    • B.

      String

    • C.

      Mystring

    • D.

      My

    Correct Answer
    A. My string
    Explanation
    The output of this code segment will be "my string". This is because the two variables, myString01 and myString02, are concatenated together using the comma in the print statement. This results in the two strings being printed out together with a space in between them.

    Rate this question:

  • 25. 

    Will the close() function get called in this code? try:    f = open("filename.txt")    print(f.read())    print(1 / 0) finally:   f.close()

    • A.

      Yes

    • B.

      No

    Correct Answer
    A. Yes
    Explanation
    The close() function will get called in this code because it is placed inside a finally block. The finally block is always executed, regardless of whether an exception is raised or not. In this case, even though an exception (ZeroDivisionError) occurs after reading the file, the finally block will still be executed, ensuring that the file is closed properly.

    Rate this question:

  • 26. 

    Which line of code writes "Hello world!" to a file?

    • A.

      Write(file, "Hello world!")

    • B.

      Write("Hello world!", file)

    • C.

      File.write("Hello world!")

    Correct Answer
    C. File.write("Hello world!")
    Explanation
    The correct answer is "file.write("Hello world!")". This line of code uses the "write" function of the "file" object to write the string "Hello world!" to the file. The first option "write(file, "Hello world!")" is incorrect because it passes the file object as the first argument instead of calling the write function on the file object. The second option "write("Hello world!", file)" is also incorrect because it tries to call the write function directly without the file object.

    Rate this question:

  • 27. 

    You have the following code segment: myString01 = “my“ myString02 = “string” print(myString01 + myString02) What is the output of this code?

    • A.

      My string

    • B.

      String

    • C.

      Mystring

    • D.

      My

    Correct Answer
    C. Mystring
    Explanation
    The code segment defines two variables, myString01 and myString02, with the values "my" and "string" respectively. The print statement concatenates the values of these two variables using the + operator. Therefore, the output of this code is "mystring".

    Rate this question:

  • 28. 

    True or False: A variable is a placeholder for data.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    A variable is a placeholder for data because it can store and represent different values. It allows programmers to assign values to a variable and manipulate or use that data throughout their code. By using variables, we can make our code more flexible and reusable, as we can change the value stored in a variable without having to modify the entire code. Therefore, the statement "A variable is a placeholder for data" is true.

    Rate this question:

  • 29. 

    You have the following code segment: myString = “hello world” print( myString.upper() + “ “ + myString ) What is the output of this code?

    • A.

      HELLO WORLD

    • B.

      HELLO WORLD hello world

    • C.

      Hello world hello world

    • D.

      HELLO WORLD HELLO WORLD

    Correct Answer
    B. HELLO WORLD hello world
    Explanation
    The code segment first converts the string "hello world" to uppercase using the upper() method, resulting in "HELLO WORLD". Then, it concatenates this uppercase string with a space and the original string "hello world". Therefore, the output of this code is "HELLO WORLD hello world".

    Rate this question:

  • 30. 

    Which two code segments output the number 20 to the console window?

    • A.

      MyValue01 = “10” myValue02 = “10” myValue01 = myValue01 + myValue02 print(myValue02)

    • B.

      MyValue01 = 10 myValue02 = 10 myValue01 = myValue01 + myValue02 print(myValue01)

    • C.

      MyValue01 = 10 myValue02 = 10 print(myValue01 + myValue02)

    • D.

      MyValue01 = “10” myValue02 = “10” print(myValue01 + myValue02)

    Correct Answer(s)
    B. MyValue01 = 10 myValue02 = 10 myValue01 = myValue01 + myValue02 print(myValue01)
    C. MyValue01 = 10 myValue02 = 10 print(myValue01 + myValue02)
    Explanation
    The first code segment outputs the number 20 to the console window because it assigns the value 10 to myValue01 and myValue02, then adds them together and assigns the result to myValue01. Finally, it prints the value of myValue01, which is 20. The second code segment also outputs the number 20 to the console window because it assigns the value 10 to myValue01 and myValue02, then adds them together using the "+" operator and prints the result, which is 20.

    Rate this question:

  • 31. 

    True or False: You can combine a numeric value and a string by using the + symbol. 

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    In many programming languages, you can combine (concatenate) a numeric value and a string by using the + symbol, but the numeric value must first be converted to a string. For example, in Python:
    python
    Copy code
    number = 5 string = " apples" result = str(number) + string print(result) # Output: "5 apples"
    In this example, the numeric value 5 is converted to a string using str(number) before being concatenated with the string " apples".

    Rate this question:

  • 32. 

    What is an exception?

    • A.

      A variable

    • B.

      A function

    • C.

      An event that occurs due to incorrect code or input

    Correct Answer
    C. An event that occurs due to incorrect code or input
    Explanation
    An exception is an event that occurs due to incorrect code or input. It is a situation where the program encounters an unexpected condition or error that it cannot handle. When an exception occurs, the program's normal flow is disrupted, and it may terminate or take alternative paths to handle the exception. Exceptions are used to handle and manage errors in programming languages, allowing developers to catch and handle these exceptional situations gracefully, rather than crashing the program.

    Rate this question:

  • 33. 

    class Sales: def __init__(self, id): self.id = id id = 100 val = Sales(123) print (val.id)

    • A.

      All

    • B.

      100

    • C.

      123

    • D.

      None

    Correct Answer
    C. 123
    Explanation
    The code defines a class called Sales with an __init__ method that takes an argument called id. Inside the __init__ method, the value of id is assigned to the instance variable self.id. Then, the local variable id is assigned the value 100. Finally, an instance of the Sales class is created with the id argument 123, and the value of the instance variable val.id is printed, which is 123.

    Rate this question:

  • 34. 

    Which two operators can be used on numeric values in Python?

    • A.

      @

    • B.

      %

    • C.

      +

    • D.

      #

    Correct Answer(s)
    B. %
    C. +
    Explanation
    The two operators that can be used on numeric values in Python are the modulo operator (%) and the addition operator (+). The modulo operator returns the remainder of a division operation, while the addition operator performs addition between two numbers.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Jul 16, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Jul 19, 2018
    Quiz Created by
    Catherine Halcomb
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.