Python MCQs

Python is an object-oriented, high-level, interpreted programming language with dynamic semantics. Python has a rich set of high-level in-built data structures (data types) which combined with dynamic typing and data typing. It makes Python a very popular and attractive programming language for rapid application development, scripting.

Python MCQs: This section contains multiple-choice questions and answers on the Python programming language. It will help the students to test their skills and prepare well for their exams.

List of Python MCQs

1. Python is a ___object-oriented programming language.

  1. Special purpose
  2. General purpose
  3. Medium level programming language
  4. All of the mentioned above

Answer: B) General purpose

Explanation:

As a General Purpose Object-Oriented Programming Language, Python can model real-world entities, which makes it a useful tool for data scientists. Because it performs type checking at runtime, it is also known as dynamically typed code. Python is a general-purpose programming language, which means that it is widely used in every domain. This is due to the fact that it is very simple to understand and scalable, which allows for rapid development.

Discuss this Question


2. Amongst the following, who is the developer of Python programming?

  1. Guido van Rossum
  2. Denis Ritchie
  3. Y.C. Khenderakar
  4. None of the mentioned above

Answer: A) Guido van Rossum

Explanation:

Python programming was created by Guido van Rossum. It is also called general-purpose programming language.

Discuss this Question


3. Amongst which of the following is / are the application areas of Python programming?

  1. Web Development
  2. Game Development
  3. Artificial Intelligence and Machine Learning
  4. All of the mentioned above

Answer: D) All of the mentioned above

Explanation:

Python programming is used in a variety of fields, including web development, game development, artificial intelligence, and machine learning, among others. Web Development - Python provides a number of web development frameworks, including Django, Pyramid, and Flask, among others. Security, flexibility, and scalability are all attributes of this framework. Development of Video Games - PySoy and PyGame are two Python libraries that are used in the development of video games. Artificial Intelligence and Machine Learning - There are a large number of open-source libraries that can be used when developing AI/ML applications, and many of these libraries are free.

Discuss this Question


4. Amongst which of the following is / are the Numeric Types of Data Types?

  1. int
  2. float
  3. complex
  4. All of the mentioned above

Answer: D) All of the mentioned above

Explanation:

Numeric data types include int, float, and complex, among others. In information technology, data types are the classification or categorization of knowledge items. It represents the type of information that is useful in determining what operations are frequently performed on specific data. In the Python programming language, each value is represented by a different python data type. Known as Data Types, this is the classification of knowledge items or the placement of the information value into a specific data category. It is beneficial to be aware of the quiet operations that are frequently performed on a worth.

Discuss this Question


5. list, tuple, and range are the ___ of Data Types.

  1. Sequence Types
  2. Binary Types
  3. Boolean Types
  4. None of the mentioned above

Answer: A) Sequence Types

Explanation:

The sequence Types of Data Types are the list, the tuple, and the range. In order to store multiple values in an organized and efficient manner, we use the concept of sequences. There are several types of sequences, including strings, Unicode strings, lists, tuples, bytearrays, and range objects. Strings and Unicode strings are the most common. Dictionary and set data structures are used to store non-sequential information.

Discuss this Question


6. Float type of data type is represented by the float class.

  1. True
  2. False

Answer: A) True

Explanation:

The float data type is represented by the float class of data types. A true number with a floating-point representation is represented by the symbol. It is denoted by the use of a decimal point. Optionally, the character e or E followed by a positive or negative integer could be appended to the end of the string to indicate scientific notation.

Discuss this Question


7. bytes, bytearray, memoryview are type of the ___ data type.

  1. Mapping Type
  2. Boolean Type
  3. Binary Types
  4. None of the mentioned above

Answer: C) Binary Types

Explanation:

The Binary type's data type is represented by the bytes, byte array, and memory view types. Binary data manipulation is accomplished through the use of bytes and byte array. The memory view makes use of the buffer protocol in order to access the memory of other binary objects without the need to make a copy of the data. Bytes objects are immutable sequences of single bytes that can only be changed. When working with ASCII compatible data, we should only use them when necessary.

Discuss this Question


8. The type() function can be used to get the data type of any object.

  1. True
  2. False

Answer: A) True

Explanation:

The type() function can be used to find out what type of data an object contains. Typing an object passed as an argument to Python's type() function returns the data type of the object passed as an argument to Python's type() function. This function is extremely useful during the debugging phase of the process.

Discuss this Question


9. Binary data type is a fixed-width string of length bytes?

  1. True
  2. False

Answer: A) True

Explanation:

