NEW
Font size
WorksheetsAssignment 2(CIVIL)
Total questions: 25
Worksheet time: 13mins
1. What is the output of the following for loop and range() function
for num in range(-2,-5,-1):
print(num, end=", ")
-2, -1, -3, -4
-2, -1, 0, 1, 2, 3,
-2, -1, 0
-2, -3, -4,
Given the nested if-else structure below, what will be the value of x after code execution completes
x = 0
a = 0
b = -5
if a > 0:
if b < 0:
x = x + 5
elif a > 5:
x = x + 4
else:
x = x + 3
else:
x = x + 2
print(x)
2
0
3
4
What is the output of the following range() function
for num in range(2,-5,-1):
print(num, end=", ")
2, 1, 0
2, 1, 0, -1, -2, -3, -4, -5
2, 1, 0, -1, -2, -3, -4
2, 1, 0, -1, -2
What is the output of the following if statement
a, b = 12, 5
if a + b:
print('True')
else:
print('False')
False
True
Which of the following is a valid conditional statement in Python?
if a = 5:
if a == 5:
if a <> 5:
if (a = 5):
What is the outcome if the elif and else blocks are missing in an if statement?
Syntax error
he program will stop
Nothing, it's optional
Runtime error
What is the output of the following code?
x = 10;
if x > 5:
print("Greater")
Error
Greater
Nothing
Less
What will the following code print?
x = 10;
if x < 10:
print("Less");
else:
print("Not Less")
Less
Not Less
Error
Nothing
What is printed when the temperature is 25 degrees?
If temperature > 30,
print "Hot",
elif temperature > 20,
print "Warm",
else
"Cold"
Prints "Hot"
Prints "Warm"
Prints "Cold"
No output
Identify the error:
if x > 10
print("Greater")
Missing parentheses around condition
No error
Missing colon after condition
Incorrect comparison operator
What is the use of a for loop in Python?
To repeat a block a fixed number of times
To check a condition
To declare variables
To handle exceptions
Which statement immediately terminates a loop in Python?
break
continue
exit
stop
What does the continue statement do inside a loop?
Pauses the loop
Stops the loop
Skips the current iteration
Exits the program
In a while loop, what happens if the condition never becomes False?
The loop runs forever
The loop stops after 10 iterations
Syntax error
The loop doesn't start
What is the output of the following code?
for i in range(3):
print(i)
"0 1 2"
"1 2 3"
"0 1 2 3"
"Error"
What will the following code print?
i = 5;
while i > 0:
print(i);
i -= 1
5 4 3 2 1
5 4 3 2
1 2 3 4 5
Infinite loop
How do you access the last element of a list named myList?
myList[0]
myList[-1]
myList[len(myList)]
myList[-2]
What is a function in Python?
A variable
A module
A block of code
An operator
What is the difference between arguments and parameters in Python functions?
No difference
Parameters define, arguments call
Arguments define, parameters call
Based on data type
What will the following function return:
def example():
return 5+5
5+5
10
0
None
What is the output of this code:
def add(x, y):
return x + y;
print(add(3, 4))
3
4
7
Error
Identify the error:
def my_func(x, y):
return x + y;
my_func(1)
Missing argument
Syntax error
Logic error
No error
Find the mistake:
import math;
print(math.sqr(4))
Incorrect module
Incorrect function name
Wrong number of arguments
No error
Which of the following is the use of id() function in python?
Every object in Python doesn’t have a unique id
In Python Id function returns the identity of the object
None of the mentioned
All of the mentioned
Guess the correct output of the following code?
str1 = "PYnative" print(str1[1:4], str1[:5], str1[4:], str1[0:-1], str1[:-1])
PYn PYnat ive PYnativ vitanYP
Yna PYnat tive PYnativ vitanYP
Yna PYnat tive PYnativ PYnativ
