Python File Quiz: Trivia Test!

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 Singaporetips
S
Singaporetips
Community Contributor
Quizzes Created: 1 | Total Attempts: 8,887
| Attempts: 8,887 | Questions: 37
Please wait...
Question 1 / 37
0 %
0/100
Score 0/100
1. What is the value of x after executing the code shown below?

x = 'hello world'
x = x.upper()

Value of x is ______

Explanation

The code first assigns the string 'hello world' to the variable x. Then, the x.upper() function is called, which converts all the characters in x to uppercase. Therefore, the value of x after executing the code will be 'HELLO WORLD'.

Submit
Please wait...
About This Quiz
Python File Quiz: Trivia Test! - Quiz

Get ready for this Python File Quiz: Trivia Test! Python is a high-level programming language that allows the user to focus on the core functionality of the python application, and it can be used to build just about anything with the right tools. Python supports modules and packages, which encourages... see moreprogram modularity and code re-use. Test your understanding of Python programming by taking up the test below. Best of luck! All the best! see less

Personalize your quiz and earn a certificate with your name on it!
2. Colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

What is the value of colors[2]?

Explanation

The value of colors[2] is "yellow" because in Python, the indexing of a list starts from 0. Therefore, colors[2] refers to the element at the index 2 in the list "colors", which is "yellow".

Submit
3. Fill in the blank to produce the following output:

[11, 22, 33, 44, 55, 66, 77, 88]

print range(11, 90, __ )

Explanation

The correct answer is 11 because the given code is using the range function to generate a sequence of numbers starting from 11 and ending at 90 (exclusive) with a step size of 11. This means that the sequence will include 11 and every subsequent number that is a multiple of 11 until it reaches or exceeds 90. Therefore, the output will be [11, 22, 33, 44, 55, 66, 77, 88].

Submit
4. Complete the code to print the following numbers:
1
3
5
7
9

for i in range(1, 10, __):
    print i

Explanation

The code is using a for loop with a range starting from 1 and ending at 10, with a step size of 2. This means that it will iterate over the numbers 1, 3, 5, 7, and 9. The code then prints each of these numbers on a new line.

Submit
5. Refer to the the code below:

a_list = [10, 20, 30, 40].

What is the value of a_list[-2]?

Explanation

The value of a_list[-2] is 30. In Python, negative indices are used to access elements from the end of a list. So, a_list[-2] refers to the second last element in the list, which is 30.

Submit
6. Def funA(a, b):
    return a * b

def funB(c, d):
   return a + b

x = funA(funB(3,4), 5)

What is the value of x after executing the code shown above?
x = ____

Explanation

The value of x is 35 because the code first calls the function funB(3,4) which returns the value 7. Then, it calls the function funA(7,5) which returns the value 35. Therefore, x is assigned the value 35.

Submit
7. To print out the text as shown below, what code should we execute?

I love to take a ride in his sport's car!

Explanation

The correct answer is "print 'I love to take a ride in his sport's car!'" because it uses single quotes to enclose the string and includes the correct punctuation within the string.

Submit
8. Which of the following is not a valid data type in python? 

Explanation

In Python, "double" is not a valid data type. Python does not have a separate data type for double precision floating-point numbers like some other programming languages. Instead, Python has a single floating-point data type called "float" which can represent both single and double precision numbers.

Submit
9. Refer to the the code below:

a_list = [10, 20, 30, 40].

What is the value of a_list[-1]?

Explanation

The value of a_list[-1] is 40 because the index -1 refers to the last element in the list. In this case, the last element of the list a_list is 40.

Submit
10. Which line of code will add a new element 10 to the end of the list?

a_list = [2,4,6,8]

Explanation

The line of code "a_list.append(10)" will add a new element 10 to the end of the list. The append() function is used to add an element to the end of a list. In this case, it will add the element 10 to the list "a_list", resulting in the list [2, 4, 6, 8, 10].

Submit
11. The x % y operator is used to check for remainders when x is divided by y.

We can use it to check for even or odd numbers. To check for even numbers, what should the condition be like?

if _______:
   print "x is an even number"

Explanation

The condition should be "x % 2 == 0" because when a number is divided by 2 and the remainder is 0, it indicates that the number is even.

Submit
12. Write down the value of x after executing the code given below:

def funA(a):
    return a

def funB(b):
   return b * 2

def funC(c):
   return c + 3

x = funA(funB(funC(1)))

x is _______.

Explanation

