menu

Python Basics


1. Which of the following is a valid way to comment a single line of Python code?

// This is a comment

/* This is a comment */

# This is a comment

// This is a comment //


2. Which of the following is the correct way to sort a list in descending order in Python?

list.sort(reverse=True)

list.sort(reverse=False)

list.reverse()

None of the above


3. Which of the following is a valid Python variable name?

1_name

my_variable

$test

class


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. 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()


6.

What is the output of the following code?

print(7 % 3)

2

3

1

7


7. Which of the following is the correct way to check if a key is in a dictionary in Python?

dict.contains(key)

dict.has_key(key)

key in dict

dict.get(key)


8. Which of the following is the correct way to define a multi-line string in Python?

This is a multi-line string

'''This is a multi-line string'''

"This is a multi-line string"

All of the above


9. Which of the following is the correct way to convert a string to an integer in Python?

int(string)

str(int)

string.int()

All of the above


10. Which of the following is the correct way to concatenate two lists in Python?

list1.add(list2)

list1.append(list2)

list1 + list2

list1.extend(list2)