menu

Python Basics


1. Which of the following is the correct syntax for a while loop in Python?

while i in range(10):

while (i < 10)

while (i != 10)

while i = 10


2. Which of the following is the correct syntax for defining a function in Python?

function my_function():

def my_function():

define my_function():

func my_function():


3. Which of the following is NOT a data type in Python?

Integer

Float

Double

String


4. Which of the following is the correct way to define a function in Python?

function_name(arguments):

def function_name(arguments):

function_name(arguments)

None of the above


5. 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


6. Which of the following is the correct way to read a line from a file in Python?

file.readline()

file.read(line)

read(file, line)

file.read()


7. Which of the following is the correct way to define a class in Python?

class my_class():

define class my_class:

def my_class():

class my_class:


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

x = 1

y = "Hello"

z = 3.14

2 = a


9.

What is the output of the following code?

x = 3
if x > 5:print("x is greater than 5")
elif x > 2:print("x is greater than 2")
else:print("x is less than or equal to 2")

x is greater than 5

x is greater than 2

x is less than or equal to 2

None of the above


10. 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: