ilovepdf_merged (3)
ilovepdf_merged (3)
General Concepts
1. What is BIOS?
BIOS (Basic Input/Output System) is firmware stored on a small memory chip
on the motherboard. It’s the first software that runs when a computer starts
up. It performs hardware initialization during the booting process and provides
runtime services for operating systems and programs. The BIOS maintains
settings through a complementary metal-oxide-semiconductor (CMOS) chip.
4. Functionalities of an OS
• Process Management: Creates, schedules, and terminates processes
• Memory Management: Allocates and deallocates memory to programs
• File System Management: Handles file operations and storage
• Device Management: Controls input/output devices
• Security Management: Provides security and access control
• User Interface: Provides interface between user and computer
• Network Management: Handles network connections and protocols
1
5. Different Types of Number Systems
1. Binary (Base-2): Uses 0 and 1
2. Octal (Base-8): Uses digits 0-7
3. Decimal (Base-10): Uses digits 0-9
4. Hexadecimal (Base-16): Uses digits 0-9 and letters A-F
9. Define Identifiers
Identifiers are names given to variables, functions, arrays, classes, etc. in a
program. Rules for identifiers: - Must start with letter or underscore - Can
contain letters, digits, and underscores - Case-sensitive - Cannot use keywords
- No special characters except underscore
2
• short: -32,768 to 32,767 (2 bytes)
• long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (8 bytes)
• float: 1.2E-38 to 3.4E+38 (4 bytes)
• double: 2.2E-308 to 1.8E+308 (8 bytes)
C Programming Basics
14. C Program for Bitwise Operations
#include <stdio.h>
int main() {
int a = 12; // 1100 in binary
int b = 25; // 11001 in binary
3
return 0;
}
int main() {
char buffer[100];
write(1, "Enter text: ", 11);
read(0, buffer, 100);
write(1, "You entered: ", 12);
write(1, buffer, 100);
return 0;
}
4
• Global scope
• Multiple file access
Example program:
#include <stdio.h>
void increment() {
static int i = 0; // static local variable
auto int j = 0; // automatic variable
i++;
j++;
count++;
printf("i=%d, j=%d, count=%d\n", i, j, count);
}
int main() {
for(int k = 0; k < 3; k++)
increment();
return 0;
}
// Call by value
void swapByValue(int a, int b) {
5
int temp = a;
a = b;
b = temp;
}
// Call by reference
void swapByReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
swapByValue(x, y);
printf("After swap by value: x=%d, y=%d\n", x, y);
swapByReference(&x, &y);
printf("After swap by reference: x=%d, y=%d\n", x, y);
return 0;
}
Recursion
23. What is Recursion?
Recursion is a programming concept where a function calls itself to solve a
smaller instance of the same problem. It consists of: - Base case (termination
condition) - Recursive case - Progress toward base case
6
towerOfHanoi(n-1, auxiliary, source, destination);
}
int main() {
int n = 3;
towerOfHanoi(n, 'A', 'B', 'C');
return 0;
}
b. Factorial:
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n-1);
}
int main() {
int n = 5;
printf("Factorial of %d is %d\n", n, factorial(n));
return 0;
}
c. Fibonacci Series:
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n-1) + fibonacci(n-2);
}
int main() {
int n = 10;
for(int i = 0; i < n; i++)
printf("%d ", fibonacci(i));
return 0;
}
Arrays
25. Array and Data Structure
An array is a collection of similar data elements stored in contiguous memory
locations. It’s called a data structure because: - Organizes data in a structured
7
way - Provides efficient access to elements - Allows operations like insertion,
deletion - Supports various algorithms - Enables complex data manipulation
int main() {
int arr[10];
// Store numbers
for(int i = 0; i < 10; i++)
arr[i] = i + 1;
// Print numbers
for(int i = 0; i < 10; i++)
printf("%d ", arr[i]);
return 0;
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
8
for(int i = *n; i > pos; i--)
arr[i] = arr[i-1];
arr[pos] = value;
(*n)++;
}
int main() {
int arr[100] = {1, 2, 3, 4, 5};
int n = 5;
// Insert 10 at position 2
insert(arr, &n, 2, 10);
// Print array
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
int main() {
int arr[] = {12, 35, 1, 10, 34, 1};
int n = sizeof(arr)/sizeof(arr[0]);
int first = arr[0], second = -1;
9
}
10
30. Program to Remove Duplicates (continued)
#include <stdio.h>
int main() {
int arr[] = {1, 2, 2, 3, 4, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
int j = 0;
// Remove duplicates
for(int i = 0; i < n-1; i++) {
if(arr[i] != arr[i+1]) {
arr[j] = arr[i];
j++;
}
}
arr[j] = arr[n-1];
// Print array
for(int i = 0; i <= j; i++)
printf("%d ", arr[i]);
return 0;
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = sizeof(arr)/sizeof(arr[0]);
int odd[50], even[50];
int o = 0, e = 0;
1
printf("\nOdd numbers: ");
for(int i = 0; i < o; i++)
printf("%d ", odd[i]);
return 0;
}
int main() {
int arr[] = {1, 2, 2, 3, 3, 3, 4, 4, 4, 4};
int n = sizeof(arr)/sizeof(arr[0]);
int main() {
int arr[] = {1, 2, 2, 3, 3, 3, 4, 5, 6, 6};
int n = sizeof(arr)/sizeof(arr[0]);
int count = 0;
2
if(j == n)
count++;
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
int position = 2;
splitArray(arr, n, position);
return 0;
}
// Linear Search
int linearSearch(int arr[], int n, int key) {
3
for(int i = 0; i < n; i++) {
if(arr[i] == key)
return i;
}
return -1;
}
if(arr[mid] == key)
return mid;
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr)/sizeof(arr[0]);
int key = 10;
return 0;
}
4
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
return 0;
}
int main() {
int arr[] = {12, 3, 5, 7, 19};
int n = sizeof(arr)/sizeof(arr[0]);
int k = 2;
sort(arr, n);
5
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = sizeof(arr)/sizeof(arr[0]);
return 0;
}
#define N 3
// Matrix Addition
void addMatrix(int A[][N], int B[][N], int C[][N]) {
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++)
C[i][j] = A[i][j] + B[i][j];
}
// Matrix Multiplication
void multiplyMatrix(int A[][N], int B[][N], int C[][N]) {
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
C[i][j] = 0;
for(int k = 0; k < N; k++)
C[i][j] += A[i][k] * B[k][j];
}
}
}
int main() {
int A[N][N] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
6
int B[N][N] = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}};
int C[N][N];
addMatrix(A, B, C);
multiplyMatrix(A, B, C);
return 0;
}
int main() {
int arr1D[] = {1, 2, 3, 4, 5};
int arr2D[][3] = {{1, 2, 3},
{4, 5, 6}};
process1DArray(arr1D, 5);
process2DArray(arr2D, 2);
return 0;
}
Pointers
41. Pointers and Pointer Arithmetic
#include <stdio.h>
int main() {
7
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
// Increment/decrement
ptr++; // moves to next integer
ptr--; // moves back
// Array indexing
printf("Third element: %d\n", ptr[2]);
return 0;
}
int main() {
// malloc
int *ptr1 = (int*)malloc(5 * sizeof(int));
// calloc
int *ptr2 = (int*)calloc(5, sizeof(int));
// realloc
ptr1 = (int*)realloc(ptr1, 10 * sizeof(int));
// free memory
free(ptr1);
free(ptr2);
return 0;
}
int main() {
int *arr[3];
int a = 10, b = 20, c = 30;
8
arr[0] = &a;
arr[1] = &b;
arr[2] = &c;
return 0;
}
int main() {
int *ptr = NULL;
if(ptr == NULL)
printf("This is a null pointer\n");
return 0;
}
struct Point {
int x;
int y;
} p1, p2; // p1 and p2 are structure variables
int main() {
9
p1.x = 10;
p1.y = 20;
return 0;
}
enum Days {
SUNDAY = 1,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
int main() {
enum Days today = WEDNESDAY;
printf("Day number: %d\n", today);
return 0;
}
49. fflush(stdin)
fflush(stdin) is used to clear the input buffer. It’s commonly used before reading
character input to prevent buffer issues.
typedef struct {
int x;
10
int y;
} Point;
int main() {
Point p1;
Integer i = 10;
return 0;
}
struct Student {
char name[50];
int roll;
int age;
};
int main() {
struct Student s1;
strcpy(s1.name, "John");
s1.roll = 1;
s1.age = 20;
return 0;
}
11
53. Employee Records Program
“‘c #include <stdio.h> #include <string.h>
struct Employee { int id; char name[50]; float salary; };
int main() { struct Employee emp[10];
// Input data
for(int i = 0; i < 10; i++) {
printf("Enter employee details:\n");
scanf("%d %s %f", &emp[i].id, emp[i].name,
12
53. Employee Records Program (continued)
#include <stdio.h>
#include <string.h>
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
struct Employee emp[10];
// Input data
for(int i = 0; i < 10; i++) {
printf("Enter employee details:\n");
scanf("%d %s %f", &emp[i].id, emp[i].name, &emp[i].salary);
}
// Display data
printf("\nEmployee Records:\n");
for(int i = 0; i < 10; i++) {
printf("ID: %d\n", emp[i].id);
printf("Name: %s\n", emp[i].name);
printf("Salary: %.2f\n\n", emp[i].salary);
}
return 0;
}
struct Node {
int data;
struct Node* next; // Self-referential pointer
};
int main() {
struct Node* head = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
1
head->data = 1;
head->next = NULL;
return 0;
}
struct Address {
char street[50];
char city[30];
char state[20];
};
struct Employee {
int id;
char name[50];
struct Address addr; // Nested structure
};
int main() {
struct Employee e1 = {1, "John", {"123 St", "NYC", "NY"}};
printEmployee(e1);
updateSalary(&e1);
return 0;
}
2
56. Differences between Unions and Structures
Structures: - All members have their own memory space - Can access all mem-
bers simultaneously - Size is sum of all member sizes (with padding) - Used
when all members are needed
Unions: - All members share same memory space - Can access only one member
at a time - Size is the largest member size - Used when only one member is
needed at a time
File Handling
57. File Handling Programs
a. Creating and Opening Files:
#include <stdio.h>
int main() {
FILE *fp;
// Write to file
fprintf(fp, "Hello, World!\n");
// Close file
fclose(fp);
fclose(fp);
return 0;
}
b. Reading from File:
#include <stdio.h>
3
int main() {
FILE *fp;
char buffer[100];
fp = fopen("test.txt", "r");
if(fp == NULL) {
printf("Error opening file!");
return 1;
}
fclose(fp);
return 0;
}
c. Writing to File:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("test.txt", "w");
if(fp == NULL) {
printf("Error opening file!");
return 1;
}
fclose(fp);
return 0;
}
int main() {
4
// Create file
FILE *fp = fopen("newfile.txt", "w");
if(fp != NULL) {
fprintf(fp, "New file content\n");
fclose(fp);
}
// Delete file
if(remove("newfile.txt") == 0)
printf("File deleted successfully\n");
else
printf("Error deleting file\n");
return 0;
}
b. File Operations (Read, Write, Append):
#include <stdio.h>
int main() {
FILE *fp;
char buffer[100];
// Write mode
fp = fopen("file.txt", "w");
fprintf(fp, "Initial content\n");
fclose(fp);
// Append mode
fp = fopen("file.txt", "a");
fprintf(fp, "Appended content\n");
fclose(fp);
// Read mode
fp = fopen("file.txt", "r");
while(fgets(buffer, 100, fp))
printf("%s", buffer);
fclose(fp);
return 0;
}
c. Count, Convert, Replace, and Reverse Operations:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
5
// Count characters in file
int countChars(FILE *fp) {
int count = 0;
char ch;
while((ch = fgetc(fp)) != EOF)
count++;
return count;
}
fclose(temp);
}
fclose(temp);
}
6
}
buffer[size] = '\0';
int main() {
FILE *fp = fopen("test.txt", "r+");
if(fp == NULL) return 1;
fclose(fp);
return 0;
}
This completes the comprehensive answer key for all questions in the C program-
ming question bank. Each program includes proper error handling, comments,
and demonstrates the key concepts being tested. The programs can be used as
reference material for learning and practice.