1.
Which is the correct operator for power(x^y)?
Correct Answer
B. X**y
Explanation
The correct operator for power(x^y) is X**y. This is the exponentiation operator in many programming languages, including Python. It raises the value of X to the power of y. The caret symbol (^) is not commonly used for exponentiation in programming languages, and X^^y is not a valid operator. Therefore, the correct answer is X**y.
2.
What is the output of the following? sentence = 'horses are fast'
regex = re.compile('(?P\w+) (?P\w+) (?P\w+)')
matched = re.search(regex, sentence)
print(matched.groups())
Correct Answer
B. (‘horses’, ‘are’, ‘fast’)
Explanation
The given code uses regular expressions to search for a pattern in the sentence "horses are fast". The pattern is defined as three groups of words separated by spaces. The matched.groups() method returns a tuple containing the matched groups, which in this case are ('horses', 'are', 'fast').
3.
Which of the following cannot be a variable?
Correct Answer
C. In
Explanation
The word "in" cannot be a variable because it is a reserved keyword in Python. Reserved keywords are words that have a special meaning and purpose in the programming language, and they cannot be used as variable names. In Python, "in" is used as a keyword to check if a value is present in a sequence, such as a list or a string. Therefore, it cannot be used as a variable name.
4.
What is the output of the following? x = "abcdef"
i = "a"
while i in x[1:]:
print(i, end = " ")
Correct Answer
C. No output
Explanation
The output of the given code is "no output" because the while loop condition checks if the variable "i" is in the string "x[1:]", which is "bcdef". Since "i" is not present in "bcdef", the loop does not execute and no output is printed.
5.
What is the output of the following? for i in '':
print (i)
Correct Answer
B. (nothing is printed)
Explanation
The given code uses a for loop to iterate over an empty string. Since there are no characters in the string, the loop does not execute and nothing is printed. This is why the output is "nothing is printed".
6.
What is the output of the following code ?- >>>example = "helle"
- >>>example.rfind("e")
Correct Answer
A. 4
Explanation
The output of the code is 4. The rfind() function is used to find the last occurrence of a specified substring within a string. In this case, the last occurrence of the letter "e" in the string "helle" is at index 4.
7.
What is the output when following code is executed ?- >>>print("D", end = ' ')
- >>>print("C", end = ' ')
- >>>print("B", end = ' ')
- >>>print("A", end = ' ')
Correct Answer
C. D C B A
Explanation
The code will print the letters "D C B A" in that order, with each letter separated by a space. The "end = ' '" argument in the print function specifies that a space should be used as the separator between the printed items. Therefore, the output will be "D C B A".
8.
What is the output of the following?print("abcdef".center(10, '12'))
Correct Answer
D. Error
Explanation
The output of the given code will be "error". The center() method is used to center-align a string within a specified width. In this case, the specified width is 10. However, the second argument provided for center() should be a single character, not a string. In the given code, "12" is provided as the second argument, which is a string of two characters. Hence, an error will occur.
9.
What is the output of the following?print("abcdef".find("cd") == "cd" in "abcdef")
Correct Answer
B. False
Explanation
The code is comparing the result of the find() method with the expression "cd" in "abcdef". The find() method returns the index of the first occurrence of the substring "cd" in the string "abcdef". In this case, "cd" is found at index 2. However, the expression "cd" in "abcdef" returns a boolean value indicating whether the substring "cd" is present in the string "abcdef". Since "cd" is present in "abcdef", the expression evaluates to True. Therefore, the correct answer should be True, not False.
10.
What is the output of the following?print('my_string'.isidentifier())
Correct Answer
A. True
Explanation
The output of the given code is True. The isidentifier() method is used to check if a string is a valid identifier in Python. In this case, the string 'my_string' is a valid identifier as it consists of only alphanumeric characters and underscores, and does not start with a number. Therefore, the isidentifier() method returns True.
11.
What is the output of the following?print('1.1'.isnumeric())
Correct Answer
B. False
Explanation
The isnumeric() method in Python returns True if all characters in a string are numeric, otherwise it returns False. In this case, the string '1.1' contains a decimal point, which is not considered numeric. Therefore, the output of the code is False.
12.
To shuffle the list(say list1) what function do we use ?
Correct Answer
C. Random.shuffle(list1)
Explanation
The correct answer is random.shuffle(list1). This function is used to shuffle the elements in a list randomly. It takes a list as an argument and rearranges its elements in a random order. By using this function, the original order of the list is changed, providing a randomized version of the list.
13.
What is the output when following code is executed ?- >>>"Welcome to Python".split()
Correct Answer
A. [“Welcome”, “to”, “Python”]
Explanation
The given code is using the split() method on the string "Welcome to Python". The split() method splits a string into a list of substrings based on a specified delimiter, which is by default a space. Therefore, the output of the code will be a list ["Welcome", "to", "Python"], as each word is separated by a space.
14.
What will be the output?- values = [[3, 4, 5, 1], [33, 6, 1, 2]]
-
- v = values[0][0]
- for row in range(0, len(values)):
- for column in range(0, len(values[row])):
- if v < values[row][column]:
- v = values[row][column]
-
- print(v)
Correct Answer
D. 33
Explanation
The code initializes the variable v with the first element of the first sublist in the values list, which is 3. Then, it iterates over each element in the values list using nested for loops. If an element is greater than v, v is updated with that element. The last element in the values list is 33, which is greater than all the other elements. Therefore, the final value of v is 33, and that is the output.
15.
What will be the output?- d1 = {"john":40, "peter":45}
- d2 = {"john":466, "peter":45}
- d1 > d2
Correct Answer
C. Error
Explanation
The given code will result in an error. This is because the comparison operator ">" cannot be used to compare two dictionaries directly.
16.
What will be the output?- >>>t=(1,2,4,3)
- >>>t[1:-1]
Correct Answer
C. (2, 4)
Explanation
The output will be (2, 4) because when we use the slice notation [1:-1], it selects elements from index 1 to the second-to-last element of the tuple. In this case, the tuple t has elements (1, 2, 4, 3), so the slice [1:-1] will select elements 2 and 4, resulting in the tuple (2, 4).
17.
To open a file c:\scores.txt for writing, we use
Correct Answer
B. Outfile = open(“c:\\scores.txt”, “w”)
Explanation
The correct answer is `outfile = open("c:\\scores.txt", "w")`. This is because when specifying a file path in Python, backslashes (\) are used to escape special characters. Since the backslash itself is a special character, it needs to be escaped by using another backslash. Therefore, in order to specify the file path "c:\scores.txt", we need to use "c:\\scores.txt". The "w" argument in the `open()` function is used to indicate that the file should be opened in write mode.
18.
What is the output of this program?- fo = open("foo.txt", "rw+")
- print "Name of the file: ", fo.name
-
- # Assuming file has following 5 lines
- # This is 1st line
- # This is 2nd line
- # This is 3rd line
- # This is 4th line
- # This is 5th line
-
- for index in range(5):
- line = fo.next()
- print "Line No %d - %s" % (index, line)
-
- # Close opend file
- fo.close()
Correct Answer
C. Displays Output
Explanation
The program opens a file named "foo.txt" in read-write mode and assigns it to the variable "fo". It then prints the name of the file, which is "foo.txt".
Next, it enters a loop that iterates 5 times. In each iteration, it reads a line from the file using the "next()" method of the file object and assigns it to the variable "line". It then prints the line number and the content of the line.
Finally, it closes the file.
Since the program does not contain any syntax errors and the file "foo.txt" exists, it will display the output as expected.
19.
What is the output of this program?- import sys
-
- sys.stdout.write(' Hello\n')
- sys.stdout.write('Python\n')
Correct Answer
D. Hello
Python
Explanation
The program uses the sys.stdout.write() function to print two strings: "Hello" and "Python". The write() function does not automatically add a newline character, so the two strings are printed on the same line. Therefore, the output of the program will be "Hello Python".
20.
What is the use of “w” in file handling?
Correct Answer
B. Write
Explanation
The use of "w" in file handling is to open a file in write mode. This allows the program to write data to the file, overwriting any existing content. By using "w", the program can create a new file if it doesn't exist or truncate the file if it does exist. This mode is commonly used when the program needs to update or modify the content of a file.
21.
What is the output of the below program?- def printMax(a, b):
- if a > b:
- print(a, 'is maximum')
- elseif a == b:
- print(a, 'is equal to', b)
- else:
- print(b, 'is maximum')
- printMax(3, 4)
Correct Answer
C. 4 is maximum
Explanation
The program defines a function called printMax that takes two arguments, a and b. It then uses an if-else statement to compare the values of a and b. If a is greater than b, it prints "a is maximum". If a is equal to b, it prints "a is equal to b". Otherwise, it prints "b is maximum". In this case, the values of a and b are 3 and 4 respectively, so the output will be "4 is maximum".
22.
What is called when a function is defined inside a class?
Correct Answer
D. Method
Explanation
A function defined inside a class is called a method. In object-oriented programming, a method is a behavior or action that an object can perform. It is associated with a specific class and can access the data and other methods within that class. Methods are used to define the behavior of objects and to perform specific tasks or operations.
23.
What is the output of below program?- def f(x, y, z): return x + y + z
-
- f(2, 30, 400)
Correct Answer
A. 432
Explanation
The program defines a function f that takes three arguments x, y, and z and returns their sum. Then, the function is called with the arguments 2, 30, and 400. So, the output of the program is the sum of these three numbers, which is 432.
24.
Which module in Python supports regular expressions?
Correct Answer
A. Re
Explanation
The correct answer is "re". The "re" module in Python supports regular expressions. Regular expressions are used for pattern matching and manipulating strings. The "re" module provides functions and methods for working with regular expressions in Python.
25.
Which of the following creates a pattern object?
Correct Answer
C. Re.compile(str)
Explanation
The correct answer is "re.compile(str)". The "re.compile()" function in Python's "re" module is used to create a pattern object. This pattern object can then be used to perform various operations like searching, matching, and replacing strings using regular expressions.