It is a fixed-width string of length bytes, where the length bytes is declared as an optional specifier to the type, and its width is declared as an integer. If the length is not specified, the default value is 1. When necessary, values are right-extended to fill the entire width of the column by using the zero byte as the first byte.

Discuss this Question


10. Varbinary data type returns variable-width string up to a length of max-length bytes?

  1. TRUE
  2. FALSE

Answer: A) TRUE

Explanation:

Varbinary - a variable-width string with a length of max-length bytes, where the maximum number of bytes is declared as an optional specifier to the type, and where the maximum number of bytes is declared as an optional specifier to the type. The default attribute size is 80 bytes, and the maximum length is 65000 bytes. The default attribute size is 80 bytes. The range of binary values is not extended to fill the entire width of the column.

Discuss this Question


11. Amongst which of the following is / are the logical operators in Python?

  1. and
  2. or
  3. not
  4. All of the mentioned above

Answer: D) All of the mentioned above

Explanation:

Python's logical operators are represented by the terms and, or, and not. In Python, logical operators are used to perform logical operations on the values of variables that have been declared. Either true or false is represented by the value. The truth values provide us with the information we need to figure out the conditions. In Python, there are three types of logical operators: the logical AND, the logical OR, and the logical NOT operators. Keywords or special characters are used to represent operators in a program.

Discuss this Question


12. Is Python supports exception handling?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

Unexpected events that can occur during a program's execution are referred to as exceptions, and they can cause the program's normal flow to be interrupted. Python provides exception handling, which allows us to write less error-prone code while also testing various scenarios that may result in an exception later on in the process.

Discuss this Question


13. What is the name of the operator ** in Python?

  1. Exponentiation
  2. Modulus
  3. Floor division
  4. None of the mentioned above

Answer: A) Exponentiation

Explanation:

The ** is an exponentiation operator in the Python programming language. In Python, the ** operator is used to raise the number on the left to the power of the exponent on the right, which is represented by the symbol **. In other words, in the expression 2 ** 3, 2 is raised to the third power, which is a positive number. In mathematics, we frequently see this expression written as 23, but what is really happening is that the numbers 2 and 3 are being multiplied by themselves three times. In Python, we would get the same result of 8 by running either 2 ** 3 or 2 * 2 * 2.

Discuss this Question


14. The % operator returns the ___.

  1. Quotient
  2. Divisor
  3. Remainder
  4. None of the mentioned above

Answer: C) Remainder

Explanation:

The % operator (it is an arithmetic operator) returns the amount that was left over. This is useful for determining the number of times a given number is multiplied by itself.

Discuss this Question


15. Amongst which of the following is / are the method of list?

  1. append()
  2. extend()
  3. insert()
  4. All of the mentioned above

Answer: D) All of the mentioned above

Explanation:

list.append(x), list.extend(iterable), list.insert(i, x) are the methods of list. list.append(x) - add an item to the end of the list. list.extend(iterable) - extend the list by appending all the items from the iterable. list.insert(i, x) Insert an item at a given position.

Discuss this Question


16. The list.pop ([i]) removes the item at the given position in the list?

  1. True
  2. False

Answer: A) True

Explanation:

The external is not a valid variable scope in PHP.

Discuss this Question


17. The list.index(x[, start[, end]]) is used to ___.

  1. Return zero-based index in the list
  2. Raises a ValueError if there is no such item
  3. Both A and B
  4. None of the mentioned above

Answer: C) Both A and B

Explanation:

The index(x[, start[, end]]) is used to return the zero-based index in the list of the first item whose value is equal to x. index() is used to return the zero-based index in the list of the first item whose value is equal to x. If there is no such item, the method raises a ValueError. The optional arguments start and end are interpreted in the same way as in the slice notation and are used to restrict the search to a specific subsequence of the list of elements. Instead of using the start argument to calculate the index, the returned index is computed relative to the beginning of the full sequence.

Discuss this Question


18. Python Dictionary is used to store the data in a ___ format.

  1. Key value pair
  2. Group value pair
  3. Select value pair
  4. None of the mentioned above

Answer: A) Key value pair

Explanation:

Python Dictionary is used to store the data in a key-value pair format, which is similar to that of a database. The dictionary data type in Python is capable of simulating the real-world data arrangement in which a specific value exists for a specific key when the key is specified. It is the data-structure that can be changed. Each element of the dictionary is defined as follows: keys and values.

Discuss this Question


19. The following is used to define a ___.

d = {
	<key>: <value>,
	<key>: <value>,
	.
	.
	.
	<key>: <value>
}
  1. Group
  2. List
  3. Dictionary
  4. All of the mentioned above

