0% found this document useful (0 votes)
3 views6 pages

Python Theory Questions for Beginners

The document contains a series of theory questions and multiple-choice questions based on the book 'Learn Python in 24 Hours for Beginners.' It covers topics such as data structures, control statements, functions, classes, loops, dictionaries, exception handling, and logging in Python. Each section includes questions with correct answers provided for educational purposes.

Uploaded by

patrick moseray
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views6 pages

Python Theory Questions for Beginners

The document contains a series of theory questions and multiple-choice questions based on the book 'Learn Python in 24 Hours for Beginners.' It covers topics such as data structures, control statements, functions, classes, loops, dictionaries, exception handling, and logging in Python. Each section includes questions with correct answers provided for educational purposes.

Uploaded by

patrick moseray
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SECTION-A

Here are five theory questions based on the book Learn Python in 24 Hours for Beginners
(Second Edition by S. Basu)

1. Explain the difference between lists, tuples, and dictionaries in Python, highlighting their
mutability and use cases.

2. What is the role of the __init__ method in a Python class, and why is it often referred to
as a constructor?

3. Describe how Python control statements (if-elif-else, for, while, break, and
continue) manage the flow of a program.

4. What does the line if __name__ == "__main__": mean in Python, and how does it affect
program execution when modules are imported?

5. Compare and contrast Python exception handling (try-except-finally) with normal


error handling. Why is exception handling important in programming?

SECTION-B

1. Installation & Basics

When installing Python on Windows, why is it important to add Python to the PATH
environment variable?

A. To automatically update Python to the latest version


B. To be able to run Python commands from any directory in the command prompt
C. To enable Python to connect to the internet
D. To allow Python to compile C++ code

Answer: B

2. Data Structures

Which of the following is a key difference between a Python list and a tuple?

A. Lists are mutable, while tuples are immutable


B. Lists store only integers, while tuples store any data type
C. Tuples are faster than lists and can only store strings
D. Lists use parentheses (), while tuples use square brackets []

Answer: A
3. Control Statements

Which of the following if-elif-else statements correctly checks if a number x is positive,


negative, or zero?

A.

if x > 0:

print("Positive")

elif x < 0:

print("Negative")

else:

print("Zero")

B.

if x = 0:

print("Zero")

elif x > 0:

print("Positive")

else:

print("Negative")

C.

if x != 0:

print("Positive")

elif x == 0:

print("Negative")

else:

print("Zero")

D.

if x > 0:

print("Zero")

elif x < 0:
print("Positive")

else:

print("Negative")

Answer: A

4. Functions

Which function correctly checks if a string is a palindrome?

A.

def is_palindrome(s):

return s == s[::-1]

B.

def is_palindrome(s):

return s == s[1:]

C.

def is_palindrome(s):

return s == [Link]()

D.

def is_palindrome(s):

return s == reversed(s)

Answer: A

5. Classes & Inheritance

Which of the following correctly demonstrates inheritance in Python?

A.

class Employee:

pass

class Tester:

pass
B.

class Employee:

def __init__(self, id, name):

[Link] = id

[Link] = name

class Tester(Employee):

def __init__(self, id, name, department):

super().__init__(id, name)

[Link] = department

C.

class Employee():

def __init__(self, id, name):

[Link] = id

[Link] = name

class Tester:

def __init__(self, department):

[Link] = department

D.

class Employee:

id = 0

name = ""

class Tester:

department = ""

Answer: B
6. Loops

What will be the output of the following code?

cars = ["Kia", "Toyota", "Ford"]

for car in cars:

print(car)

A. Kia Toyota Ford (all on one line)

B.

Kia

Toyota

Ford

C. Ford Toyota Kia

D. Error: for loop not valid in Python

Answer: B

7. Dictionaries

Which of the following methods is used to get only the keys from a Python dictionary?

A. [Link]()
B. [Link]()
C. [Link]()
D. [Link]()

Answer: B

8. Try-Except

What is the purpose of the finally block in a try-except structure?

A. To catch all errors not handled by except


B. To execute code only if no error occurs
C. To ensure code inside it runs no matter what, whether an error occurs or not
D. To stop execution of the program immediately

Answer: C
9. Logging

By default, which logging levels are displayed when using Python’s logging module without
configuration?

A. DEBUG and INFO


B. WARNING, ERROR, and CRITICAL
C. ERROR only
D. All levels are displayed

Answer: B

10. __name__ == "__main__"

What does the line if __name__ == "__main__": mean in a Python script?

A. It checks whether Python is installed on the system


B. It ensures that code runs only if the file is executed directly, not when imported as a
module
C. It declares the main function of a Python program
D. It allows a Python script to run faster

Answer: B

You might also like