1.
The following control structure
n = 3
if n == 2:
print("Hello")
elif n == 3:
print("World")
else:
print("Nothing")
Results is:
Correct Answer
C. The string "World" is printed to the standard out
Explanation
The code assigns the value 3 to the variable n. Then, it checks if n is equal to 2. Since n is not equal to 2, it moves to the next condition and checks if n is equal to 3. Since n is equal to 3, it executes the code block under the elif statement, which prints the string "World" to the standard output.
2.
Which Of The Following Command Is Used To Open A File “C:\Temp.Txt” In Read-Mode Only?
Correct Answer
B. Infile = open(“c:\\temp.txt”, “r”)
Explanation
The correct answer is "infile = open(“c:\\temp.txt”, “r”)". This is because the double backslash "\\" is used to escape the special characters in the file path. In this case, it is used to escape the backslash character "\" in the file path "c:\temp.txt". The "r" mode is used to open the file in read-mode only, allowing only reading operations to be performed on the file.
3.
Which Of The Following Command Is Used To Open A File “C:\Temp.Txt” In Append-Mode?
Correct Answer(s)
A. Outfile = open(“c:/temp.txt”, “a”)
D. Outfile = open(“c:\\temp.txt”, “a”)
Explanation
The correct answer is "outfile = open(“c:/temp.txt”, “a”),outfile = open(“c:\\temp.txt”, “a”)" because the "a" mode in the open() function is used to open a file in append mode. This means that if the file already exists, new data will be added to the end of the file, and if the file does not exist, a new file will be created. The first option "outfile = open(“c:/temp.txt”, “a”)" uses forward slashes in the file path, which is a valid way to specify the path in Python. The second option "outfile = open(“c:\\temp.txt”, “a”)" uses double backslashes to escape the backslash character in the file path, which is also a valid way to specify the path in Python.
4.
What is the type of a? a = {1,2:3}
Correct Answer
D. It causes SyntaxError
Explanation
The given assignment statement `a = {1,2:3}` causes a syntax error because it is trying to create a dictionary with both a key-value pair (`2:3`) and an element (`1`) without a key. In Python, dictionaries require key-value pairs, and sets require only elements. Therefore, the correct answer is "It causes SyntaxError."
5.
What is the type of b?
a = "bay"
b = a[0]
Correct Answer
D. Str
Explanation
The variable "b" is assigned the value of the first character of the string "a". Since the value of "a" is a string, the type of "b" will also be a string.
6.
What is the result of this code? a=[1,2,3,4,5] a[3:1:-1]
Correct Answer
C. [4, 3]
Explanation
This code is using list slicing to extract a portion of the list 'a'. The syntax for list slicing is 'a[start:end:step]'. In this case, the start index is 3, the end index is 1, and the step is -1. This means that it will start at index 3, go backwards towards index 1, and include every element in between. Therefore, the result will be [4, 3].
7.
What is the result of this code? a=[1,2,3,4,5,6,7,8,9] a[::2]
Correct Answer
D. [1,3,5,7,9]
Explanation
The code is using slicing to extract elements from the list "a". The syntax [::2] means that it will start from the beginning of the list and take every second element. So, the result will be a new list containing the elements [1, 3, 5, 7, 9].
8.
How to assign a tuple of length 1 to a?
Correct Answer
B. A = (1,)
Explanation
In order to assign a tuple of length 1 to variable "a", we need to use the syntax "(1,)". This creates a tuple with a single element, which in this case is the integer 1. The comma after the 1 is necessary to indicate that it is a tuple and not just a parentheses grouping.
9.
What will be placed in a? a = 2,3
Correct Answer
C. (2,3)
Explanation
The answer (2,3) suggests that the value of 'a' will be assigned as a tuple containing the values 2 and 3.
10.
What is the output of the below program?
>>> logfile = open('test.log', 'a')
>>> logfile.write('line 2')
>>> logfile.close()
>>> print file('test.log').read()</pre>
Correct Answer
A. Line 2
Explanation
The program opens a file called "test.log" in append mode and writes the string "line 2" to it. Then, the file is closed. Finally, the program reads the contents of the file "test.log" and prints it. The contents of the file will be "line 2".