Answer: C) Dictionary

Explanation:

With the help of curly braces (), we can define a dictionary that contains a list of key-value pairs that are separated by commas. Each key and its associated value are separated by a colon (:). For example:

d = {
	<key>: <value>,
	<key>: <value>,
	.
	.
	.
	<key>: <value>
}

Discuss this Question


20. Python Literals is used to define the data that is given in a variable or constant?

  1. True
  2. False

Answer: A) True

Explanation:

It is possible to define literals in Python as data that is provided in a variable or constant. Literal collections are supported in Python as well as String and Numeric literals, Boolean and Boolean expressions, Special literals, and Special expressions.

Discuss this Question


21. Conditional statements are also known as ___ statements.

  1. Decision-making
  2. Array
  3. List
  4. None of the mentioned above

Answer: A) Decision-making

Explanation:

Conditional statements, also known as decision-making statements, are used to make decisions. In programming, we want to be able to control the flow of execution of our program, and we want to be able to execute a specific set of statements only if a specific condition is met, and a different set of statements only if the condition is not met. As a result, we use conditional statements to determine whether or not a specific block of code should be executed based on a given condition.

Discuss this Question


22. The if statement is the most fundamental decision-making statement?

  1. True
  2. False

Answer: A) True

Explanation:

The if statement is the most fundamental decision-making statement, and it determines whether or not the code should be executed based on whether or not the condition is met. If the condition in the if statement is met, a code body is executed, and the code body is not otherwise executed. The statement can be as simple as a single line of code or as complex as a block of code.

Discuss this Question


23. Amongst which of the following if syntax is true?

  1. if condition:
        #Will executes this block if the condition is true
    
  2. if condition
    {
        #Will executes this block if the condition is true
    }
    
  3. if(condition)
        #Will executes this block if the condition is true
    
  4. None of the mentioned above

Answer: A)

if condition:
    #Will executes this block if the condition is true

Explanation:

If is a keyword which works with specified condition. If statement in Python has the subsequent syntax:

if condition:
    #Will executes this block if the condition is true 

Discuss this Question


24. Amongst which of the following is / are the conditional statement in Python code?

  1. if a<=100:
  2. if (a >= 10)
  3. if (a => 200)
  4. None of the mentioned above

Answer: A) if a<=100:

Explanation:

The if statement in Python is used to make decisions in various situations. It contains a body of code that is only executed when the condition specified in the if statement is true; if the condition is not met, the optional else statement is executed, which contains code that is executed when the else condition is met.

Discuss this Question


25. Which of the following is not used as conditional statement in Python?

  1. switch
  2. if...else
  3. elif
  4. None of the mentioned above

Answer: A) switch

Explanation:

Python does not have a switch or case statement like other programming languages. Because Python lacks switch statement functionality in comparison to other programming languages, it is not recommended for beginners. As a result, we use other alternatives that can replace the functionality of the switch case statement and make programming easier and faster. We employ dictionary mapping to get around this limitation.

Discuss this Question


26. Which of the following is false regarding conditional statement in Python?

  1. If-elif is the shortcut for the if-else chain
  2. We use the dictionary to replace the Switch case statement
  3. We cannot use python classes to implement the switch case statement
  4. None of the mentioned above

Answer: C) We cannot use python classes to implement the switch case statement

Explanation:

It is possible to shorten the if-else chain by using the if-elif construct. Use the if-elif statement and include an else statement at the end, which will be executed if none of the if-elif statements in the previous section are true. As a replacement for the Switch case statement, we use the dictionary data type, whose key values function similarly to those of the cases in a switch statement. When implementing the switch case statement in Python, we can make use of Python classes. A class is a type of object function Object() { [native code] } that can be extended with properties and methods. So, let's look at an example of how to perform a switch case using a class by creating a switch method within the Python switch class and then calling it.

Discuss this Question


27. In Python, an else statement comes right after the block after 'if'?

  1. True
  2. False

Answer: A) True

Explanation:

After the 'if' condition, an else statement is placed immediately after the block. if-else statements are used in programming in the same way that they are used in the English language. The following is the syntax for the if-else statement:

if(condition):
    Indented statement block for when condition is TRUE
else:	
    Indented statement block for when condition is FALSE

Discuss this Question


28. In a Python program, Nested if Statements denotes?

  1. if statement inside another if statement
  2. if statement outside the another if statement
  3. Both A and B
  4. None of the mentioned above

Answer: A) if statement inside another if statement

Explanation:

