InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
Write for statement to print the series 105,98,91,… .7 |
|
Answer» for i in range(105, 8, -7): print i |
|
| 2. |
Write for statement to print the series 10,20,30, ……., 300 |
|
Answer» for i in range(10, 301, 10): print i |
|
| 3. |
Write the while loop to print the series: 5,10,15,…100 |
|
Answer» i=5 while i <= 100: print i i = i + 5 |
|
| 4. |
Rewrite the following while loop into for loop:i=88while(i>=8): print ii- = 8 |
|
Answer» for i in range(88, 9, -8) print i |
|
| 5. |
Convert the following for loop into while loop, for i in range (1,100):if i % 4 == 2 :print i, “mod”, 4 , “= 2” |
|
Answer» i=1 while i < 100: if i % 4 == 2: print i, “mod”, 4 , “= 2” i = i +1 |
|
| 6. |
Convert the following while loop into for loop:i = 0while i < 100:if i % 2 == 0:print i, “is even”else:print i, “is odd”i = i + 1 |
|
Answer» for i in range(100): if i % 2 == 0: print i, “is even” else : print i, “is odd” |
|
| 7. |
What are the two ways of output using print()? |
|
Answer» Ordinarily, each print statement produces one line of output. You can end the print statement with a trailing ’ to combine the results of multiple print statements into a single line. |
|
| 8. |
Why does the expression 2 + 3*4 result in the value 14 and not the value 24? |
|
Answer» Operator precedence rules* make the expression to be interpreted as 2 + (3*4) hence the result is 14. |
|
| 9. |
What is the difference between input() and raw_input()? |
|
Answer» raw_input() takes the input as a string whereas input() basically looks at what the user enters, and automatically determines the correct type. We use the input function when you are expecting an integer from the end-user, and raw_input when you are expecting a string. |
|
| 10. |
Convert the following while loop into for loopchar = ""print “Press Tab Enter to stop ...”iteration = 0while not char == “\t” and not iteration > 99:print “Continue?”char = raw_input()iteration+ = 1 |
|
Answer» char = "" print “Press Tab Enter to stop ...” for iteration in range(99): if not char == ‘\t’: print “Continue?” char = raw_input() |
|
| 11. |
Convert the following for loop into while loop.for i in range(10):for j in range(i):print '$',print" |
|
Answer» i=0 while i < 10: j=0 while j < i: print '$’ print" |
|
| 12. |
Rewrite the following while loop into for loop:i = 10while i<250:print ii = i+50 |
|
Answer» for i in range(10, 250, 50): print i |
|
| 13. |
Rewrite the following for loop into while loop:for a in range(25,500,25):print a |
|
Answer» a=25 while a < 500: print a a = a + 25 |
|
| 14. |
Rewrite the following for loop into while loop:for a in range(90, 9, -9):print a |
|
Answer» a = 90 while a > 9: print a a = a-9 |
|
| 15. |
Which string method is used to implement the following:1. To count the number of characters in the string.2. To change the first character of the string in capital letter.3. To check whether given character is letter or a number.4. To change lowercase to uppercase letter.5. Change one character into another character. |
|
Answer» 1. len(str) 2. str.capitalize() 3. ch.isalnum() 4. str.upper() 5. str.replace(old,new) |
|
| 16. |
Name the function / method required for1. Finding second occurrence of m in madam.2. get the position of an item in the list. |
|
Answer» 1. find 2. index |
|
| 17. |
Out of the following, find the identifiers, which cannot be used for naming Variable or Functions in a Python program:_Cost, Price*Qty, float, switch, Address one, Delete, Number12, do |
|
Answer» Price *Qty, float, Address one, do |
|
| 18. |
Out of the following find those identifiers, which can not be used for naming Variable or Functions in a Python Program : Days * Rent, For, A_price, Grand Total, do, 2Clients, Participant, My city |
|
Answer» Illegal variables or functions name is as below: Days * Rent, do, 2Clients, For and Grant Total Because of being either keyword or including space or operator or starting with an integer. |
|
| 19. |
Name the modules to which the following functions belong:1. Uniform ()2. fabs () |
|
Answer» 1. random () 2. math () |
|
| 20. |
Differentiate between the round() and floor() functions with the help of suitable example. |
|
Answer» The function round() is used to convert a fractional number into whole as the nearest next whereas the function floor() is used convert to the nearest lower whole number, e.g., round (5.8) = 6, round (4.1) = 5 and floor (6.9) = 6, floor (5.01) = 5 |
|
| 21. |
Out of the following, find those identifiers, which cannot be used for naming Variables or functions in a Python program: Total * Tax, While, Class, Switch, 3rd Row, finally, Column 31, Total. |
|
Answer» Total * Tax, class, 3rd Row, finally |
|
| 22. |
Name the Python Library modules which need to be imported to invoke the following functions :1. sqrt()2. dump() |
|
Answer» 1. math 2. pickle |
|
| 23. |
Name the Python Library modules which need to be imported to invoke the following functions :1. load ()2. pow () |
|
Answer» 1. pickle 2. math |
|
| 24. |
How many times will Python execute the code inside the following while loop?i = 1while i < 10000 and i > 0 and 1:print “ Hello ...”i = 2 * i |
|
Answer» 14 times will Python execute the code inside the following while loop. Hello ...Hello ... Hello ... Hello ... Hello ... Hello ... Hello ... Hello ... Hello ... Hello ... Hello ... Hello ... Hello ... Hello ... Process finished. |
|
| 25. |
Explain the two strategies employed by Python for memory allocation. |
|
Answer» Python uses two strategies for memory allocation- (i) Reference counting (ii) Automatic garbage collection Reference Counting: works by counting the number of times an object is referenced by others in the system. When an object’s reference count reaches zero, Python collects it automatically. Automatic Garbage Collection: Python schedules garbage collection based upon a threshold of object allocations and object deallocations. When the number of allocations minus the number of deallocations is greater than the threshold number, the garbage collector is run and the unused blocks of memory are reclaimed. |
|
| 26. |
Carefully observe the following python code and answer the question that follows:x=5 def func2(): x=3 global x x=x+1 print x print x On execution the above code produces the following output. 6 3 Explain the output with respect to the scope of the variables. |
|
Answer» Names declared with global keyword have to be referred at the file level. This is because of the global scope. If no global statement is being used the variable with the local scope is accessed. Hence, in the above code, the statement succeeding the statement global x informs Python to increment the global variable x Hence, the output is 6 i.e. 5 + 1 which is also the value for global x. When x is reassigned with the value 3 the local x hides the global x and hence 3 printed. (2 marks for explaining the output) (Only 1 mark for explaining global and local namespace.) |
|
| 27. |
What is the difference between a tuple and a list? |
|
Answer» A tuple is immutable whereas a list is mutable. A tuple cannot be changed whereas a list can be changed internally. A tuple uses parentheses (()) whereas a list uses square brackets ([]). tuple initialization: a = (2, 4, 5) list initialization: a = [2, 4, 5] |
|
| 28. |
What are the differences between arrays and lists? |
|
Answer» An array holds the fixed number of values. The list is of variable-length – elements can be dynamically added or removed An array holds values of a single type. List in Python can hold values of a mixed data type. |
|
| 29. |
Explain find() function? |
|
Answer» find (sub[,start[,end]]) This function is used to search the first occurrence of the substring in the given string. It returns the index at which the substring starts. It returns -1 if the substring doesn’t occur in the string. (eg) str = “computer” - str.findf("om”) → 1 |
|
| 30. |
What is the use of negative indices in slicing? |
|
Answer» Python counts from the end (right) if negative indices are given. (eg) S = “Hello” print S[:-3] >> He print S[-3:] >> llo |
|
| 31. |
What does “slice” do? |
|
Answer» The slice[n:m] operator extracts subparts from a string. It doesn’t include the character at index m. (eg) S = “Hello World” print s[0:4] → Hell |
|
| 32. |
What does “not in” do? |
|
Answer» “not in” is a membership operator. It evaluates to true if it does not finds a variable/string in the specified sequence. Otherwise it evaluates to false, (eg) S = “Hello World” if “Hell” not in S: print “False” will print False. |
|
| 33. |
What does “in” do? |
|
Answer» “in” is a membership operator. It evaluates to true if it finds a variable/string in the specified sequence : Otherwise i+evaluates to false. (eg) S = “Hello World" if “Hell” in S: print “True” will print True. |
|
| 34. |
How many times will Python execute the code inside the following while loop? You should answer the question without using the interpreter! Justify your answers.i = 0while i < 0 and i > 2 :print “Hello ...”i = i+1 |
|
Answer» 0 times will Python execute the code inside the following while loop. |
|
| 35. |
What are the advantages of keyword arguments? |
|
Answer» It is easier to use since we need not remember the order of the arguments. We can specify the values for only those parameters which we want, and others have default values. |
|
| 36. |
What are keyword arguments? |
|
Answer» If there is a function with many parameters and we want to specify only some of them in function call, then value for such parameters can be provided by using their names instead of the positions. These are called keyword argument. (eg) def simpleinterest(p, n=2, r=0.6) ' def simpleinterest(p, r=0.2, n=3) |
|
| 37. |
What are default arguments? |
|
Answer» Python allowes function arguments to have default values; if the function is called without the argument, the argument gets its default value. |
|
| 38. |
What is the difference between parameters and arguments? |
|||||||||
Answer»
|
||||||||||
| 39. |
How can we import a module in Python? |
|
Answer» 1. using import Syntax: import[,...] Example: import math, cmath 2. using from Syntax: fromimport[, ,.. ,] Example: from fib. import fib, fib2. |
|
| 40. |
What is the difference between ‘/’ and ‘//’ ? |
|
Answer» // is Integer or Floor division whereas / is normal division (eg) 7.0 // 2 → 3.0 7.0/2 → 3.5 |
|
| 41. |
What are the logical operators of Python? |
|
Answer» or, and, not are the logical operators of Python. |
|
| 42. |
State whether the statement is True or False? No matter the underlying data type if values are equal returns true,char ch1, ch2;if (ch1==ch2)print “Equal” |
|
Answer» True two values of the same data types can be equal. |
|
| 43. |
How many times is the following loop executed?i = 100while (i<=200):print ii + =20 |
|
Answer» 6 times is the while loop executed. |
|
| 44. |
How many times is the following loop executed? for a in range(100,10,-10):print a |
|
Answer» 9 times is the for loop executed. |
|