Font size
WorksheetsPython Quiz-5 AX
Total questions: 148
Worksheet time: 3hrs 55mins
4
3
2
1
3
2
1
0
4
3
2
1
3
2
1
0
4
3
2
1
3
2
1
0
8
6
4
2
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
......(repeated continuously)
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
(repeated continuously)
break
False
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
(repeated continuously)
For the code shown, what will happen?
It will print all the numbers from 0 to 11
It will print all the numbers from 0 to 11 in increments of 3
Infinite loop
It will not run (Syntax error)
A while loop is used when
You know how many times you are going to loop
You want to loop for an unknown number of times, and possibly forever
You only want to run the code in the loop once
You see the word while in the problem description
What is the final value of n?
(a)
Why is iteration important?
It determines the order in which instructions are carried out
It allows code to be simplified by removing duplicated steps into a block that can be executed many times
It allows multiple paths through a program
It ensures the code works correctly
What is the output of this code?
It would print 'So good to see you' infinite number of times
There would be no output
It would print an error message.
It would print 'Enter correct code'.
The condition for a while loop to continue could include which of the following?
While something equals something
While something is True
While something is greater than something
All of these
What is the final value of the variable 'number'?
(a)
What is the output?
5
4
3
2
1
5 4 3 2 1
4
3
2
1
0
4 3 2 1 0
To increment means to
increase the value of a variable
decrease the value of a variable
add 1 to the variable
subtract 1 from the variable
What is the output?
12 9 6 3 0
0 3 6 9 12
0 3 6 9
9 6 3 0
A counter is (check ALL that apply)
A variable used in a loop to count the number of times something happened
usually initialised to zero
usually incremented inside the loop
a person with a pen and paper
used only outside of the loop
What is the output?
0 5 10
5 10
0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140......etc
12 7 2
What is the output?
12 17 22
12
5
5 10
What is the output?
Hello world
Hello there
Error
Hello there
Hello there
Hello there
Hello there
Hello there
...(forever)
What is the output?
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
(repeated continuously)
Hello world!
Hello world!
break
Error
What is the output?
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
Hello world!
(repeated continuously)
Hello world!
Hello world!
break
Error
What is the output is the user types "Python"?
Welcome
Try again!
Error
Enter your password
What will this code output?
Nothing
1
4
7
Bye
Jon
Jon
Bye
Jon
Jon
Jon
Bye
Jon
Jon
Jon
Jon
Jon
Jon
Bye
What will be the output of this code?
This loop will not run
An infinite loop
It'll print all numbers from 1-9
It'll print all numbers from 1-10
What will happen if the user enters the sequence (each time through the loop)
2, 5, 3, q
Loop finishes and prints 10
Loop never ends
Program throws a ValueError when calling the int() function
Program asks user to enter another num
Welcome
Error
Try again!
Enter your password
Hello world!
Hello world!
False
Error
Hello world! Hello world! Hello world! Hello world! Hello world! Hello world! (repeated continuously)
0 1 2
012
0
1
2
0 1 2 3
0
1
2
3
l e t t e r s
letters
0 1 2 3 4 5 6
l e t t e r
letter
vortex vortex vortex vortex
vortex
vortex
vortex
vortex
vortex, vortex, vortex, vortex
0 1 2 3
0 1 2 3 4
6
0
1
3
6
0
1
2
3
5
3
2
16
32
2
4
8
16
2
4
8
16
32
cat
cat
cat
cat
cat
catcatcatcatcat
cat cat cat cat cat
5
4
3
2
1
0
count
count artistic
1
8
7
10
2
3
8
an error will be received
0 1 2 3 4
0 1 2 3 4 5
5
4
this won't work properly, infinite loop
0 2 4 6 8 10
p n e e e n
10
n
0
b = = k = = p = r
= e e = e e = e =
= = = =
e e e e e
b k p r
$ $ $ $ $
$ $ $ $
$ $ $
$ $
$ $ $ $ $
$ $
$ $ $ $
$ $ $ $ $
$ $
20
25
21
infinite loop
sum: 39
sum: 38
sum: 24
sum: 57
11
7 8 9 10 11
12
7 8 9 10 11 12
25
35
26
46
0
3 5 7 9
24
error
the output of this program group --------
A
B
Print i as long as i is less than 6 and i is initiated with the value of 1 :
Which program is correct
Print i where i is initiated with the value of 0 as long as i is less than 6 and continue to the next iteration if i is 3
To match this output which one is the correct program
Stop the loop if i is 3.
break
continue
In the loop, when i is 3, jump directly to the next iteration.
break
continue
What does the flowchart in the image represent?
The syntax of a while loop in Python
The execution flow of a while loop in Python
The error handling in a while loop in Python
The variable declaration in a while loop in Python
What will be the output of the given Python code snippet?
The code will print "Hello World" once.
The code will print "Hello World" ten times.
The code will print "Hello World" indefinitely.
The code will cause an error.
What will happen when the following Python code is executed?
The loop will run indefinitely because 's' is never typed.
The loop will end immediately without waiting for user input.
The loop will ask the user to input a letter and will only end when the user types 's'.
The loop will cause an error because the variable 'letter' is not properly defined.
What is the initial value of the variable 'letter' in the given Python code?
's'
' '
''
None
The program above
prints all the odd numbers between 1 and 20
prints all even numbers between 1 and 20
print all the odd numbers between 1 and 21
prints all the even numbers between 1 and 21
The equivalent of the above for loop using the while syntax would be
The above program prints
Numbers 1 to 10 in reverse order
Numbers 1 to 11 in reverse order
Numbers 1 to 10
Numbers 1 to 11
The program above prints
Numbers between 1 and 100 that are multiples of 5
Numbers between 1 and 100
Numbers between 1 and 100 divisible by 5
Numbers between 1 and 101 that are multiples of 5
The code above prints all numbers between 1 and 30 that are
not divisible by 5
divisible by 5
The program above prints the sum of
all odd numbers less than 100
all even numbers less than 100
all numbers less than 100
The program above prints the sum of all the numbers between 0 and 101 that are
alternate odd numbers
alternate even numbers
alternate numbers
The program above prints each number between 1 and 10
as many times as the number itself
10 times
11 times
How many times will 1 be printed
3
4
5
6
for i in 100 :
print ( i )
Syntax error
prints numbers 0 to 100
prints numbers 1 to 100
prints numbers 0 to 99
prints numbers 1 to 99
What is the output of the program above
10
15
18
17
16
A while loop equivalent of the for loop above is
What is the value of the variable sum after the code above executes
54
44
64
34
74
str()
converts any type of value to string data type
converts string to integer
Fixed number of fibonacci series
0 and 1
1 and 2
1 and 1
Choose the correct fibonacci series
0 1 2 3 5 8 13
0 1 1 2 3 5 8
1 2 3 5 8 15 23
Examples of fibonacci
Correct sequence of while loop
Condition, Action, initialisation, Increment/decrement
initialisation, Condition, Action, Increment/decrement
initialisation, Condition, Increment/decrement, Action
What is the result of following Program in python3
a=4
b=2
c=a-b*6
print("\n a={0} \n b={1} \n c={2}".format(a,b,c))
12
-8
8
-12
What is the result of following Program in python3
for i in range(3,0,-1)
print("{0} Python".format(i))
3 Python
2 Python
1 Python
2 Python
1 Python
0 Python
3 Python
error
What is the result of following Program in python3
if(32%2==0):
print("32")
else:
print("2")
2
32
error
What is the result of following Program in python3
for i in range(1,4):
print(i)
if(i==2):
break;
1
1
2
1
2
3
4
error
person = input('Enter your name: ')
print('Hello', + person)
x = int(input("Please enter an integer: "))
if x<0:
x=0
print("Your negative number was changed to zero")
If a=(1,2,3,4), a[1:-1] is _________
a) Error, tuple slicing doesn’t exist
b) [2,3]
c) (2,3,4)
d) (2,3)
Suppose list1 is [2, 33, 222, 14, 25], What is list1[:-1]?
a) [2, 33, 222, 14]
b) Error
c) 25
d) [25, 14, 222, 33, 2]
What is the order of precedence in python?
a) Exponential, Parentheses, Multiplication, Division, Addition, Subtraction
b) Exponential, Parentheses, Division, Multiplication, Addition, Subtraction
c) Parentheses, Exponential, Multiplication, Division, Subtraction, Addition
d) Parentheses, Exponential, Multiplication, Division, Addition, Subtraction
Which of the following functions is a built-in function in python?
a) factorial()
b) print()
c) seed()
d) sqrt()
To add a new element to a list we use which Python command?
a) list1.addEnd(5)
b) list1.addLast(5)
c) list1.append(5)
d) list1.add(5)
What will be the output of the following Python statement?
>>>"abcd"[2:]
a) a
b) ab
c) cd
d) dc
What will be the output of the following Python statement?
>>>"abcd"[2:]
a) a
b) ab
c) cd
d) dc
What will be the output of the following Python code?
>>> str1 = 'hello'
>>> str2 = ','
>>> str3 = 'world'
>>> str1[-1:]
a) olleh
b) hello
c) h
d) o
Suppose list1 is [2445,133,12454,123], what is max(list1)?
a) 2445
b) 133
c) 12454
d) 123
Which of the following statements create a dictionary?
a) d = {}
b) d = {“john”:40, “peter”:45}
c) d = {40:”john”, 45:”peter”}
d) All of the mentioned
What will be the output of the following Python code?
a={1:"A",2:"B",3:"C"}
b={4:"D",5:"E"}
a.update(b)
print(a)
a) {1: ‘A’, 2: ‘B’, 3: ‘C’}
b) Method update() doesn’t exist for dictionaries
c) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}
d) {4: ‘D’, 5: ‘E’}
Find the output of the following:-
int sum=0, n=10;
for( int i=n; i>=1; -- i)
{ sum= sum+ i; }
System.out.println(" Sum is "+ sum) ;
55
10
28
50
Consider these functions:
push() : push an element into the stack
pop() : pop the top-of-the-stack element
top() : returns the item stored in top-of-the-stack-node
What will be the output after performing these sequence of operations
push(20);
push(4);
top();
pop();
pop();
pop();
push(5);
top();
20
4
5
Stack Underflow
How many times do while loop is guaranteed to execute?
0
1
depends upon condition
No gurantee
Which of the following is an exit controlled loop?
While loop.
For loop.
do-while loop.
None of the above.
print([i.lower() for i in "HELLO"])
[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
‘hello’
[‘hello’]
hello
print([[i+j for i in "abc"] for j in "def"])
[‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’]
[[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]]
[[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]]
[‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’]
print([if i%2==0: i; else: i+1; for i in range(4)])
[0, 2, 2, 4]
[1, 1, 3, 3]
error
none of the mentioned
Write the list comprehension to pick out only negative integers from a given list ‘a’.
[x<0 in a]
[x for x<0 in a]
[x in a for x<0]
[x for x in a if x<0]
Write a list comprehension for number and its cube for l=[1, 2, 3, 4, 5, 6, 7, 8, 9]
[x**3 for x in l]
[x*3 for x in l]
[x**3 in l]
[x^3 in l]
Read the information given below carefully and write a list comprehension such that the output is: [‘e’, ‘o’]
w="hello"
v=('a', 'e', 'i', 'o', 'u')
[x for w in v if x in v]
[x for x in w if x in v]
[x for x in v if w in v]
[x for v in w for x in w]
Write a list comprehension for producing a list of numbers between 1 and 1000 that are divisible by 3.
[x in range(1, 1000) if x%3==0]
[x for x in range(1000) if x%3==0]
[x%3 for x in range(1, 1000)]
[x%3=0 for x in range(1, 1000)]
Write a list comprehension equivalent for the Python code shown below.
[i for i in range(1, 100) if int(i*0.5)==(i*0.5)]
[i for i in range(1, 101) if int(i*0.5)==(i*0.5)]
[i for i in range(1, 101) if int(i*0.5)=(i*0.5)]
[i for i in range(1, 100) if int(i*0.5)=(i*0.5)]
Write a list comprehension to produce the list: [1, 2, 4, 8, 16, ..., 1024].
[2**x for x in range(0, 10)]
[2**x for x in range(11)]
[2*x for x in range(0, 11)]
[2*x for x in range(1, 11)]
What will be the output of the following Python code?
[‘good’, ‘oh’, ‘excellent’, ‘450’ ]
[‘good’]
[‘good’, ‘#450’]
[‘oh!’, ‘excellent!’, ‘#450’]
What is used to separate elements in a list?
Bracket
Comma
Full Stop
Space
An empty list can be declared as
fruitList = ""
fruitList = ["empty"]
fruitList == []
fruitList = []
Each item in a list has an "address" or index. The first index in a Python list is what?
1
0
a
A
What will be the output?
names1 = ['Amir', 'Bala', 'Chales']
if 'amir' in names1:
print(1)
else:
print(2)
None
1
2
error
What brackets are used to create a list?
()
[]
fruitlist1 = ["banana","grapes","apple"]
fruitlist2 = ["banana","grapes","apple","grapes"]
fruitlist3 = fruitlist1 + fruitlist2
print(fruitlist3)
["banana","grapes","apple"]
["banana","grapes","apple","grapes"]
fruitlist1 + fruitlist2
["banana","grapes","apple","banana","grapes","apple","grapes"]
mylist = ["a","b"]
print(mylist*2)
["a","b","a","b"]
["a","b","a"]
["a","b","a","a"]
["a","b","a"
mylist = ["a","b"]
mylist[1] = "c"'
print(mylist)
["a","c"]
["a","b"]
["a","b","c"]
mylist
Consider this list, what will be the highest index the list can have:
li=[1,2,3,4,5]
2
3
4
5
Consider this list, what will be the output of the following:
li=[1,2,3,4,3,2,1]
print(li[-3])
4
3
2
1
Consider this list, what will be the output of the following:
li=[1,2,3,4,3,2,1]
print(li[3])
4
3
2
1
Consider this list, what will be the output of the following:
li=[1,2,3,4,3,2,1]
print(li[3:4])
1
2
3
4
Write the output of the following code
L=[1, 2, 3, 4, 5, [6,7,8]]
print(L[5])
[6, 7, 8]
6, 7, 8
6
Error
Q12. Write the output of the following code :
>>>L=[“Amit”,”Sumit”,”Naina”]
>>>print(L[1:-1])
[‘Sumit’]
[a]
[Naina]
None of the above
L=[1,2,3,4,5]
for i in L:
print(i, end=” “)
a. 1 2 3 4 5
b. 1, 3, 5
c. 1, 2, 3, 4, 5
d. None of the above
Write the output of the following code :
L=[“Amit”,”Sumit”,”Naina”]
L1=[“Sunil”]
print(L + L1)
a. [‘Amit’ , ‘Sumit’ , ‘Naina’ , [‘Sunil’]]
b. [‘Amit’ , ‘Sumit’ , ‘Naina’ , ‘Sunil’]
c. List can not concatenate
d. None of the above
Result of list slice is also a list?(T/F)
a. True
b. False
Index of last element in list is n-1, where n is total number of elements.(T/F)
a. True
b. False
In list slicing, the start and stop can be given beyond limits. If it is then
raise Index Error exception
raise ValueError exception
Return elements falling between specified start and stop values
return the entire list
Consider this list, what will be the output of the following:
li=[1,2,3]
print(li[3])
1
2
3
error
print([i.lower() for i in "HELLO"])
[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
‘hello’
[‘hello’]
hello
print([[i+j for i in "abc"] for j in "def"])
[‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’]
[[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]]
[[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]]
[‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’]
print([if i%2==0: i; else: i+1; for i in range(4)])
[0, 2, 2, 4]
[1, 1, 3, 3]
error
none of the mentioned
Write the list comprehension to pick out only negative integers from a given list ‘a’.
[x<0 in a]
[x for x<0 in a]
[x in a for x<0]
[x for x in a if x<0]
Write a list comprehension for number and its cube for l=[1, 2, 3, 4, 5, 6, 7, 8, 9]
[x**3 for x in l]
[x*3 for x in l]
[x**3 in l]
[x^3 in l]
Read the information given below carefully and write a list comprehension such that the output is: [‘e’, ‘o’]
w="hello"
v=('a', 'e', 'i', 'o', 'u')
[x for w in v if x in v]
[x for x in w if x in v]
[x for x in v if w in v]
[x for v in w for x in w]
Write a list comprehension for producing a list of numbers between 1 and 1000 that are divisible by 3.
[x in range(1, 1000) if x%3==0]
[x for x in range(1000) if x%3==0]
[x%3 for x in range(1, 1000)]
[x%3=0 for x in range(1, 1000)]
Write a list comprehension equivalent for the Python code shown below.
[i for i in range(1, 100) if int(i*0.5)==(i*0.5)]
[i for i in range(1, 101) if int(i*0.5)==(i*0.5)]
[i for i in range(1, 101) if int(i*0.5)=(i*0.5)]
[i for i in range(1, 100) if int(i*0.5)=(i*0.5)]
Write a list comprehension to produce the list: [1, 2, 4, 8, 16, ..., 1024].
[2**x for x in range(0, 10)]
[2**x for x in range(11)]
[2*x for x in range(0, 11)]
[2*x for x in range(1, 11)]
What will be the output of the following Python code?
[‘good’, ‘oh’, ‘excellent’, ‘450’ ]
[‘good’]
[‘good’, ‘#450’]
[‘oh!’, ‘excellent!’, ‘#450’]