Nesting an if statement within another if statement is referred to as nesting in the programming community. It is not always necessary to use a simple if statement; instead, you can combine the concepts of if, if-else, and even if-elif-else statements to create a more complex structure.

Discuss this Question


29. What will be the output of the following Python code?

a=7
if a>4: print("Greater")
  1. Greater
  2. 7
  3. 4
  4. None of the mentioned above

Answer: A) Greater

Explanation:

When only one statement needs to be executed within an if block, the short hand if statement is used to accomplish this. This statement can be included in the same line as the if statement, if necessary. When using Python's Short Hand if statement, the following syntax is used:

if condition: statement

Discuss this Question


30. What will be the output of the following Python code?

x,y = 12,14

if(x+y==26):
    print("true")
else:
    print("false")
  1. true
  2. false

Answer: A) true

Explanation:

In this code the value of x = 12 and y = 14, when we add x and y the value will be 26 so x+y= =26. Hence, the given condition will be true.

Discuss this Question


31. What will be the output of the following Python code?

x=13

if x>12 or x<15 and x==16:
    print("Given condition matched")
else:
    print("Given condition did not match")
  1. Given condition matched
  2. Given condition did not match
  3. Both A and B
  4. None of the mentioned above

Answer: A) Given condition matched

Explanation:

In this code the value of x = 13, and the condition 13>12 or 13<15 is true but 13==16 becomes falls. So, the if part will not execute and program control will switch to the else part of the program and output will be "Given condition did not match".

Discuss this Question


32. Consider the following code segment and identify what will be the output of given Python code?

a = int(input("Enter an integer: "))
b = int(input("Enter an integer: "))

if a <= 0:
    b = b +1
else:
    a = a + 1
  1. if inputted number is a negative integer then b = b +1
  2. if inputted number is a positive integer then a = a +1
  3. Both A and B
  4. None of the mentioned above

Answer: C) Both A and B

Explanation:

In above code, if inputted number is a negative integer, then b = b +1 and if inputted number is a positive integer, then a = a +1. Hence, the output will be depending on inputted number.

Discuss this Question


33. In Python, ___ defines a block of statements.

  1. Block
  2. Loop
  3. Indentation
  4. None of the mentioned above

Answer: C) Indentation

Explanation:

Python's concept of indentation is extremely important because, if the Python code is not properly indented, we will encounter an Indentation Error and the code will not be able to be successfully compiled. In Python, to indicate a block of code, we must indent each line of the block by the same amount on each line of the block. As a result, indentation denotes the beginning of a block of statements.

Discuss this Question


34. An ___ statement has less number of conditional checks than two successive ifs.

  1. if else if
  2. if elif
  3. if-else
  4. None of the mentioned above

Answer: C) if-else

Explanation:

A single if-else statement requires fewer conditional checks than two consecutives if statements. If the condition is true, the if-else statement is used to execute both the true and false parts of the condition in question. The condition is met, and therefore the if block code is executed, and if the condition is not met, the otherwise block code is executed.

Discuss this Question


35. In Python, the break and continue statements, together are called ___ statement.

  1. Jump
  2. goto
  3. compound
  4. None of the mentioned above

Answer: B) goto

Explanation:

With the goto statement in Python, we are basically telling the interpreter to skip over the current line of code and directly execute another one instead of the current line of code. You must place a check mark next to the line of code that you want the interpreter to execute at this time in the section labelled "target."

Discuss this Question


36. What will be the output of the following Python code?

num = 10

if num > 0:
    print("Positive number")
elif num == 0:
    print("Zero")
else:
    print("Negative number")
  1. Positive number
  2. Negative number
  3. Real number
  4. None of the mentioned above

Answer: A) Positive number

Explanation:

In this case, the If condition is evaluated first and then the else condition. If it is true, the elif statement will be executed. If it is false, nothing will happen. elif is an abbreviation for else if. It enables us to check for multiple expressions at the same time. Similarly, if the condition for if is False, the condition for the next elif block is checked, and so on. If all of the conditions are met, the body of the else statement is run.

Discuss this Question


37. The elif statement allows us to check multiple expressions.

  1. True
  2. False

Answer: A) True

Explanation:

It is possible to check multiple expressions for TRUE and to execute a block of code as soon as one of the conditions evaluates to TRUE using the elif statement. The elif statement is optional in the same way that the else statement is. The difference between elif and else is that, unlike else, where there can only be one statement, elif statements can be followed by an arbitrary number of statements.

Discuss this Question


38. What will be the output of the following Python code?

