12 Class- Exception Handling in Python objective

Q1. What is the purpose of using Exception Handling in Python?

a) To prevent the occurrence of errors in the program
b) To ignore the errors that occur in the program
c) To identify and handle errors that occur in the program
d) To stop the execution of the program when errors occur

Answer: c) To identify and handle errors that occur in the program

Explanation: Exception handling is used to identify and handle errors that occur during the execution of a program. By using exception handling, we can gracefully handle errors and prevent the program from crashing.

Q2. What is an Exception in Python?

a) An error that occurs during the execution of a program
b) A warning message that is displayed during the execution of a program
c) A syntax error that occurs when writing code in Python
d) A logical error that occurs when the program does not produce the expected output

Answer: a) An error that occurs during the execution of a program

Explanation: An exception is an error that occurs during the execution of a program. It is caused by something unexpected happening during the execution of a program, such as a divide by zero error, a file not found error, or a type error.

Q3. What is the syntax for handling exceptions in Python?

a) try-except
b) try-catch
c) try-exception
d) try-finally

Answer: a) try-except

Explanation: The syntax for handling exceptions in Python is try-except. The try block contains the code that might raise an exception, and the except block contains the code that will handle the exception if it occurs.

Q4. Which of the following is NOT a type of built-in exception in Python?

a) ZeroDivisionError
b) TypeError
c) FileNotFoundError
d) DatabaseError

Answer: d) DatabaseError

Explanation: While ZeroDivisionError, TypeError, and FileNotFoundError are all built-in exceptions in Python, DatabaseError is not. DatabaseError is a base class for exceptions that occur when interacting with a database, but it is not a built-in exception in Python.

Q5. What is the purpose of the else block in Python exception handling?

a) To handle the exception that occurred in the try block
b) To execute code if no exception occurred in the try block
c) To execute code after the finally block has executed
d) To ignore the exception that occurred in the try block

Answer: b) To execute code if no exception occurred in the try block

Explanation: The else block in Python exception handling is used to execute code if no exception occurred in the try block. If an exception occurs in the try block, the code in the else block will not be executed. The else block is optional and can be omitted if it is not needed.

Q6. What is the purpose of the finally block in Python exception handling?

a) To handle the exception that occurred in the try block
b) To execute code if no exception occurred in the try block
c) To execute code after the try and except blocks have executed, regardless of whether an exception occurred or not
d) To ignore the exception that occurred in the try block

Answer: c) To execute code after the try and except blocks have executed, regardless of whether an exception occurred or not

Explanation: The finally block in Python exception handling is used to execute code after the try and except blocks have executed, regardless of whether an exception occurred or not. This is useful for cleaning up resources, closing files, and releasing locks. The finally block is optional and can be omitted if it is not needed.

Q7. Can multiple except blocks be used in a single try block in Python?

a) No, only one except block can be used in a try block
b) Yes, multiple except blocks can be used in a try block to handle different types of exceptions
c) Yes, but each except block must be enclosed in its own try block
d) No, each exception must be handled separately in a separate function

Answer: b) Yes, multiple except blocks can be used in a try block to handle different types of exceptions

Explanation: Multiple except blocks can be used in a single try block in Python to handle different types of exceptions. Each except block will catch a specific type of exception, and the code in that block will be executed if that type of exception occurs.

Q8. What is the purpose of the raise statement in Python?

a) To raise an exception manually
b) To handle an exception that occurred in the program
c) To ignore an exception that occurred in the program
d) To exit the program when an exception occurs

Answer: a) To raise an exception manually

Explanation: The raise statement in Python is used to raise an exception manually. This is useful when you want to handle a specific error condition in your program and raise an exception to signal that condition. When a raise statement is executed, it causes the specified exception to be raised, which can then be handled by an except block.

Q9. What is the difference between a try-except block and a try-finally block in Python?

a) A try-except block is used to handle exceptions, while a try-finally block is used for cleanup code
b) A try-except block is used for cleanup code, while a try-finally block is used to handle exceptions
c) A try-except block is used to handle exceptions, while a try-finally block is used to execute code after the try and except blocks have executed
d) There is no difference between a try-except block and a try-finally block in Python

Answer: c) A try-except block is used to handle exceptions, while a try-finally block is used to execute code after the try and except blocks have executed

Explanation: The try-except block in Python is used to handle exceptions that may occur during the execution of a program, while the try-finally block is used to execute code after the try and except blocks have executed, regardless of whether an exception occurred or not. The try-finally block is useful for cleanup code, such as releasing resources or closing files, that needs to be executed regardless of whether an exception occurred.

 

Q10. What is the recommended approach to handling exceptions in Python?

a) Ignoring exceptions and letting the program crash if an error occurs
b) Using try-except blocks to handle exceptions as they occur
c) Using try-finally blocks to handle exceptions as they occur
d) Using if statements to check for potential

Q11. What is the syntax for raising a custom exception in Python?

a) raise Exception(“Custom Exception”)
b) raise custom_exception(“Custom Exception”)
c) raise custom_exception
d) custom_exception(“Custom Exception”)

Answer: b) raise custom_exception(“Custom Exception”)

Explanation: To raise a custom exception in Python, you need to define a new exception class that inherits from the built-in Exception class. You can then raise an instance of your custom exception class using the raise statement, passing a message string as an argument. The syntax for raising a custom exception is: raise custom_exception(“Custom Exception”).

Q12. What is the purpose of the else block in Python exception handling?

a) To handle the exception that occurred in the try block
b) To execute code if no exception occurred in the try block
c) To execute code after the try and except blocks have executed, regardless of whether an exception occurred or not
d) To ignore the exception that occurred in the try block

Answer: b) To execute code if no exception occurred in the try block

Explanation: The else block in Python exception handling is used to execute code if no exception occurred in the try block. This block is optional and follows all the except blocks. If no exception occurred in the try block, the code in the else block will be executed. If an exception occurred, the code in the else block will be skipped.

Q13. What is the purpose of the assert statement in Python?

a) To raise an exception if a condition is not true
b) To handle an exception that occurred in the program
c) To ignore an exception that occurred in the program
d) To exit the program when an exception occurs

Answer: a) To raise an exception if a condition is not true

