Module-1 Introduction To Python
Module-1 Introduction To Python
INTRODUCTION TO PYTHON
Ans: d. #
Ans: c. +4
8. To start Python from the command prompt, use the command ________.
a. Execute python
b. Run python
c. Python
d. Go python
Ans: c. Python
9. If you enter 1, 2, 3, in one line, when you run this program, what will be displayed?
a. 1.0
b. 2.0
c. 3.0
d. 4.0
Ans: b. 2.0
x = 1
x = 2 * x + 1
print(x)
a. 0
b. 1
c. 2
d. 3
Ans: d. 3
x, y = 1, 2
x, y = y, x
print(x, y)
a. 11
b. 22
c. 12
d. 21
Ans: d. 2 1
17.
2.21 25 % 1 is _____
A. 1
B. 2
C. 3
D. 4
E. 0
2.22 24 % 5 is _____
A. 1
B. 2
C. 3
D. 4
E. 0
x = 1
y = x = x + 1
print("y is", y)
A. y is 0.
B. y is 1 because x is assigned to y first.
C. y is 2 because x + 1 is assigned to x and then x is assigned to y.
D. The program has a compile error since x is redeclared in the statement int y = x = x +
1.
print("A", end = ' ')
print("B", end = ' ')
print("C", end = ' ')
print("D", end = ' ')
A. ABCD
B. A, B, C, D
C. A B C D
D. A, B, C, D will be displayed on four lines
4.31 The order of the precedence (from high to low) of the operators +, *, and, or is:
A. and, or, *, +
B. *, +, and, or
C. *, +, and, or
D. *, +, or, and
E. or, and, *, +
5.1 How many times will the following code print "Welcome to Python"?
count = 0
while count < 10:
print("Welcome to Python")
count += 1
A. 8
B. 9
C. 10
D. 11
E. 0
x = 0
while x < 4:
x = x + 1
print("x is", x)
A. x is 0
B. x is 1
C. x is 2
D. x is 3
E. x is 4
count = 0
while count < 100:
# Point A
print("Welcome to Python!")
count += 1
# Point B
# Point C
A. count < 100 is always True at Point A
B. count < 100 is always True at Point B
C. count < 100 is always False at Point B
D. count < 100 is always True at Point C
E. count < 100 is always False at Point C
5.4 How many times will the following code print "Welcome to Python"?
count = 0
while count < 10:
print("Welcome to Python")
A. 8
B. 9
C. 10
D. 11
E. infinite number of times
number = 6
while number > 0:
number -= 3
print(number, end = ' ')
A. 6 3 0
B. 6 3
C. 3 0
D. 3 0 -3
E. 0 -3
sum = 0
for d in range(0, 10, 0.1):
sum += sum + d
A. The program has a syntax error because the range function cannot have three
arguments.
B. The program has a syntax error because the arguments in the range must be integers.
C. The program runs in an infinite loop.
D. The program runs fine.
A:
for count in range(1, 10):
print("Welcome to Python")
B:
for count in range(0, 10):
print("Welcome to Python")
C:
for count in range(1, 11):
print("Welcome to Python")
D:
for count in range(1, 12):
print("Welcome to Python")
A. BD
B. ABC
C. AC
D. BC
E. AB
5.11 Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100?
A:
sum = 0
for i in range(1, 99):
sum += i / (i + 1)
print("Sum is", sum)
B:
sum = 0
for i in range(1, 100):
sum += i / (i + 1)
print("Sum is", sum)
C:
sum = 0
for i in range(1.0, 99.0):
sum += i / (i + 1)
print("Sum is", sum)
D:
sum = 0
for i in range(1.0, 100.0):
sum += i / (i + 1)
print("Sum is", sum)
A. BCD
B. ABCD
C. B
D. CDE
E. CD
for i in range(1, 11):
print(i, end = " ")
A. 1 2 3 4 5 6 7 8 9
B. 1 2 3 4 5 6 7 8 9 10
C. 1 2 3 4 5
D. 1 3 5 7 9
E. 2 4 6 8 10
y = 0
for i in range(0, 10):
y += i
print(y)
A. 10
B. 11
C. 12
D. 13
E. 45
y = 0
for i in range(0, 10, 2):
y += i
print(y)
A. 9
B. 10
C. 11
D. 20
y = 0
for i in range(10, 1, -2):
y += i
print(y)
A. 10
B. 40
C. 30
D. 20
Pattern A Pattern B Pattern C Pattern D
1 1 2 3 4 5 6 1 1 2 3 4 5 6
1 2 1 2 3 4 5 2 1 1 2 3 4 5
1 2 3 1 2 3 4 3 2 1 1 2 3 4
1 2 3 4 1 2 3 4 3 2 1 1 2 3
1 2 3 4 5 1 2 5 4 3 2 1 1 2
1 2 3 4 5 6 1 6 5 4 3 2 1 1
Which of the pattern is produced by the following code?
for i in range(1, 6 + 1):
for j in range(6, 0, -1):
print(j if j <= i else " ", end = " ")
print()
A. Pattern A
B. Pattern B
C. Pattern C
D. Pattern D
for i in range(10):
for j in range(10):
print(i * j)
A. 100
B. 20
C. 10
D. 45
for i in range(10):
for j in range(i):
print(i * j)
A. 100
B. 20
C. 10
D. 45
balance = 10
while True:
if balance < 9: break
balance = balance - 9
A. Yes
B. No
sum = 0
item = 0
while item < 5:
item += 1
sum += item
if sum > 4: break
print(sum)
A. 5
B. 6
C. 7
D. 8
sum = 0
item = 0
while item < 5:
item += 1
sum += item
if sum >= 4: continue
print(sum)
A. 15
B. 16
C. 17
D. 18
balance = 10
while True:
if balance < 9: continue
balance = balance - 9
A. Yes
B. No
number = 25
isPrime = True
i = 2
while i < number and isPrime:
if number % i == 0:
isPrime = False
i += 1
print("i is", i, "isPrime is", isPrime)
A. i is 5 isPrime is True
B. i is 5 isPrime is False
C. i is 6 isPrime is True
D. i is 6 isPrime is False
number = 25
isPrime = True
for i in range(2, number):
if number % i == 0:
isPrime = False
break
print("i is", i, "isPrime is", isPrime)
A. i is 5 isPrime is True
B. i is 5 isPrime is False
C. i is 6 isPrime is True
D. i is 6 isPrime is False
5.29 Suppose the input for number is 9. What will be displayed by the following program?
number = eval(input("Enter an integer: "))
isPrime = True
for i in range(2, number):
if number % i == 0:
isPrime = False
print("i is", i)
if isPrime:
print(number, "is prime")
break
else:
print(number, "is not prime")
A. i is 3 followed by 9 is prime
B. i is 3 followed by 9 is not prime
C. i is 2 followed by 9 is prime
D. i is 2 followed by 9 is not prime
Chapter 6 Functions