i=5
if  i>11 : print ("i is greater than 11")
  1. No output
  2. Abnormal termination of program
  3. Both A and B
  4. None of the mentioned above

Answer: C) Both A and B

Explanation:

In the above code, the assign value of i = 5 and as mentioned in the condition if 5 > 11: print ("i is greater than 11"), here 5 is not greater than 11 so condition becomes false and there will not be any output and program will be abnormally terminated.

Discuss this Question


39. What will be the output of the following Python code?

a = 13
b = 15
print("A is greater") if a > b else print("=") if a == b else print("B is greater")
  1. A is greater
  2. B is greater
  3. Both A and B
  4. None of the mentioned above

Answer: B) B is greater

Explanation:

In the above code, the assign value for a = 13 and b = 15. There are three conditions mentioned in the code,

  1. print("A is greater") if a > b , here 13 is not greater than 15 so condition becomes false
  2. print("=") if a == b , here 13 is not equal to 15 so condition becomes false
  3. else print("B is greater"), condition 1 and 2 will not be true so program control will switch to else part and output will be "B is greater".

Discuss this Question


40. If a condition is true the not operator is used to reverse the logical state?

  1. True
  2. False

Answer: A) True

Explanation:

In order to make an if statement test whether or not something occurred, we must place the word not in front of our condition. When the not operator is used before something that is false, it returns true as a result. And when something that is true comes before something that is false, we get False. That is how we determine whether or not something did not occur as claimed. In other words, the truth value of not is the inverse of the truth value of yes. So, while it may not appear to be abstract, this operator simply returns the inverse of the Boolean value.

Discuss this Question


41. Loops are known as ___ in programming.

  1. Control flow statements
  2. Conditional statements
  3. Data structure statements
  4. None of the mentioned above

Answer: A) Control flow statements

Explanation:

The control flow of a program refers to the sequence in which the program's code is executed. Conditional statements, loops, and function calls all play a role in controlling the flow of a Python program's execution.

Discuss this Question


42. The for loop in Python is used to ___ over a sequence or other iterable objects.

  1. Jump
  2. Iterate
  3. Switch
  4. All of the mentioned above

Answer: B) Iterate

Explanation:

It is possible to iterate over a sequence or other iterable objects using the for loop in Python. The process of iterating over a sequence is referred to as traversal. Following syntax can be follow to use for loop in Python Program

for val in sequence:
    ...
    loop body
    ...

For loop does not require an indexing variable to set beforehand.

Discuss this Question


43. With the break statement we can stop the loop before it has looped through all the items?

  1. True
  2. False

Answer: A) True

Explanation:

In Python, the word break refers to a loop control statement. It serves to control the sequence of events within the loop. If you want to end a loop and move on to the next code after the loop; the break command can be used to do so. When an external condition causes the loop to terminate, it represents the common scenario in which the break function is used in Python.

Discuss this Question


44. The continue keyword is used to ___ the current iteration in a loop.

  1. Initiate
  2. Start
  3. End
  4. None of the mentioned above

Answer: C) End

Explanation:

The continue keyword is used to terminate the current iteration of a for loop (or a while loop) and proceed to the next iteration of the for loop (or while loop). With the continue statement, you have the option of skipping over the portion of a loop where an external condition is triggered, but continuing on to complete the remainder of the loop. As a result, the current iteration of the loop will be interrupted, but the program will continue to the beginning of the loop. The continue statement will be found within the block of code that is contained within the loop statement, and is typically found after a conditional if statement.

Discuss this Question


45. Amongst which of the following is / are true about the while loop?

  1. It continually executes the statements as long as the given condition is true
  2. It first checks the condition and then jumps into the instructions
  3. The loop stops running when the condition becomes fail, and control will move to the next line of code.
  4. All of the mentioned above

Answer: D) All of the mentioned above

Explanation:

While loops are used to execute statements repeatedly as long as the condition is met, they are also used to execute statements once. It begins by determining the condition and then proceeds to execute the instructions. Within the while loop, we can include any number of statements that we want. The condition can be anything we want it to be depending on our needs. When the condition fails, the loop comes to an end, and the execution moves on to the next line of code in the program.

Discuss this Question


46. The ___ is a built-in function that returns a range object that consists series of integer numbers, which we can iterate using a for loop.

  1. range()
  2. set()
  3. dictionary{}
  4. None of the mentioned above

Answer: A) range()

Explanation:

This type represents an immutable sequence of numbers and is commonly used in for loops to repeat a specific number of times a given sequence of numbers. The range() function in Python generates an immutable sequence of numbers beginning with the given start integer and ending with the given stop integer. For loops, we can use the range() built-in function to return an object that contains a series of integer numbers, which we can then iterate through using a for loop.

Discuss this Question


47. What will be the output of the following Python code?

for i in range(6):
    print(i)
  1. 0
    1
    2
    3
    4
    5
  2. 0
    1
    2
    3
  3. 1
    2
    3
    4
    5
  4. None of the mentioned above

Answer: A)
0
1
2
3
4
5

Explanation:

The range(6) is define as function. Loop will print the number from 0.

Discuss this Question


48. The looping reduces the complexity of the problems to the ease of the problems?

  1. True
  2. False

Answer: A) True

Explanation:

The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the program so that instead of writing the same code again and again, we can repeat the same code for a finite number of times.

Discuss this Question


49. The while loop is intended to be used in situations where we do not know how many iterations will be required in advance?

  1. True
  2. False

Answer: A) True

Explanation:

The while loop is intended to be used in situations where we do not know how many iterations will be required in advance. When a while loop is used, the block of statements within it is executed until the condition specified within the while loop is satisfied. It is referred to as a pre-tested loop in some circles.

Discuss this Question


50. Amongst which of the following is / are true with reference to loops in Python?

  1. It allows for code reusability to be achieved.
  2. By utilizing loops, we avoid having to write the same code over and over again.
  3. We can traverse through the elements of data structures by utilizing looping.
  4. All of the mentioned above

Answer: D) All of the mentioned above

Explanation:

Following point's shows the importance of loops in Python.

  • It allows for code reusability to be achieved.
  • By utilizing loops, we avoid having to write the same code over and over again.
  • We can traverse through the elements of data structures by utilizing looping.

Discuss this Question


51. A function is a group of related statements which designed specifically to perform a ___.

  1. Write code
  2. Specific task
  3. Create executable file
  4. None of the mentioned above

Answer: B) Specific task

Explanation:

A function is a group of related statements designed specifically to perform a specific task. Functions make programming easier to decompose a large problem into smaller parts. The function allows programmers to develop an application in a modular way. As our program grows larger and larger, functions make it more organized and manageable.

Discuss this Question


52. Amongst which of the following is a proper syntax to create a function in Python?

  1. def function_name(parameters):
        ...
        Statements
        ...
    
  2. def function function_name:
        ...
        Statements
        ...
    
  3. def function function_name(parameters):
        ...
        Statements
        ...
    
  4. None of the mentioned above

Answer: A)

def function_name(parameters):
    ...
    Statements
    ...

Explanation:

To define a function we follow the syntax mentioned in the answer section. def keyword marks the start of the function header. We start from the def keyword and write the name of the function along with function parameters. Function naming follows the naming rules to write identifiers in Python. Arguments or parameters are passed as function arguments. Function arguments are optional. A colon (:) denotes the end of the function header.

def function_name(parameters):
    ...
    Statements
    ...

Discuss this Question


53. Once we have defined a function, we can call it?

  1. True
  2. False

Answer: A) True

Explanation:

Once a function has been defined, it can be called from another function, a program, or even from the Python prompt itself. To call a function, we simply type the name of the function followed by the appropriate parameters into the command line.

For example-

def user_name(name):
    # This function greets to user 
    # to put name
    print("Hello, " + name + ".")

user_name("Amit")	# Amit passed as function argument

# Output: Hello, Amit.

Discuss this Question


54. Amongst which of the following shows the types of function calls in Python?

  1. Call by value
  2. Call by reference
  3. Both A and B
  4. None of the mentioned above

Answer: C) Both A and B

Explanation:

Call by value and Call by reference are the types of function calls in Python.

  • Call by value - When, we call a function with the values i.e. to pass the variables (not their references), the values of the passing arguments cannot be changed inside the function.
  • Call by reference - When, we call a function with the reference/object, the values of the passing arguments can be changed inside the function.

Discuss this Question


55. What will be the output of the following Python code?

def show(id,name):
    print("Your id is :",id,"and your name is :",name)

show(12,"deepak")
  1. Your id is: 12 and your name is: deepak
  2. Your id is: 11 and your name is: Deepak
  3. Your id is: 13 and your name is: Deepak
  4. None of the mentioned above

Answer: A) Your id is: 12 and your name is: deepak

Explanation:

If we define a function in Python with parameters, and at the time of calling function it requires parameters. In above code passing arguments are 12, and Deepak. So, Output will be Your id is: 12 and your name is: deepak

Discuss this Question


56. Amongst which of the following is a function which does not have any name?

  1. Del function
  2. Show function
  3. Lambda function
  4. None of the mentioned above

