CS Exam #2

¡Supera tus tareas y exámenes ahora con Quizwiz!

What is the purpose of the continue statement in a loop?

It skips the rest of the current iteration and goes to the next iteration.

What is the difference between the "equal to" operator and the "assignment operator"?

The equal to operator (==) compares two values and returns True if they are equal, while the assignment operator (=) assigns a value to a variable.

What will happen if you try to access a local variable outside its function?

The program will throw an error.

What is the output of the following code: def add(x, y): return x + y print(add(3))

TypeError: add() missing 1 required positional argument: 'y'

How can you define a function that takes an arbitrary number of arguments?

Use *args in the function definition.

What is the difference between break and continue?

break breaks out of the loop, and continues on to the next line of code AFTER the loop. continue breaks out of the loop's current iteration, and continues onto the first line of code in the next iteration.

What is the correct way to define a function in Python?

def my_function():

Which method is used to get all the keys in a dictionary?

dict.keys()

What is the data type of the return value of the .keys() method of dictionaries?

dict_keys

What is the output of the following code? s = "Python" print(s[2:5])

'tho'

Given the following dictionary: dict1 = {"Tony":100, "Jack":200, "Jill":120, "Ronald":140} What will the following line of code output? print(dict1.get("Sam",0))

0

What is the output of the following code? my_list = [1, 0, 7] print(my_list[1])

0

What is the output of the following code? for i in range(3): print(i)

0 1 2

What is the output of the following code? my_dict = {'a': 1, 'b': 2} print(my_dict['a'])

1

What will the following code output? def multiply(a, b=2): return a * b print(multiply(4, 3))

12

What is the output of the following code? my_list = [1, 2, 3] print(len(my_list))

3

What will the following code output? s = "Hello" print(len(s))

5

What is the output of the following code: def add(x, y=5): return x + y print(add(3))

8

What will the following code output? def funFunction(target, arg1 = ""): arg1 = arg1 + str(target) return arg1 for x in range(10): fun = funFunction(x) print(fun)

9

What is a Python dictionary?

A collection of key-value pairs.

Which of the following statements is true about functions in Python?

A function can return multiple values as a tuple.

How are strings defined in Python?

By enclosing characters in double quotes " or single quotes '.

What will the following code output? def greet(name): print(f"Hello, {name}!") greet("Alice")

Hello, Alice!

What is the output of the following code? s = "Python" print(s[1])

Index Error

Which of the following is true about Python's while loop?

It continues executing as long as a specified condition is True.

Which statement best describes the else clause in a Python for loop?

It is executed only if the loop completes all its iterations without a break.

What is the purpose of the break statement in a loop?

It stops the entire loop execution.

What is the output of the following code? my_dict = {'A': 1, 'B': 2} print(my_dict['a'])

KeyError: 'a'

What is the main difference between a tuple and a list?

Lists are mutable and Tuples are not.

What is the default return value of a Python function if no return statement is provided?

None

What value does the following function call return? print("Hello World!")

None

What is the data type of None?

NoneType

What will the following code output? s = "Python" s.upper() print(s)

Python

Which of the following are built in Data Types in Python

String, Integer, and Float

What should the following Expression evaluate to: 'I have eaten ' + 99 + ' burritos.'

Throws an error, you can't add Integers and Strings.

What is the purpose of the return statement in a Python function?

To send a value back to the caller of the function.

What does the following code output? s = "Python Programming" print(s.split())

['Python', 'Programming']

What does the following code output? my_list = [1, 2, 3, 4] my_list.pop(2) print(my_list)

[1, 2, 4]

What will the following code output? def funFunction(target, arg1 = []): arg1 = arg1 + [target] return arg1 for x in range(10): fun = funFunction(x) print(fun)

[9]

What will the following code output? def funFunction(target, arg1 = []): arg1 = arg1 + target return arg1 for x in range(10): fun = funFunction(x) print(fun)

[9]

How can you add an element to the end of a list in Python?

list.append(item)

Which method is used to convert a string to lowercase?

lower()

What is the output of the following code? s = "Python" print(s[-1])

n

Which of the following methods removes an element from a list by its value?

remove()

What function would you use to convert a value of the integer type into a string?

str()

Which of the following methods removes whitespace from the beginning and end of a string?

strip()

How can you check if a substring exists in a string?

substring in string

What will the following code output? my_dict = {'x': 5, 'y': 10} my_dict['z'] = 15 print(my_dict)

{'x': 5, 'y': 10, 'z': 15}


Conjuntos de estudio relacionados

matematikos pagringai savokos (57-87)

View Set

Name the 80s Song! (Using the Lyrics)

View Set

chapter 8: receivables, bad debt expense, and interest revenue

View Set