Question 1
Multiple choice
Which of the following expressions evaluate to True? (Choose two.)
-
A
-
B
-
C
float(`3.14') == str(`3.' + `14')
-
D
121 + 1 ==int(`1' + 2 * `2')
Reveal answer details
Close answer details
Correct answersB, D
Explanation'xYz'.lower() becomes 'xyz'. String comparison is lexicographic by character code, and lowercase 'x' sorts after uppercase 'X', so 'xyz' > 'XY' is true. Also, '1' + 2 * '2' forms '122'; converting it with int gives 122, equal to 121 + 1.
What is the expected behavior of the following code? 
-
A
the code is erroneous and it will not execute
-
B
-
C
-
D
Reveal answer details
Close answer details
Correct answerB
Explanation1 / 3 is a floating-point exponent, and raising 8 to that exponent produces its cube root, 2.0, for x. The conditional expression tests whether this value is less than 2.3, which is true. It therefore chooses the value before if, assigning the floating-point literal 2. to y, printed as 2.0.
Question 3
Multiple choice
Select the valid fun () invocations: (Select two answers) 
-
A
-
B
-
C
-
D
Reveal answer details
Close answer details
Correct answersB, D
ExplanationThe parameter a is required, while b has the default value 0. fun(a=0) supplies the required argument by keyword and lets b keep its default. fun(1) supplies a positionally and also uses the default for b. Supplying only b leaves a missing, and a positional argument cannot follow a keyword argument.
The__bases__property contains:
-
A
base class locations (addr)
-
B
base class objects (class)
-
C
-
D
Reveal answer details
Close answer details
Correct answerB
ExplanationEach entry in a class's __bases__ tuple is an actual class object representing one direct superclass. This allows code to inspect the base itself, including its name, module, and inheritance information. The tuple does not merely hold textual base names, numeric identities, or locations; those details could be derived separately from the class objects if needed.
Question 5
Multiple choice
What is true about Python class constructors? (Choose two.)
-
A
there can be only one constructor in a Python class
-
B
the constructor cannot be invoked directly under any circumstances
-
C
the constructor cannot return a result other than None
-
D
the constructor's first parameter must always be named self
Reveal answer details
Close answer details
Correct answersA, C
ExplanationA class has one active initialization method named __init__; defining that name again replaces the previous definition rather than creating an overloaded constructor. Python also requires __init__ to return None, whether explicitly or by reaching the end of the method. Returning any other result causes object creation to fail with a TypeError.
The following class hierarchy is given. What is the expected out of the code? 
-
A
-
B
-
C
-
D
Reveal answer details
Close answer details
Correct answerD
ExplanationB().do() invokes the inherited b method, which calls self.a(); dynamic dispatch selects B.a and prints B. C().do() likewise invokes inherited b, but self is a C instance, so C.a prints C. Since both print calls suppress line endings and add no separating text, the combined output is BC.
Question 7
Multiple choice
Which of the following expressions evaluate to True? (Choose two.)
-
A
-
B
-
C
-
D
Reveal answer details
Close answer details
Correct answersB, D
ExplanationThe in operator checks whether a substring exists inside a string and is case-sensitive. A string always contains itself as a substring, so the comparison with the identical text returns True. Converting a lowercase character to uppercase changes it to T, which matches the first character of Thames. Substrings must appear in the exact order and with the same letter case.
Question 8
Multiple choice
What is true about Python class constructors? (Choose two.)
-
A
there can be more than one constructor in a Python class
-
B
the constructor must return a value other than None
-
C
the constructor is a method named__init__
-
D
the constructor must have at least one parameter
Reveal answer details
Close answer details
Correct answersC, D
ExplanationThe initialization method used as the class constructor is named __init__. It is called with the newly created instance as its first positional argument, conventionally named self, so its definition needs at least that parameter even when no other initialization data is required. The method initializes the object and must complete with None rather than return another value.
Question 9
Multiple choice
Which of the following snippets will execute without raising any unhandled exceptions? (Choose two.) 
-
A
-
B
-
C
-
D
Reveal answer details
Close answer details
Correct answersB, C
ExplanationIn Option B, division by zero raises an exception inside the try suite, and the bare except catches it; the handler's division is valid, and the else suite is skipped. In Option C, sqrt(-1) raises a value-related exception, which is likewise caught, and the handler successfully evaluates sqrt(0). Neither leaves an exception unhandled.
Question 10
Multiple choice
What can you deduce from the line below? (Select two answers) 
-
A
import a.b.c should be placed before that line
-
B
f () is located in subpackage c of subpackage b of package a
-
C
-
D
the function being invoked is called a.b.c.f ()
Reveal answer details
Close answer details
Correct answersA, D
ExplanationThe expression resolves the qualified name a.b.c.f and then calls the resulting function. The package hierarchy must already be available under those names; placing import a.b.c before the line establishes that hierarchy in the normal way. The function being invoked through the expression is therefore called a.b.c.f().
Question 11
Multiple choice
What is true about Python class constructors? (Select two answers)
-
A
the constructor's first parameter identifies an object currently being created
-
B
the constructor cannot use the default values of the parameters
-
C
the constructor can be invoked directly under strictly defined circumstances
-
D
super-class constructor is invoked implicitly during constructor execution
Reveal answer details
Close answer details
Correct answersA, C
ExplanationThe first parameter of __init__, conventionally called self, receives the object currently being initialized, allowing assignments to that object's attributes. The method can also be invoked directly when a suitable instance is supplied, such as explicitly calling a superclass initializer for that instance. Parameter defaults are permitted, and a superclass initializer is not called automatically by an overriding initializer.
Question 12
Single choice
What is the expected output of the following code? 
-
A
-
B
-
C
-
D
Reveal answer details
Close answer details
Correct answerB
Explanationmap applies the lambda to each element of ('a', 'b', 'c'). For every character, ord obtains its code, 1 is added, and chr converts the next code back to a character. The tuple m is therefore ('b', 'c', 'd'). Index -2 selects its second-to-last element, so c is printed.
Question 13
Multiple choice
Which line can be used instead of the comment to cause the snippet to produce the following expected output? (Select two answers) Expected output: 1 2 3 Code: 
-
A
-
B
-
C
-
D
Reveal answer details
Close answer details
Correct answersA, C
ExplanationInitially a=2, b=3, and c=1. With c,b,a=b,a,c, the right side is evaluated first as 3,2,1, leaving a=1, b=2, c=3. With a,b,c=c,a,b, the right side is 1,2,3 and creates the same final values. Either simultaneous assignment therefore makes print(a,b,c) produce 1 2 3.
Question 14
Single choice
What is the expected output of the following code? 
-
A
-
B
The code will cause a runtime exception
-
C
-
D
Reveal answer details
Close answer details
Correct answerD
ExplanationThe comprehension first creates [0, 1, 2, 3, 4]. filter applies the lambda to each value and retains those for which x % 2 == 0, namely 0, 2, and 4. Converting the filter result to a list therefore creates three elements, and len(lst) prints 3.
Question 15
Multiple choice
Which of the following statements are true? (Choose two.)
-
A
if invoking open () fails, an exception is raised
-
B
open () requires a second argument
-
C
open () is a function which returns an object that represents a physical file
-
D
instd, outstd. errstd are the names of pre-opened streams
Reveal answer details
Close answer details
Correct answersA, C
Explanationopen() attempts to establish access to a file, and a failure such as an unavailable path or disallowed mode raises an exception. On success, it returns a file object representing the opened file and providing operations such as read, write, and close. Its mode argument is optional because it defaults to reading; the standard stream names are stdin, stdout, and stderr.
Question 16
Multiple choice
Assuming that the code below has been placed inside a file named code.py and executed successfully, which of the following expressions evaluate to True? (Choose two.) 
-
A
ClassA.__module__ == `__main__'
-
B
-
C
-
D
len(ClassB.__bases__) == 2
Reveal answer details
Close answer details
Correct answersB, C
ExplanationThe special variable __name__ is set to "__main__" when the file is executed directly. The object is created from ClassA, and since ClassA does not define a custom __str__ method, str(Object) does not return "Object"; therefore, option C would not be true. However, the shown ClassB defines __str__, so if an instance of ClassB were created, str(Object) would return "Object". The __bases__ attribute of ClassB contains only ClassA, so its length is 1, not 2. ClassA.__module__ is "__main__" only when executed directly, but it is not guaranteed from the filename alone.
Question 17
Single choice
Python strings can be "glued" together using the operator:
-
A
-
B
-
C
-
D
Reveal answer details
Close answer details
Correct answerD
ExplanationWhen both operands are strings, the + operator performs concatenation: it creates a new string containing the left operand followed immediately by the right operand. This is the operation described as gluing strings together. The dot is used for attribute access, while & and underscore do not concatenate Python strings.
Question 18
Single choice
A property that stores information about a given class's super-classes is named:
-
A
-
B
-
C
-
D
Reveal answer details
Close answer details
Correct answerD
ExplanationA class's __bases__ attribute records its direct superclasses. Its value is a tuple containing the actual base class objects in the order used in the class declaration. Because it stores class objects rather than names, identifiers, or memory locations, it can be used directly for inheritance introspection, including examining each base class's own attributes.
Question 19
Single choice
What is the expected behavior of the following code? 
-
A
-
B
-
C
the code is erroneous and it will not execute
-
D
Reveal answer details
Close answer details
Correct answerC
ExplanationThe outer handler is written as except ArithmeticError without the required trailing colon. That is a syntax error, detected while Python parses the code and before foo(0) can run. Consequently, none of the exception-handling arithmetic changes to m occur and the final print statement is never reached; the code will not execute.
Question 20
Multiple choice
If you need a function that does nothing, what would you use instead of XXX? (Select two answers) 
-
A
-
B
-
C
-
D
Reveal answer details
Close answer details
Correct answersA, B
Explanationpass is an explicit no-operation statement, so it supplies the required function body without performing work. A bare return is also valid: it immediately ends the call and implicitly returns None. Either statement can replace XXX to create a callable function whose body performs no substantive action.
Question 21
Multiple choice
Which of the following expressions evaluate to True? (Choose two.)
-
A
ord("Z") - ord("z") == ord("0")
-
B
-
C
-
D
Reveal answer details
Close answer details
Correct answersB, C
Explanationord('A') gives the character's numeric code, adding 1 reaches the code for 'B', and chr converts it back, so that comparison is true. In the other expression, the escape sequence represents one literal apostrophe inside the string. It is one character rather than two source symbols, so len returns 1.
Question 22
Single choice
What is the expected behavior of the following snippet?  It will:
-
A
cause a runtime exception
-
B
-
C
-
D
Reveal answer details
Close answer details
Correct answerA
ExplanationThe call passes integer 0 to parameter l and the list [1] to parameter I. The return expression consequently attempts to evaluate 0[[1]], treating the integer as an indexable object and a list as its index. Integers do not support this subscription operation, so evaluation causes a runtime exception instead of printing a value.
Question 23
Single choice
What is the expected behavior of the following code? 
-
A
-
B
the code is erroneous and it will not execute
-
C
-
D
Reveal answer details
Close answer details
Correct answerD
Explanationint('2A') cannot interpret the entire string as a base-10 integer, so it raises ValueError. Exception handlers are checked from top to bottom, and the first handler matches that exact exception type. It assigns 2 to n; the later ArithmeticError and bare handlers are skipped, after which 2 is printed.
|