Misc Stuff and Q&A
The first argument in the insert() method is the value of the element to be inserted
The list.insert(i, element) method adds an element element to an existing list at position i. So the first argument is location or index.
Tup = (4)
Answer of this question is Tup=(4) as if you define it this way it becomes type int. Rest of the options are example of tuple.
An integer number preceded by an 0x (Zero-x) will be treated as:
Hexadecimal
It's a hexadecimal number. The numbers starting with 0x are hexadecimal (base 16). We use the hex() function to convert values to hexadecimal format for display purposes.
An integer number preceded by an 0o (Zero-o) will be treated as:
Octal Number having 0o or 0O as prefix represents octal number which can have 0 to 7 as one of the digits in it.
The binary number has the following base:
2 Explanation Binary number system, in mathematics, positional numeral system employing 2 as the base and so requiring only two different symbols for its digits, 0 and 1
Hexadecimal has the following base:
16 Explanation Decimal uses ten digits, binary uses two digits, and hexadecimal uses sixteen digits. Since we only have the ten digits from the decimal system to use, we substitute letters for everything above the number nine. Therefore, the digits in hexadecimal are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. They represent zero through nine, then A is worth ten, B is worth eleven, C is worth twelve, D is worth thirteen, E is fourteen, and F wraps up with a value of fifteen. So, there are sixteen digits in hexadecimal.
Octal has the following base:
8
Octal stands for eight. We prefix octal numbers with a zero followed by a lowercase o, like ‘0o’. The eight digits of octal are 0, 1, 2, 3, 4, 5, 6, 7.
What will be the output of the following code snippet?
Explanation For such questions always consider True is equal to 1 and False is equal to 0
Be it comparison or addition this terminology will help to solve.
In this question
print(True > False) is like print(1>0) hence it will give True
print(True < False) is like print(1<0) hence it will give False
Q & A
THESE ARE NOT REAL EXAM QUESTIONS, THESE ARE USED TO EXPLAIN CONCEPTS UNDER THE FORM OF A QUESTION TO GET A FEEL FOR THE EXAM FORMAT
If the following piece of code resides inside the abc.py file, what is the output when the file is run?
Explanation The 'module' property holds the name of the module, the function/class was defined in. When a file is run on its own in python, the module is set to 'main' but if the class had been referenced inside another file and then the code had been run, the output would have been 'abc'.
2. Which of the following lines of code can be used to get the python version being used?
Explanation 'platform.python_implementation' returns a string identifying the Python IMPLEMEMTATION not the version. Possible return values are: ‘CPython’, ‘IronPython’, ‘Jython’, ‘PyPy’. Options 3-5 are just plain wrong and are placed to confuse
3. What is the output of the following snippet of code:
Explanation The 'find' function finds the exact match of substring in the main string, since the main string contains 'vision' but NOT 'Vision', it will return -1. This is important to remember about find that when it doesn't find a match, it returns -1. This -1 should not be confused with the index -1.
4. sys.path
Explanation The correct answer choices are facts about 'sys.path' variable. It is good to memorize them. Also pay close attention to the wrong choices as they can be used to confuse you on the real test
5. What is true about the variable 'var' in the following class? (Select 2 Answers)
Explanation Any variable defined in a class WITHOUT the self keyword, is a class variable and will be shared throughout all instances (objects) of the class.
7. What is true about string comparison in Python? (Select 3 Answers)
These are facts, just memories them
8. The file abc.txt contains the following 3 lines of text:
Explanation When a file is opened in write mode ('w'), all previous contents are lost.
9. The file abc.txt contains the following 3 lines of text:
What is the output of the following snippet of code in abc.py file?
Explanation The file object will be a generator that reads in file items line by line. So when we use it in list comprehension we get a list containing lines in the file. Since there are 3 lines, the first 2 lines will also have the '\n' character which specifies new line (although '\n' is not visible in the text file it represents next line).
10. What is the output of the following snippet of code?
TypeError occurs
Explanation AttributeError can be defined as an error that is raised when an attribute reference or assignment fails. Here none of that is happening.
One handy way of deciding between value error and type error is by understanding that value error occurs when there is a problem with the content of the object you tried to assign the value to for example the statement int("a")
produces a ValueError because the value being provided to int is of the wrong type
(Don't associate with TypeError).Now the 'type' of the 'func' function in the question is such that it REQUIRES a value
. It is the TYPE (read nature) of this function that it must need a value to work and if we call it simply like func() it violates its nature. Hence a TypeError occurs.
11. What will be the result of running the following snippet of code?
Explanation The 'dir' function will return all the properties and methods, even built-in properties for any entity passed to it. In this case it is the 'math' module
12. What is the output of the following snippet of code?
The class on the left is given priority: (B)
Explanation You need to remember this rule that whenever there are more than 2 classes from which a class is inheriting from and both classes contain a function/variable with same name then the class name on the left is given priority and its functions/variables are called so in this case 'B' is first so its function is called.
13. What is the output of the following snippet of code in abc.py file?
Explanation The 'read()' method returns the specified number of bytes from the file. Default is -1 which means the whole file. Since we are providing it the number 4, it will read 4 bytes of data from the file. As each character takes up 1 byte of space, 4 characters will be read from the file and they will be as they are present in the file.
14. Types
Explanation The 'isdigit' function checks if all the characters in the text are digits. Since '.' the decimal point is not a digit hence
is incorrect. Strangely enough, the 'isdecimal' function also works the same way and hence
is also incorrect. The 'isalpha' function checks if all the characters in the text are letters, since '2.37' contains digits, hence
is wrong.
15. Correct Given the following snippet of code:
Explanation 'init' is the attribute of both classes and objects. Note: Even though init is the attribute of objects, it is NOT present in the dict property of objects but IS present in the dict property of class.
is incorrect because A is not an attribute of B but is the super class of B.
is incorrect because the super() function is used to give access to methods and properties of a parent class and is not an attribute of child class.
16. What is the output of the following snippet of code?
Explanation Function A is returning function B. Function B takes in 1 argument and it is local to it even though its name is same as the variable b inside function A. This means that when we call A(2), it has no effect on function B. So when we call x(2), it is as if we are simply running function B, i.e. 2+3 so answer is 5.
17. Strings Explanation Strings in python are compared character by character from left to right and whenever a lower codepoint is encountered, the string with the higher code point value is declared greater.
is correct because although all code points are equal from left to right but since the string on the right is longer hence it is greater.
is incorrect because when comparing from left to right, character by character, the first character of left string is 'A' while first character of right string is 'a' and the lower case characters have higher code point values hence right string should be greater. Same reason why
is incorrect as well.
18. What is the output of the following snippet of code?
Explanation You have to remember that the 'bases' property holds the information about the immediate super class of the class. It is not aware of any other hierarchy. Also, it contains the super classes in a tuple and NOT a list.
19. What is the output of the following snippet of code?
Explanation The Math.ceil() function always rounds a number up to the next largest integer. For -2.3 the the next highest integer is -2 (NOT -3, be careful you are dealing with a negative number). For questions related to ceil and floor it is always better to draw a number line and place the numbers on it. The factorial function gives the product of all positive integers less than or equal to the input number. So factorial(3) = 321 = 6. So final answer = 6-2 = 4
20. What is the output of the following snippet of code:
Explanation The 'find' function returns the index of the first match within the string, since the first 'e' is encountered at index 1, the output is 1.
21. What is the output of the following snippet of code?
Explanation The 'args' property is a tuple containing the values passed when the exception is raised.
22. What is the output of the following snippet of code?
True
Explanation The 'isinstance()' function checks if the object (first argument) is an instance or subclass of the class (second argument). Although object c is derived from class C but class C inherits from B which in turn inherits from A, hence c will be considered an instance of A. Consider it like your family tree. You will be considered related to your Great Grand Parents even though you are not directly their child.
23. The file abc.txt contains the following 3 lines of text:
What is the output of the following snippet of code in abc.py file?
Explanation The 'readline()' method returns ONE line from the file. You can also specify how many bytes from the line to return, by using the size parameter, which in this case is 3. So it will read the first 3 bytes from the first line hence answer is 'abc'.
24. The fact that the following snippet of code throws an Attribute error depicts which phenomena of OOP?
Explanation
In Python, Polymorphism lets us define methods in the child class that have the same name as the methods in the parent class.
In inheritance, the child class inherits the methods from the parent class. In this question, there is no child class so both these options are out.
Abstraction is the process of hiding the real implementation of a function from the user and emphasizing only on how to use the function. This is also not happening in this question.
When a variable name is defined starting with __ (2 underscores), it is a private variable. So it cannot be accessed outside the class. This concept of public and private variables is related to encapsulation. Encapsulation is restricting access to methods and variables based on their requirement.
Data hiding is just a term I made up to confuse although it is the essence of encapsulation but it is not the correct option.
25. What is true about the following snippet of code?
Explanation The choice() method returns a randomly selected element from the specified sequence. So output cannot be a list. Since the output is random we cannot say for sure whether it will be 3. But we can say that output CAN be 2 or 3 or 1.
26. What is the output of the following code snippet?
Explanation A TypeError occurs because it is the nature of strings to be immutable and the above code is violating this.
27. Given the following classes
Explanation To solve this question you need to be aware of the rule that when declaring a sub class which inherits from 2 (or more) classes which themselves are related through inheritance, the super class (class higher up in the inheritance hierarchy) needs to be passed after the sub class (class lower in the inheritance hierarchy). Using this rule lets analyze the answer choices:
In
when declaring class 'a3' we are passing in class A BEFORE class B even though class A is the super class while B is the sub class. Therefore this option is incorrect (remember, sub classes come before super classes!).
These options are correct:
because we are declaring class 'a2' and 'a1' with classes D and C, there are not directly related in the inheritance chain so their order does not matter. Consider the inheritance chain has a human analogy: Grand parents -> Parents -> Children
. It is a linear relation. Using this analogy, D will be the uncle/aunt while C will be the nephew/niece. The above rule does not apply to this 'non-linear' relation.
is incorrect because B comes before C even though B is the super class.
is incorrect because A is the super class and should come after D.
28. What is the output of the following snippet of code?
Explanation The 'isinstance' function checks whether the object specified in first argument is an instance of the class or tuple of classes specified in the second argument. For the case when a tuple of classes is provided, the check is an 'OR' relation NOT an 'AND' relation
. So in the above scenario, the 'isinstance' function call can be translated like this: isinstance(c, B) or instance(c, A)
So even though object 'c' is not an instance of class A, the output will be True because the left side of the OR statement is TRUE
.
29. What is the output of the following code snippet?
Explanation Strings are immutable so deleting any characters is not possible.
29. What is the output of the following snippet of code?
Explanation Private class variables can be accessed inside and outside the class by using the way shown in question to access them.
print(A.__Var)
and print(A._A__Var)
are equivalent statements.
30. What is true about the output of the following snippet of code?
Explanation Even though it seems that the class is empty and therefore its '__dict__'
property should return an empty dictionary BUT this is not the case. It contains metadata about the class like the module info etc.
31. What is true about the assert keyword? (Select 2 Answers)
Explanation The correct answer choices are facts about 'assert' keyword. It is good to memorize them. Also pay close attention to the wrong choices as they can be used to confuse you on the real test.
32. What is the output of the following snippet of code?
Explanation 'rfind' is similar to 'find' except that it starts searching for the substring from the right of the target string. BUT even though it is searching from the right, the index it returns is still positive. Just like 'find' it also returns the index of the FIRST match and NOT all matches.
33. Which is module, package and entity?
Import Entity "a" from module "y" from package "x"
Explanation The package name comes first followed by the module name, finally we import the entity required.
34. What is the output of the following snippet of code?
Explanation This is a slightly complicated list comprehension question. Focus on the right side, we are iterating through the list 'a' using the variable 'i'. Next if you focus on left side, we are creating a new list, with numbers starting from 0 and ending on the value i-1 (The 'range()' function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops BEFORE a specified number, which in this case is 'i'. Since we are stopping BEFORE, that is why I said i-1).
Now first item in list 'a' is 1, so left side outputs a list starting from 0 and ending on 1-1=0, therefore the list will only have 0 as the item, it will be [0].
Next item in list 'a' is 2, so left side outputs a list starting from 0 and ending on 2-1=1, therefore the list will be [0,1].
Last item in list 'a' is 3, so left side outputs a list starting from 0 and ending on 3-1=2, therefore the list will be [0,1,2].
Since a list comprehension statement outputs a list of items, here it will output a list of lists, the variable 'b' becomes:
The indexing is such that first index value refers to the index of the outer list at which we want to access the item, for example if I want to access the 3rd item (index 2), I will write, b[2]. The next index value refers to the index value corresponding to the index of the inner list that was selected through previous index. If we selected the 3rd list using b[2], then b[2][1] refers to the '1' inside [0,1,2] because '1' is on index 1.
Using this information we can know that b[1] would refer to list [0, 1], and b[1][1] would refer to 1 which is on the index 1 in list [0,1]. So output is 1.
UTF-8 is encoding & Unicode is a standard
35. What is the output of the following snippet of code?
Explanation 'rfind' is similar to 'find' except that it starts searching for the substring from the right of the target string. BUT even though it is searching from the right, the index it returns is still positive. Just like 'find' it also returns the index of the FIRST match and NOT all matches. The first argument of rfind is the substring to find, the second argument and third argument define the substring from the main string in which we have to find the string provided in first argument. So in this case a.rfind('r', 0, 9) We are looking for the character 'r' from the right in the substring 'refrigera' because 0-9 indexes define it as such.
Last updated