Unit 1
Unit 1
Unit 1
1-Mark Questions
3-Marks Questions
10-Marks Questions
1-Mark Questions
3-Marks Questions
1. Differentiate between local and global variables.
o Local variables: Declared within a function and accessible only within that
function.
o Global variables: Declared outside any function and can be accessed
throughout the program.
2. Explain the significance of accumulation patterns in sequences.
Accumulation patterns involve repetitive operations, such as summing values in a
sequence or accumulating results over time, useful in loops or iterative processes for
problems like calculating totals or averages.
3. Describe the concept of sequence mutation with examples.
Sequence mutation refers to modifying an element in a sequence like a list or array.
For example, in Python, lst[0] = 10 changes the first element of the list lst to 10.
4. List and explain the types of data types in programming.
o Primitive data types: Integer, float, character, boolean.
o Composite data types: Arrays, lists, tuples, dictionaries.
o User-defined data types: Structs, classes, enums.
5. Write a short note on bitwise operators.
Bitwise operators operate on binary numbers at the bit level. Common bitwise
operators include AND (&), OR (|), XOR (^), NOT (~), and shifts (<<, >>).
10-Marks Questions
python
Copy code
arr = [1, 3, 5, 2]
max_val = arr[0]
for num in arr:
if num > max_val:
max_val = num
Sequences and iteration are crucial in problems like searching, sorting, and data
processing.
1-Mark Questions
3-Marks Questions
c
Copy code
for (int i = 0; i < 5; i++) { printf("%d\n", i); }
o While loop: Used when the number of iterations is not known beforehand, and
the loop continues as long as a condition is true.
Example:
c
Copy code
int i = 0;
while (i < 5) { printf("%d\n", i); i++; }
4. What are user-defined data types? Explain with an example.
User-defined data types are data types created by the programmer to represent
complex structures. For example, in C, a struct can define a user-defined data type
to store multiple related data elements:
c
Copy code
struct Person {
char name[50];
int age;
};
c
Copy code
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
10-Marks Questions
c
Copy code
#include <stdio.h>
int main() {
int scores[5] = {80, 85, 90, 95, 100};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += scores[i];
}
float average = sum / 5.0;
printf("Average score: %.2f\n", average);
return 0;
}
c
Copy code
int arr[5] = {10, 20, 30, 40, 50};
Example of a 2D array in C:
c
Copy code
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
1-Mark Questions
1. Define a dictionary.
A dictionary is a data structure that stores key-value pairs, where each key is unique
and maps to a specific value. In Python, it is defined as {key: value}.
2. What is recursion?
Recursion is a process where a function calls itself to solve smaller instances of the
same problem.
3. Name two dictionary operations.
o get(key): Retrieves the value associated with the key.
o update(): Adds or updates a key-value pair in the dictionary.
4. What is a library function?
A library function is a pre-defined function provided by a programming language or
library, which can be called to perform specific tasks, like mathematical calculations
or input/output operations.
5. State the advantage of modular programming.
Modular programming allows for better code organization, reusability, and
maintainability by breaking a program into smaller, manageable functions or modules.
3-Marks Questions
c
Copy code
int factorial(int n) {
if (n == 1) return 1;
return n * factorial(n - 1);
}
10-Marks Questions
c
Copy code
#include <stdio.h>
int sum(int arr[], int size) {
int total = 0;
for (int i = 0; i < size; i++) {
total += arr[i];
}
return total;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int result = sum(arr, 5);
printf("Sum: %d\n", result);
return 0;
}
python
Copy code
my_dict = {'a': 1, 'b': 2}
print(my_dict.get('a')) # Output: 1
my_dict.update({'c': 3})
print(my_dict) # Output: {'a': 1, 'b': 2, 'c': 3}
1-Mark Questions
3-Marks Questions
python
Copy code
import csv
data = [['Name', 'Age'], ['John', 25], ['Jane', 28]]
with open('data.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerows(data)
10-Marks Questions
1. Discuss the concepts of file handling with examples of reading and writing
operations.
File handling allows interaction with files through opening, reading, writing, and
closing. Example:
python
Copy code
# Writing to a file
with open('output.txt', 'w') as file:
file.write("Hello, world!")
python
Copy code
import csv