0% found this document useful (0 votes)
2 views8 pages

Python Programming Quiz Questions

Uploaded by

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

Python Programming Quiz Questions

Uploaded by

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

Join Venus Publication Whats’App Group (Send Your College Name, Branch & Semester to +91 9007069442)

Venus Python Programming 1


Model set -1 Ans.(b)
(xi) The method __________ can be used to convert a
list into a set.
Python Programming (a) toSet() (b) set() (c) list()
Ans.(b)
Group - A (xii)The method __________ allows you to traverse items
[Link] the most suitable answer from the following in a dictionary.
options. [Marks: 1 x 20 =20] (a) items() (b) keys() (c) values()
(i) A variable in Python is an example of a(n) Ans.(a)
__________. (xiii)The term __________ refers to values passed to a
(a) function (b) identifier (c) object function when it is called.
Ans.(b) (a) parameters (b) arguments (c) inputs
(ii) An integer, float, and complex are examples of Ans.(b)
__________ data types. (xiv)A function that takes no arguments is called a
__________ function.
(a) mutable (b) numeric (c) string
(a) empty (b) no-arg (c) parameterless
Ans.(b) Ans.(c)
(iii)Strings in Python are __________ data types. (xv)Default parameters are defined in a function by
(a) mutable (b) immutable (c) iterable assigning a value in the __________.
Ans.(b) (a) return statement (b) function definition
(iv) A tuple is defined by __________. (c) call statement
(a) square brackets (b) curly braces (c) parentheses Ans.(b)
Ans.(c) (xvi)A function can return multiple values using a
__________.
(v) A __________ can be used to create a sequence of
(a) list (b) tuple (c) dictionary
numbers in a loop.
Ans.(b)
(a) Set (b) Range (c) List
(xvii)In OOP, a blueprint for creating objects is called a
Ans.(b)
__________.
(vi) The __________ operator is used for logical
(a) module (b) class (c) function
conjunction in Python.
Ans.(b)
(a) & (b) and (c) &&
(xviii)An instance of a class is referred to as an
Ans.(b)
__________.
(vii)A variable holding the value of a floating-point
(a) object (b) variable (c) function
number is of type __________.
Ans.(a)
(a) int (b) float (c) complex
(xix)The __________ method is called before an object
Ans.(b)
is destroyed to perform cleanup actions.
(viii)To check if a variable x is not None, you can use
(a) finalize (b) delete (c) del
the __________ operator.
Ans.(c)
(a) is (b) != (c) is not
(xx)Inheritance enables code __________ by allowing
Ans.(c)
new classes to use existing code.
(ix) The syntax for creating an empty list is __________.
(a) duplication (b) reuse (c) complication
(a) [] (b) {} (c) ()
Ans.(b)
Ans.(a)
Group - B
(x) The first element of the list [‘apple’, ‘banana’, Answer all five questions : [Marks: 4 x 5 =20]
‘cherry’] can be accessed using __________. Q2. What is the difference between a keyword and an
(a) list[1] (b) list[0] (c) list[-1] identifier? Provide examples.
For 100% success go through Venus Question Bank (Concept) ......(Available at your nearest book shop)
For 100% success go through Venus Question Bank (Concept) ......(Available at your nearest book shop)
Venus Python Programming 2
Ans. are they different?
Aspect Keyword Identifier [Link] to Chapter 2 of Venus Publication Question
Bank
Reserved word with a User-defined name OR
Definition predefined meaning for program Q3. Explain “if statements” support by python? Explain
in a language. elements. with example.
Ans.“if statement” is the simplest decision control state-
ment in python. “if statement having three components
1. If keyword 2. Condition 3. Action block
Usage Used to structure and Used to name  If statement tests the action and returns the result, re-
control the flow of the variables, functions, sult is in Boolean data type i.e. True of False, if condi-
program. etc. tion is satisfied then it return “True” otherwise it re-
Reserved Cannot be changed or Defined by the user turns “False”.
redefined. and can be  if condition which we want to test is true then “action
block” will be executed, action block can be single line
modified.
or multiple lines. “:” is the end of “if condition” header
if, else, while, return. age, totalSum, and start of “action” segment. Action segment start with
Examples getUserData. the indentation (i.e. white space). The statements you
wish to include within the action segment should fol-
low the same indentation as the first statement. If you
OR miss the indentation the interpreter will interpret that
[Link] Lvalue and Rvalue with a suitable Python the statement is outside the segment.
example. If condition is not true then it will not execute state-
[Link] Python, Lvalue(Left Value) and Rvalue (Right Value) ments in action block, it will execute next statement
are terms used in assignment operations. after action block.
 Lvalue refers to a storage location that can appear on Syntax as follows :
