Sie sind auf Seite 1von 17

MODULE-1

INTRODUCTION TO PYTHON

1. The three right arrows displayed on the python console represents


a. Expression prompt
b. New window prompt
c. System restart
d. Python shell problem

Ans: a. Expression prompt

2. Continuation prompt in python is given in the following cases


a. When comment is specified
b. When left parenthesis is not found
c. When right parenthesis is not found
d. Both a & c

Ans: d. Both a & c

3. Comments in python are prefixed by


a. $
b. %
c. //
d. #

Ans: d. #

4. Which of the following statements is false?


a. Python specifies an upper bound on the size of integers
b. Python concentrates on indentation
c. Python neglects the remainder part in Integer division
d. Strings in python can either use single quotes or double quotes

Ans: a. Python specifies an upper bound on the size of integers

5. The ‘in’ operator in python returns true if


a. Left argument is found in the right argument
b. Right argument is found in the left argument
c. Left argument has precedence over right argument
d. Right argument has precedence over left argument

Ans: a. Left argument is found in the right argument

6. The alternative syntax for binary operator is denoted by


a. Associative rules
b. Function call
c. Modules
d. Packages

Ans: b. Function call

7. What is the value of abs(~3) ?


a. +3
b. -3
c. +4
d. -4

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?

number1, number2, number3 = eval(input("Enter three numbers: "))


  # Compute average
average = (number1 + number2 + number3) / 3
# Display result
print(average)

a. 1.0
b. 2.0
c. 3.0
d. 4.0
Ans: b. 2.0

10. What will be displayed by the following code?

x =  1
x =  2 * x + 1 
print(x)

a. 0
b. 1
c. 2
d. 3
Ans: d. 3

11. What will be displayed by the following code?


x =  1
x =  x  + 2.5 
print(x)
a. 1
b. 3
c. 3.5
d. The statements are illegal
Ans: c. 3.5

12. What will be displayed by the following code?

x, y =  1, 2
x, y =  y,  x
print(x, y)

a. 11
b. 22
c. 12
d. 21
Ans: d. 2 1

13. What is the result of 45 / 4?


a. 10
b. 11
c. 11.25
d. 12
Ans: c. 11.25

14. What is the result of 45 // 4?


a. 10
b. 11
c. 11.25
d. 12
Ans: b. 11

15. Which of the following expressions will yield 0.5?


(i) 1/2
(ii) 1.0 / 2
(iii) 1 // 2
(iv) 1.0 // 2
(v) 1 / 2.0

a. (i), (ii) and (iii)


b. (i), (ii) and (iv)
c. (i), (ii) and (v)
d. (ii), (iii), (iv) and (v)
Ans: c. (i), (ii) and (v)

16. Which of the following expression results in a value 1?


a. 2 % 1
b. 15 % 4
c. 25 % 5
d. 37 % 6
Ans:

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

2.23  2 ** 3 evaluates to __________.


A. 9
B. 8
C. 9.0
D. 8.0

2.24  2 ** 3.0 evaluates to __________.


A. 9
B. 8
C. 9.0
D. 8.0

2.25  2 * 3 ** 2 evaluates to __________.


A. 36
B. 18
C. 12
D. 81

2.26  What is y displayed in the following code?

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.

2.27  Which of the following is equivalent to 0.025?


A. 0.25E-1
B. 2.5e-2
C. 0.0025E1
D. 0.00025E2
E. 0.0025E+1

2.29  What is the result of evaluating 2 + 2 ** 3 / 2?


A. 4
B. 6
C. 4.0
D. 6.0

2.39  Which of the following functions return 4.


A. int(3.4)
B. int(3.9)
C. round(3.4)
D. round(3.9)

2.40  Which of the following functions cause an error?


A. int("034")
B. eval("034")
C. int("3.4")
D. eval("3.4")

3.1  What is max(3, 5, 1, 7, 4)?


A. 1
B. 3
C. 5
D. 7
E. 4

3.2  What is min(3, 5, 1, 7, 4)?


A. 1
B. 3
C. 5
D. 7
E. 4

3.3  What is round(3.52)?