Explanation: The assert statement in Python is used to raise an exception if a condition is not true. The syntax of the assert statement is: assert condition, message. If the condition is true, the program continues to execute normally. If the condition is false, an AssertionError is raised with the specified error message.

Q14. What is the purpose of the traceback module in Python?

a) To handle exceptions that occur in the program
b) To display a traceback of the current call stack
c) To ignore exceptions that occur in the program
d) To exit the program when an exception occurs

Answer: b) To display a traceback of the current call stack

Explanation: The traceback module in Python is used to display a traceback of the current call stack. A traceback is a report of the function calls that led up to an exception. It shows the line of code that caused the exception and the line of code that called that line, and so on, all the way back to the top-level of the program. The traceback module can be used to display this information in a human-readable format.

Q15. What is the purpose of the finally block in Python exception handling?

a) To handle the exception that occurred in the try block
b) To execute code if no exception occurred in the try block
c) To execute code after the try and except blocks have executed, regardless of whether an exception occurred or not
d) To ignore the exception that occurred in the try block

Answer: c) To execute code after the try and except blocks have executed, regardless of whether an exception occurred or not

Explanation: The finally block in Python exception handling is used to execute code after the try and except blocks have executed, regardless of whether an exception occurred or not. This block is optional and follows all the except blocks. The code in the finally block is executed even if an exception occurred in the try block and was handled by an except block, or if the try block executed successfully with no exceptions. The purpose of the finally block is to ensure that certain cleanup actions, such as closing a file or a network connection, are always performed.

Q16. Which of the following is an example of a built-in exception in Python?

a) FileExistsError
b) FileNotFoundError
c) ZeroDivisionError
d) TypeError

Answer: c) ZeroDivisionError

Explanation: ZeroDivisionError is an example of a built-in exception in Python. It is raised when attempting to divide by zero. The other options are also examples of built-in exceptions in Python, but they are related to file operations and data types, respectively.

Q17. What is the purpose of the except block in Python exception handling?

a) To handle the exception that occurred in the try block
b) To execute code if no exception occurred in the try block
c) To execute code after the try and except blocks have executed, regardless of whether an exception occurred or not
d) To ignore the exception that occurred in the try block

Answer: a) To handle the exception that occurred in the try block

Explanation: The except block in Python exception handling is used to handle the exception that occurred in the try block. This block is optional and follows the try block. It specifies the type of exception to catch and what code to execute when the exception occurs. Multiple except blocks can be used to catch different types of exceptions. If no exception occurred in the try block, the code in the except block will be skipped.

Q18. Which of the following is NOT true about Python exception handling?

a) It is used to handle unexpected errors that occur during program execution
b) It uses try, except, and finally blocks to manage exceptions
c) It is not recommended to catch and handle all exceptions in a single except block
d) It is recommended to use the assert statement to handle expected errors in the program

Answer: d) It is recommended to use the assert statement to handle expected errors in the program

Explanation: The assert statement in Python is used to raise an exception if a condition is not true. It is typically used to check for programming errors, not expected errors. Python exception handling is used to handle unexpected errors that occur during program execution. It uses try, except, and finally blocks to manage exceptions. It is recommended to catch and handle specific exceptions in separate except blocks and to use the assert statement for programming errors.

Q19. How can you raise your own exception in Python?

a) Using the raise keyword followed by the name of the exception class
b) Using the throw keyword followed by the name of the exception class
c) Using the assert keyword followed by the name of the exception class
d) Using the try keyword followed by the name of the exception class

Answer: a) Using the raise keyword followed by the name of the exception class

Explanation: You can raise your own exception in Python using the raise keyword followed by the name of the exception class. For example, if you want to raise an exception when a certain condition is not met, you can write code like this:

code

if x < 0:
raise ValueError(“x cannot be negative”)
This will raise a ValueError with the message “x cannot be negative” if x is less than 0.

Q20. What happens if an exception is not caught in Python?

a) The program will continue running as if nothing happened
b) The program will terminate with an error message
c) The program will enter an infinite loop
d) The program will prompt the user to handle the exception

Answer: b) The program will terminate with an error message

Explanation: If an exception is not caught in Python, the program will terminate with an error message. The error message will typically include information about the type of exception that occurred, as well as the line number where the exception occurred. It is important to handle exceptions in your code to prevent the program from terminating unexpectedly.

Page : 1           2           3           4

12 Class- Exception Handling in Python

What are exceptions?
Exceptions are events that occur during program execution that disrupt the normal flow of the program and can lead to errors. Examples of exceptions include division by zero, invalid input, file not found, etc.

Handling exceptions
In Python, exceptions can be handled using the try-except block. The code that might raise an exception is placed inside the try block, and the code to handle the exception is placed inside the except block.

Types of exceptions
Python has many built-in exceptions, including NameError, TypeError, ValueError, ZeroDivisionError, etc. It is also possible to create custom exceptions using the raise statement.

Raising exceptions
Developers can raise exceptions manually using the raise statement. This is useful when an error occurs that cannot be handled by the program, and it needs to be passed on to a higher-level function or the user.

Finally block
The finally block is a piece of code that is executed whether or not an exception is raised. It is typically used for cleanup code, such as closing files or releasing resources.

Exception hierarchy
Python has a hierarchy of exception classes, with the base class being Exception. This allows developers to catch multiple exceptions at once using a single except block.

Debugging
Exception handling is an essential tool for debugging Python programs. By handling exceptions gracefully, developers can identify and fix errors in their code more easily.

Page : 1           2           3           4

Class 10 Artificial Intelligence Unit 1 Introduction to Artificial Intelligence

Class 10 Artificial Intelligence Unit 1 Introduction to Artificial Intelligence Session 1 : Foundational Concepts of Artificial Intelligence MCQs and Question Answers

Unit 1 Introduction to Artificial Intelligence

Session 1 : Foundational Concepts of Artificial Intelligence

Multipole choice Questions  and Question Answers

1. To make decisions, _________ is necessary.
(a) Information (b) Intelligence (c) Both a and b (d) Programming

Answer: (b) Intelligence

2. What stimulates us to initiate the process of reasoning?
(a) Process (b) Sensing (c) Reasoning (d) Programming

Answer: (b) Sensing

3. The processing of what we sense is called ______?
(a) Process (b) Sensing (c) Reasoning (d) Programming