The code is a series of nested function calls. First, the function funC(1) is called, which returns 1 + 3 = 4. Then, the result of funC(1), which is 4, is passed as an argument to funB. funB(4) returns 4 * 2 = 8. Finally, the result of funB(4), which is 8, is passed as an argument to funA. funA(8) simply returns the value passed to it, which is 8. Therefore, the value of x after executing the code is 8.

Submit
13. What is wrong with the function below?

def square(x):
    x = x**2
    return x
    print "The value of x has been squared"

squared_value = square(10)
print squared_value

Explanation

The function is currently returning the squared value of x, but the print statement that follows the return statement will never be executed. This is because once a return statement is encountered, the function immediately exits and returns the value. Therefore, any code after the return statement will not be executed. To fix this, the return statement should be moved to the last line of the function, so that the squared value is returned and then printed outside of the function.

Submit
14. What is the value of x after executing the code shown below?

i = 0
x = 0
while i < 10:
    if i % 2 == 0:
        x += 1
    i += 1

x = _____.

Explanation

The code initializes two variables, i and x, to 0. It then enters a while loop that will continue as long as i is less than 10. Inside the loop, it checks if i is divisible by 2 (i.e., if i is even). If i is even, it increments x by 1. Finally, it increments i by 1. Since the loop will run 5 times (when i is 0, 2, 4, 6, and 8), x will be incremented 5 times, resulting in a final value of 5.

Submit
15. A_list = range(2, 11, 2)
a_list = a_list[1: -1]

Look at the options given, which number is not contained in a_list?

Explanation

The given code creates a list called "a_list" with numbers from 2 to 10 (excluding 10) with a step size of 2. Then, the code slices the list from index 1 to the second-to-last index, which removes the first and last elements from the list. Therefore, the resulting "a_list" will contain the numbers 4, 6, and 8. The number 2 is not contained in "a_list".

Submit
16. Which of the following is not a valid function name?

Explanation

The function name "1function" is not valid because function names cannot start with a number. They can only start with a letter or an underscore.

Submit
17. Study the code given below:

import random
x = random.randint(1,6)
y = random.randint(1,6)
z = random.randint(1,6)

a = x + y + z

What is the possible range of values for a?

Explanation

The possible range of values for 'a' can be determined by considering the minimum and maximum values that can be generated for 'x', 'y', and 'z'. Since each of these variables is assigned a random integer between 1 and 6, the minimum value for 'a' would be 1 + 1 + 1 = 3, and the maximum value would be 6 + 6 + 6 = 18. Therefore, the possible range of values for 'a' is 3 to 18.

Submit
18. Study the code given below, and choose the most appropriate answer:
a = [1, 4, 20, 2, 5]
x = a[0]
for i in a:
    if i < x:
        x = i
print x

Explanation

The given code initializes the variable x with the first element of the list a. Then, it iterates through each element of the list a. If the current element is smaller than x, x is updated with the current element. Finally, the value of x is printed. Therefore, the value of x will be the smallest number in the list a.

Submit
19. Which of the following will print the number of 'A' in the word: "HOW ARE YOU"?

word = "HOW ARE YOU"?

Explanation

The correct answer is "print word.count('A')". This will print the number of occurrences of the letter 'A' in the word "HOW ARE YOU". The count() method is used to count the number of occurrences of a substring within a string. In this case, 'A' is the substring being counted in the variable "word".

Submit
20. Write down the value of x after executing the code shown below:

x = 0

for i in range(2, 10, 2):
    x = x + i

print x

x is ______.

Explanation

The code initializes a variable "x" to 0. Then, using a for loop, it iterates over the range from 2 to 10 with a step of 2. Inside the loop, the value of "x" is updated by adding the current value of "i" to it. Since "i" takes the values 2, 4, 6, and 8, the final value of "x" will be 20.

Submit
21. What is the value of a?
a  = 1 == 1 and not 1 != 1

a = _____

Explanation

The value of "a" in the given expression is "True". This is because the expression "1 == 1" evaluates to True, indicating that the statement "1 is equal to 1" is true. Since the "and" operator is used, the second part of the expression "not 1 != 1" is also evaluated. The expression "1 != 1" evaluates to False, indicating that the statement "1 is not equal to 1" is false. The "not" operator negates this result, making it True. Therefore, the overall expression "1 == 1 and not 1 != 1" evaluates to True.

Submit
22. X = "1"
y = 10

Pick the code which will produce an error when executed.

Explanation

The code "print x + y" will produce an error when executed because it is attempting to concatenate a string ("x") with an integer ("y") which is not allowed in Python.

