I is correct ii is correct
Explain the use of scanf() and printf() with examples
#include <stdio.H>
int main() {
int a = 10;
float b = 5.5;
printf(“The value of a is %d and b is %.2f\n”, a, b);
return 0;
}
The value of a is 10 and b is 5.50
#include <stdio.H>
int main() {
int age;
printf(“Enter your age: “);
scanf(“%d”, &age);
printf(“You are %d years old.\n”, age);
return 0;
} , You are 21 years old.
Explain the following types of function arguments with examples:[6] i) Required arguments ii) Keyword arguments
1. Required arguments must be passed to the function in the correct order
2. If any required argument is missing, it causes an error
3. These are the basic type of function arguments
Example: def greet(name) , print(“Hello”, name)
greet(“Alice”) # Output: Hello Alice
1. Keyword arguments are passed using parameter names (e.G., name=”Alice”)
2. The order does not matter when using keyword arguments
3. They make the code clearer and more readable
Example: def greet(name, age) , print(“Name:”, name) , print(“Age:”, age)
greet(age=25, name=”Bob”) , # Output:
# Name: Bob
Define a function. Explain function definition and function call with suitable example
A function is a block of code that performs a specific task. It runs only when it is called and helps in code reuse and modularity.
This is where you create the function and write the code inside it.
Syntax : def function_name(parameters):
# block of code
This is when you use the function by its name to make it run.
Syntax: function_name(arguments)
# Function Definition : def greet(name) print(“Hello”, name)
# Function Call : greet(“Alice”)
Output: Hello Alice
B) What is a lambda function? Explain with a suitable example
A lambda function in Python is a small, anonymous function. It can take any number of arguments but has only one expression.
The result of the expression is automatically returned.
Syntax: lambda arguments: expression
Ex: def add10(x):
return x + 10
print(add10(5)) # Output: 15
Using lambda: add10 = lambda x: x + 10
print(add10(5)) # Output: 15
Explain following string methods with example. [6] i) strip() ii) index() iii) isdigit()
1. Removes any leading (start) and trailing (end) spaces from a string. 2. Does not remove spaces between words
Example: text = ” Hello World ” , cleaned = text.Strip() , print(cleaned) # Output: “Hello World”
1. Returns the position (index) of the first occurrence of a character or substring. 2. Gives an error if the item is not found.
Example: text = “Hello” , pos = text.Index(‘e’) , print(pos) # Output: 1 (because ‘e’ is at position 1)
1. Checks if the entire string is made of only digits (0-9). 2. Returns True if all characters are digits, else False.
Example: num = “12345”
print(num.Isdigit()) # Output: True
word = “abc123”
print(word.Isdigit()) # Output: False
Explain class variables and object variable with suitable example.
Class variable:
Shared by all objects of the class. Defined inside the class, but outside any method. Changes to class variables affect all objects (unless overridden).
Object variable: Unique for each object.
Defined inside methods, usually inside __init__. Changes affect only that specific object.
class Student: # Class variable, school_name = “ABC School”, def __init__(self, name, grade):
# Object (instance) variables , self.Name = name , self.Grade = grade
# Create two objects : s1 = Student(“Alice”, “5th”) , s2 = Student(“Bob”, “6th”)
# Access class variable: print(s1.School_name) # Output: ABC School , print(s2.School_name) # Output: ABC School
# Access object variables: print(s1.Name) # Output: Alice , print(s2.Name) # Output: Bob