Answer: C) Lambda function

Explanation:

Lambda function is an anonymous function, which means that it does not have a name, as opposed to other functions. Unlike other programming languages, Python allows us to declare functions without using the def keyword, which is what we would normally do to declare a function. As an alternative, the lambda keyword is used to declare the anonymous functions that will be used throughout the program. When compared to other functions, lambda functions can accept any number of arguments, but they can only return a single value, which is represented by an expression.

Syntax:

lambda arguments: expression

Discuss this Question


57. Can we pass List as an argument in Python function?

  1. Yes
  2. No

Answer: A) Yes

Explanation:

In a function, we can pass any data type as an argument, such as a string or a number or a list or a dictionary, and it will be treated as if it were of that data type inside the function. The following code exemplifies this –

def St_list(student):
  for x in student:
    print(x)
students = ["Anil", "Rex", "Jerry"]
St_list(students) 

"""
Output:
Anil
Rex
Jerry
"""

Discuss this Question


58. A method refers to a function which is part of a class?

  1. True
  2. False

Answer: A) True

Explanation:

A method is a function that is a part of a class that has been defined. It is accessed through the use of an instance or object of the class. A function, on the other hand, is not restricted in this way: it simply refers to a standalone function. This implies that all methods are functions, but that not all functions are methods in the same sense.

Discuss this Question


59. The return statement is used to exit a function?

  1. True
  2. False

Answer: A) True

Explanation:

The return statement is used to exit a function and go back to the place from where it was called.

Syntax of return:

return [expression_list]

In this statement, you can include an expression that will be evaluated and the resulting value will be returned. A function will return the None object if there is no expression in the statement or if the return statement itself is not present within a function's body.

Discuss this Question


60. Scope and lifetime of a variable declared in a function exist till the function exists?

  1. True
  2. False

Answer: A) True

Explanation:

It is the portion of a program where a variable is recognized that is referred to as its scope. It is not possible to see the parameters and variables defined within a function from outside of the function. As a result, they are limited in their application. The lifetime of a variable is the period of time during which the variable is stored in the memory of the computer. The lifetime of variables contained within a function is equal to the length of time the function is in operation. When we return from the function, they are completely destroyed. As a result, a function does not retain the value of a variable from previous calls to the function.

Discuss this Question


61. File handling in Python refers the feature for reading data from the file and writing data into a file?

  1. True
  2. False

Answer: A) True

Explanation:

File handling is the capability of reading data from and writing it into a file in Python. Python includes functions for creating and manipulating files, whether they are flat files or text documents. We will not need to import any external libraries in order to perform general IO operations because the IO module is the default module for accessing files.

Discuss this Question


62. Amongst which of the following is / are the key functions used for file handling in Python?

  1. open() and close()
  2. read() and write()
  3. append()
  4. All of the mentioned above

Answer: D) All of the mentioned above

Explanation:

The key functions used for file handling in Python are: open(), close(), read(), write(), and append(). the open() function is used to open an existing file, close() function is used to close a file which opened, read() function is used when we want to read the contents from an existing file, write() function is used to write the contents in a file and append() function is used when we want to append the text or contents to a specific position in an existing file.

Discuss this Question


63. Amongst which of the following is / are needed to open an existing file?

  1. filename
  2. mode
  3. Both A and B
  4. None of the mentioned above

Answer: C) Both A and B

Explanation:

In most cases, only the filename and mode parameters are required, with the rest of the parameters implicitly set to their default values.

Following code demonstrates the example of how to open a file -

f = open ("file.txt") 

Discuss this Question


64. Binary files are stored in the form of 0s and 1s?

  1. True
  2. False

Answer: A) True

Explanation:

Binary files are also stored in terms of bytes (0s and 1s), but, unlike text files, these bytes do not represent the ASCII values of the characters that are contained within them. A binary file is a sequence of bytes that is stored in a computer's memory. Even a single bit change can corrupt a file, rendering it unreadable by the application that is attempting to read it. In addition, because the binary file's contents are not human readable, it is difficult to correct any errors that may occur in the binary file.

Discuss this Question


65. The function file_object.close() is used to ___.

  1. To open the existing file
  2. To append in an opened file
  3. To close an opened file
  4. None of the mentioned above

Answer: C) To close an opened file

Explanation:

To close a file that has been opened, use the file object.close() function. To accomplish this, the Python language provides the close() method. When a file is closed, the system releases the memory that was allocated to it.

Discuss this Question


