Preview

02 - Genius level aptitude (Coding)

 1. In Python the precedence order is first NOT then AND and in last OR. Evaluate and predict the output of the following:
a = True
b = False
c = False
 
if not a or b:
    print 1
elif not a or not b and c:
    print 2
elif not a or b or not b and a:
    print 3
else:
    print 4

  3

  2

  4

  1

 2. What is the output in the following program and can you explain why?
count = 1
 
def doThis():
 
    global count
 
    for i in (1, 2, 3): 
        count += 1
 
doThis()
 
print count

  The answer is '3' and it is to do with the looping structure

  The answer is '1' and it is to do with the looping structure

  The answer is '2' and it is to do with the global and local variables

  The answer is '4' and it is to do with the global and local variable declaration

 3. What is the output of the following two for loops (python) program?
for i in range(2):
	print i

for i in range(4,6):
	print i

  3,4,5,6

  0,1,4,5

  1,2,4,5

  0,2,4

 4. The following program returns the output '1,2,3,4'. What is the function 'filter' doing?
values = [1, 2, 3, 4]
numbers = set(values)
 
def checknums(num):
    if num in numbers:
        return True
    else:
        return False
 
for i in  filter(checknums, values):
    print i

  None of the above

  It returns all items from list values which return True when passed to the function “checknums”.

  It returns all items from list values which return False when passed to the values() function.

  It returns all items from list values which return True when passed to the function “checknums”.

 5. Can you predict the output of the following program?
check1 = ['Learn', 'Quiz', 'Practice', 'Contribute']
check2 = check1
check3 = check1[:]
 
check2[0] = 'Code'
check3[1] = 'Mcq'
 
count = 0
for c in (check1, check2, check3):
    if c[0] == 'Code':
        count += 1
    if c[1] == 'Mcq':
        count += 10
 
print count

  12

  10

  11

  9

 6. In this program the output is: L*oo#frph#e|#wkhq1 …the code ciphers the plain text and specifically ….
line = "I'll come by then."
eline = ""
for i in line:
    eline += chr(ord(i)+3)
print(eline) 

  Each line is moved forward and then replaced by the third character in the following line

  None of the above

  Each character is moved to its 3rd character (back instead of forward) by decreasing the ASCII value

  Each character is moved to its 3rd next character by increasing the ASCII value

 7. Python is a low level language and C is an extremely high level language.

  FALSE

  TRUE

 8. The following code snippet is in the language 'C' that you may not have come across. Can you predict the output?
#include"stdio.h"
int main()
{
    int i;
    if(i=(2,1,0))
        printf("Geeksforgeeks ");
    else
        printf("Hello ");
    printf("%d\n",i);
}

  testandtrack 0

  Hello 0

  Hello 2

  Hello 3

 9. In the python interpreter, why doesn't 0.2 + 0.1 = 0.3?

  None of the above

  because .1 cannot be represented exactly in a binary floating point representation (binary is not perfect!)

  This is to preserve long term accuracy and reduce the necessity to provide exact values

  This is one of the peculiarities of python - it rounds off for very small numbers to improve efficiency

 10. In the following program the output is 1,2,3,4. What is the -1 doing?
for i in [1, 2, 3, 4][::-1]:
    print (i)

  Actually, the output is 4,3,2,1 and the [::-1] reverses the list

  The -1 starts at the end of the list, but then prints it out in order

  Actually, the output is 1,3,2,4, as the -1 is a randomiser which produces the list output in random

  The -1 ensures that the list is printed in order as it starts from the beginning (-1 index)