Week 2
Week 2
1. At any given point during a program's execution, what value is held within a
variable?
a. The value it was initially assigned when it was created (Only if the value of the
variable has not been modified within the program).
b. The sum of all values that have ever been assigned to it (Incorrect).
c. The most recent value that was assigned to it (Yes, it will hold for any
modifications to the variable a during the program execution).
d. The average of all values that have ever been assigned to it.
Answer: Option c
Variables are used to store some values in programming languages. The values can be a
number or a string.
a = 10
...
a = a+1
print(a)
The variable a will hold the value 10, at any stage in the program, until and unless it’s value
has been modified.
Which of the following represents the correct output if the user inputs "25"?
a. The value is: 25
b. 25
c. The value is:
d. None of the above
Answer: Option a
The value is: 25
3. What are the possible loop values that can be specified in the range argument of a
for loop in Python, and why are these values used?
a. Integers, representing the starting and ending points of the loop, allowing
iteration through a specified range of numbers.
b. Floating-point numbers, enabling iteration through decimal ranges with
precise steps.
c. Strings, facilitating iteration through characters in a specified string
d. Tuples, allowing iteration through multiple sequences simultaneously.
Answer option a
Loops are statements in programming languages used to repeat a set of instructions
multiple times.
Write a program to print “Hello World” 10 times.
for i in range(10):
print(“Hello World”)
Print “Hello World” for 1.5 times: it does not make any sense.
4. What values can be used in the argument of a while loop in Python, and why are
these values used?
a. Integers, representing the starting and ending points of the loop, allowing
iteration through a specified range of numbers
b. Boolean expressions or conditions, enabling the loop to execute until the
condition becomes False.
c. Floating-point numbers, allowing iteration through decimal ranges with
precise steps.
d. Strings, facilitating iteration through characters in a specified string.
Answer: Option b. The argument inside a while loop expects a boolean expression or
condition that restricts the loop to execute until the condition becomes false.
while(some condition or boolean):
do something
Update the iterator so that the conditon becomes false after some finite number of
iterations.
5. What types of conditional entries can be used in a Python if statement, and why are
these entries used?
a. Integers, representing specific numeric values to check against a variable,
ensuring a match for equality
b. Boolean expressions or conditions, evaluating to True or False and
determining the path of execution in the code.
c. Strings, enabling comparison of text values for exact matches in the
condition
d. Floating-point numbers, allow range-based conditions to check if a variable
falls within a specific numerical range.
Answer: Option b
Answer: Option a
n x i = n*i
n x 1 = n*1
n x 2 = n*2
Answer: Option a
Answer: Option a, b
result = 1 * 1 counter = 2
result = 1 * 2 counter 2 + 1
result = 2 * 3 counter 3 + 1
result = 6 * 4 counter = 4 + 1
result 24 * 5 counter 5 + 1
Shorthand operators
a=2
a += 1 # a = a + 1
a -= 1 # a = a - 1
a *= 1 # a = a * 1
a /= 1 # a = a / 1
9. An Integrated Development Environment (IDE) is a software application that
provides comprehensive facilities to computer programmers for software
development. Which of the following are IDEs?
a. PyCharm
b. Spyder
c. Visual Studio Code (VS Code)
d. C# (pronounced "C sharp")
Answer: Option a, b, c
10. A Python distribution is a software bundle, which contains a Python interpreter and
the Python standard library. What is Anaconda being discussed in lectures?
a. A species of snake
b. A type of programming language
c. An integrated development environment (IDE)
d. A Python distribution for scientific computing and data science
Answer: Option d
Programming Problems
Write a program that takes two numbers as input and tells whether they are equal or not.
if (num1 == num2):
if (num1 != num2):
Write a program that takes user age as input and tells whether the user is eligible to vote or
not.
Voting eligibility criteria: The user’s age should be greater than or equal to 18.
Write a program in Python to check if the given year as input is a leap year or not.
Write a program to accept XY coordinates of a point and tell the quadrant in which the point
lies.