Answer: (c) Reasoning

4. A man-made thing is called _______?
(a) Artificial (b) Ready Made (c) Intelligence (d) Artificial Intelligence

Answer: (a) Artificial.

5. To be able to learn different languages ________ intelligence is required.
(a) Spatial visual (b) Existential (c) Linguistic (d) Naturalist

Answer: (c) Linguistic

6. To be able to interact and related with the surroundings and environment is _______ intelligence
(a) Naturalist (b) Climate (c) Intrapersonal (d) None

Answer: (c) Naturalist

7. Ability to understand the logic, theories, formulae, and expressions is _______ intelligence
(a) Naturalist (b) Climate (c) Intrapersonal (d) Mathematical and Logical

Answer: (d) Mathematical and Logical

8. Communicating with others with an understanding of ow they feel and perceive is _______ intelligence
(a) Interpersonal (b) Intrapersonal (c) Existential (d) Mathematical and Logical

Answer: (a) Interpersonal

9. Amrit has the ability to create, identify and understand rhymes and patterns. He is having _______ intelligence
(a) Naturalist (b) Climate (c) Intrapersonal (d) Musical

Answer: (d) Musical

10. Intelligence is directly related to ______.
(a) Fact (b) Learning (c) Brain (d) None

Answer: (b) Learning

11. Intelligence can be defined as ______.
(a) Ability to interact with the real-world

(b) Reasoning and Planning

(c) Learning and adaptation

(d) All of the above

Answer: (d) All of the above

Question Answers – 1 Mark & 2 Marks.

1. What is Artificial? (1 Mark)
Answer: Artificial is something that is man-made, which does not occur naturally.

2. What is Intelligence? (1 Mark)
Answer: Intelligence is the ‘ability to perceive or infer information, and to retain it as knowledge to be applied towards adaptive behaviours within an environment or context.’

Intelligence is the ability to observe and understand information, and preserve it as knowledge to apply in various situations or context through suitable adaptive behaviours.

3. What simulates us initiating the process of reasoning? (1 Mark)
Answer: Sensing

4. What is reasoning? (1 Mark)
Answer: The process of sensing (i.e. input – through sense, perceive, receive ) is known as reasoning.

5. How many types of reasoning? (1 Mark)
Answer: There are two types of reasoning – Deductive and Inductive

6. Define:- Deductive and Inductive? (2 Marks)
Answer: Deductive: In deductive reasoning facts are analysed and guarantee a conclusion.

Inductive: In inductive reasoning, facts only support the conclusion without any guarantee.

7. By which organ a human can sense? (1 Mark)
Answer: A human can sense through receptive organs (eye, ear, nose, mouse, skin, etc).

8. How a machine can sense? (1 Mark)
Answer: Machine can sense through the following sensing devices –

Image scanner, Audio sensor, Speech recognition engine, Biometric sensor, motion sensor, thermal sensor, light sensor, Proximity (distance) sensor, etc.

9. What are the different traits/characteristics/types of intelligence? (2 Marks)
Answer: The abilities/traits that are involved in intelligence are: –

Natural Intelligence
Interpersonal Intelligence
Intrapersonal Intelligence
Musical Intelligence
Mathematical Logical Reasoning Intelligence
Linguistics Intelligence
Spatial Visual Intelligence
Kinaesthetic Intelligence
Existential Intelligence
10. What is Mathematical Logical Reasoning intelligence? (1 Mark)
Answer: A person’s ability to regulate, measure, and understand numerical symbols, abstraction, and logic, known as Mathematical Logical Reasoning. Mathematical Logical Reasoning are carried out in the left, back and front of the brains.

11. What is Linguistic intelligence? (1 Mark)
Answer: Linguistic Intelligence means language processing skills both in terms of understanding or implementation in writing or verbally.

12. What is Spatial Visual intelligence? (1 Mark)
Answer: Spatial Visual intelligence is the ability to perceive the visual world and the relationship of one object to another.

13. What is Kineasthetic intelligence? (1 Mark)
Answer: Kinaesthetic Intelligence is the ability that is related to how a person uses his limbs in a skilled
manner.

14. What is Musical intelligence? (1 Mark)
Answer: Musical Intelligence is about a person’s ability to recognize and create sounds, rhythms, and sound patterns

15. What is Intrapersonal intelligence? (1 Mark)
Answer: Intrapersonal Intelligence describes how high the level of self-awareness someone has. Starting from realizing weakness, and strength, to his own feelings

16. What is Interpersonal intelligence? (1 Mark)
Answer: Interpersonal Intelligence is the ability to communicate with others by understanding other people’s feelings & influence the person.

17. What is Existential intelligence? (1 Mark)
Answer: Existential Intelligence is an additional category of intelligence relating to religious and spiritual awareness.

18. What is Naturalist intelligence? (1 Mark)
Answer: Naturalist Intelligence is an additional category of intelligence relating to the ability to process the information of the environment around us.

19. What is the difference between Interpersonal intelligence and Intrapersonal Intelligence? (1 Mark)
Answer: Interpersonal Intelligence involves interpersonal skills, communication skills, and ability to interact with others, while Intrapersonal intelligence involves the skills of self-awareness, self-discipline, self-management, self-evaluation, etc.

20. What is AI? (1 Mark)
Answer: AI is the science and engineering of making Intelligent machines.

21. What do you mean by AI? (1 Mark)
Answer: AI refers to the study of principles, concepts and technology for building such machines and systems that should think, act and learn like humans.

22. What are the AI challenges to making machine intelligence? (2 Marks)
Answer: AI challenges are

Retain the facts as knowledge
Recall the knowledge in a situation.
Think, analyse and apply logic.
Make useful and accurate predictions.
Make decisions and upgrade their intelligence algorithm themselves.

23. How do machines become artificially intelligent? (1 Mark)
Answer: Machine becomes intelligent once they are trained with some information that helps them achieve their tasks.

AI machines also keep updating their knowledge to optimise their output.

24. What is the most important part of intelligence? (1 Mark)
Answer: Decision Making

25. What is decision-making? (1 Mark)
Answer: Decision-making is the capability that helps in taking the right decisions in different situations.

The basis of decision-making depends upon the availability of information and how we experience and understand it.

