1.
Given the numeric variable myNumber, which lines of code properly prints the value? (Select 2)
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.
2.
You have the following code segment:
testValue = 2
testValue = 10 + (testValue * 2)
What is the value of testValue?
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.
3.
Which piece of code shows a new class Spam inheriting from 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".
4.
Which two sections of code print 'Hello World' to the console?
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.
5.
You have the following code segment:
myValue = “yes”
if myValue != “yes” :
print(“Hello”)
print(“World”)
What is the output from this code?
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".
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?
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.
7.
What is the result of this code?
def print_double(x):
print(2 * x)
print_double(3)
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.
8.
Which symbol is used to check whether two variables are the same?
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.
9.
Which code segment will NOT reach its print() function?
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.
10.
Which two lines of code are valid strings in Python?
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.
11.
True or False: A string can be surrounded by three sets of single quotation marks or by three sets of double quotation marks.
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.
12.
Which line of code has the proper syntax for the print statement?
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.
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?
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.
14.
Which line of code produces an error?
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.
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'.
16.
What is the output of this code?
>>> int("3" + "4")
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".
17.
What is the output of this code?
>>> float("210" * int(input("Enter a number:" )))
Enter a number: 2
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".
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".
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.
20.
What is the result of this code?
if 1 + 1 * 3 == 6:
print("Yes")
else:
print("No")
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".
21.
Which value type does input() return?
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.
22.
Which number is not printed by this code?
try:
print(1)
print(20 / 0)
print(2)
except ZeroDivisionError:
print(3)
finally:
print(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.
23.
True or False: Variables can be assigned only once.
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.
24.
You have the following code segment:
myString01 = “my“
myString02 = “string”
print(myString01 , myString02)
What is the output of this code?
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.
25.
Will the close() function get called in this code?
try:
f = open("filename.txt")
print(f.read())
print(1 / 0)
finally:
f.close()
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.
26.
Which line of code writes "Hello world!" to a file?
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.
27.
You have the following code segment:
myString01 = “my“
myString02 = “string”
print(myString01 + myString02)
What is the output of this code?
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".
28.
True or False: A variable is a placeholder for data.
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.
29.
You have the following code segment:
myString = “hello world”
print( myString.upper() + “ “ + myString )
What is the output of this code?
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".
30.
Which two code segments output the number 20 to the console window?
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.
31.
True or False: You can combine a numeric value and a string by using the + symbol.
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".
32.
What is an exception?
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.
33.
class Sales:
def __init__(self, id):
self.id = id
id = 100
val = Sales(123)
print (val.id)
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.
34.
Which two operators can be used on numeric values in Python?
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.