menu

Python Basics


1.

What is the output of the following code?

x = 5
print("x =", x)

x = 5

x

5

x, 5


2. Which of the following is NOT a comparison operator in Python?

==

!=

<=

<>


3. Which of the following is the correct way to open a file in Python?

file = open(filename, "r")

file = open("r", filename)

file = open(filename, "w")

None of the above


4. Which of the following is NOT a valid way to declare a variable in Python?

x = 1

y = "Hello"

z = 3.14

2 = a


5.

What is the result of the following code?

x = ["apple", "banana", "cherry"]
print(len(x))

3

5

6

p


6. Which of the following is the correct way to open a file in Python?

open_file("example.txt")

open("example.txt")

file.open("example.txt")

with open("example.txt") as file:


7.

What is the output of the following code?

print(7 % 3)

2

3

1

7


8. Which of the following is the correct way to concatenate two strings in Python?

string1.join(string2)

string1.concat(string2)

string1 + string2

All of the above


9.

What is the output of the following code?

x = [1, 2, 3]
y = x
y[0] = 4
print(x)

[1, 2, 3]

[4, 2, 3]

[1, 4, 3]

[1, 2, 4]


10.

What is the output of the following code?

x = 2
y = 5
z = x + y
print("The sum of", x, "and", y, "is", z)

The sum of 2 and 5 is 7

The sum of x and y is z

The sum of x and y is 7

None of the above