1.
Which of the following is not a valid data type in python?
Correct Answer
C. Double
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.
2.
Complete the code to print the following numbers:13579for i in range(1, 10, __): print i
Correct Answer
2
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.
3.
Complete the code to print the following patterns:
1 1 1 1 12 2 2 2 23 3 3 3 34 4 4 4 45 5 5 5 5
for i in range(1, 6, 1): print ____ * 5
Correct Answer
str(i)
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.
4.
Study the code below:
for i in x: print iWhich of the option below is incorrect?
Correct Answer
B. X is an int
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.
5.
Refer to the the code below:a_list = [10, 20, 30, 40].What is the value of a_list[-1]?
Correct Answer
D. 40
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.
6.
Refer to the the code below:a_list = [10, 20, 30, 40].What is the value of a_list[-2]?
Correct Answer
C. 30
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.
7.
Fill in the blank to produce the following output:[11, 22, 33, 44, 55, 66, 77, 88]print range(11, 90, __ )
Correct Answer
11
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].
8.
Which of the following is NOT a valid call to the function funA?def funA(a, b): print a + int(b)
Correct Answer
C. FunA("3", 4.0)
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.
9.
What is the value of a?a = 1 == 1 and not 1 != 1a = _____
Correct Answer
True
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.
10.
What is the value of x?x= 0for i in range(2, 20, 3): if i %2 == 0: x += 1print x
Correct Answer
B. 3
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.
11.
Study the code given below:import randomx = random.randint(1,6)y = random.randint(1,6)z = random.randint(1,6)a = x + y + zWhat is the possible range of values for a?
Correct Answer
D. 3 to 18
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.
12.
Colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] What is the value of colors[2]?
Correct Answer
yellow
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".
13.
What is the value of x after executing the code shown below?i = 0x = 0while i < 10: if i % 2 == 0: x += 1 i += 1x = _____.
Correct Answer
5
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.
14.
Which of the following options create an instance of Laptop correctly?class Laptop: def __init__(self, capacity): self.capacity = capacity self.color = 'green'
Correct Answer
C. Laptop = Laptop(2000)
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.
15.
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
Correct Answer
C. Laptop = Laptop(2000)
laptop.change_color('white')
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.
16.
What is the value of x after executing the code shown below?x = 'hello world'x = x.upper()Value of x is ______
Correct Answer
HELLO WORLD
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'.
17.
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?
Correct Answer
A. 2
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".
18.
Which line of code will add a new element 10 to the end of the list?a_list = [2,4,6,8]
Correct Answer
C. A_list.append(10)
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].
19.
Def funA(a, b): return a * bdef funB(c, d): return a + bx = funA(funB(3,4), 5)What is the value of x after executing the code shown above?x = ____
Correct Answer
35
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.
20.
Which of the following is not a valid function name?
Correct Answer
A. 1function
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.
21.
Study the code below.x = 1y = 3In Python, x/y returns you 0, which is wrong. Which of the solutions below will fix this.
Correct Answer
B. Use float(x)/y instead.
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.
22.
To print out the text as shown below, what code should we execute?I love to take a ride in his sport's car!
Correct Answer
C. Print "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.
23.
X = "1"y = 10Pick the code which will produce an error when executed.
Correct Answer
E. Print x + y
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.
24.
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?
Correct Answer
A. Print convert_to_text(convert_to_grade(20))
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.
25.
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?
Correct Answer
E. My_list_2 = range(1,13,2)
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].
26.
Def fun_A(): print 1def fun_B(): return 1Which of the following code will output the value 2?
Correct Answer
B. Print fun_B() + fun_B()
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`.
27.
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"
Correct Answer
B. X % 2 == 0
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.
28.
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
Correct Answer
C. We should put the return x at the last line of code in the function square because any code within the function after return will not get executed.
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.
29.
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]?
Correct Answer
D. My_list.pop(5)
my_list.remove(19)
my_list.remove(55)
my_list.remove(55)
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.
30.
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 = _______
Correct Answer
14
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".
31.
Which of the following will NOT produce an error?
Correct Answer
C. A = [1,2,3]
a.append(4)
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.
32.
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
Correct Answer
C. The value of x is the largest number in the list.
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.
33.
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
Correct Answer
B. The value of x is the smallest number in the list.
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.
34.
Which of the following will print the number of 'A' in the word: "HOW ARE YOU"?word = "HOW ARE YOU"?
Correct Answer
C. Print word.count('A')
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".
35.
Complete the code to print the following patterns:
5 5 5 5 54 4 4 4 43 3 3 3 32 2 2 2 21 1 1 1 1
for i in range(5, 0, ____ ): print str(i) * 5
Correct Answer
-1
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.
36.
Write down the value of x after executing the code shown below:x = 0for i in range(2, 10, 2): x = x + iprint xx is ______.
Correct Answer
20
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.
37.
Write down the value of x after executing the code given below:def funA(a): return adef funB(b): return b * 2def funC(c): return c + 3x = funA(funB(funC(1)))x is _______.
Correct Answer
8
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.