26. You are locked inside a room with 3 doors to move out of the locked room and you need to find a safe door to get your way out. (2 Mark)
Behind the 1st door is a lake with a deadly shark.
The 2nd door has a mad psychopath ready to kill with a weapon and
The third one has a lion that has not eaten since the last 2 months.?
Answer: Gate number 3.

The reason being that since the lion has not eaten for 2 months, he wouldn’t have survived till now and would already be dead.

27. What is Artificial intelligence? (1 Mark)
Answer: When a machine possesses the ability to mimic human traits, i.e., make decisions, predict the future, learn and improve on its own, it is said to have artificial intelligence.

OR

A machine is artificially intelligent when it can accomplish tasks by itself – collect data, understand it, analyze it, learn from it, and improve it.

28. Write the name of Applications of Artificial Intelligence around us? (2 Marks)
Answer: Articificially Intelligence applications are-

(a) Google Assitant (b) Auto Suggest and auto-corrects (c) Cortana (d) Siri (e) Google Maps (f) Weather forecast (g) Real Time Language Translators (h) Sophia (i) Biometric Security Sensor

29. Give the example of voice assistance? ( 2 Marks)
Answer: Voice Assistants are – (a) Google Assitant (b) Cortana (c) Siri (d) Alexa

30. How do machines become Artificially Intelligent? ( 2 Marks)
Answer: Machines become intelligent once they are trained with some information that helps them achieve their tasks.

AI machines also keep updating their knowledge to optimise their output.

31. How do humans become intelligent? (1 Marks)
Answer: Humans become more and more intelligent with time as they gain experiences during their lives.

32. Give two examples where Google is using the concept of AI? (2 Marks)
Answer: (i) Google Search (ii) Auto suggests & Auto corrects.

33. Give 4 examples of pocket voice assistants. (2 Marks)
Answer: Very common examples of pocket voice assistants are Alexa, Google Assistant, Cortana, and Siri.

34. Give one example which helps in direction. (1 Mark)
Answer: Google Map

35. How AI changes the experiences of Gaming? (1 Marks)
Answer: A lot of games nowadays are backed up with AI which helps in enhancing the graphics, coming up with new difficulty levels, encouraging gamers, etc. For example: EA Sports (FIFA)

36. How AI taking care of our habits, like & dislike ? (2 Marks)
Answer: AI has not only made our lives easier but has also been taking care of our habits, likes, and dislikes. Due to AI following changes are noticed in our daily life: –

Netflix, Amazon, Spotify, YouTube, etc. show us recommendations on the basis of what we like.
Cater to our needs of connecting with friends on social media platforms with apps like Facebook and Instagram.
Send us customized notifications about our online shopping details, auto-create playlists according to our requests, and so on.
Taking selfies was never this fun as Snapchat filters make them look so cool

37. How AI helps in health? (1 Marks)
Answer: AI is also being used to monitor our health. A lot of chatbots and other health apps are available, which continuously monitor the physical and mental health of its users.

Arguments of Python

Arguments of Python

1.What is an argument in Python?

A. A variable declared inside a function
B. A value passed to a function when calling it
C. A method used to modify an object
D. A conditional statement used in a loop

Answer :B
Explanation: B. An argument in Python refers to a value passed to a function when calling it.

2.What is a default argument in Python?

A. A variable declared inside a function
B. A value passed to a function when calling it
C. A value assigned to a parameter in a function definition
D. A method used to modify an objectA


Answer :C
Explanation: C. A default argument in Python is a value assigned to a parameter in a function definition, which is used if no argument is passed for that parameter when calling the function.

3.How can you define a function in Python with arguments?

A. def function_name():
B. def function_name(arguments):
C. def function_name(*arguments):
D. def function_name(arguments,*args):

Answer : D
Explanation: D. You can define a function in Python with arguments by using the syntax def function_name(arguments,*args):, where “arguments” refers to the required arguments and “*args” refers to any additional arguments.

4.What is a keyword argument in Python?

A. An argument passed by its name instead of position
B. An argument that must be passed to a function
C. An argument that is optional when calling a function
D. An argument that has a default value

Answer :A
Explanation: A. A keyword argument in Python is an argument passed by its name instead of position, which allows you to specify the value of any parameter by its name.

5.What is a variable-length argument in Python?

A. An argument passed by its name instead of position
B. An argument that must be passed to a function
C. An argument that is optional when calling a function
D. An argument that can take any number of values

Answer 😀
Explanation: D. A variable-length argument in Python is an argument that can take any number of values, which is defined using the syntax *args in a function definition.

6.What is the purpose of the **kwargs argument in Python?

A. To pass a variable number of arguments to a function
B. To pass keyword arguments to a function
C. To define a function that takes a variable number of arguments
D. To define a function that takes keyword arguments

Answer :B
Explanation: B. The **kwargs argument in Python is used to pass keyword arguments to a function.

7.How can you pass arguments to a Python function as a tuple?

A. By using the syntax *args when calling the function
B. By using the syntax **kwargs when calling the function
C. By using the syntax *args in the function definition
D. By using the syntax **kwargs in the function definition

Answer :A
Explanation: A. You can pass arguments to a Python function as a tuple by using the syntax *args when calling the function, which converts the arguments to a tuple.


8.How can you pass arguments to a Python function as a dictionary?

A. By using the syntax *args when calling the function
B. By using the syntax **kwargs when calling the function
C. By using the syntax *args in the function definition
D. By using the syntax **kwargs in the function definition

Answer :B
Explanation: B. You can pass arguments to a Python function as a dictionary by using the syntax **kwargs when calling the function, which converts the arguments to a dictionary.

9.Can a Python function have both required and optional arguments?

A. Yes, a function can have both required and optional arguments.
B. No, a function can only have either required or optional arguments.
C. It depends on the version of Python being used.
D. It depends on the type of function being defined.

Answer :A
Explanation: A. A Python function can have both required and optional arguments by defining the required arguments first, followed by any optional arguments with default values.

10.How can you pass a list as an argument to a Python function?

A. By passing the list as a comma-separated series of values
B. By enclosing the list in square brackets when calling the function
C. By using the syntax *args when calling the function
D. By using the syntax **kwargs when calling the function

Answer :B
Explanation: B. You can pass a list as an argument to a Python function by enclosing the list in square brackets when calling the function.

