Python
True We can create different level of headers or sections, sub-sections etc. by using multiple # (such as #, ##, ###)
# at the start of text cell is used to create different levels of headers such as header, sub-header, sub-sub-header, etc True False
True Anything to the right of # sign in a code cell is not run in Python because Python treats it as a comment and does not execute that cell.
Anything to the right of # sign in a code cell does not run in Google Colab True False
Key:Value
Dictionaries are used to store data values in _______ pairs.
immutable
Does range() give you a mutable or immutable sequence?
str The type function is used to print the type of variable. So in this case the variable is "Hi" which is by default stored as a string variable. Therefore the output will be printed as str.
Given, a ="Hi" What will be the output of type(a)? Hi str float None of the above
attributes = { "Brand": "Apple", "RAM (in GB)": 4, "Storage (in GB)": 128, "Price (in $): 800, } print(attributes)
How are dictionaries defined? Write one for: Brand: Apple, RAM (in GB): 4, and Price (in $): 800
use type(name_of_the_variable)
How do you inspect the variable?
5
How many times the following loop will run? i=5 while(i>0): i=i-1 5 4 infinite 0
type(name_of_the_variable) The type() function can be used as type(name_of_the_variable) to get the type of variable. float() function is used to convert the variable to float whereas int() is used to convert the variable to an integer.
How to inspect the type of variable in Python? float(name_of_the_variable) int(name_of_the_variable) var(name_of_the_variable) type(name_of_the_variable)
All of the mentioned
Identify the advantage of Google Colaboratory among the following Google Colaboratory consists of many pre-installed libraries It becomes easy to share the work using Google Colaboratory Google Colaboratory uses the cloud for computation All of the mentioned
decimals discount = .045
In Python, what is a float? Give an example.
Text that can be written with ' or " quotes brand = "Apple"
In Python, what is a string? Give an example.
Numbers with NO decimal price = 900
In Python, what is an integer? Give an example.
*args
Python allows us flexibility in terms of the number of arguments passed to a function by using ______.
We have to be very careful about spacing. (each tab is 4 spaces)
Python is a tabbed based language. What does this mean?
True
The lambda function is generally used when we require an anonymous function for a small task True False
if-elif-else statement
What do we use to do multiple conditional statements?
if-else statement
What do we use to do single conditional statements?
# or ' ' '
What symbols do we use to write a comment?
Error
What will be the output of the following code? a = b = TRUEif (a == b): print("Hello")else: print("Well done") Hello Well done TRUE Error
Error Here a is by default stored as a string variable. When we use function int(a) it tries to convert the non-numeric string into an integer and that's not possible because of which Python will prompt a value error.
What will be the output of the following code? a="Hi"b=int(a)type(b) float int str Error
[0, 1, 2, 3]
What will be the output of the following code? def func(a): print(a) A = [0,1,2,3] func(A) 'A' [0, 1, 2, 3] a Error
5 6 The function will take two arguments, set the first argument's value to 5, set the second argument's value to 6, and then return the arguments. Irrespective of the arguments passed to the function, the output will always be 5 6
What will be the output of the following code? def function(var1=2, var2=4): var2 = 6 var1 = 5 print(var1, var2) function(10, 20) 2 4 5 6 10 24 12 20
6 As we have specified *numbers, Python will expect one or more positional arguments. The for loop will iterate over these arguments and keep multiplying them to the variable result. The final value of result will be returned by the function.
What will be the output of the following code? def total_addition(*numbers): addition = 0 for number in numbers: addition += number return additiontotal_addition(1,2,3) addition 1, 2, 3 6 0
I am 25 years old In Python two strings can be added together using the + operator.
What will be the output of the following code? print("I am " + "25 years old") I am 25 years old I am25 years old "I am " + "25 years old"
I am 25 years old By default, Python considers whitespace as the separator between the different values passed to the print() function. Therefore, the values in the code will be printed with whitespace between the values passed. The output of the code will be I am 25 years old
What will be the output of the following code? print("I am",25,"years old") I am25years old I am 25 years old "I am",25,"years old" None of the above
16 Python will first calculate the value of 4*4 as 16, and then the calculated value will be printed as the output.
What will be the output of the following code? print(4*4) 4*4 16 Error 256
range(0, 5)
What will be the output of the following code? print(range(5)) range(0, 5) range 5 0, 1, 2, 3, 4 0, 1, 2, 3, 4, 5
Colaboratory
Which of the following app is used to open .ipynb file? Colaboratory Python Collabrify None of the mentioned
Code Cells Code cells are used to write the codes whereas text cells are used to specify heading, descriptions etc.
Which of the following cell can execute Python code? Text cells Code cells
True
A function can be defined inside another function in Python. True False
True
A function is a block of instructions that performs a specific task. Functions break the program into modular chunks which can be reused later. True False
Tuples are immutable while lists are mutable. The entries in the Tuple can not be changed as they are immutable whereas entries in the List can be modified hence are called mutable.
Identify the correct option with respect to Python Both tuples and lists are mutable. Tuples are immutable while lists are mutable. Both tuples and lists are immutable. Tuples are mutable while lists are immutable.
B. The addition of 4 and 3 is not correct Hello
Identify the correct output of the following code if 4 + 3 == 9: print("The addition of 4 and 3 is correct")else: print("The addition of 4 and 3 is not correct")print("Hello") A. The addition of 4 and 3 is correct Hello B. The addition of 4 and 3 is not correct Hello C. The addition of 4 and 3 is not correct D. Hello
All of the above
Identify the correct statement for the following dictionary X={1:"USA", 2:"India", 3:"China"} 1, 2 and 3 are keys of dictionary USA, India and China are the values of dictionary There are total three items in the dictionary All of the above
10
Identify the output of the following code sum = 0 for number in range(1,5): sum = sum + number print(sum) sum 15 10 0
True
If the if-elif-else statement is used when we have multiple conditions to execute, the given statement will be:
False Variables need not be declared in advance in Python. To create a variable, we just assign it a value.
In Python, a variable must be declared before it is assigned a value True False
A true or false statement, it must have a capital first letter example = True example = False
In Python, what is a boolean? Give an example.
A. When you don't know the exact number of iterations and need to continue until a condition is met
In which scenario would you use a 'while loop' instead of a 'for loop'? A. When you don't know the exact number of iterations and need to continue until a condition is met B. When you have a fixed number of iterations and want to iterate over a sequence C. When you want to execute code only once D. When you want to check conditions and make decisions
after
Keyword arguments should be declared ____ the positional arguments.
To pass a variable number of arguments to a function
What are args and kwargs used for in Python? To pass a variable number of arguments to a function To define the arguments of a function To specify the return type of a function To handle exceptions in a function
The inputs into a function that allow users to pass values to the function.
What are function parameters? The inputs into a function that allow users to pass values to the function. The outputs of a function that are returned to the user. The internal components of a function that define its behavior. The documentation and comments within a function.
Markdown cells and code cells In Google Colab, there are two types of cells: Code Cells: Code cells are used to write and execute code. They allow you to write and run Python code interactively. You can enter multiple lines of code within a code cell and execute them by pressing Shift+Enter or by clicking the Play button. Code cells can also display the output of the executed code. Text Cells: Text cells, also known as Markdown cells, are used for documentation and explanatory text. You can write formatted text, including headings, lists, links, and images, using Markdown syntax. Text cells are useful for providing explanations, documenting code, adding comments, or writing notes. Markdown cells do not execute code; they are purely for displaying text and formatting it in a readable manner.
What are the two types of cells in Google Colab? Markdown cells and code cells Formatted text cells and code cells Text cells and code cells Header cells and code cells
[2, 3, 4, 5]
What does print(list(range(2,6))) give you?
[5, 10, 15, 20] first 5: this is where the list return will start 21: ending at the value 21 second 5: the return will increment by 5
What does print(list(range(5, 21, 5))) give you? What does each number represent?
[6, 8, 10, 12]
What does print(list(range(6, 14, 2))) give you?
Only the code corresponding to the first true condition is executed
What happens if multiple conditions in a conditional block are true? Only the code corresponding to the first true condition is executed All code corresponding to true conditions is executed The code corresponding to the last true condition is executed The code corresponding to the false conditions is executed
Each condition is tested sequentially until a true condition is found or the else statement is reached
What happens if multiple elif statements are defined in an if-elif-else block? Each condition is tested sequentially until a true condition is found or the else statement is reached All conditions are tested simultaneously Only the first condition is tested Only the last condition is tested
The code inside the loop will not be executed
What happens if the condition in a 'while loop' is never met? The code inside the loop will not be executed The loop will continue indefinitely The loop will exit immediately The loop will throw an error
Comments are not executed and are used for documentation purposes When you execute code with comments in Python, the comments are completely ignored by the Python interpreter. They have no impact on the execution of the code or the program's output.
What happens when you execute code with comments in Python? Comments are not executed and are used for documentation purposes Comments are executed as part of the code Comments are executed and displayed as output Comments cause an error and stop the execution of the code
You get a TypeError In Python, tuples are immutable, which means that once a tuple is created, you cannot change the value of an item within it. If you attempt to modify or assign a new value to an item in a tuple, it will result in a TypeError being raised.
What happens when you try to change the value of an item in a tuple? You get a TypeError The value of the item is changed The item is removed from the tuple The tuple is resized
A way to map one value to another value using key-value pairs.
What is a dictionary? A way to map one value to another value using key-value pairs. A way to store data for a single attribute. A way to store multiple values in a single variable. A way to store complex information using lists.
An argument defined by using a name and a default value
What is a keyword argument in Python? An argument defined by using a name and a default value An argument that is required in a function call An argument that is passed by reference An argument that is used for exception handling
.ipynb
What is default file extension of Jupyter Notebook? .py .npy .ipynb .pynb
It allows for a flexible number of keyword arguments
What is the advantage of using kwargs in a function? It allows for a flexible number of keyword arguments It simplifies the function definition It improves the performance of the function It ensures the correctness of the function
A. Positional arguments are defined by their position, while keyword arguments are defined by their name
What is the difference between positional and keyword arguments in Python? A. Positional arguments are defined by their position, while keyword arguments are defined by their name B. Positional arguments have default values, while keyword arguments are required C. Positional arguments can be passed in any order, while keyword arguments must be passed in a specific order D. Positional arguments are optional, while keyword arguments are required
Tuples are immutable, while lists are mutable Tuples are immutable, meaning they cannot be changed, while lists are mutable.
What is the key difference between tuples and lists? Tuples are immutable, while lists are mutable Tuples are mutable, while lists are immutable Tuples can only store integers, while lists can store any data type Tuples can be resized, while lists have a fixed size
A. A list comprehension is shorter and can be more appealing in certain contexts
What is the primary difference between using a for loop and a list comprehension? A. A list comprehension is shorter and can be more appealing in certain contexts B. A list comprehension allows for defining mathematical operations C. A for loop is shorter and can be more appealing in certain contexts D. A for loop allows for defining mathematical operations
To specify what should be returned from a function to the user.
What is the purpose of a return statement in a function? To specify what should be returned from a function to the user. To define the inputs of a function. To break a program into modular chunks. To organize and manage a program.
To perform repetitive tasks efficiently
What is the purpose of looping statements? To perform repetitive tasks efficiently To execute code only once To check conditions and make decisions To define variables and assign values
Keys are used to access the corresponding values in a dictionary.
What is the relationship between keys and values in a dictionary? Keys are used to access the corresponding values in a dictionary. Keys and values are interchangeable in a dictionary. Values are used to access the corresponding keys in a dictionary. Keys and values are unrelated in a dictionary.
'for some value in some sequence, execute some code'
What is the standard syntax for a 'for loop' in Python? 'for some value in some sequence, execute some code' 'while some condition is true, perform some operations' 'if some condition is true, execute some code' 'try to execute some code, and if an error occurs, handle it'
[expression for item in iterable]
What is the syntax for a list comprehension? [expression for item in iterable] [item for expression in iterable] [expression in iterable for item] [item in iterable for expression]
Tuple The class of the variable can be determined by using the type() method. We just give input to Python as type(variable_name) and Python returns the output as the class of the variable.
What is the type of data structure shown below? X = (10,"Range", "Great", -54, 11, 12) Tuple List Dictionary None of the above
120 As we have specified *int, Python will expect one or more positional arguments. The for loop will iterate over these arguments and keep multiplying them to the variable result. The final value of result will be returned by the function.
What will be the output of the code below? def fun(*int): result = 1 for x in int: result *= x return result fun(1,2,3,4,5) 1*2*3*4*5 1, 2, 3, 4, 5 120 1
Error The code in the question raises an error the keyword arguments (kwargs) are specified before the positional arguments (args). The correct way to declare a function with kwargs and args is as follows: def my_function(a, b, *args, **kwargs): return a+b my_function(5,6)
What will be the output of the code below? def my_function(a, b, **kwargs, *args) :return a+b my_function(5,6) Error 5+6 a+b 11
A. [1, 2, 3, 4, 5]
What will be the output of the code below? list_1 = [1, 2, 3, 4, 5]list_comp = [ i for i in list_1 ]print(list_comp) A. [1, 2, 3, 4, 5] B. 1 2 3 4 5 C. 1 2 3 4 5 D. Error
How are youHow are you
What will be the output of the following Python code? def say(message, times): print(message*times) say("How are you", 2) How are you, 2 How are youHow are you How are you, times message, 2
Error Tuples cannot be modified, so the pop() method does not work here. Python will raise an Attribute Error in this case.
What will be the output of the following code snippet? X = (20,23,-50,5.6,98) X.pop(2) print(X) (20,23,5.6,98) (20,-50,5.6,98) Error (20,23,-50,5.6,98)
int a is 4.5 so by default Python will take it as float. When we are using the function int(a) Python does the conversion of a variable from its parent type to a new type.
What will be the output of the following code snippet? a=4.5b=int(a)type(b) float int str Error
(-50,5.6) Using X[2:-1] Python select the elements having indexes 2, and 3 (-1 is an exclusive index so it will be ignored). So here elements at index positions 2 and 3 are -50 and 5.6.
What will be the output of the following code? X = (20,23,-50,5.6,98) print(X[2:-1]) (-50, 23) (-50, 5.6) (23, -50, 5.6, 98) Error
[11] X[-2:-1] means selecting the elements from index -2 (inclusive) to -1 (exclusive) from the list. So the element at index position -2 means 4 is 11. Therefore the correct answer is [11].
What will be the output of the following code? X = [10,"Range", "Great", -54, 11, 12]print(X[-2:-1]) [] [12] [11,12] [11]
Range With X[1], Python will select the element at index 1 from the list X, which is "Range" in this case.
What will be the output of the following code? X = [10,"Range", "Great", -54, 11, 12]print(X[1]) 10 Range Correct Option 'Range' Error
['Great', -54, 11] X[2:-1] means selecting the elements from index 2 (inclusive) to -1 (exclusive) from the list. So, the elements at index positions 2, 3, 4 are fetched, and X[2:-1] will give ['Great', -54, 11].
What will be the output of the following code? X = [10,"Range", "Great", -54, 11, 12]print(X[2:-1]) ["Range", 10] ["Range", "Great"] ['Great', -54, 11] ["Great", "Range"]
2 X[2:4] means selecting the elements from index 2 (inclusive) to 4 (exclusive) from the list. So, the elements at index positions 2 and 3 are fetched, and X[2:4] will give ["Great", -54]. The len() function is used to calculate the length of the list. ["Great", -54] has two elements in it, so len(X[2:4]) will return 2.
What will be the output of the following code? X = [10,"Range", "Great", -54, 11, 12]print(len(X[2:4])) 1 2 3 4
a is of type <class 'int'> and c is of type <class 'float'> The variable a=30 is an integer, whereas the variable c=a/b which indicates that calculate division of the two variables a and b and return the output as float.
What will be the output of the following code? a = 30b = 8c = a/bprint("a is of type",type(a), "and c is of type", type(c)) a is of type <class 'int'> and c is of type <class 'float'> a is of type <class 'int'> and c is of type <class 'int'> a is of type <class float'> and c is of type <class 'float'> None of the above
{1: 'USA', 3: 'China'}
What will be the output of the following code? dictionary={1:"USA", 2:"India", 3:"China"}dictionary.pop(2)print(dictionary) {3:"China"} {1: 'USA', 2: 'India'} {1: 'USA', 3: 'China'} Error
{1: 'USA', 2: 'India', 3: 'Japan'}
What will be the output of the following code? dictionary={1:"USA", 2:"India", 3:"China"}dictionary.update({3:"Japan"})print(dictionary) {3:"Japan"} {1:"USA", 2:"India", 3:"China"} {1: 'USA', 2: 'India', 3: 'Japan'} Error
B. 2 4 6 8
What will be the output of the following code? for i in range(1,5):print(i*2) A. i*2 B. 2 4 6 8 C. [1, 2, 3, 4] D. 2
Error The above code will raise an error because lambda functions cannot use the return statement. Hence, an error for invalid syntax will be raised
What will be the output of the following code? mul = lambda a,b: return a*b mul(5,6) 30 5*6 30.0 Error
C. 1 2 3 4 5
What will be the output of the following code? x = [1, 2, 3, 4, 5] while x: print(x.pop(0)) A. 1 2 3 4 B. 1 2 3 4 5 C. 1 2 3 4 5 D. Error
15.0 A Python lambda expression consists of - A keyword: lambda - A bound variable: x - A body: (x+2)*5/2 The parentheses after the lambda function definition containing 4 specify the argument to be passed to the function. Python executes the body of the lambda function by substituting argument 4 for x.
What would be the output of the following code snippet? (lambda x: (x+2)*5/2)(4) 4.0 15 15.0 Error
if a > 2:
Which is the valid conditional statement among the following to check if a is greater than 2? if a > 2 if (a > 2) if a > 2: if "a > 2"
elif
Which keyword do we use to add alternative conditions for the if statement? else if elseif elif All of the above
week_list = [ "Week" + str(i) for i in range(5)] print(week_list)
Which of the following codes will print the output below? ['Week0', 'Week1', 'Week2', 'Week3', 'Week4'] week_list = [ "Week" + "i" for i in range(5)] print(week_list) week_list = [ "Week" + i for i in range(5)] print(week_list) week_list = [ Week + str(i) for i in range(5)] print(week_list) week_list = [ "Week" + str(i) for i in range(5)] print(week_list)
Both True and False Boolean is a type of variable which can take two values, True and False.
Which of the following is a Boolean variable? True False Both True and False None of the above
def fun(*args):return args
Which of the following is the correct function declaration that can execute both the function calls mentioned below ? fun(1, 2, 3) fun(23, 24) def fun(args*):return args def fun(args):return args def fun(*args):return args def fun(*args*):return args
string_variable + str(integer_variable) The 'str()' function is used to convert the integer variable to a string before combining it with the string variable.
Which of the following is the correct way to combine a string and an integer variable in Python? string_variable + str(integer_variable) Correct Option string_variable + integer_variable integer_variable + string_variable str(integer_variable) + string_variable
number=5 To declare a variable assignment operator '=' is used. So the correct answer is number=5.
Which of the following statement represents the correct method to declare or define the variable? number=5 let number is 5 number-5 number#5
A is a positional argument and B is a keyword argument Argument A does not have a default value and is specified first, so it is a positional argument. Argument B has a default value specified, so it is a keyword argument.
Which of the following statements is correct for the code below? def fun(A, B=30): return A + B A is a keyword argument and B is a positional argument A and B are both keyword arguments A is a positional argument and B is a keyword argument A and B are both positional arguments
Boolean
Which of the given options is not a type of data structure in Python? Tuple Boolean Dictionary List
Loops
_____ are used in Python to iterate over a sequence. Loops Range Both A and B None of the above
*kwargs
______ is used to pass a variable number of keyword arguments to a Python function.
The price after providing 5 percent discount is $855.0 The price after providing 10 percent discount is $810.0 The price after providing 15 percent discount is $765.0 The price after providing 20 percent discount is $720.0
print(list(range(5, 21, 5))) ------>output is [5, 10, 15, 20] What will this give you? price = 900 for i in range(5, 21, 5): discount = prince * (i / 100) discounted_price = price - discount print( "The price after providing ", i, " percent discount is $", discounted_price, sep="", )