Login Account

Python Questions

K
Kajal Saini
Score: 🔥 44
Beginner
Took 2 months
Programming Language
Python
80%
Questions Attempted
14 / 15
Correct Answers
12
Incorrect Answers
3
Points Earned
🔥 12

Question 1

What will the following code print: print(10 > 5 and 'apple' == 'orange')?

Explanation:

The expression uses the and operator. While 10 > 5 is True, 'apple' == 'orange' is False. For 'and' to return True, both conditions must be True.

Question 2

What is the value of the expression: 10 > 5 and 3 < 1?

Explanation:

The and operator returns True only if both operands are True. Here, 10 > 5 is True, but 3 < 1 is False, making the entire expression False.

Question 3

What is the output of the following Python code snippet?

print(type(5.0))

Explanation:

The type() function in Python returns the data type of a given value. In this case, 5.0 is a floating-point number, hence the output is <class 'float'>.

Question 4

What is the correct way to write and run a Python script named 'my_script.py' from the command line?

Explanation:

To execute a Python script, you use the command 'python' followed by the name of the script file ('my_script.py' in this case).

Question 5

What will the following code snippet print?

x = 5
y = 2 * x
print(y)

Explanation:

The code assigns 5 to 'x', then calculates 'y' as 2 times 'x' (which is 10), and finally prints the value of 'y'.

Question 6

What is the correct way to take integer input from the user in Python?

Explanation:

The 'input()' function returns a string, so we need to convert it to an integer using 'int()' to store it as a numerical value.

Question 7

What will the following Python code print to the console?

name = input("Enter your name: ")
print("Hello,", name)

Explanation:

The code first prompts the user to enter their name. After the user provides input, the code then prints a greeting concatenated with the user's input.

Question 8

How many times will the word 'Hello' be printed?

i = 0
while i < 3:
	print('Hello')
	i += 1

Explanation:

The loop runs as long as 'i' is less than 3. 'i' is incremented by 1 in each iteration, so the loop runs for i = 0, i = 1, and i = 2, printing 'Hello' three times.

Question 9

How many times will the following loop iterate?

for i in range(1, 5): print(i)

Explanation:

The range(1, 5) function generates a sequence of numbers from 1 (inclusive) to 5 (exclusive), resulting in 4 iterations.

Question 10

Which of the following data structures in Python is mutable?

Explanation:

Lists in Python are mutable, meaning their elements can be changed after creation. Tuples and strings are immutable.

Question 11

What is the output of the following Python code?

my_list = [1, 2, 3, 4, 5]
print(my_list[1:3])

Explanation:

In Python slicing, the starting index is inclusive, and the ending index is exclusive. Therefore, my_list[1:3] will return a new list containing elements from index 1 (the second element) up to, but not including, index 3.

Question 12

What is the correct way to add a new key-value pair to an existing dictionary in Python?

Explanation:

To add a new key-value pair to a dictionary, you can simply assign a value to a new key using the assignment operator =. If the key already exists, its value will be updated.

Question 13

What is the output of the following Python code?

string = 'Hello, world!'
print(string[7:12])

Explanation:

In Python, string slicing uses a zero-based index. string[7:12] extracts characters from index 7 (inclusive) to 12 (exclusive), resulting in the substring 'world'.

Question 14

What is the correct way to create a string literal containing both single and double quotes in Python?

Explanation:

Python allows using either single or double quotes to define string literals. You can embed single quotes within double-quoted strings and vice versa.