11.What is the purpose of the * operator in Python function arguments?

A. To unpack a list or tuple into separate arguments
B. To pack a list or tuple into a single argument
C. To pass a dictionary as an argument
D. To specify a required argument

Answer :A
Explanation: A. The * operator in Python function arguments is used to unpack a list or tuple into separate arguments.

12.What is the purpose of the ** operator in Python function arguments?

A. To unpack a list or tuple into separate arguments
B. To pack a list or tuple into a single argument
C. To pass a dictionary as an argument
D. To specify a required argument

Answer :C
Explanation: C. The ** operator in Python function arguments is used to pass a dictionary as an argument.

13.Can you specify the order of optional arguments in a Python function?

A. Yes, the order of optional arguments can be specified in a Python function.
B. No, the order of optional arguments is always fixed.
C. It depends on the version of Python being used.
D. It depends on the type of function being defined.

Answer :A
Explanation: A. The order of optional arguments in a Python function can be specified by defining the required arguments first, followed by the optional arguments with default values in the desired order.

14.What is the purpose of the *args and **kwargs arguments in a Python function?

A. To pass a variable number of arguments to a function
B. To define a function that takes a variable number of arguments
C. To pass keyword arguments to a function
D. To specify the order of optional arguments in a function

Answer :A
Explanation: A. The *args and **kwargs arguments in a Python function are used to pass a variable number of arguments and/or keyword arguments to a function.

15.What is the difference between a mutable and immutable object in Python?

A. A mutable object can be changed after it is created, while an immutable object cannot be changed.
B. A mutable object can only be changed using the del statement, while an immutable object cannot be changed.
C. A mutable object can only be changed using a method, while an immutable object cannot be changed.
D. A mutable object cannot be used as an argument to a function, while an immutable object can.

Answer :A
Explanation: A. The difference between a mutable and immutable object in Python is that a mutable object can be changed after it is created, while an immutable object cannot be changed.a

16.What is the purpose of the * symbol when used with a list in Python?

A. To pack the list into a single argument
B. To unpack the list into separate arguments
C. To convert the list to a tuple
D. To reverse the order of the elements in the list

Answer :B
Explanation: B. The * symbol when used with a list in Python is used to unpack the list into separate arguments.

17.What is the difference between keyword arguments and positional arguments in Python?

A. Keyword arguments are optional, while positional arguments are required.
B. Keyword arguments are specified using a keyword, while positional arguments are specified based on their position.
C. Keyword arguments are always passed as dictionaries, while positional arguments are always passed as tuples.
D. There is no difference between keyword arguments and positional arguments.

Answer :B
Explanation: B. The difference between keyword arguments and positional arguments in Python is that keyword arguments are specified using a keyword, while positional arguments are specified based on their position.

Arguments of Python

18.Can a Python function have a default value for a required argument?

A. Yes, a function can have a default value for a required argument.
B. No, a function cannot have a default value for a required argument.
C. It depends on the version of Python being used.
D. It depends on the type of function being defined.

Answer :B
Explanation: B. A required argument in Python cannot have a default value. If a default value is specified, the argument becomes optional.

19.What is the purpose of the * operator in Python function calls?

A. To unpack a list or tuple into separate arguments
B. To pack a list or tuple into a single argument
C. To pass a dictionary as an argument
D. To specify a required argument

Answer :A
Explanation: A. The * operator in Python function calls is used to unpack a list or tuple into separate arguments.

20.What is the purpose of the ** operator in Python function calls?

A. To unpack a list or tuple into separate arguments
B. To pack a list or tuple into a single argument
C. To pass a dictionary as an argument
D. To specify a required argument

Answer :C
Explanation: C. The ** operator in Python function calls is used to pass a dictionary as an argument.

21.What is the difference between a function and a method in Python?

A. A function is defined inside a class, while a method is defined outside of a class.
B. A function operates on objects of a specific type, while a method operates on the class as a whole.
C. A function is a standalone block of code, while a method is associated with an object or class.
D. There is no difference between a function and a method.

Answer :C
Explanation: C. The difference between a function and a method in Python is that a function is a standalone block of code, while a method is associated with an object or class.

22.Can a Python function take no arguments?

A. Yes, a function can take no arguments in Python.
B. No, a function must always take at least one argument.
C. It depends on the version of Python being used.
D. It depends on the type of function being defined.

Answer :A
Explanation: A. A Python function can take no arguments by simply defining the function with no parameters.

23.Can a Python function take an infinite number of arguments?

A. Yes, a function can take an infinite number of arguments in Python.
B. No, a function can only take a finite number of arguments.
C. It depends on the version of Python being used.
D. It depends on the type of function being defined.

Answer :A
Explanation: A. A Python function can take an infinite number of arguments by using the *args or **kwargs syntax.

24.What is the purpose of the lambda keyword in Python?

A. To define a function
B. To define a class
C. To define a variable
D. To define a loop

Answer :A
Explanation: A. The lambda keyword in Python is used to define a function, specifically an anonymous function that can be defined in one line.

25.What is the purpose of the *args syntax in a Python function definition?

A. To specify required arguments
B. To specify keyword arguments
C. To specify an arbitrary number of positional arguments
D. To specify an arbitrary number of keyword arguments

Answer :C
Explanation: C. The *args syntax in a Python function definition is used to specify an arbitrary number of positional arguments.

26.What is the purpose of the **kwargs syntax in a Python function definition?

A. To specify required arguments
B. To specify keyword arguments
C. To specify an arbitrary number of positional arguments
D. To specify an arbitrary number of keyword arguments

Answer 😀
Explanation: D. The **kwargs syntax in a Python function definition is used to specify an arbitrary number of keyword arguments.

27.What is the difference between a mutable and immutable object in Python?

A. A mutable object can be changed after it is created, while an immutable object cannot be changed.
B. A mutable object cannot be changed after it is created, while an immutable object can be changed.
C. A mutable object is a subclass of an immutable object.
D. There is no difference between a mutable and immutable object.

Answer :A
Explanation: A. The difference between a mutable and immutable object in Python is that a mutable object can be changed after it is created, while an immutable object cannot be changed. Examples of mutable objects include lists and dictionaries, while examples of immutable objects include strings and tuples.

