1.
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 represented using either single quotes or double quotes.
2.
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 can be used in some programming languages to represent multi-line strings. It provides a convenient way to include special characters, line breaks, and formatting within the string without the need for escape sequences.
3.
Which line of code has the proper syntax for the print statement?
Correct Answer
D. Print( ‘it\’s a rainy day’ )
Explanation
The line of code "print( ‘it\’s a rainy day’ )" has the proper syntax for the print statement because it uses single quotes to enclose the string and escapes the apostrophe within the string using a backslash.
4.
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 \n and \n we can do newlines!". This is because the "\n" character is used to represent a newline in Python, so when the code is executed, it will print the text with the newlines intact.
5.
Which value type does input() return?
Correct Answer
B. String
Explanation
The input() function in Python returns a string value. This means that whenever we use the input() function to get user input, the value returned will be in the form of a string. If we want to use this input as an integer or a float, we need to explicitly convert it using the int() or float() functions, respectively.
6.
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 declared, its value can be changed or updated throughout the program. This allows for flexibility and the ability to store and manipulate different values as needed. Therefore, the statement "Variables can be assigned only once" is false.
7.
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 code segment declares two variables, myString01 and myString02, and assigns them the values "my" and "string" respectively. Then, it prints the values of both variables separated by a space. Therefore, the output of this code will be "my string".
8.
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 concatenates the values of myString01 and myString02 using the + operator. The value of myString01 is "my" and the value of myString02 is "string". Therefore, the output of the code is "mystring".
9.
True or False: A variable is a placeholder for data.
Correct Answer
A. True
Explanation
A variable is a placeholder for data because it is used to store and represent different values or information within a program. It can hold various types of data such as numbers, text, or objects, and its value can be changed throughout the execution of the program. By using variables, programmers can manipulate and work with data in a flexible and dynamic way. Therefore, it is correct to say that a variable acts as a placeholder for data.
10.
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 converts the string "hello world" to uppercase using the `.upper()` method and then concatenates it with a space and the original string. Therefore, the output of this code would be "HELLO WORLD hello world".
11.
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 assigns the value "10" to myValue01 and myValue02, then concatenates them and assigns the result to myValue01. Finally, it prints the value of myValue01, which is 20. The second code segment assigns the value 10 to myValue01 and myValue02, then adds them together and prints the result, which is also 20.
12.
True or False: You can combine a numeric value and a string by using the + symbol.
Correct Answer
B. False
Explanation
The + symbol in programming is used for concatenation, which is the process of combining two strings together. It is not used to combine a numeric value and a string. To combine a numeric value and a string, you would need to convert the numeric value to a string first and then concatenate them. Therefore, the statement is false.
13.
Which two operators can be used on numeric values in Python?
Correct Answer(s)
B. %
C. +
Explanation
The % operator is used for modulus, which returns the remainder of a division operation. The + operator is used for addition, which adds two or more numeric values together. These two operators are commonly used in Python for performing mathematical operations on numeric values.
14.
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 correct answer is "print(myNumber)" and "print("%d" % myNumber)". The first option prints the value of the variable directly, while the second option uses string formatting to insert the value of the variable into the string before printing it.
15.
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 initializes the variable testValue with the value 2. Then, it assigns the result of the expression 10 + (testValue * 2) to testValue. Since testValue is currently 2, the expression evaluates to 10 + (2 * 2) = 10 + 4 = 14. Therefore, the value of testValue is 14.
16.
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")
This section checks if myValue is equal to "Hello World" and if it is, it prints "Hello World" to the console.
2. myValue = "Hello World"
if myValue == "Hello World":
print(myValue)
This section also checks if myValue is equal to "Hello World" and if it is, it prints the value of myValue, which is "Hello World", to the console.
17.
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 print("Hello") statement is not executed. Finally, the print("World") statement is executed, resulting in the output "World".
18.
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 will be "Item Purchased". This is because the condition in the if statement is true, as the currentAmount is equal to the itemCost. Therefore, the code inside the if block will be executed, which is to print "Item Purchased".
19.
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 the two variables and returns true if they are equal, and false otherwise.
20.
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 block inside the if statement will not be executed.
21.
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, concatenating a string with a number is not allowed and results in a type mismatch error.
22.
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 will be 'TCSTCSTCS'.
23.
What is the output of this code?
>>> int("3" + "4")
Correct Answer
C. 34
Explanation
The code is concatenating the strings "3" and "4" using the + operator. Then, it is converting the resulting string "34" into an integer using the int() function. The int() function converts a string representation of a number into an actual integer. Therefore, the output of the code is the integer 34.
24.
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 first prompts the user to enter a number, which is then converted to an integer. The string "210" is then multiplied by this integer. The resulting string "210210" is then converted to a float, resulting in the output of "210210.0".
25.
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 this 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 "num < 5" is not executed because the condition is false. Finally, the last if statement "num == 7" is true, so the code inside is executed and "7" is printed. However, since there is no print statement for "5", it is not included in the output.
26.
A shorter option for an "else if" statement is:
Correct Answer
elif
Explanation
The correct answer is "elif" because it is a shorter way to write "else if" in programming. It is used as a conditional statement to check for multiple conditions after an initial "if" statement. The "elif" statement allows for more concise and readable code by avoiding nested if statements.
27.
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 is false. The expression 1 + 1 * 3 evaluates to 4, not 6. Therefore, the code will execute the else block and print "No".
28.
Which line of code will cause an error?
num = [5, 4, 3, [2], 1]
print(num[0])
print(num[3][0])
print(num[5])
Correct Answer
C. Line 4
Explanation
Line 4 will cause an error because the index 5 is out of range for the list "num". The list "num" only has indices from 0 to 4.