Question 1: What is the output of the following Python code?
```python
numbers = [1, 2, 3, 4, 5]
result = sum(numbers)
print(result)
```
A) 15
B) 10
C) 5
D) Error
Answer: A) 15
Explanation: The code calculates the sum of all numbers in the `numbers` list using the `sum()` function and then prints the result, which is 15.
-------------------------------------------------------------------------
Question 2: What does the following Python code do?
```python
text = "Hello, World!"
result = text.split(",")
```
A) Splits the text at commas and stores the parts in a list.
B) Reverses the text.
C) Removes all commas from the text.
D) Raises an error.
Answer: A) Splits the text at commas and stores the parts in a list.
Explanation: The `split(",")` method splits the text at each comma and returns a list containing the parts. In this case, the result will be `['Hello', ' World!']`.
-------------------------------------------------------------------------
Question 3: How do you read a user's input from the console in Python?
A) read()
B) input()
C) get_input()
D) console_input()
Answer: B) input()
Explanation: The `input()` function is used to read user input from the console in Python. It reads the input as a string and can be used to prompt the user for data during program execution.
-------------------------------------------------------------------------
Question 4: What is the purpose of the `os` module in Python?
A) To perform mathematical operations.
B) To work with dates and times.
C) To interact with the operating system.
D) To generate random numbers.
Answer: C) To interact with the operating system.
Explanation: The `os` module in Python provides functions to interact with the operating system. It allows you to perform tasks like managing files and directories, getting system information, and executing system commands.
-------------------------------------------------------------------------
Question 5: Which Python module is used for handling JSON data?
A) json
B) pyjson
C) datajson
D) jsondata
Answer: A) json
Explanation: The `json` module in Python is used for handling JSON data. It provides functions to parse JSON strings into Python objects (like dictionaries) and convert Python objects into JSON strings.
Question 1: What is the output of the following Python code?
```python
print("Hello, World!")
```
A) Hello, World!
B) Hello
C) World!
D) Error
Answer: A) Hello, World!
Explanation: The `print()` function is used to display output on the console. In this code, it will print the string "Hello, World!".
-------------------------------------------------------------------------
Question 2: What will be the output of the following Python code?
```python
name = "Alice"
age = 30
print("My name is", name, "and I am", age, "years old.")
```
A) My name is Alice and I am 30 years old.
B) My name is and I am years old.
C) Alice 30
D) Error
Answer: A) My name is Alice and I am 30 years old.
Explanation: The `print()` function can accept multiple arguments separated by commas. It will concatenate the arguments and print them as a single string.
-------------------------------------------------------------------------
Question 3: What does the `end` parameter in the `print()` function do?
A) It specifies the character to separate multiple items in the output.
B) It specifies the character to append at the end of the output.
C) It specifies the character to append between different print statements.
D) It specifies the character to append at the beginning of the output.
Answer: B) It specifies the character to append at the end of the output.
Explanation: By default, the `end` parameter is set to "\n", which means a newline character is appended at the end of the output. However, you can change it to any other character or an empty string to control the ending of the output.
-------------------------------------------------------------------------
Question 4: What will be the output of the following Python code?
```python
x = 10
y = 20
print(x, y, sep=" - ")
```
A) 10 - 20
B) 10 20
C) x - y
D) Error
Answer: A) 10 - 20
Explanation: The `sep` parameter in the `print()` function is used to specify the separator between different items in the output. In this code, we set `sep` to " - ", so the output will be "10 - 20".
-------------------------------------------------------------------------
Question 5: What will the following Python code print?
```python
print("Hello", end="")
print("World")
```
A) HelloWorld
B) Hello World
C) Hello\nWorld
D) Error
Answer: A) HelloWorld
Explanation: The `end` parameter, when set to an empty string, removes the newline character that would be appended at the end of the output. So, the two print statements will print their outputs without a newline in between, resulting in "HelloWorld".
MCQ on Print in Python with Answer and exaplanation
Question 1: What is a variable in Python?
A) A block of code that performs a specific task.
B) A reserved word that cannot be used as an identifier.
C) A container used to store data with a specific name.
D) A function that takes no arguments.
Answer: C) A container used to store data with a specific name.
Explanation: In Python, a variable is a named location in memory used to store data. It allows us to assign values to a specific name and access the data using that name.
-------------------------------------------------------------------------
Question 2: Which of the following is a valid Python variable name?
A) my-variable
B) 123abc
C) _myVariable
D) class
Answer: C) _myVariable
Explanation: A valid Python variable name can start with an underscore (_) or a letter (a-z, A-Z) and can be followed by letters, digits, or underscores.
-------------------------------------------------------------------------
Question 3: What is the output of the following Python code?
```python
x = 5
y = "hello"
print(x + y)
```
A) 5hello
B) hello5
C) Error: unsupported operand type(s) for +: 'int' and 'str'
D) Error: invalid syntax
Answer: C) Error: unsupported operand type(s) for +: 'int' and 'str'
Explanation: In this code, you are trying to perform addition between an integer and a string, which is not allowed. Python throws an error for this operation.
-------------------------------------------------------------------------
Question 4: What is the scope of a variable defined inside a function?
A) Global scope
B) Local scope
C) Enclosed scope
D) Built-in scope
Answer: B) Local scope
Explanation: Variables defined inside a function have local scope, which means they can only be accessed within that function. They are not accessible outside the function.
-------------------------------------------------------------------------
Question 5: How can you check the data type of a variable in Python?
A) check_type(variable)
B) typeOf(variable)
C) typeof variable
D) type(variable)
Answer: D) type(variable)
Explanation: The `type()` function is used to check the data type of a variable in Python. It returns the type of the variable, such as int, str, list, etc.
Generate MCQ on Data Type Python with Answer and exaplanation
Question 1: Which of the following is not a built-in data type in Python?
A) int
B) str
C) list
D) char
Answer: D) char
Explanation: In Python, there is no specific data type called "char". Instead, individual characters are represented as strings of length 1.
Question 2: What is the data type of the variable x
in the following code?
python
x = 5.7
A) int
B) float
C) str
D) bool
Answer: B) float
Explanation: The presence of a decimal point in the value of x
indicates that it is a floating-point number, hence the data type is "float".
Question 3: What will be the output of the following code?
python
x = "Hello"
y = "World"
result = x + y
print(result)
A) HelloWorld
B) Hello World
C) Hello + World
D) Error
Answer: A) HelloWorld
Explanation: The +
operator is used to concatenate two strings. So, result
will be "HelloWorld".
Question 4: Which data type is used to store an ordered collection of elements?
A) int
B) str
C) list
D) dict
Answer: C) list
Explanation: Lists in Python are used to store an ordered collection of elements, which can be of different data types.
Question 5: What is the data type of the following value: [1, 2, 3]
?
A) int
B) str
C) list
D) tuple
Answer: C) list
Explanation: The square brackets [ ]
represent a list in Python, so the value [1, 2, 3]
is of data type "list".
Comments
Post a Comment