Submit
23. What is the value of x after executing the code shown below?

def funA(x):
    return str(x)

def funB(y)
    return str(2 * y)

x = funA(1) + funB(2)

x = _______

Explanation

The code first calls the function funA(1), which returns the string "1". Then it calls the function funB(2), which returns the string "4". Finally, it concatenates the two strings "1" and "4" using the "+" operator, resulting in the string "14". Therefore, the value of x after executing the code is "14".

Submit
24. Complete the code to print the following patterns: 5 5 5 5 5
4 4 4 4 4
3 3 3 3 3
2 2 2 2 2
1 1 1 1 1 for i in range(5, 0, ____ ):
    print str(i) * 5

Explanation

The code is using a for loop with the range function to iterate from 5 to 0 (exclusive) with a step of -1. The string representation of the current number is then multiplied by 5 and printed. The correct answer of -1 in the blank indicates that the step size should be -1 to count down from 5 to 0.

Submit
25. Study the code given below, and choose the most appropriate answer:
a = [1, 4, 20, 2, 5]
x = a[0]
for i in a:
    if i > x:
        x = i
print x

Explanation

The code iterates through each element in the list 'a' and checks if the current element is greater than the current value of 'x'. If it is, then the value of 'x' is updated to the current element. This process continues until all elements have been checked. Therefore, the final value of 'x' will be the largest number in the list.

Submit
26. Which of the following will NOT produce an error?

Explanation

The given code will not produce an error because it is correctly using the append() method to add the value 4 to the list a. The append() method is used to add elements to the end of a list, so it does not cause any error in this case.

Submit
27. A function, convert_to_grade takes in a number and returns the letter grade for it.
Another function, convert_to_text takes in a letter grade and returns some words of encouragement based on the letter grade.

If you would like to get some words of encouragement for someone who scored 20 marks, what code should be executed?

Explanation

The correct code to execute in order to get some words of encouragement for someone who scored 20 marks is "print convert_to_text(convert_to_grade(20))". This code first calls the function "convert_to_grade(20)" to convert the score of 20 into a letter grade. Then, it passes the resulting letter grade to the function "convert_to_text" to get the words of encouragement based on the letter grade. Finally, it prints the words of encouragement on the console.

Submit
28. Def fun_A():
   print 1

def fun_B():
   return 1

Which of the following code will output the value 2?

Explanation

The correct answer is `print fun_B() + fun_B()`. This is because the function `fun_B()` returns the value `1`, and when we add `1` to `1`, we get `2`.

Submit
29. Which of the following options create an instance of Laptop and change the color of the laptop to white correctly?

class Laptop:
    def __init__(self, capacity):
        self.capacity = capacity
        self.color = 'green'

    def change_color(self, new_color):
       self.color = new_color

Explanation

The correct answer is "laptop = Laptop(2000)\nlaptop.change_color('white')". This is because it first creates an instance of the Laptop class with a capacity of 2000. Then, it calls the change_color method on the laptop instance and passes 'white' as the new_color argument. This correctly changes the color of the laptop to white.

Submit
30. What is the value of x?

x= 0
for i in range(2, 20, 3):
  if i %2 == 0:
      x += 1

print x

Explanation

The given code initializes the variable x to 0. Then, it enters a for loop that iterates over the range from 2 to 20 with a step size of 3. Inside the loop, it checks if the current value of i is divisible by 2. If it is, it increments the value of x by 1. After the loop ends, the value of x is printed. In this case, the loop runs 6 times with the values of i being 2, 5, 8, 11, 14, and 17. Out of these, only 2 and 14 are divisible by 2, so the value of x is incremented twice. Therefore, the final value of x is 2+1=3.

Submit
31. Which of the following options create an instance of Laptop correctly?

class Laptop:
    def __init__(self, capacity):
        self.capacity = capacity
        self.color = 'green'

Explanation

The correct answer is "laptop = Laptop(2000)". This creates an instance of the Laptop class with a capacity of 2000. The other options either have incorrect arguments or are missing the required argument for the capacity parameter.

Submit
32. Complete the code to print the following patterns: 1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5 for i in range(1, 6, 1):
    print ____ * 5

Explanation

The correct answer is "str(i)" because the code is using a for loop to iterate over the range from 1 to 6 (exclusive) with a step of 1. Inside the loop, it is printing the string representation of the current value of 'i' multiplied by 5. Since 'i' is an integer, it needs to be converted to a string using the str() function before multiplying it by 5. This will result in the desired pattern of repeating numbers.