the left-hand side of an assignment operator. It must be if (condition) :
assignable, i.e., capable of storing a value. # Statements to execute if
 Rvalue refers to the value on the right-hand side of the # condition is true
assignment operator. It is the data or expression that Flow chart of “if statement shown Fig. 1
the Lvalue will hold.
Example of Lvalue and Rvalue
Lvalue: `x` is a variable that can store a value
Rvalue: `10` is a value assigned to `x`
x = 10
The expression `(x + 5)` is an Rvalue because it com-
putes a value
Lvalue: `y` is assignable
y=x+5
Lvalue can't be a literal, so the following is invalid:
10 = x # This would raise a SyntaxError
Explanation:
1. `x` is an Lvalue because it is a variable that can hold a
value.
2. `10` is an Rvalue because it is a literal value assigned
to `x`.
3. `y` is an Lvalue, and the expression `x + 5` is an Rvalue
because it evaluates to a value (`15`) assigned to `y`. Figure 1 : Flowchart-if statement
4. You cannot assign a value to an Rvalue like a literal  In Fig. 2 two cases of single example are depicted, in
(`10 = x`), as literals are not storage locations. the first case “if condition” satisfied as “value of al is
equal to 0” hence if action block is executed.
[Link] the `break` and `continue` statements. How  In second case “if condition is not satisfied hence”, if
Join Venus Publication Whats’App Group (Send Your College Name, Branch & Semester to +91 9007069442)
Join Venus Publication Whats’App Group (Send Your College Name, Branch & Semester to +91 9007069442)
Venus Python Programming 3
action block doesn’t get executed”, it comes out of “if the `lambda` keyword. It is often used when a short,
statement” and executes statement after “if action throwaway function is required, typically for operations
block”. that are not complex enough to warrant a full `def`
block.
Key Features:
(i) Conciseness: Lambda functions are written in a
single line, making them concise.
(ii) Anonymous: They don’t have a name unless
explicitly assigned.
(iii)Quick Use: Often used with functions like `map()`,
`filter()`, and `reduce()`.
Syntax:
python
lambda arguments: expression
```
Example:
```python
Lambda function to double a number
double = lambda x: x * 2
Using the lambda function
print(double(5)) # Output: 10
Figure 2: if statement ```
[Link] is string concatenation? Provide a program that OR
concatenates three strings. [Link] between local scope and global scope
[Link] concatenation is the process of joining two or with examples.
more strings to form a single string. In programming, [Link] Scope
this is often achieved using operators (`+` in Python, (i) Definition: Variables declared inside a function or
Java, JavaScript, etc.), methods (`concat` in Java), or block are considered to have a local scope. They are
functions (like `strcat` in C). accessible only within that specific function or block.
Input strings (ii) Lifetime: A local variable exists only during the
string1 = "Hello" execution of the function or block in which it is
string2 = " " defined.
string3 = "World" (iii)Purpose: Used to store temporary data that is specific
Concatenation to a particular function.
result = string1 + string2 + string3 Example:
Output the result ```python
print("Concatenated string:", result) def my_function():
``` local_var = 10 # Local variable
Output: print("Inside function:", local_var)
``` my_function()
Concatenated string: Hello World # print(local_var) # This will raise an error because
``` local_var is not accessible outside the function.
OR ```
[Link] is a list different from a tuple? Illustrate with Global Scope:
examples. (i) Definition: Variables declared outside all functions
[Link] to Chapter 3 of Venus Publication Question or blocks have a global scope. They are accessible
Bank throughout the program, including inside functions
(unless shadowed by a local variable of the same
[Link] is the purpose of a lambda function? Write a name).
simple example to illustrate its usage. (ii) Lifetime: A global variable exists for the duration of
AnsPurpose of a Lambda Function:A lambda function the program's execution.
in Python is a small, anonymous function defined using (iii)Purpose: Used to store data that needs to be shared
For 100% success go through Venus Question Bank (Concept) ......(Available at your nearest book shop)
For 100% success go through Venus Question Bank (Concept) ......(Available at your nearest book shop)
Venus Python Programming 4
across multiple functions. Hello World
Example: (iv) Close a File:Every time when we open the file, as a
```python good practice we need to ensure to close the file, In
global_var = 20 # Global variable python, we can use close() function to close the file.
def my_function(): When we close the file, it will free up the resources that
print("Inside function:", global_var) # Accessible were tied with the file.
inside the function Example:
my_function() f = open(“[Link]”, ‘r’)
print("Outside function:", global_var) # Accessible print ([Link]())
outside the function [Link]()
``` (v) Create & Delete a File:In python, we can create a new
[Link] do you mean by Classes and Object in Python file using the open method.
Programming? Example:
[Link] to Chapter 5 of Venus Publication Question f = open(“[Link]”, “w”)
Bank. [Link]()
OR Group-C
[Link] the operations perform on files. Answer all five questions : [Marks: 6 x 5 =30]
[Link] operations which you can perform on files are: [Link] Python data type with an example.
(i) Open a File:Python provides a built-in function called [Link] Data Types
open() to open a file, and this function returns a file  Data types are used to define type of variable.
object called the handle and it is used to read or modify  In Python there are five types of data type that are used
the file. commonly and those are -
Syntax:
file_object = open(filename)
Example:
I have a file called [Link] in my disk and I want to open
it. This can be achieved by:
#if the file is in the same directory
f = open(“[Link]”) Fig. Data types in Python
#if the file is in a different directory 1. Numeric : Python numeric data type is used to hold
f = open(“C:/users/Python/[Link]”) numeric values like;
(ii) Reading Data from the File:In order to read the file,  int-holds signed integers of non-limited length.
first, we need to open the file in reading mode.  long-holds long integers
Example:  float-holds floating precision numbers and it’s accurate
f = open(“[Link]”, ‘r’) upto 15 decimal places.
#To print the content of the whole file  complex-holds complex numbers.
print([Link]()) In Python we need not to declare datatype while
#To read only one line declaring a variable like C or C++. We can simply just
print([Link]()) assign values in a variable. But if we want to see what
(iii)Writing Data to File:In order to write the data into a type of numerical value is it holding right now, we can
file, we need to open the file in write mode. use type(). For example -
Example:
f = open(“[Link]”, ‘w’)
[Link](“Hello Python \n”)
#in the above code ‘\n’ is next line which means in the
text file it will write Hello Python and point the cursor
to the next line
[Link](“Hello World”)
Output:
Now if we open the [Link] file, we can see the content
as:
Hello Python
Join Venus Publication Whats’App Group (Send Your College Name, Branch & Semester to +91 9007069442)
Join Venus Publication Whats’App Group (Send Your College Name, Branch & Semester to +91 9007069442)
Venus Python Programming 5

