0% found this document useful (0 votes)
4 views3 pages

Unit 4

njn

Uploaded by

akash97523
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)
4 views3 pages

Unit 4

njn

Uploaded by

akash97523
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
You are on page 1/ 3

Unit 4: Functions and Modular Programming

Q1. What is a dictionary in programming? Explain dictionary operations with


examples.

Definition:

A dictionary is a data structure that stores data in key-value pairs.

• Operations: Add, delete, update, search.


• Example in Python:

python
Copy code
my_dict = {'name': 'Alice', 'age': 25}
my_dict['age'] = 26 # Update value

Q2. Importance of modular programming with examples.

Definition:

Modular programming divides a program into smaller, manageable modules.

Advantages:

1. Enhances code readability.


2. Eases debugging and testing.
3. Promotes reusability.

• Example:

c
Copy code
void display() {
printf("Hello");
}

Q3. What is a function? Explain types of functions with examples.

Definition:

A function is a block of reusable code.

Types:

1. Library Functions: Predefined (e.g., printf).


2. User-Defined Functions: Created by the programmer.
o Example:

c
Copy code
int sum(int a, int b) {
return a + b;
}

Q4. Discuss call-by-value and call-by-reference with examples.

Aspect Call-by-Value Call-by-Reference


Definition Passes a copy of the value Passes the address
Effect on Original No change Original value can be changed
Example:
c
Copy code
void func(int x) { x = 10; } // Value
void func(int *x) { *x = 10; } // Reference

Q5. What is recursion? Explain with an example.

Definition:

Recursion is a process where a function calls itself.

• Example: Factorial calculation:

c
Copy code
int factorial(int n) {
if (n == 1) return 1;
return n * factorial(n - 1);
}

Unit 5: Files and Memory Management


Q1. What is a file in programming? Explain file operations with examples.

Definition:

A file is a data container used to store data permanently.

Operations: Open, read, write, close.

• Example:

c
Copy code
FILE *f = fopen("file.txt", "r");

Q2. What are memory management operations in C programming?

Definition:

Memory management involves dynamic allocation and deallocation.

• Example: malloc, calloc, free.

c
Copy code
int *ptr = (int *)malloc(sizeof(int));
free(ptr);

Q3. Differentiate calloc and malloc.

Aspect malloc calloc


Initialization No Initializes to 0
Syntax malloc(size) calloc(n, size)

Q4. Write a short note on realloc and free function.

realloc:

• Resizes allocated memory dynamically.


• Syntax: ptr = realloc(ptr, new_size);.

free:

• Frees dynamically allocated memory.


• Syntax: free(ptr);.

Q5. Discuss variable lifetime and visibility.

Definition:

• Lifetime: Duration for which a variable exists in memory.


o Static variables persist throughout program execution.
o Local variables exist only within a function.
• Visibility: Scope of a variable.
o Local: Limited to a function.
o Global: Accessible throughout the program.

You might also like