Python Quiz For Beginners

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Catherine Halcomb
Catherine Halcomb
Community Contributor
Quizzes Created: 1443 | Total Attempts: 6,714,231
| Attempts: 4,943 | Questions: 34
Please wait...
Question 1 / 34
0 %
0/100
Score 0/100
1. Which symbol is used to check whether two variables are the same?

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.

Submit
Please wait...
About This Quiz
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... see moreof this quiz and get to test your understanding of Python.
So, are you ready? Let's start then.
Good luck!
see less

Personalize your quiz and earn a certificate with your name on it!
2. What is an exception?

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.

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

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.

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

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.

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

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.

Submit
6. A shorter option for an "else if" statement is:

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.

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

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.

Submit
8. Which two lines of code are valid strings in Python?  

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.

Submit
9. 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?

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.

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

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

Submit
11. True or False: Variables can be assigned only once.

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.

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

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.

Submit
13. What is the result of this code? if 1 + 1 * 3 == 6:    print("Yes") else:    print("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".

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

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.

Submit
15. Which line of code produces an error?

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.

Submit
16. Which value type does input() return?

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.

Submit
17. Which piece of code shows a new class Spam inheriting from 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".

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

Explanation

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

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

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.

Submit
20. You have the following code segment: myValue = "yes" if myValue != "yes" :     print("Hello") print("World") What is the output from this code?

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

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

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.

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

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

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

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

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

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.

Submit
25. You have the following code segment: myString01 = "my" myString02 = "string" print(myString01 + myString02) What is the output of this code?

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

Submit
26. 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?

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.

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

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.

Submit
28. You have the following code segment: myString01 = "my" myString02 = "string" print(myString01 , myString02) What is the output of this code?

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.

Submit
29.
class Sales:
    def __init__(self, id):
        self.id = id
        id = 100

val = Sales(123)
print (val.id)

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.

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

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.

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

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

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

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

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

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.

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

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.

Submit
View My Results

Quiz Review Timeline (Updated): Jul 16, 2024 +

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
Cancel
  • All
    All (34)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Which symbol is used to check whether two variables are the same?
What is an exception?
True or False: A variable is a placeholder for data.
What is the result of this code? ...
Which two operators can be used on numeric values in Python?
A shorter option for an "else if" statement is:
 You have the following code segment: ...
Which two lines of code are valid strings in Python?  
You have the following code segment: ...
You have the following code segment: ...
True or False: Variables can be assigned only once.
True or False: A string can be surrounded by three sets of single...
What is the result of this code? ...
Which line of code writes "Hello world!" to a file?
Which line of code produces an error?
Which value type does input() return?
Which piece of code shows a new class Spam inheriting from Egg?
What is the output of this code? ...
Will the close() function get called in this code? ...
You have the following code segment: ...
Which line of code has the proper syntax for the print statement?
What is the output of this code? ...
True or False: You can combine a numeric value and a string by using...
Which number is not printed by this code? ...
You have the following code segment: ...
You have the following code segment: ...
Which two sections of code print 'Hello World' to the console?
You have the following code segment: ...
Class Sales: ...
Which code segment will NOT reach its print() function?
What is the output of this code? ...
What is the output of this code? ...
Which two code segments output the number 20 to the console window?
Given the numeric variable myNumber, which lines of code properly...
Alert!

Advertisement