Questions

2 Marks Quetions

🎯

Test yourself on this topic

18 questions · timed · auto-graded

Question 12 Marks
Write a program in Python to Take two strings as input. Print out either the longer string, or that the two strings are of equal length. For example:
Input:dhruv, aseem
Output: The two strings are of equal length
Answer
self
View full question & answer
Question 22 Marks
Amit does not understand the need for lists. Explain to Amit how it will help in many kind of programs.
Answer
Lists allow to congregate values and variables. For two numbers, variables a and b (or any two names) is sufficient. However, if there are many numbers, so many different variables will create clutter in the program. So, lists should be used.
View full question & answer
Question 32 Marks
Arijit cannot think of a way to print the contents of a list in one go. He uses the for loop for it. How can he print a list data in one go?
Answer
If the list has the variable name, abc, then
print(abc)
Will print the whole list.
View full question & answer
Question 42 Marks
Ajit is a programmer who is learning about operators. He is confused about the usage of + operator as it can be used to combine strings too. Explain to him clearly the role of the + operator in a Python program?
Answer
Related to strings, + operator allows concatenation of strings. Thus,
"123" + "a number"
Gives the output$\quad$123a number
However, when used with numbers, it has the meaning of addition and adds numbers. Thus,
12 + 2 + 0
Gives the output$\quad$123
View full question & answer
Question 52 Marks
You are looking at a very complex Python code that runs into many lines. To debug it, you must find out what the data type of two of the variables in the program are. What function in Python allows you to find out the data type of the variables?
Answer
The function type() can give the output as the data type of the input. Thus, if a is of type string, then:
type (a)
Gives$\quad$<class 'str'>$\quad$as output
View full question & answer
Question 62 Marks
In the following problem, how will you use if ..elif..else effectively?
Marks > 95$\quad$- -$\quad$grade A
Marks > 80$\quad$- -$\quad$grade B
Marks > 72$\quad$- -$\quad$grade C
Marka <=72$\quad$- -$\quad$ Fail
Answer
marks = int (input("Marks ?"))
if marks > 95:
print("Grade A")
elif marks > 80:
print("Grade B"}
elif marks > 72:
print("Grade C")
elif marks <= 72:
print("Fail.")
View full question & answer
Question 72 Marks
How is it signified in Python which statements lie within a loop and which lie outside of the loop? Use example(s) in the answer.
Answer
for x in range(9): #Line 1
print (x) #Line 2
print("This is inside loop") #Line 3
print("This is outside loop") #Line 4
In the above code, Line 2 and Line 3 are within the for loop, as they are both similarly indented. Line 4 is not indented, so is printed only once i.e. after the loop is exited.
View full question & answer
Question 82 Marks
Write a program to take three numbers as input and print the largest out of them. All the numbers are distinct.
Answer
ni = int (input ("First number:"))
n2 = int (input ("Second number:"))
n3 = int (input ("Third number"))
if nl > n2 and n1 > n3:
print (nl," is the largest number")
elif n2 > n3 and n2 > n1:
print (n2, "is the largest number")
elif n3 > nl and n3 > n2:
print (n3, "is the largest number")
View full question & answer
Question 92 Marks
Amit is not able to understand the need for whitespace indentation. Explain to him how Python uses whitespace.
Answer
self
View full question & answer
Question 102 Marks
Rohan is unable to understand how associativity is important in expression evaluation. Take some examples and show that left to right and right to left associativity are in general different. (Hint: Use the exponentiation operator as example or integer divide or divide)
Answer
self
View full question & answer
Question 112 Marks
Ankur has been given two points on the coordinate axes. He must find out the Euclidean distance between these points. Using the formula for this for points (x1,y1) and (x2,y2) as:
d = sqrt((x1-x2)2+(y1-y2)2),
Write a Python program that takes these 2 points as input and displays the distance between these points.
Answer
self
View full question & answer
Question 122 Marks
Anuj has been given the problem of printing all odd numbers from 100 to 200. Using a while loop, how can this be achieved?
Answer
self
View full question & answer
Question 152 Marks
(Write a program in Python to Take three strings as input and print out only those strings that are even in length. For example:
Sayan$\quad$(Input)
Deepak$\quad$(Input)
Deepak
Satish$\quad$(Input)
Satish
Answer
self
View full question & answer
Question 162 Marks
Write a program in Python to Verify the mathematical identity:
(a+b)²= a² + 2ab + b²
By taking three sets of examples for a and b, as integers.
Answer
self
View full question & answer
Question 172 Marks
Write a program in Python to Take two strings as input. Print out either the longer string, or that the two strings are of equal length. For example:
Input:aseem, pankaj
Output: The longer string is pankaj
Answer
self
View full question & answer
Question 182 Marks
Write a program in Python where three integers are taken as input. You must print the three numbers in a decreasing order. Assume that the numbers are distinct. For example:
Input: 5, 1, 3
Output: 5, 3, 1
Answer
self
View full question & answer