28.What is the purpose of the * operator in Python function definitions?

A. To unpack a list or tuple into separate arguments
B. To pack a list or tuple into a single argument
C. To specify a required argument
D. To specify a keyword argument

Answer :B
Explanation: B. The * operator in Python function definitions is used to pack a list or tuple into a single argument.

29.What is the purpose of the ** operator in Python function definitions?

A. To unpack a list or tuple into separate arguments
B. To pack a list or tuple into a single argument
C. To specify a required argument
D. To specify a keyword argument

Answer 😀
Explanation: D. The ** operator in Python function definitions is used to specify a keyword argument.

30.What is the purpose of the default argument value in a Python function definition?

A. To specify a required argument
B. To specify a keyword argument
C. To specify a default value for an optional argument
D. To specify a default value for a required argument

Answer :C
Explanation: C. The purpose of the default argument value in a Python function definition is to specify a default value for an optional argument. If the argument is not specified by the caller, it will use the default value.

31.What is the purpose of the * symbol when used in a function call in Python?

A. To unpack a list or tuple into separate arguments
B. To pack a list or tuple into a single argument
C. To specify a required argument
D. To specify a keyword argument

Answer :A
Explanation: A. The * symbol when used in a function call in Python is used to unpack a list or tuple into separate arguments.

32.What is the purpose of the ** symbol when used in a function call in Python?

A. To unpack a list or tuple into separate arguments
B. To pack a list or tuple into a single argument
C. To specify a required argument
D. To specify a keyword argument

Answer :B
Explanation: B. The ** symbol when used in a function call in Python is used to pack a list or tuple into a single argument.

33.Which of the following is not a valid way to pass arguments to a Python function?

A. By position
B. By keyword
C. By reference
D. As a default value

Answer :C
Explanation: C. Passing arguments to a Python function by reference is not a valid option. Arguments are passed by value in Python, which means that the function receives a copy of the argument, not the original object.

34.What is the purpose of the *args and **kwargs syntax in a Python function call?

A. To specify required arguments
B. To specify keyword arguments
C. To specify an arbitrary number of positional and keyword arguments
D. To specify a default value for an optional argument

Answer :C
Explanation: C. The *args and **kwargs syntax in a Python function call is used to specify an arbitrary number of positional and keyword arguments.

Arguments of Python

35.What is the difference between *args and **kwargs in Python?

A. *args is used for positional arguments and **kwargs is used for keyword arguments.
B. *args is used for keyword arguments and **kwargs is used for positional arguments.
C. *args is used for an arbitrary number of positional arguments and **kwargs is used for an arbitrary number of keyword arguments.
D. There is no difference between *args and **kwargs in Python.

Answer :C
Explanation: C. The difference between *args and **kwargs in Python is that *args is used for an arbitrary number of positional arguments, while **kwargs is used for an arbitrary number of keyword arguments.

36.Which of the following is not a valid way to specify a default value for an argument in a Python function definition?

A. default=None
B. default=[]
C. default={}
D. default=()

Answer 😀
Explanation: D. Using default=() is a valid way to specify a default value for a function argument in Python.

37.What is the purpose of the *args syntax in a Python function definition?

A. To specify required arguments
B. To specify keyword arguments
C. To specify an arbitrary number of positional arguments
D. To specify a default value for an optional argument

Answer :C
Explanation: C. The *args syntax in a Python function definition is used to specify an arbitrary number of positional arguments.

38.What is the purpose of the **kwargs syntax in a Python function definition?

A. To specify required arguments
B. To specify keyword arguments
C. To specify an arbitrary number of positional arguments
D. To specify a default value for an optional argument

Answer :B
Explanation: B. The **kwargs syntax in a Python function definition is used to specify keyword arguments.

39.Which of the following is true about Python function argument order?

A. Required positional arguments must come before optional positional arguments.
B. Optional positional arguments must come before required positional arguments.
C. Keyword arguments must come before positional arguments.
D. Positional arguments must come before keyword arguments.

Answer :A
Explanation: A. In Python, required positional arguments must come before optional positional arguments in a function definition.

Variables And Operators of Python

Variables And Operators of Python

1.What is the correct syntax to declare a variable in Python?

a) var = 5
b) variable = 5
c) variable == 5
d) var 5

Answer: b) variable = 5

Explanation: In Python, the syntax to declare a variable is to write the variable name followed by an equal sign and the value you want to assign to the variable. Therefore, option b) is the correct syntax to declare a variable in Python.

2.What is the modulus operator used for in Python?

a) It multiplies two numbers.
b) It divides two numbers.
c) It returns the remainder of a division operation.
d) It performs exponentiation.

Answer: c) It returns the remainder of a division operation.

Explanation: The modulus operator (%) returns the remainder of a division operation. For example, 7 % 3 would return 1, because 3 goes into 7 twice with a remainder of 1.

3.Which operator is used for exponentiation in Python?

a) *
b) ^
c) **
d) //

Answer: c) **

Explanation: The double asterisk operator (**) is used for exponentiation in Python. For example, 2 ** 3 would return 8, because 2 to the power of 3 is 8.


4.What is the difference between the “==” operator and the “=” operator in Python?

a) The “==” operator assigns a value to a variable, while the “=” operator compares two values for equality.
b) The “==” operator compares two values for equality, while the “=” operator assigns a value to a variable.
c) The “==” operator performs addition, while the “=” operator performs assignment.
d) The “==” operator performs multiplication, while the “=” operator performs division.


Answer: b) The “==” operator compares two values for equality, while the “=” operator assigns a value to a variable.

Explanation: The “==” operator is used to compare two values for equality. For example, 5 == 5 would return True, because 5 is equal to 5. The “=” operator is used to assign a value to a variable. For example, x = 5 would assign the value of 5 to the variable x.

5.What is the result of the following operation: 10 / 3 ?

a) 3.33
b) 3
c) 3.0
d) 4

Answer: a) 3.33

Explanation: In Python, when you divide two integers, the result will be a float if the division results in a decimal. Therefore, the result of 10 / 3 would be 3.33.

6.Which operator is used for floor division in Python?

a) *
b) //
c) %
d) **


Answer: b) //