66. Python always makes sure that any unwritten or unsaved data is written to the file before it is closed?

  1. True
  2. False

Answer: A) True

Explanation:

Whenever a file is closed, Python ensures that any unwritten or unsaved data is flushed out or written to the file's header before the file is closed. As a result, it is always recommended that we close the file once our work is completed. Additionally, if the file object is reassigned to a different file, the previous file is automatically closed as well.

Discuss this Question


67. The write() method takes a string as an argument and ___.

  1. writes it to the text file
  2. read from the text file
  3. append in a text file
  4. None of the mentioned above

Answer: A) writes it to the text file

Explanation:

The write() method accepts a string as an argument and writes it to the text file specified by the filename parameter. The write() method returns the number of characters that were written during a single execution of the write() function. A newline character (n) must also be added at the end of every sentence to indicate the end of a line.

Discuss this Question


68. The seek() method is used to ___.

  1. Saves the file in secondary storage
  2. Position the file object at a particular position in a file
  3. Deletes the file form secondary storage
  4. None of the mentioned above

Answer: B) Position the file object at a particular position in a file

Explanation:

The seek() method is used to position a file object at a specific location within a file's hierarchy.

Discuss this Question


69. Amongst which of the following function is / are used to create a file and writing data?

  1. append()
  2. open()
  3. close()
  4. None of the mentioned above

Answer: B) open()

Explanation:

To create a text file, we call the open() method and pass it the filename and the mode parameters to the function. If a file with the same name already exists, the open() function will behave differently depending on whether the write or append mode is used to open the file. Write mode (w) will cause all of the existing contents of the file to be lost, and a new file with the same name will be created with the same contents as the existing file.

Discuss this Question


70. The readline() is used to read the data line by line from the text file.

  1. True
  2. False

Answer: A) True

Explanation:

It is necessary to use readline() in order to read the data from a text file line by line. The lines are displayed by employing the print() command. When the readline() function reaches the end of the file, it will return an empty string.

Discuss this Question


71. The module Pickle is used to ___.

  1. Serializing Python object structure
  2. De-serializing Python object structure
  3. Both A and B
  4. None of the mentioned above

Answer: C) Both A and B

Explanation:

Pickle is a Python module that allows you to save any object structure along with its associated data. Pickle is a Python module that can be used to serialize and de-serialize any type of Python object structure. Serialization is the process of converting data or an object stored in memory to a stream of bytes known as byte streams, which is a type of data stream. These byte streams, which are contained within a binary file, can then be stored on a disc, in a database, or transmitted over a network. Pickling is another term for the serialization process. De-serialization, also known as unpickling, is the inverse of the pickling process, in which a byte stream is converted back to a Python object through the pickling process.

Discuss this Question


72. Amongst which of the following is / are the method of convert Python objects for writing data in a binary file?

  1. set() method
  2. dump() method
  3. load() method
  4. None of the mentioned above

Answer: B) dump() method

Explanation:

The dump() method is used to convert Python objects into binary data that can be written to a binary file. The file into which the data is to be written must be opened in binary write mode before the data can be written. To make use of the dump() method, we can call this function with the parameters data object and file object. There are two objects in this case: data object and file object. The data object object is the object that needs to be dumped to the file with the file handle named file_ object.

Discuss this Question


73. Amongst which of the following is / are the method used to unpickling data from a binary file?

  1. load()
  2. set() method
  3. dump() method
  4. None of the mentioned above

Answer: B) set() method

Explanation:

The load() method is used to unpickle data from a binary file that has been compressed. The binary read (rb) mode is used to load the file that is to be loaded. If we want to use the load() method, we can write Store object = load(file object) in our program. The pickled Python object is loaded from a file with a file handle named file object and stored in a new file handle named store object. The pickled Python object is loaded from a file with a file handle named file object and stored in a new file handle named store object.

Discuss this Question


74. A text file contains only textual information consisting of ___.

  1. Alphabets
  2. Numbers
  3. Special symbols
  4. All of the mentioned above

Answer: D) All of the mentioned above

Explanation:

Unlike other types of files, text files contain only textual information, which can be represented by alphabets, numbers, and other special symbols. These types of files are saved with extensions such as.txt,.py,.c,.csv,.html, and so on. Each byte in a text file corresponds to one character in the text.

Discuss this Question


75. The writelines() method is used to write multiple strings to a file?

  1. True
  2. False

Answer: A) True

Explanation:

In order to write multiple strings to a file, the writelines() method is used. The writelines() method requires an iterable object, such as a list, tuple, or other collection of strings, to be passed to it.

Discuss this Question





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.