Submit
33. Study the code below: for i in x:
   print i

Which of the option below is incorrect?

Explanation

The given code is using a for loop to iterate over the elements in the variable "x", suggesting that "x" is an iterable object. However, the answer states that "x is an int" which is incorrect because an integer is not an iterable object and cannot be used in a for loop.

Submit
34. My_list = [1,3,5,7,9,11]

Which of the following code will produce the same contents for my_list_2 as that stored in my_list?

Explanation

The correct answer is "my_list_2 = range(1,13,2)". This is because the range function generates a sequence of numbers starting from the first parameter (1), up to but not including the second parameter (13), incrementing by the third parameter (2). Therefore, this code will produce a sequence of numbers similar to the contents of my_list, which is [1,3,5,7,9,11].

Submit
35. Which of the following is NOT a valid call to the function funA?

def funA(a, b):
    print a + int(b)

Explanation

The function funA takes two arguments, a and b, and prints the sum of a and the integer value of b. In the given options, all the calls to funA are valid except for funA("3", 4.0). This is because the function tries to convert the string "3" to an integer, which is not possible. The function can only convert strings that represent valid integers to integers.

Submit
36. Study the code below.
x = 1
y = 3

In Python, x/y returns you 0, which is wrong. Which of the solutions below will fix this.

Explanation

The code is performing integer division, which returns only the whole number quotient and discards the decimal part. To fix this and get the correct result, we need to convert either x or y (or both) to float before performing the division. Using float(x)/y will ensure that the division is performed as floating-point division, giving the correct result with the decimal part included.

Submit
37. A list is defined as below:

my_list = [1,5,10,19,55, 30,55, 99]

Which sequence of code below can produce a new list which contains
[1,10,30,99]?

Explanation

The code sequence my_list.pop(5), my_list.remove(19), my_list.remove(55), my_list.remove(55) will produce a new list which contains [1,10,30,99]. The pop(5) function will remove the element at index 5 from the list, which is 30. Then, the remove(19) function will remove the element 19 from the list, followed by removing the two occurrences of 55 using remove(55) function. Finally, the remove(55) function will remove the second occurrence of 55 from the list. This will result in a new list with the desired elements.

Submit
View My Results

Quiz Review Timeline (Updated): Mar 22, 2023 +

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

  • Current Version
  • Mar 22, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Aug 20, 2009
    Quiz Created by
    Singaporetips
Cancel
  • All
    All (37)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What is the value of x after executing the code shown below?x = 'hello...
Colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo',...
Fill in the blank to produce the following output:[11, 22, 33, 44, 55,...
Complete the code to print the following numbers:13579for i in...
Refer to the the code below:a_list = [10, 20, 30, 40].What is the...
Def funA(a, b):    return a * bdef funB(c,...
To print out the text as shown below, what code should we execute?I...
Which of the following is not a valid data type in python? 
Refer to the the code below:a_list = [10, 20, 30, 40].What is the...
Which line of code will add a new element 10 to the end of the...
The x % y operator is used to check for remainders when x is divided...
Write down the value of x after executing the code given below:def...
What is wrong with the function below?def square(x):   ...
What is the value of x after executing the code shown below?i = 0x =...
A_list = range(2, 11, 2)a_list = a_list[1: -1]Look at the options...
Which of the following is not a valid function name?
Study the code given below:import randomx = random.randint(1,6)y =...
Study the code given below, and choose the most appropriate answer: a...
Which of the following will print the number of 'A' in the word: "HOW...
Write down the value of x after executing the code shown below:x =...
What is the value of a?a  = 1 == 1 and not 1 != 1a = _____
X = "1"y = 10Pick the code which will produce an error when executed.
What is the value of x after executing the code shown below?def...
Complete the code to print the following patterns: ...
Study the code given below, and choose the most appropriate answer: a...
Which of the following will NOT produce an error?
A function, convert_to_grade takes in a number and returns the...
Def fun_A():   print 1def fun_B():   return...
Which of the following options create an instance of Laptop and change...
What is the value of x?x= 0for i in range(2, 20, 3):  if i %2 ==...
Which of the following options create an instance of Laptop...
Complete the code to print the following patterns: ...
Study the code below: ...
My_list = [1,3,5,7,9,11]Which of the following code will produce the...
Which of the following is NOT a valid call to the function funA?def...
Study the code below.x = 1y = 3In Python, x/y returns you 0,...
A list is defined as below:my_list = [1,5,10,19,55, 30,55, 99]Which...
Alert!

Advertisement