Explanation: The double slash operator (//) is used for floor division in Python. Floor division returns the largest integer less than or equal to the result of the division operation. For example, 7 // 3 would return 2, because the largest integer less than or equal to 2.33 is 2.

7.What is the order of precedence for arithmetic operators in Python?

a) +, -, *, /, %, **
b) **, *, /, %, +, –
c) *, /, %, +, -, **
d) **, %, *, /, +, –

Answer: b) **, *, /, %, +, –

Explanation: In Python, the order of precedence for arithmetic operators is as follows: exponentiation (**), multiplication (*), division (/), modulus (%), addition (+), and subtraction (-).

8.What is the result of the following operation: 5 * 2 ** 3?

a) 16
b) 40
c) 80
d) 256

Answer: c) 80

Explanation: In Python, exponentiation has a higher order of precedence than multiplication. Therefore, the expression 2 ** 3 will be evaluated first, resulting in 8. Then, the expression 5 * 8 will be evaluated, resulting in 40. Therefore, the final result is 40.

9.What is the result of the following operation: 15 % 4?

a) 3
b) 3.75
c) 4
d) 3.5

Answer: a) 3

Explanation: The modulus operator (%) returns the remainder of a division operation. In this case, 15 divided by 4 is 3 with a remainder of 3. Therefore, the result of 15 % 4 is 3.

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

a) 1st_place
b) first_place
c) first-place
d) FirstPlace

Answer: b) first_place

Explanation: In Python, variable names must begin with a letter or underscore. They can then be followed by any combination of letters, numbers, and underscores. Therefore, option b) first_place is a valid variable name in Python. Option a) 1st_place is invalid because variable names cannot begin with a number. Option c) first-place is invalid because variable names cannot contain hyphens. Option d) FirstPlace is valid, but not the recommended convention as the convention for variable names in Python is lowercase_with_underscores.

11.What is the result of the following operation: 4 + 5.0?

a) 9
b) 9.0
c) 45
d) TypeError

Answer: b) 9.0

Explanation: In Python, when you add an integer to a float, the result will be a float. Therefore, the result of 4 + 5.0 would be 9.0.

12.What is the result of the following operation: 2 ** 3 ** 2?

a) 64
b) 512
c) 40353607
d) 134217728

Answer: d) 134217728

Explanation: In Python, exponentiation has right-to-left associativity. Therefore, the expression 3 ** 2 will be evaluated first, resulting in 9. Then, the expression 2 ** 9 will be evaluated, resulting in 512. Therefore, the final result is 512.

13.Which operator is used to check if two values are not equal in Python?

a) !=
b) ==
c) =
d) <>

Answer: a) !=

Explanation: The “!=” operator is used to check if two values are not equal in Python. For example, 5 != 3 would return True, because 5 is not equal to 3.

14.Which operator is used to concatenate two strings in Python?

a) +
b) –
c) *
d) /

Answer: a) +

Explanation: The plus operator (+) is used to concatenate two strings in Python. For example, “hello” + “world” would result in the string “helloworld”.

15.What is the result of the following operation: 10 // 3.0?

a) 3.33
b) 3
c) 3.0
d) 4

Answer: b) 3

Explanation: In Python, when you perform floor division on an integer and a float, the result will be a float. Therefore, the expression 10 // 3.0 would first perform floor division, resulting in 3, and then return the result as a float because one of the operands is a float.

16.Which operator is used to compare two values and return True if the first value is greater than the second value in Python?

a) >
b) <
c) ==
d) >=

Answer: a) >

Explanation: The greater than operator (>) is used to compare two values and return True if the first value is greater than the second value in Python. For example, 5 > 3 would return True, because 5 is greater than 3.

17.What is the result of the following operation: 7 / 2?

a) 3.5
b) 3
c) 4
d) 2

Answer: a) 3.5

Explanation: In Python, when you divide two integers, the result will be a float if the division results in a decimal. Therefore, the result of 7 / 2 would be 3.5.

Variables And Operators of Python

18.Which operator is used to perform logical AND in Python?

a) &&
b) &
c) ||
d) |

Answer: b) &

Explanation: The ampersand (&) operator is used to perform logical AND in Python. For example, (5 > 3) & (7 < 10) would return True, because both expressions evaluate to True.

19.What is the result of the following operation: 3 + 4 * 2?

a) 10
b) 14
c) 11
d) 24

Answer: b) 14

Explanation: In Python, the order of precedence for arithmetic operators is as follows: exponentiation (**), multiplication (*), division (/), modulus (%), addition (+), and subtraction (-). Therefore, the multiplication operation (4 * 2) will be performed first, resulting in 8. Then, the addition operation (3 + 8) will be performed, resulting in 11. Therefore, the final result is 14.

20.Which of the following is a valid way to initialize a variable in Python?

a) variable x = 5
b) x = 5
c) x == 5
d) set x to 5


Answer: b) x = 5

Explanation: In Python, you can initialize a variable by simply assigning a value to it using the equals sign (=). Therefore, option b) x = 5 is a valid way to initialize a variable in Python. Option a) is invalid because variable names cannot have spaces, and the correct syntax is “variable_name = value”. Option c) is a comparison operation and will not initialize a variable. Option d) is not valid Python syntax.

21.Which operator is used to perform logical OR in Python?

a) &&
b) |
c) ||
d) &

Answer: c) ||

Explanation: The double vertical bar (||) operator is not used to perform logical OR in Python. The correct operator to use for logical OR is the double pipe (||) operator. For example, (5 > 3) || (7 < 10) would return True, because at least one of the expressions evaluates to True.

22.What is the result of the following operation: 5 % 2?

a) 2.5
b) 2
c) 1.5
d) 1

Answer: d) 1

Explanation: In Python, the modulus operator (%) returns the remainder of integer division. Therefore, the result of 5 % 2 would be 1, because 5 divided by 2 is 2 with a remainder of 1.a

23.Which operator is used to compare two values and return True if the first value is less than or equal to the second value in Python?

a) <=
b) >=
c) !=
d) ==a

Answer: a) <=

Explanation: The less than or equal to operator (<=) is used to compare two values and return True if the first value is less than or equal to the second value in Python. For example, 3 <= 5 would return True, because 3 is less than or equal to 5.

24.What is the result of the following operation: “hello” * 3?

a) “hellohellohello”
b) “hello3”
c) 9
d) TypeError