4. Tuple :
 Tuple is a collection of elements and it is similar to the
List. But the items of the tuple are separated by comma
and the elements are enclosed in ( ) parenthesis.
 Tuples are immutable or read-only. That means we can
2. String : not modify the size of tuple or we cannot change the
 String is a collection of characters. value of items of the tuple.
 In Python, we can use single quote, double quote or  Here are examples of tuples -
triple quote to define a string.
 We can use two operators along with the string one is +
and another is *.
 The + operator is used to concatenate the two strings.
While * operator is used as a repetition operation.
Following execution illustrates it -

5. Dictionary
 Dictionary is a collection of elements in the form of
key:value pair.
 The elements of dictionary are present in the curly
brackets.
 For example -
3. List :
 It is similar to array in C or C++ but it can
simultaneously hold different types of data in list.
 It is basically an ordered sequence of some data written
using square brackets ([]) and commas (,)
 For example -

For 100% success go through Venus Question Bank (Concept) ......(Available at your nearest book shop)
For 100% success go through Venus Question Bank (Concept) ......(Available at your nearest book shop)
Venus Python Programming 6
OR
[Link] and explain the arithmetic operators in Python
with examples.
[Link] to Chapter 1 of Venus Publication Question
Bank.
[Link]`if-else` and `if-elif-else` statements with
suitable examples.
[Link] to Chapter 2 of Venus Publication Question
Bank.
OR
[Link] for loop with syntax and example.
[Link] for loop
The for loop is another popular way of using iteration. [Link] are tuples? Explain with an example.
The syntax of for loop is Ans.A tuple is an ordered collection of elements in Python.
Syntax Unlike lists, tuples are immutable, meaning their
for variable in sequene: elements cannot be changed, added, or removed after
Body of for loop creation. Tuples are often used when you want to ensure
Example the integrity of the data and when performance is
for val in numbers: important since they are generally faster than lists for
val = val + 1 fixed collections of items.
The variable takes the value of the item inside the Characteristics of Tuples:
sequence on each iteration. (i) Ordered: Tuples maintain the order of elements.
Loop continues until we reach the item in the sequence. (ii) Immutable: Once created, you cannot change, add, or
The body of for loop is separated from the rest of the remove items.
code using indentation. (iii)Can contain different data types: A tuple can hold
The flowchart is as shown below– items of different data types like integers, strings, or
other objects.
(iv) Can be nested: Tuples can contain other tuples or data
structures.
Syntax:A tuple is created by placing a comma-separated
sequence of elements within parentheses `()`.
```python
Example of a tuple
my_tuple = (1, "apple", 3.14)
```
Accessing Elements:
```python
print(my_tuple[0]) # Output: 1
print(my_tuple[1]) # Output: "apple"
```
Example:
[Link] a Python program to check whether given num-
```python
ber is even or odd.
Defining a tuple
[Link] (“Enter value of n”)
person = ("Alice", 25, "Engineer")
n = int (input())
Accessing tuple elements
if n%2 = = 0:
name = person[0] # "Alice"
print (“Even Number”)
age = person[1] # 25
in n%2 = 1:
profession = person[2] # "Engineer"
print (“Odd Number”)
Tuples can also be used for packing and unpacking:
name, age, profession = person
print(name) # "Alice"
print(age) # 25
Join Venus Publication Whats’App Group (Send Your College Name, Branch & Semester to +91 9007069442)
Join Venus Publication Whats’App Group (Send Your College Name, Branch & Semester to +91 9007069442)
Venus Python Programming 7
print(profession) # "Engineer" contains two modules: `[Link]` and
``` `[Link]`. You can import them like this:
In this example, `person` is a tuple containing three ```python
elements. We can access the elements using indexing # [Link]
or unpack the tuple into individual variables. from my_package import module1
OR from my_package.module2 import some_function
[Link] indexing and slicing in strings with ```
examples. Key Differences:
[Link] to Chapter 3 of Venus Publication Question (i) Module: A single file containing Python code (`.py`
Bank. file).
(ii) Package: A directory containing multiple Python
[Link] a module and a package in Python? modules and a special `__init__.py` file to make it a
Ans. Module:A module is a single file that contains package.
Python code. It can include functions, classes, Packages help you organize large codebases, while
variables, and runnable code. You can import and modules help organize smaller pieces of related code.
use the functions or classes defined in a module in OR
another Python script. A module is simply a Python [Link] will you define and call a function in python?
file with the `.py` extension. Give illustrative example.
Example of a module:Suppose you have a file [Link] to Chapter 4 of Venus Publication Question
`math_operations.py` with the following code: Bank.
```python
# math_operations.py [Link] is inheritance? List and explain the types of
def add(a, b): inheritance supported in Python.
return a + b [Link] to Chapter 5 of Venus Publication Question
def subtract(a, b): Bank.
return a - b OR
``` [Link] is polymorphism in OOP? How is it achieved
This file is a module named `math_operations`. To in Python?
use this module in another script, you would import [Link] in Object-Oriented Programming
it: (OOP) refers to the ability of different objects to
```python respond to the same method or message in a way
# [Link] that is appropriate to their specific types. In other
import math_operations words, polymorphism allows a single interface to
result = math_operations.add(3, 4) represent different underlying forms (data types or
print(result) objects).
``` There are two types of polymorphism in OOP:
Package:A package is a collection of Python modules (i) Compile-time polymorphism (Method Overloading)
grouped together in a directory. It is essentially a (ii) Runtime polymorphism(Method Overriding)
directory containing multiple module files and a special Python primarily supports runtime polymorphism
file named `__init__.py` (which can be empty) that tells because Python does not support method
Python this directory should be treated as a package. overloading in the traditional sense (i.e., multiple
Packages allow you to organize your modules into methods with the same name but different
subdirectories, making your code easier to manage, signatures).
especially for larger projects. (i) Method Overriding (Runtime Polymorphism)
Example of a package structure:  In Python, subclasses can override methods of a parent
``` class. When a subclass redefines a method, the
my_package/ subclass's version of the method is called instead of the
__init__.py parent class's version.
[Link]  The method in the subclass can have the same name,
[Link] but with different functionality, allowing for dynamic
``` dispatch.
In this example, `my_package` is a package that
For 100% success go through Venus Question Bank (Concept) ......(Available at your nearest book shop)
For 100% success go through Venus Question Bank (Concept) ......(Available at your nearest book shop)
Venus Python Programming 8
Example:
```python
class Animal: For Doubts Discussion
def sound(self): Join : “[Link]
print("Some animal sound")
class Dog(Animal):
def sound(self): For 100% success go through Venus Question
print("Bark") Bank for 1st to 6th sem (Concept) ......(Avail-
class Cat(Animal): able at your nearest book shop)
def sound(self):
print("Meow")
# Polymorphism in action
animals = [Dog(), Cat()]
for animal in animals:
[Link]() # Dog barks, Cat meows
```
Output:
```
Bark
Meow
```
In this example, despite calling the same method
`sound()`, the actual method executed depends on the
object's class (either `Dog` or `Cat`), demonstrating
runtime polymorphism.
(ii) Duck Typing(Implicit Polymorphism):Python also
uses duck typing, where the type or class of an object
is less important than the methods or behaviors it
implements. If an object can perform the expected
behavior (e.g., has a `sound()` method), it is treated as For 100% success go through Venus Question
the required type, even if it doesn't explicitly inherit Bank for 1st to 6th sem (Concept) ......(Avail-
from a particular class. able at your nearest book shop)
Example:
```python
class Bird:
def sound(self):
print("Tweet")
class Human:
def sound(self):
print("Speak")
def make_sound(entity):
[Link]() # Duck typing: Any object with a sound()
method can be used
make_sound(Dog()) # Bark
make_sound(Cat()) # Meow
make_sound(Bird()) # Tweet
make_sound(Human()) # Speak
```
Here, any object that has the `sound()` method can be
passed to the `make_sound()` function, demonstrating
polymorphism through duck typing.

Join Venus Publication Whats’App Group (Send Your College Name, Branch & Semester to +91 9007069442)

You might also like