In: Computer Science
Question 6
Which of the following for loops will find the largest element in the array numbers, assuming numbers has already been assigned a collection of numeric values?
Question 6 options:
largest = None |
|
largest = None |
|
largest = None |
|
largest = None |
Question 7
Which of the following is the correct output from this sequence of statements?
array1 = [1, 2, 3]
array2 = [2, 3]
print((array1 + array2) * 2)
Question 7 options:
[1, 2, 3, 2, 3, 1, 2, 3, 2, 3] |
|
[1, 2, 3, 2, 3] |
|
[1, 2, 3, 2, 3, 2, 3] |
|
[1, 2, 3] |
Question 8
Which of the following is the correct output from this sequence of statements?
primes = [2, 3, 5, 7, 11, 13]
primes[3:] = [1, 2, 3]
print(primes[:4])
Question 8 options:
[2, 3, 5, 7] |
|
[2, 3, 5] |
|
[3, 5, 1] |
|
[2, 3, 5, 1] |
Question 9
Which of the following is the correct output from the following sequence of statements?
sequence = [1, 3, 5, 7, 9]
for i in range(len(sequence)):
sequence[i] = i + sequence[i]
print(sequence)
Question 9 options:
[2, 7, 14, 23, 34] |
|
[1, 4, 7, 10, 13] |
|
[2, 6, 10, 14, 18] |
|
[1, 4, 9, 16, 25] |
Question 10
Which of the following is the correct output from this sequence of statements?
numbers = [18, 27, 42, 13, 21, 8, 11]
print(max(numbers))
Question 10 options:
42 |
|
140 |
|
7 |
|
8 |
Question 6:
largest = None
for i in range(len(numbers)):
if largest is None or numbers[i] > largest:
largest = numbers[i]
The for loop will find the largest element in the array numbers, assuming numbers has already been assigned a collection of numeric values.
In every iteration it checks for largest and assign a list number if the condition is true.
Sample Code:
Sample run:
Code to Copy (Refer above images of code for
indentation):
#example list
numbers=[1,2,10,2,4,15,8,5]
largest = None
#for loop iterate through list
for i in range(len(numbers)):
#compare and assign largest
if largest is None or numbers[i] > largest:
largest = numbers[i]
#print largest
print(largest)
Question 7:
[1, 2, 3, 2, 3, 1, 2, 3, 2, 3]
The above list is generated with the given code.
array1 = [1, 2, 3]
array2 = [2, 3]
print((array1 + array2) * 2)
(array1+array2)=[1,2,3,2,3]*2=[1,2,3,2,3,1,2,3,2,3]
So the above option is correct.
Sample run:
Question 8:
[2, 3, 5, 1]
The above list is printed with the given code. Because in the code slicing is used.
#list initialization
primes = [2, 3, 5, 7, 11, 13]
#modify above list from 3 rd index element
primes[3:] = [1, 2, 3]
#print list upto index 3
print(primes[:4])
initial list -[2,3,5,7,11,13]
primes[3:]=[1,2,3] so primes list modified as [2,3,5,1,2,3]
print(primes[:4]) prints upto index 3.
so it prints 2,3,5,1.
So the above list is printed.
Sample run:
Question 9:
[1, 4, 7, 10, 13]
The above list is printed by given code
#list initialization
sequence = [1, 3, 5, 7, 9]
for i in range(len(sequence)):
#using loop add index to value
sequence[i] = i + sequence[i]
print(sequence)
In the code, the index value is added to respective value.
1 index 0 =1+0 =1
3 index 1= 3+1=4
5 index 2=5+2=7
7 index 3=7+3=10
9 index 4=9+4=13
The final list printed is [1,4,7,10,13]
Sample run:
Question 10:
42
42 is printed because max() function return maximum value.
So in the given list 42 is highest.
numbers = [18, 27, 42, 13, 21, 8, 11] -42 maximum
Sample run: