Python Final Exam Questions and Answers

0% found this document useful (0 votes)
373 views6 pages
License
© 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

PYTHON FINAL EXAM

1. What is Python?

a) High-level programming language

b) Low-level programming language

c) Assembly language

d) Machine language

2. Which of the following is the correct way to comment out multiple lines in Python?

a) // This is a comment //

b) # This is a comment #

c) /* This is a comment */

d) ''' This is a comment '''

3. What is the purpose of the 'pass' statement in Python?

a) To create a loop

b) To do nothing

c) To exit a function

d) To print a message

4. Which of the following is not a valid variable name in Python?

a) my_ variable

b) 123_variable

c) _variable

d) variable_123
5. What is the result of the expression 3 * 2 + 8 / 4 in Python?

a) 9.0

b) 7.0

c) 8.0

d) 5.0

6. How do you declare a function in Python?

a) function my_function():

b) def my_function():

c) function = my_function():

d) declare my_function():

7. What is the output of print ("G-TEC" + " " + "Computers") in Python?

a. G-TEC Computers

b. G-TECComputers

c. G-TEC + Computers

d. Error

8. Which data type is used to represent a sequence of characters in Python?

a. int

b. float

c. str

d. bool

9. What is the purpose of the 'len()' function in Python?


a. To find the length of a list

b. To convert a string to lowercase

c. To check if a variable is defined

d. To generate random numbers

10. Which of the following statements is used for decision-making in Python?

a. for

b. while

c. if

d. switch

11. What does the 'self' keyword refer to in Python?

a. It is used for exception handling

b. It refers to the current instance of the class

c. It is a built-in function

d. It is used for file input/output

12. Which operator is used for exponentiation in Python?

a. ^

b. **

c. ^

d. //

13. What is the purpose of the 'break' statement in Python?

a. To exit a loop prematurely


b. To skip the current iteration of a loop

c. To resume the next iteration of a loop

d. To terminate the program

14. Which of the following is a mutable data type in Python?

a. int

b. float

c. str

d. list

15. How can you open a file in binary mode in Python?

a. open ("[Link]", "b")

b. open("[Link]", "binary")

c. open("[Link]", "rb")

d. open("[Link]", "mode=b")

16. What is the purpose of the 'continue' statement in Python?

a. To exit the loop

b. To skip the rest of the code in the loop and move to the next iteration

c. To resume the next iteration of a loop

d. To terminate the program

17. Which of the following is the correct way to create a tuple with a single element?

a. (1)

b. 1,
c. (1,)

d. tuple(1)

18. What does the 'range()' function return in Python?

a. A list of numbers

b. A tuple of numbers

c. A range objects

d. A string of numbers

19. What is the purpose of the 'init' method in Python classes?

a. To initialize a variable

b. To define the constructor of the class

c. To create a new instance of the class

d. To delete an instance of the class

20. Which of the following is not a valid way to import a module in Python?

a. import my_module

b. from my_module import *

c. import my_module as mm

d. import *

21. What is the output of print(10 / 3) in Python 3?

a. 3

b. 3.3333333333333335

c. 3.0
d. Error

22. In Python, what is the purpose of the 'else' clause in a try-except block?

a. To specify the code to be executed when an exception occurs

b. To specify the code to be executed if no exception occurs

c. To specify multiple exceptions

d. To specify the code to be executed regardless of whether an exception occurs

23. What is the purpose of the 'strip()' method for strings in Python?

a. To remove whitespace from the beginning and end of a string

b. To split a string into a list

c. To concatenate two strings

d. To replace a substring in a string

24. How can you convert a string to uppercase in Python?

a. [Link]()

b. [Link]()

c. [Link]()

d. str.upper_case()

25. Which of the following is not a valid comparison operator in Python?

a. ==

b. !=

c. <= d. ><

Common questions

Powered by AI

The 'break' statement in Python is used to exit a loop prematurely when a condition is met, instantly stopping the loop execution . In a loop with an 'else' clause, the 'else' is executed only if the loop terminates naturally (i.e., not via a 'break' statement). Therefore, 'else' in this context allows for executing specific code only if the loop completes without encountering a 'break'. This design enables checks or conclusions about the loop execution status after looping .

Strings in Python are immutable, meaning once created, their content cannot be changed without creating a new object, ensuring integrity and reducing side effects in code. Lists, however, are mutable, allowing for modification after creation. This mutability introduces flexibility but can lead to bugs if unexpected changes occur, especially when lists are passed as default arguments in function definitions, which persist across calls. Understanding these characteristics is crucial to manage state and changes in Python applications .

In Python, multiline comments can be created using triple quotes, either ''' like this ''' or """ like this """, which is different from C++ or Java where multiline comments are created using /* comment */. These commenting methods in other languages indicate specific start and end points within the code, while Python doesn't specifically require a comment syntax - the triple quotes typically denote a string but can serve to comment out blocks of code in practice .

In Python 3, the division operator '/' yields a float, regardless of the input types, aligning integer division with real number division, so 10 / 3 results in 3.3333333333333335 . Conversely, in Python 2, using '/' with integer inputs would result in integer division, truncating the decimal to return an integer, such as 3. To perform true division, you must use floats or import division from the future module. This shift ensures consistent numerical operations, especially important in scientific computing requiring precision .

The 'len()' function in Python is versatile because it provides the length or size of data structures like lists, strings, tuples, and even dictionaries, effectively abstracting away the details of each structure. This makes 'len()' a powerful tool for iterating over data collections and managing data across diverse types. Implications of its usage include its reliance on data structure length metadata, ensuring efficient computation without manually iterating over elements .

High-level programming languages like Python are characterized by their abstraction from machine language, meaning they are designed to be closer to human languages and are easier to read and write. In contrast, low-level programming languages are closer to machine code, making them more difficult for humans to read and write. Assembly languages are even lower level, requiring detailed knowledge of the architecture. Python allows writing complex algorithms with simpler syntax, facilitating faster development .

In Python, 'self' refers to the instance of the class on which a method is being called, promoting encapsulation by ensuring that each method is aware of the instance and its data. This allows for proper management and interaction with an object’s own attributes and methods. Unlike languages where the instance is passed implicitly, Python makes 'self' explicit, enhancing code readability and reducing ambiguities over which instance a method is modifying or querying .

The 'pass' statement in Python is a null operation used when a statement is syntactically required but no action is necessary, essentially serving as a placeholder . In contrast, the 'continue' statement immediately terminates the current iteration of a loop and moves control back to the beginning of the loop for the next iteration, skipping the remaining code inside the loop's body for the current iteration .

In Python, constructors are defined by the __init__ method, which is not only responsible for initializing the attributes of a class with initial values but also for setting up any other state or required operations when an instance is created . This is similar to constructors in languages like Java, where they serve to initialize new objects, but Python's __init__ is more flexible due to dynamic typing allowing more diversity in initialization behavior without needing multiple constructor overloads .

Using 'from module import *' in Python imports all public names directly into the current namespace. Although it might seem convenient, this practice is generally discouraged because it can lead to namespace conflicts and makes the origin of certain functions or variables unclear, which complicates debugging and maintenance. It can unintentionally overwrite existing functions or variables in the current namespace .

You might also like