Answer: a) “hellohellohello”

Explanation: In Python, when you multiply a string by an integer, the result will be a new string that consists of the original string repeated the specified number of times. Therefore, the result of “hello” * 3 would be the string “hellohellohello”.

25.Which of the following is a valid way to declare a floating-point variable in Python?

a) float x = 5.0
b) x = float(5.0)
c) x = 5
d) x = “5.0”

Answer: b) x = float(5.0)

Explanation: In Python, you can convert an integer or a string to a float by using the float() function. Therefore,
option b) x = float(5.0) is a valid way to declare a floating-point variable in Python.
Option a) is invalid because variable names cannot have spaces, and the correct syntax is “variable_name = value”.
Option c) initializes a variable as an integer, not a float.
Option d) initializes a variable as a string, not a float.

26.Which operator is used to perform exponentiation in Python?

a) ^
b) **
c) *
d) //

Answer: b) **

Explanation: The double asterisk (**) operator is used to perform exponentiation in Python. For example, 2 ** 3 would return 8, because 2 to the power of 3 is 8.

27.What is the result of the following operation: “hello” + “world”?

a) “helloworld”
b) “hello world”
c) “hello+world”
d) TypeError

Answer: a) “helloworld”

Explanation: In Python, when you use the plus (+) operator to concatenate two strings, the result will be a new string that consists of the two original strings combined. Therefore, the result of “hello” + “world” would be the string “helloworld”.

28.Which of the following is a valid way to declare a boolean variable in Python?

a) x = true
b) x = False
c) x = TRUE
d) x = 0

Answer: b) x = False

Explanation: In Python, the boolean values are True and False, with the first letter capitalized. Therefore, option b) x = False is a valid way to declare a boolean variable in Python. Option a) is invalid because the value should be capitalized as False. Option c) is invalid because TRUE is not a valid boolean value in Python. Option d) initializes a variable as an integer, not a boolean.

29.What is the result of the following operation: “hello”[1]?

a) “h”
b) “e”
c) “l”
d) “o”

Answer: b) “e”

Explanation: In Python, you can access individual characters in a string by using indexing. Indexing starts at 0, so “hello”[1] refers to the second character in the string, which is “e”.

30.Which operator is used to perform floor division in Python?

a) //
b) /
c) %
d) **

Answer: a) //

Explanation: The double slash (//) operator is used to perform floor division in Python. Floor division returns the quotient of a division operation, rounded down to the nearest integer. For example, 7 // 2 would return 3, because 7 divided by 2 is 3 with a remainder of 1.

31.Which of the following is NOT a valid variable name in Python?

a) my_variable
b) 123variable
c) MY_VARIABLE
d) _my_variable

Answer: b) 123variable

Explanation: In Python, variable names must start with a letter or an underscore, and can only contain letters, numbers, and underscores. Therefore, option b) 123variable is not a valid variable name in Python because it starts with a number.a

32.What is the result of the following operation: 4 + 2 * 3?

a) 18
b) 10
c) 16
d) 24

Answer: b) 10

Explanation: In Python, when you have multiple operators in a single expression, the order of operations (PEMDAS) is followed: Parentheses, Exponents, Multiplication and Division (from left to right), Addition and Subtraction (from left to right). Therefore, in the expression 4 + 2 * 3, the multiplication is performed first, resulting in 6, and then the addition is performed, resulting in 10.

33.Which operator is used to perform logical NOT in Python?

a) !
b) &&
c) ||
d) &

Answer: a) !

Explanation: The exclamation mark (!) operator is used to perform logical NOT in Python. It is placed in front of a boolean expression and returns the opposite boolean value. For example, !(5 > 3) would return False, because 5 is greater than 3, but the exclamation mark reverses the result to False.

34.What is the result of the following operation: len(“hello”)?

a) “hello”
b) 5
c) 6
d) TypeError

Answer: b) 5

Explanation: In Python, the len() function returns the number of items in a sequence. For strings, it returns the number of characters in the string. Therefore, the result of len(“hello”) would be 5, because “hello” has five characters.

Variables And Operators of Python

35.Which of the following is a valid way to declare an integer variable in Python?

a) x = 3.0
b) x = “3”
c) x = int(3)
d) x = 3++

Answer: c) x = int(3)

Explanation: In Python, you can convert a float or a string to an integer by using the int() function. Therefore, option c) x = int(3) is a valid way to declare an integer variable in Python. Option a) initializes a variable as a float, not an integer. Option b) initializes a variable as a string, not an integer. Option d) is not a valid syntax in Python.

36.What is the result of the following operation: 5 % 2?

a) 2.5
b) 2
c) 3
d) 1.5

Answer: d) 1

Explanation: In Python, the percent (%) operator is used to perform modulus, which returns the remainder of a division operation. Therefore, the result of 5 % 2 would be 1, because 5 divided by 2 is 2 with a remainder of 1.

37.Which operator is used to perform identity comparison in Python?

a) ==
b) !=
c) is
d) not

Answer: c) is

Explanation: The “is” operator is used to perform identity comparison in Python. It tests if two variables refer to the same object in memory. For example, if you have two variables x and y that both point to the same list object [1,2,3], then x is y would return True because they refer to the same object.

38.What is the result of the following operation: 4 / 2?

a) 2.0
b) 2
c) 1.5
d) 0.5

Answer: a) 2.0

Explanation: In Python, the division operator (/) performs floating-point division, which means that it returns a float value even if both operands are integers. Therefore, the result of 4 / 2 would be 2.0, which is a float value.

39.Which of the following is a valid way to declare a string variable in Python?

a) x = ‘hello’
b) x = “hello”
c) x = ”’hello”’
d) All of the above

Answer: d) All of the above

Explanation: In Python, you can declare a string variable using either single quotes (”), double quotes (“”) or triple quotes (”’ ”’). All three options are valid and produce the same result.

40.What is the result of the following operation: 2 ** 0.5?

a) 1.41
b) 1
c) 2
d) TypeError

Answer: a) 1.41

Explanation: In Python, the double asterisk (**) operator is used to perform exponentiation. When you raise a number to a fractional power, it performs a square root operation. Therefore, 2 ** 0.5 would return the square root of 2, which is approximately 1.41.