A. 3.5
B. 3
C. 5
D. 4
E. 3.0

3.4  What is round(6.5)?


A. 4
B. 5
C. 6
D. 7
E. 8

3.5  What is round(7.5)?


A. 4
B. 5
C. 6
D. 7
E. 8

3.10  Which of the following is the correct expression of character 4?


A. 4
B. "4"
C. '4'
3.11  In Python, a string literal is enclosed in __________.
A. parentheses
B. brackets
C. single-quotes
D. double-quotes
E. braces

3.19  The expression "Good " + 1 + 2 + 3 evaluates to ________.


A. Good123
B. Good6
C. Good 123
D. Illegal expression

3.20  What will be displayed by the following code?

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.3  The word True is ________.


A. a Python keyword
B. a Boolean literal
C. same as value 1
D. same as value 0

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

5.2  What is the output of the following code?

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

5.3  Analyze the following code.

 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

5.5  What will be displayed when the following code is executed?

    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

Section 5.3 The for Loop


5.6  Analyze the following statement:

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.

5.7  Which of the following loops prints "Welcome to Python" 10 times?

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.8  The function range(5) return a sequence ______________.


A. 1, 2, 3, 4, 5
B. 0, 1, 2, 3, 4, 5
C. 1, 2, 3, 4
D. 0, 1, 2, 3, 4

5.9  Which of the following function returns a sequence 0, 1, 2, 3?


A. range(0, 3)
B. range(0, 4)
C. range(3)
D. range(4)

5.10  Which of the following function is incorrect?


A. range(0, 3.5)
B. range(10, 4, -1)
C. range(1, 3, 1)
D. range(2.5, 4.5)
E. range(1, 2.5, 4.5)

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

5.12  The following loop displays _______________.

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

5.13  What is the output for y?

y = 0
for i in range(0, 10):
    y += i

print(y)
A. 10
B. 11
C. 12
D. 13
E. 45

5.14  What is the output for y?

y = 0
for i in range(0, 10, 2):
    y += i

print(y)
A. 9
B. 10
C. 11
D. 20

5.15  What is the output for y?

y = 0
for i in range(10, 1, -2):
    y += i

print(y)
A. 10
B. 40
C. 30
D. 20

5.16  Given the following four patterns,

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

5.19  How many times is the print statement executed?

for i in range(10): 
    for j in range(10):
        print(i * j)
A. 100
B. 20
C. 10
D. 45

5.20  How many times is the print statement executed?

for i in range(10): 
    for j in range(i):
        print(i * j)
A. 100
B. 20
C. 10
D. 45

Section 5.7 Keywords break and continue


5.21  Will the following program terminate?

balance = 10

while True:
    if balance < 9: break
    balance = balance - 9
A. Yes
B. No

5.22  What is sum after the following loop terminates?

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

5.23  What is sum after the following loop terminates?

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

5.24  Will the following program terminate?

balance = 10

while True:
    if balance < 9: continue
    balance = balance - 9
A. Yes
B. No

Section 5.8 Case Study: Displaying Prime Numbers


5.25  What will be displayed by after the following loop terminates?

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

5.26  What will be displayed by after the following loop terminates?

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.27  What is the number of iterations in the following loop:

  for i in range(1, n):


      # iteration
A. 2*n
B. n
C. n - 1
D. n + 1

5.28  What is the number of iterations in the following loop:


  for i in range(1, n + 1):
      # iteration
A. 2*n
B. n
C. n - 1
D. n + 1

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

Sections 6.2 Defining a Function


6.1  If a function does not return a value, by default, it returns ___________.
A. None
B. int
C. double
D. public
E. null

6.2  The header of a function consists of ____________.


A. function name
B. function name and parameter list
C. parameter list

6.3  A function _________.


A. must have at least one parameter
B. may have no parameters
C. must always have a return statement to return a value
D. must always have a return statement to return multiple values

Sections 6.3 Calling a Function


6.4  Arguments to functions always appear within __________.
A. brackets
B. parentheses
C. curly braces
D. quotation marks

Das könnte Ihnen auch gefallen