wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

DCSMAT-DataStructures-Condititonal-iterative

Total questions: 20

Worksheet time: 10mins

Name
Class
Date
1.

Which of these commands adds a new item, "banana," to the end of the list fruits?

a)

fruits.append("banana")

b)

fruits.add("banana")

c)

fruits.insert("banana")

d)

fruits.push("banana")

2.

Which conditional expression checks if total_sales is over $500?

a)

total_sales == 500

b)

total_sales < 500

c)

total_sales > 500

d)

total_sales != 500

3.

Which of these commands removes the first item from the list 'vegetables'?

a)

vegetables.remove(0)

b)

vegetables.pop(0)

c)

vegetables.delete(0)

d)

vegetables.shift()

4.

Which conditional expression checks if 'username' is not empty?

a)

username == ""

b)

username != ""

c)

username > ""

d)

username < ""

5.

Which loop type is most suitable for iterating through each item in a list of customer names?

a)

while loop

b)

for loop

c)

if statement

d)

switch statement

6.

How do you access the value of "price" in the dictionary product = {"name": "apple", "price": 1.5}?

a)

product["price"]

b)

product(price)

c)

product->price

d)

product.price

7.

Which conditional expression correctly checks if a number x is even?

a)

if x % 2 == 0:

b)

if x % 2 != 0:

c)

if x / 2 == 1:

d)

if x * 2 == 0:

8.

If sales = [200, 400, 500, 300], what function finds the highest sales figure?

a)

max(sales)

b)

min(sales)

c)

sum(sales)

d)

average(sales)

9.

Given a list items = ["apple", "banana", "apple", "orange"], which command counts how many times "apple" appears?

a)

items.count("apple")

b)

items.total("apple")

c)

items.find("apple")

d)

items.check("apple")

10.

Which loop sums only the positive numbers in the list numbers = [-3, 5, -1, 6, 8]?

a)

for num in numbers: if num > 0: total += num

b)

for num in numbers: if num < 0: total += num

c)

for num in numbers: total += num

d)

for total in numbers: if num > 0

11.

What command would you use to sort the list numbers = [3, 1, 4, 1, 5] in ascending order?

a)

numbers.sort()

b)

numbers.order()

c)

sorted(numbers)

d)

numbers.arrange()

12.

Which command would you use to check if the key 'email' exists in the dictionary user_info = {"name": "John", "age": 30}?

a)

'email' in user_info

b)

user_info.has_key('email')

c)

user_info.contains('email')

d)

user_info.check('email')

13.

Which of these commands would create a new list containing only the even numbers from the list numbers = [1, 2, 3, 4, 5]?

a)

even_numbers = [num for num in numbers if num % 2 == 0]

b)

even_numbers = filter(lambda x: x % 2 == 0, numbers)

c)

even_numbers = list(filter(lambda x: x % 2 == 0, numbers))

d)

even_numbers = numbers.even()

14.

If sales = 700, which nested statement prints "High Sales" only if sales > 500 and sales < 1000?

a)

if sales > 500 and sales < 1000: print("High Sales")

b)

if sales > 500 or sales < 1000: print("High Sales")

c)

if sales < 500 and sales < 1000: print("High Sales")

d)

if sales > 500 or sales > 1000: print("High Sales")

15.

How do you change the first item in the list products = ["apple", "banana", "mango"] to "orange"?

a)

products[0] = "orange"

b)

products[1] = "orange"

c)

products.add("orange")

d)

products.replace("apple", "orange")

16.

What command would you use to reverse the order of the list colors = ["red", "green", "blue"]?

a)

colors.reverse()

b)

colors.invert()

c)

reversed(colors)

d)

colors.flip()

17.

Which command would you use to remove the key 'age' from the dictionary user_info = {"name": "John", "age": 30}?

a)

del user_info['age']

b)

user_info.remove('age')

c)

user_info.pop('age')

d)

user_info.delete('age')

18.

Which conditional expression checks if a variable 'status' is equal to 'active'?

a)

status == 'active'

b)

status != 'active'

c)

status > 'active'

d)

status < 'active'

19.

Which command would you use to find the length of the list items = ["apple", "banana", "orange"]?

a)

len(items)

b)

items.length()

c)

items.size()

d)

count(items)

20.

What command would you use to convert the string "123" to an integer?

a)

int("123")

b)

str("123")

c)

float("123")

d)

number("123")