Chapter 1
C Programming
TU BCA past exam questions
Past questions and answers
59 questions set from this chapter, 11 of them more than once. Most repeated first.
- Asked 2 times
- 2018 · 5 marks
- 2019 · 5 marks
What is software process model? Differentiate between cohesion and coupling in
programming.
Answer
Software process model is an abstract, structured description of the activities (requirements analysis, design, coding, testing, deployment and maintenance) and the order in which they are carried out to develop software. Examples are the waterfall, prototyping, incremental, spiral and RAD models. It provides a framework for planning, controlling and delivering a software project.
Difference between cohesion and coupling
| Basis | Cohesion | Coupling |
|---|---|---|
| Meaning | Degree to which the elements inside one module belong together | Degree of interdependence between different modules |
| Scope | Within a module (intra-module) | Between modules (inter-module) |
| Desired level | High cohesion is good | Low (loose) coupling is good |
| Effect | Makes a module easy to understand and reuse | Low coupling makes changes in one module affect others less |
| Types | Functional, sequential, communicational, procedural, temporal, logical, coincidental | Data, stamp, control, external, common, content |
In programming, a function that only computes the area of a circle is highly cohesive, while functions that share global variables are tightly coupled.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- Asked 2 times
- 2018 · 5 marks
- 2019 · 5 marks
Define keyword and identifiers. Explain rules for defining valid identifiers.
Answer
Keyword: A keyword is a reserved word of the C language whose meaning is already fixed by the compiler and which cannot be used as a variable or function name. Examples: int, float, if, else, while, for, return, switch, break. C89 has 32 keywords.
Identifier: An identifier is a name given by the programmer to a variable, function, array, structure, label, etc. Examples: sum, total_marks, main, student1.
Rules for defining valid identifiers
- It may contain only letters (A-Z, a-z), digits (0-9) and the underscore (
_). - The first character must be a letter or an underscore, never a digit (
2abcis invalid). - No blank space or special symbol such as
@,#,$,-,.is allowed (my-nameis invalid). - A keyword cannot be used as an identifier (
int,forare invalid). - C is case sensitive, so
Total,totalandTOTALare three different identifiers. - Standard C guarantees only the first 31 characters (63 in C99) to be significant, so the name should not be unnecessarily long.
Valid: _count, roll_no, x1. Invalid: 1x, roll no, float, a+b.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- Asked 2 times
- 2018 · 5 marks
- 2019 · 5 marks
List the operators used in C on the basis of utility. Explain the concept of bitwise
operator.
Answer
Operators in C on the basis of utility
- Arithmetic operators:
+ - * / % - Relational operators:
< > <= >= == != - Logical operators:
&& || ! - Assignment operators:
= += -= *= /= %= - Increment and decrement operators:
++ -- - Conditional (ternary) operator:
? : - Bitwise operators:
& | ^ ~ << >> - Special operators:
sizeof, comma,, address&, indirection*, member.and->
Bitwise operators
Bitwise operators work on the individual bits of integer operands (int, char, long), not on floats.
| Operator | Name | Rule |
|---|---|---|
& | AND | result bit is 1 only if both bits are 1 |
| | OR | result bit is 1 if at least one bit is 1 |
^ | XOR | result bit is 1 if the bits differ |
~ | One's complement | inverts every bit |
<< | Left shift | shifts bits left, filling 0s; equals multiplying by |
>> | Right shift | shifts bits right; equals dividing by |
Example with a = 12 (1100) and b = 10 (1010):
a & b= 1000 = 8a | b= 1110 = 14a ^ b= 0110 = 6a << 1= 11000 = 24a >> 2= 0011 = 3
#include <stdio.h>
int main() {
int a = 12, b = 10;
printf("%d %d %d %d %d\n", a & b, a | b, a ^ b, a << 1, a >> 2);
return 0;
}
Output: 8 14 6 24 3
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- Asked 2 times
- 2018 · 5 marks
- 2019 · 5 marks
Differentiate between while loop and do while loop. Write a C program to find input
number is prime or composite.
Answer
Difference between while and do-while loop
| Basis | while loop | do-while loop |
|---|---|---|
| Type | Entry-controlled | Exit-controlled |
| Condition checked | Before the body executes | After the body executes |
| Minimum executions | Zero (body may never run) | At least once |
| Syntax | while (cond) { ... } | do { ... } while (cond); |
| Semicolon | Not after the condition | Required after while (cond) |
Program to check whether a number is prime or composite
#include <stdio.h>
int main() {
int n, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n <= 1) {
printf("%d is neither prime nor composite\n", n);
return 0;
}
for (i = 2; i * i <= n; i++) {
if (n % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime)
printf("%d is a prime number\n", n);
else
printf("%d is a composite number\n", n);
return 0;
}
A number greater than 1 is prime if it has no divisor between 2 and ; otherwise it is composite. For n = 29 the loop finds no divisor and prints prime; for n = 15, , so it prints composite.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- Asked 2 times
- 2018 · 5 marks
- 2019 · 5 marks
What is DMA? Write a program to find the largest and smallest number in a list of N
number using DMA. 1+4]
Answer
DMA (Dynamic Memory Allocation) is the process of allocating and releasing memory at run time from the heap, instead of fixing the size at compile time. The functions in <stdlib.h> are:
malloc(size)allocates a block ofsizebytes (uninitialised).calloc(n, size)allocatesnblocks and initialises them to zero.realloc(ptr, newsize)changes the size of an allocated block.free(ptr)releases the memory.
Program to find the largest and smallest of N numbers using DMA
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *p, max, min;
printf("Enter number of elements: ");
scanf("%d", &n);
p = (int *)malloc(n * sizeof(int));
if (p == NULL) {
printf("Memory allocation failed\n");
return 1;
}
printf("Enter %d numbers:\n", n);
for (i = 0; i < n; i++)
scanf("%d", p + i);
max = min = *p;
for (i = 1; i < n; i++) {
if (*(p + i) > max) max = *(p + i);
if (*(p + i) < min) min = *(p + i);
}
printf("Largest = %d\nSmallest = %d\n", max, min);
free(p);
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- Asked 2 times
- 2018 · 5 marks
- 2019 · 5 marks
What is difference between binary file and text file? Write a C program to write some text "Welcome to BCA program" in a file test.text.
Answer
Difference between binary file and text file
| Basis | Text file | Binary file |
|---|---|---|
| Content | Stored as readable characters (ASCII) | Stored in the same raw form as in memory |
| Readability | Can be opened in any editor | Not human readable |
| Newline | Translated (\n becomes \r\n on Windows) | No translation |
| End of file | Marked by EOF character | Determined by file size |
| Size | Usually larger for numbers (digits as characters) | Compact |
| Access | Mostly sequential | Easy random access with fseek |
| Open mode | "r", "w", "a" | "rb", "wb", "ab" |
| Example | .txt, .c | .exe, .jpg, .dat |
Program to write text in file test.text
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
fp = fopen("test.text", "w");
if (fp == NULL) {
printf("Cannot open file\n");
exit(1);
}
fprintf(fp, "Welcome to BCA program");
fclose(fp);
printf("Text written successfully\n");
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- Asked 2 times
- 2018 · 5 marks
- 2019 · 5 marks
Explain any four graphics functions in C. Write a program to draw two concentric circles with center (50, 50) and radii 75 and 125.
Answer
Four graphics functions in C (from <graphics.h>, Turbo C / BGI)
initgraph(&gd, &gm, "path")initialises the graphics system by loading the graphics driver and setting graphics mode. Withgd = DETECTthe driver is detected automatically.circle(x, y, r)draws a circle with centre and radius in the current colour.line(x1, y1, x2, y2)draws a line from to .rectangle(left, top, right, bottom)draws a rectangle from its top-left to bottom-right corner.
Other useful functions: closegraph() closes the graphics mode, setcolor(c) sets the drawing colour, getmaxx()/getmaxy() return the screen limits.
Program to draw two concentric circles with centre (50, 50), radii 75 and 125
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
circle(50, 50, 75);
circle(50, 50, 125);
getch();
closegraph();
return 0;
}
Both circles use the same centre , so they are concentric. Since the centre is near the top-left corner, part of each circle lies outside the screen and is clipped.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- Asked 2 times
- 2018 · 10 marks
- 2019 · 10 marks
What is one dimensional array? How it is initialized? Write a C program to find the
sum of two matrix of order m×n.
Answer
One dimensional array: A one dimensional array is a collection of a fixed number of elements of the same data type stored in contiguous memory locations and accessed by a single index (subscript) starting from 0. Declaration: int marks[5]; creates marks[0] to marks[4].
Initialization
- Compile time with size:
int a[5] = {10, 20, 30, 40, 50}; - Without size (size taken from the list):
int a[] = {1, 2, 3}; - Partial initialization (rest become 0):
int a[5] = {1, 2};gives 1, 2, 0, 0, 0. - Run time using a loop and
scanf:
for (i = 0; i < 5; i++)
scanf("%d", &a[i]);
Program to find the sum of two matrices of order m x n
#include <stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10];
int m, n, i, j;
printf("Enter number of rows and columns: ");
scanf("%d %d", &m, &n);
printf("Enter elements of first matrix:\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &a[i][j]);
printf("Enter elements of second matrix:\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &b[i][j]);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
c[i][j] = a[i][j] + b[i][j];
printf("Sum of the matrices:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++)
printf("%d\t", c[i][j]);
printf("\n");
}
return 0;
}
Matrix addition is possible only when both matrices have the same order, and each element is .
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- Asked 2 times
- 2018 · 10 marks
- 2019 · 10 marks
Define structure and union? Write a C program using structure that reads the records of 35 students with members roll, name, address and makes and display the record of students who have obtained greater than 250 marks.
Answer
Structure: A structure is a user-defined data type, declared with the keyword struct, that groups variables of different data types under one name. Each member has its own memory, so the size is at least the sum of the members' sizes.
struct student { int roll; char name[30]; };
Union: A union is also a user-defined type declared with union, but all members share the same memory location. Its size equals the size of the largest member, and only one member can hold a valid value at a time.
| Basis | Structure | Union |
|---|---|---|
| Keyword | struct | union |
| Memory | Sum of all members | Size of largest member |
| Access | All members at once | One member at a time |
Program for 35 students, displaying those with marks greater than 250
#include <stdio.h>
struct student {
int roll;
char name[30];
char address[30];
float marks;
};
int main() {
struct student s[35];
int i;
for (i = 0; i < 35; i++) {
printf("Enter roll, name, address, marks of student %d:\n", i + 1);
scanf("%d %s %s %f", &s[i].roll, s[i].name, s[i].address, &s[i].marks);
}
printf("\nStudents with marks greater than 250:\n");
printf("Roll\tName\tAddress\tMarks\n");
for (i = 0; i < 35; i++) {
if (s[i].marks > 250)
printf("%d\t%s\t%s\t%.2f\n", s[i].roll, s[i].name, s[i].address, s[i].marks);
}
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- Asked 2 times
- 2018 · 10 marks
- 2019 · 10 marks
What is function? List its advantages. Explain the concept of function call by value and function call by reference with example.
Answer
Function: A function is a self-contained block of statements that performs a specific task and can be called whenever required. Every C program has at least one function, main().
Advantages of functions
- Avoid repetition of code; a task is written once and reused.
- Make a large program modular, easier to write, read and understand.
- Make debugging and testing easier, since each function can be tested separately.
- Allow team members to develop different functions in parallel.
- Reduce program size and make maintenance simple.
- Functions can be placed in a library and reused in other programs.
Call by value: The values of the actual arguments are copied into the formal parameters. Changes made inside the function do not affect the original variables.
#include <stdio.h>
void swap(int a, int b) {
int t = a; a = b; b = t;
printf("Inside: a=%d b=%d\n", a, b);
}
int main() {
int x = 5, y = 10;
swap(x, y);
printf("Outside: x=%d y=%d\n", x, y);
return 0;
}
Output: Inside: a=10 b=5 and Outside: x=5 y=10 (original unchanged).
Call by reference: The addresses of the actual arguments are passed using pointers, so the function works on the original variables and its changes are reflected in the caller.
#include <stdio.h>
void swap(int *a, int *b) {
int t = *a; *a = *b; *b = t;
}
int main() {
int x = 5, y = 10;
swap(&x, &y);
printf("x=%d y=%d\n", x, y);
return 0;
}
Output: x=10 y=5 (original values swapped).
| Basis | Call by value | Call by reference |
|---|---|---|
| Passed | Copy of value | Address |
| Original changed | No | Yes |
| Memory | Extra copy | No copy of data |
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- Asked 2 times
- 2021 · 5 marks
- 2022 · 5 marks
Write a C program to generate the following output using graphics functions.
BCA Program
Answer
The program uses the Turbo C graphics.h library. initgraph() switches to graphics mode, outtextxy() writes text at a given pixel position, and settextstyle() sets font, direction and size.
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
settextstyle(DEFAULT_FONT, HORIZ_DIR, 3);
outtextxy(200, 200, "BCA Program");
getch();
closegraph();
return 0;
}
The text "BCA Program" is displayed on the graphics screen at (200, 200). getch() holds the screen until a key is pressed and closegraph() returns to text mode.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2020 · 5 marks
What is computer programming? Differentiate between top down and bottom up approach of programming.
Similar questions: Define programming technique. Differentiate between top-down (2023)
Answer
Computer programming is the process of writing, testing, debugging and maintaining a set of instructions (a program) in a programming language so that a computer can perform a specific task. It involves problem analysis, algorithm design, coding, testing and documentation.
Top-down vs bottom-up approach
| Basis | Top-down approach | Bottom-up approach |
|---|---|---|
| Idea | Start with the whole problem and break it into smaller sub-problems | Start by building the small basic modules and combine them into the full system |
| Direction | General to specific | Specific to general |
| Design | Main module designed first, sub-modules later | Lowest-level modules designed and tested first |
| Used by | Procedural languages such as C | Object-oriented languages such as C++ and Java |
| Reuse | Less emphasis on reuse | Promotes reuse of existing modules |
| Testing | Uses stubs for unfinished lower modules | Uses drivers to test low-level modules |
| Example | Dividing a payroll system into input, calculation, report | Writing library functions first, then assembling the application |
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2021 · 5 marks
Define an array? Write a program to generate the following output using loop.
1
12
123
1234
12345
Similar questions: Write a C program to generate the (2022)
Answer
Array: An array is a collection of a fixed number of elements of the same data type stored in contiguous memory locations under one name and accessed by an index, for example int a[5];.
Program to print the pattern
#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= i; j++)
printf("%d", j);
printf("\n");
}
return 0;
}
The outer loop controls the row (1 to 5) and the inner loop prints the numbers 1 to on that row, so row contains digits.
Output:
1
12
123
1234
12345
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 · 5 marks
Write a C program to generate the following output using loop.
10101
0101
101
01
1
Similar questions: Define an array? Write a program to (2021)
Answer
Each row has one digit fewer than the previous row, and every row ends with 1, with 1 and 0 alternating. Row length goes from 5 down to 1. For a row of length , the digit at position () is 1 when is even, else 0.
#include <stdio.h>
int main() {
int i, j, n;
for (i = 5; i >= 1; i--) {
for (j = 0; j < i; j++) {
if ((i - 1 - j) % 2 == 0)
printf("1");
else
printf("0");
}
printf("\n");
}
return 0;
}
Output:
10101
0101
101
01
1
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 · 5 marks
Write a C program to copy contents of student.txt file into another file called info.txt.
Similar questions: What is the difference between a binary (2024)
Answer
The source file is opened in read mode and the destination in write mode. Characters are read one by one with fgetc() and written with fputc() until EOF.
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fs, *ft;
char ch;
fs = fopen("student.txt", "r");
if (fs == NULL) {
printf("Cannot open student.txt\n");
exit(1);
}
ft = fopen("info.txt", "w");
if (ft == NULL) {
printf("Cannot open info.txt\n");
fclose(fs);
exit(1);
}
while ((ch = fgetc(fs)) != EOF)
fputc(ch, ft);
printf("File copied successfully\n");
fclose(fs);
fclose(ft);
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2023 · 5 marks
Define programming technique. Differentiate between top-down and bottom-up approach with suitable example.
Similar questions: What is computer programming? Differentiate between top (2020)
Answer
Programming technique
A programming technique is a systematic method or approach for designing and organising a program so that it is clear, correct and easy to maintain. Examples are procedural (modular) programming, structured programming, top-down and bottom-up design, and object-oriented programming.
Top-down versus bottom-up
| Basis | Top-down approach | Bottom-up approach |
|---|---|---|
| Starting point | Starts from the main problem and divides it into smaller sub-problems | Starts by building the smallest, basic modules first |
| Direction | General to specific | Specific to general |
| Process | Decomposition | Composition / integration |
| Testing | Main module tested first, with stubs for lower ones | Lower modules tested first, with drivers |
| Reuse | Less emphasis on reuse | Encourages reuse of existing modules |
| Used in | Structured languages such as C | Object-oriented languages such as C++ and Java |
Example
Payroll program. Top-down: start with "Prepare payroll", split into read employee data, calculate salary, calculate tax, print slip, and divide "calculate salary" further. Bottom-up: first write small functions such as tax(), bonus() and print_slip(), then combine them into calculate_salary() and finally the complete payroll program.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2024 · 5 marks
What is the difference between a binary file and a text file? Write a C program to copy the contents of a student.txt file into another file called info.txt.
Similar questions: Write a C program to copy contents (2022)
Answer
Difference between binary file and text file
| Text file | Binary file |
|---|---|
| Stores data as readable ASCII characters. | Stores data in the same form as in memory (raw bytes). |
| Can be opened and read in any text editor. | Not human readable; needs a program to interpret it. |
Newline is translated (e.g. \n becomes \r\n on Windows). | No translation takes place. |
| Larger for numeric data (each digit is a character). | Compact; an int takes 4 bytes whatever its value. |
Opened with modes such as "r", "w". | Opened with modes such as "rb", "wb". |
Uses fprintf, fscanf, fgets, fputs, fgetc. | Uses fread, fwrite. |
Example: .txt, .c. | Example: .exe, .jpg, .dat. |
Program: copy student.txt to info.txt
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fs, *ft;
char ch;
fs = fopen("student.txt", "r");
if (fs == NULL) {
printf("Cannot open source file\n");
exit(1);
}
ft = fopen("info.txt", "w");
if (ft == NULL) {
printf("Cannot create target file\n");
fclose(fs);
exit(1);
}
while ((ch = fgetc(fs)) != EOF)
fputc(ch, ft);
printf("File copied successfully\n");
fclose(fs);
fclose(ft);
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2020 · 5 marks
What is the purpose of size of operator? Write a C program to swap the value of two integers without using third variable.
Answer
Purpose of sizeof operator: sizeof is a compile-time unary operator that returns the size, in bytes, of a data type or variable. It is used to write portable code, for example sizeof(int) is 4 on most systems, and to compute the memory needed in dynamic allocation, such as malloc(n * sizeof(int)).
Program to swap two integers without a third variable
#include <stdio.h>
int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
printf("Before swap: a = %d, b = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}
Check with , : ; ; . So , , which is the swapped pair.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2020 · 5 marks
Write a program in C to find all possible roots of quadratic equation ax² + bx + c = 0.
Answer
For the discriminant is and the roots are
- If : two real and distinct roots.
- If : two real and equal roots, .
- If : two complex conjugate roots, .
- If the equation is linear, with root .
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, d, r1, r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f %f %f", &a, &b, &c);
if (a == 0) {
if (b != 0)
printf("Linear equation, root = %.2f\n", -c / b);
else
printf("Not a valid equation\n");
return 0;
}
d = b * b - 4 * a * c;
if (d > 0) {
r1 = (-b + sqrt(d)) / (2 * a);
r2 = (-b - sqrt(d)) / (2 * a);
printf("Real and distinct roots: %.2f and %.2f\n", r1, r2);
} else if (d == 0) {
r1 = -b / (2 * a);
printf("Real and equal roots: %.2f and %.2f\n", r1, r1);
} else {
real = -b / (2 * a);
imag = sqrt(-d) / (2 * a);
printf("Complex roots: %.2f + %.2fi and %.2f - %.2fi\n", real, imag, real, imag);
}
return 0;
}
Example: gives , roots and .
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2020 · 5 marks
Why an array is required in programming? Write a C program to input age of 30 employees into array and find out second largest age from array.
Answer
Need of an array: When many values of the same type must be stored, declaring a separate variable for each (for example 30 ages) is impractical. An array stores them under one name in contiguous memory, allows access through an index, makes loops, searching and sorting easy, and keeps the program short and readable.
Program to find the second largest age among 30 employees
#include <stdio.h>
int main() {
int age[30], i, largest, second;
printf("Enter the age of 30 employees:\n");
for (i = 0; i < 30; i++)
scanf("%d", &age[i]);
largest = second = -1;
for (i = 0; i < 30; i++) {
if (age[i] > largest) {
second = largest;
largest = age[i];
} else if (age[i] > second && age[i] != largest) {
second = age[i];
}
}
if (second == -1)
printf("No second largest age exists\n");
else
printf("Second largest age = %d\n", second);
return 0;
}
The array is scanned once. Whenever a new maximum is found, the old maximum becomes the second largest. For ages 25, 40, 40, 33 the result is 33.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2020 · 5 marks
What is recursive function? Write C program to generate the Fibonacci series up to 13th term using recursive function.
Answer
Recursive function: A function that calls itself, directly or indirectly, to solve a smaller instance of the same problem is a recursive function. It must have a base condition that stops the recursion, otherwise it never ends. Example: with .
Fibonacci series up to the 13th term using recursion
The series is defined by , and .
#include <stdio.h>
int fib(int n) {
if (n == 1) return 0;
if (n == 2) return 1;
return fib(n - 1) + fib(n - 2);
}
int main() {
int i;
printf("Fibonacci series up to 13th term:\n");
for (i = 1; i <= 13; i++)
printf("%d ", fib(i));
printf("\n");
return 0;
}
Output: 0 1 1 2 3 5 8 13 21 34 55 89 144
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2020 · 5 marks
What is the advantage of pointer? Write a C program to store 10 integers into memory and find out minimum and maximum using dynamic memory allocation (DMA).
Answer
Advantages of pointers
- Allow dynamic memory allocation and release (
malloc,free). - Permit call by reference, so a function can change the caller's variables and return more than one value.
- Make array and string handling efficient through pointer arithmetic.
- Needed for data structures such as linked lists, stacks, queues and trees.
- Save memory and time, because large data (arrays, structures) is passed by address rather than copied.
Program to store 10 integers using DMA and find minimum and maximum
#include <stdio.h>
#include <stdlib.h>
int main() {
int *p, i, min, max;
p = (int *)calloc(10, sizeof(int));
if (p == NULL) {
printf("Memory not available\n");
return 1;
}
printf("Enter 10 integers:\n");
for (i = 0; i < 10; i++)
scanf("%d", p + i);
min = max = p[0];
for (i = 1; i < 10; i++) {
if (p[i] < min) min = p[i];
if (p[i] > max) max = p[i];
}
printf("Minimum = %d\nMaximum = %d\n", min, max);
free(p);
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2020 · 5 marks
What is the use of initgraph() in C program? Write a program to draw line and circle and rectangle using graphic function.
Answer
Use of initgraph(): initgraph(&graphdriver, &graphmode, "path") initialises the graphics system. It loads the graphics driver from the given BGI path into memory, selects the graphics mode and puts the screen into graphics mode. It must be called before any other graphics function. With graphdriver = DETECT, the best driver and mode are detected automatically. The mode is closed with closegraph().
Program to draw a line, a circle and a rectangle
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
line(50, 50, 250, 50); /* line from (50,50) to (250,50) */
circle(150, 200, 60); /* centre (150,200), radius 60 */
rectangle(300, 100, 450, 200); /* left, top, right, bottom */
getch();
closegraph();
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2020 · 10 marks
What is structure? Write a C program to enter id, name and address of 25 structure variable called employee and sort them in ascending order on the basis with use of pointer.
Answer
Structure: A structure is a user-defined data type, created with the keyword struct, that groups related variables of different data types under a single name, for example an employee's id, name and address.
Program: 25 employees sorted in ascending order of id using pointers
#include <stdio.h>
#include <string.h>
struct employee {
int id;
char name[30];
char address[30];
};
int main() {
struct employee emp[25], temp;
struct employee *p = emp;
int i, j;
for (i = 0; i < 25; i++) {
printf("Enter id, name and address of employee %d:\n", i + 1);
scanf("%d %s %s", &(p + i)->id, (p + i)->name, (p + i)->address);
}
/* bubble sort on id using pointer */
for (i = 0; i < 24; i++) {
for (j = 0; j < 24 - i; j++) {
if ((p + j)->id > (p + j + 1)->id) {
temp = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = temp;
}
}
}
printf("\nEmployees in ascending order of id:\n");
printf("ID\tName\tAddress\n");
for (i = 0; i < 25; i++)
printf("%d\t%s\t%s\n", (p + i)->id, (p + i)->name, (p + i)->address);
return 0;
}
The pointer p points to the first element of the array emp; (p + i)->id accesses the member of the th element, and whole structures are swapped using *(p + j).
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2020 · 10 marks
Is break keyword mandatory in switch statement? Write a program to calculate arithmetic operations (+, -, *, /) using switch case statement.
Answer
Is break mandatory in a switch statement? No, break is not mandatory; the program compiles and runs without it. However, without break, execution continues (falls through) into the following cases after a match until a break or the end of the switch is reached. So break is normally used at the end of every case to exit the switch. It is optional only in the last case or default, or when fall-through is intended (for example, several cases sharing one action).
Program for arithmetic operations using switch
#include <stdio.h>
int main() {
float a, b;
char op;
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &op);
switch (op) {
case '+':
printf("%.2f + %.2f = %.2f\n", a, b, a + b);
break;
case '-':
printf("%.2f - %.2f = %.2f\n", a, b, a - b);
break;
case '*':
printf("%.2f * %.2f = %.2f\n", a, b, a * b);
break;
case '/':
if (b != 0)
printf("%.2f / %.2f = %.2f\n", a, b, a / b);
else
printf("Division by zero is not possible\n");
break;
default:
printf("Invalid operator\n");
}
return 0;
}
Example: inputs 12, 4 and / give 12.00 / 4.00 = 3.00.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2020 · 10 marks
What do you mean by Software Development Life Cycle? Explain waterfall modelw advantage and disadvantage.
Answer
Software Development Life Cycle (SDLC): SDLC is the systematic process, made up of a series of phases, used to plan, create, test and deploy software of high quality. Its phases are: (1) requirement analysis, (2) system design, (3) implementation (coding), (4) testing, (5) deployment, and (6) maintenance.
Waterfall model
The waterfall model is the oldest, linear sequential model. Each phase (requirements, design, implementation, testing, deployment, maintenance) must be completed before the next begins, and the output of one phase is the input to the next, like water falling down steps. There is no overlap between phases and little scope to return to an earlier phase.
Advantages
- Simple, easy to understand and use.
- Phases are rigid and well defined, so management and tracking are easy.
- Each phase has specific deliverables and a review.
- Works well for small projects whose requirements are clear and stable.
- Documentation is produced at every stage.
Disadvantages
- Requirements must be fully known at the start; changes are difficult and costly.
- Working software is produced only late in the life cycle.
- High risk and uncertainty, since errors found in testing may require going back to design.
- Customer sees the product only at the end, so feedback comes late.
- Not suitable for large, long or complex projects, or those with changing requirements.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2021 · 5 marks
What are keywords? Explain the compiling process of C program.
Answer
Keywords: Keywords are reserved words in C whose meaning is predefined for the compiler. They cannot be used as identifiers. Examples: int, float, char, if, else, for, while, return, void, struct. C89 has 32 keywords.
Compiling process of a C program
A C source file (prog.c) passes through the following stages to become an executable:
- Preprocessing: The preprocessor handles lines starting with
#: it includes header files (#include), expands macros (#define), removes comments and handles conditional compilation. Output is an expanded source file. - Compilation: The compiler checks syntax and converts the expanded C code into assembly code, reporting syntax errors and warnings.
- Assembly: The assembler converts the assembly code into machine code, producing an object file (
prog.objorprog.o). - Linking: The linker combines the object file with other object files and library functions (for example
printffrom the standard library) to produce the final executable file (prog.exeora.out). - Loading and execution: The loader places the executable in memory and the CPU runs it.
Flow: prog.c -> preprocessor -> compiler -> assembler -> prog.o -> linker (with libraries) -> prog.exe.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2021 · 5 marks
Differentiate between iteration and recursion.
Answer
Difference between iteration and recursion
| Basis | Iteration | Recursion |
|---|---|---|
| Meaning | A block of code is repeated using a loop (for, while, do-while) | A function calls itself to solve smaller sub-problems |
| Termination | When the loop condition becomes false | When the base case is reached |
| Memory | Uses little memory; no extra stack frames | Uses more memory; each call adds a stack frame |
| Speed | Generally faster | Generally slower due to function-call overhead |
| Code size | Longer for some problems | Short and clear for problems like factorial, Fibonacci, tower of Hanoi, tree traversal |
| Infinite case | Infinite loop if condition never fails | Stack overflow if base case is missing |
Example, factorial:
/* Iteration */
int fact(int n) {
int f = 1, i;
for (i = 1; i <= n; i++) f *= i;
return f;
}
/* Recursion */
int factr(int n) {
if (n <= 1) return 1;
return n * factr(n - 1);
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2021 · 5 marks
Define operator. Explain any four types of operators available in C?
Answer
Operator: An operator is a symbol that tells the compiler to perform a specific mathematical, relational or logical operation on one or more operands. For example, in a + b the symbol + is the operator and a, b are operands.
Four types of operators in C
- Arithmetic operators:
+,-,*,/,%. They perform mathematical calculations.%gives the remainder, e.g.17 % 5 = 2, and/on two integers gives an integer, e.g.17 / 5 = 3. - Relational operators:
<,>,<=,>=,==,!=. They compare two values and give 1 (true) or 0 (false), e.g.5 > 3gives 1. - Logical operators:
&&(AND),||(OR),!(NOT). They combine conditions:(a > 0 && b > 0)is true only if both are true. - Assignment operators:
=,+=,-=,*=,/=,%=. They store a value in a variable;x += 5is the same asx = x + 5.
Other types: increment/decrement (++, --), bitwise (&, |, ^, ~, <<, >>), conditional (?:) and special operators (sizeof, ,).
int a = 10, b = 3;
printf("%d %d %d\n", a + b, a % b, a > b); /* 13 1 1 */
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2021 · 5 marks
Why function is required in C programming? Write a program to find smallest number from the array using function.
Answer
Why a function is required
- It avoids repeating the same code; a task written once can be called many times.
- It divides a large program into small, manageable modules (modular programming).
- It makes the program easier to read, test, debug and maintain.
- It permits reuse of code in other programs and teamwork on different parts.
- It reduces the overall program size.
Program to find the smallest number in an array using a function
#include <stdio.h>
int smallest(int a[], int n) {
int i, min = a[0];
for (i = 1; i < n; i++) {
if (a[i] < min)
min = a[i];
}
return min;
}
int main() {
int arr[100], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Smallest number = %d\n", smallest(arr, n));
return 0;
}
For the input 7, 3, 9, 2, 5 the function returns 2.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2021 · 5 marks
Why DMA is used in C language? Write a program to enter age of 20 students and count age between 18 and 25 from the array using DMA functions.
Answer
Why DMA is used
DMA (Dynamic Memory Allocation) lets a program request memory at run time from the heap using malloc(), calloc(), realloc() and release it with free() (all in <stdlib.h>).
- The size of an ordinary array must be fixed at compile time, which either wastes memory or is too small. With DMA the exact size is decided while the program runs.
- Memory can be resized (
realloc) and returned to the system (free) when no longer needed, so memory is used efficiently. - It is the basis for linked lists, trees and other dynamic data structures.
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
int *age, i, count = 0;
age = (int *)malloc(20 * sizeof(int));
if (age == NULL) {
printf("Memory not allocated\n");
return 1;
}
for (i = 0; i < 20; i++) {
printf("Enter age of student %d: ", i + 1);
scanf("%d", &age[i]);
}
for (i = 0; i < 20; i++) {
if (age[i] >= 18 && age[i] <= 25)
count++;
}
printf("Students with age between 18 and 25 = %d\n", count);
free(age);
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2021 · 10 marks
What is union? Write a C program to enter bid, title, price, pages of 100 book into structure variable called book and sort them in descending order on the basis of their title with use of pointer.
Answer
Union
A union is a user-defined data type, declared with the keyword union, whose members all share the same memory location. Its size equals the size of its largest member, and only one member can hold a valid value at a time. Example: union data { int i; float f; char c; };. A structure, in contrast, allocates separate memory for every member.
Program
The books are accessed through a pointer to the structure array and sorted in descending order of title using strcmp().
#include <stdio.h>
#include <string.h>
struct book {
int bid;
char title[50];
float price;
int pages;
};
int main() {
struct book b[100], temp, *p;
int i, j, n = 100;
p = b;
for (i = 0; i < n; i++) {
printf("Enter bid, title, price, pages of book %d:\n", i + 1);
scanf("%d %s %f %d", &(p + i)->bid, (p + i)->title,
&(p + i)->price, &(p + i)->pages);
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (strcmp((p + j)->title, (p + j + 1)->title) < 0) {
temp = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = temp;
}
}
}
printf("\nBooks in descending order of title:\n");
for (i = 0; i < n; i++)
printf("%d\t%s\t%.2f\t%d\n", (p + i)->bid, (p + i)->title,
(p + i)->price, (p + i)->pages);
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2021 · 10 marks
What is the use of data file in C? Write a program to accept 100 numbers from user and store them in odd.txt file (if number is odd) or even.txt (If number is even) then display odd numbers reading from odd.txt file.
Answer
Use of data files
Variables and arrays lose their values when the program ends, and keyboard input is tedious for large data. A data file stores data permanently on disk so that it can be saved, read again later, shared between programs and processed in large volumes. In C, files are handled through a FILE pointer with functions such as fopen(), fprintf(), fscanf() and fclose().
Program
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fo, *fe, *fr;
int i, num;
fo = fopen("odd.txt", "w");
fe = fopen("even.txt", "w");
if (fo == NULL || fe == NULL) {
printf("Cannot open file\n");
exit(1);
}
for (i = 1; i <= 100; i++) {
printf("Enter number %d: ", i);
scanf("%d", &num);
if (num % 2 != 0)
fprintf(fo, "%d\n", num);
else
fprintf(fe, "%d\n", num);
}
fclose(fo);
fclose(fe);
fr = fopen("odd.txt", "r");
printf("\nOdd numbers in odd.txt:\n");
while (fscanf(fr, "%d", &num) == 1)
printf("%d\n", num);
fclose(fr);
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2021 · 10 marks
What do you mean by System design tools? Explain any four design tools with suitable examples.
Answer
System design tools
System design tools are graphical or textual techniques used by analysts and designers to describe, plan and document how a system or program will work before coding begins. They make the logic clear, help find errors early and serve as documentation.
1. Flowchart A diagram that shows the steps of a process using standard symbols: oval (start/stop), parallelogram (input/output), rectangle (process), diamond (decision) and arrows (flow). Example: to find the larger of two numbers, Start, read A and B, decision "A > B?", if yes print A, else print B, Stop.
2. Algorithm A finite, step-by-step set of instructions written in simple language to solve a problem. Example, sum of two numbers:
- Start
- Read a, b
- sum = a + b
- Print sum
- Stop
3. Pseudocode An informal, code-like description of logic that is not tied to any language. Example:
READ marks
IF marks >= 40 THEN
PRINT "Pass"
ELSE
PRINT "Fail"
ENDIF
4. Data Flow Diagram (DFD) Shows how data moves through a system using external entities (squares), processes (circles), data stores (open rectangles) and data flows (arrows). Example: in a library system, the Member (entity) sends a "book request" to the process "Issue Book", which reads the Books data store and sends a "issue slip" back to the Member.
Other tools include decision tables, decision trees and structure charts.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 · 5 marks
How will you define pointer? Write a program that illustrate how pointer variable change the value of normal variable.
Answer
Pointer
A pointer is a variable that stores the memory address of another variable. It is declared with *, for example int *p;. The address-of operator & gives the address of a variable, and the dereference operator * accesses the value stored at that address.
Program: changing a variable through a pointer
#include <stdio.h>
int main() {
int x = 10;
int *p;
p = &x;
printf("Before: x = %d\n", x);
*p = 50; /* value of x changed using the pointer */
printf("After : x = %d\n", x);
printf("Address of x = %p, value of p = %p\n", (void *)&x, (void *)p);
return 0;
}
Output:
Before: x = 10
After : x = 50
Since p holds the address of x, the assignment *p = 50 writes 50 directly into the memory of x, so the normal variable changes.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 · 5 marks
Write a C program to calculate sum of the natural number up to N number using recursive function.
Answer
A recursive function calls itself. The sum of the first natural numbers satisfies
where is the base case that stops the recursion.
#include <stdio.h>
int sum(int n) {
if (n == 1)
return 1;
return n + sum(n - 1);
}
int main() {
int n;
printf("Enter N: ");
scanf("%d", &n);
printf("Sum of natural numbers up to %d = %d\n", n, sum(n));
return 0;
}
For : .
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 · 5 marks
Write a C program to calculate following expression: 5x √y² + 5.
Answer
The expression is read as . The program uses sqrt() and pow() from <math.h>.
#include <stdio.h>
#include <math.h>
int main() {
float x, y, result;
printf("Enter x and y: ");
scanf("%f %f", &x, &y);
result = 5 * x * sqrt(pow(y, 2) + 5);
printf("Result = %.2f\n", result);
return 0;
}
Check: for : .
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 · 5 marks
Why do you use DMA instead of array? Explain DMA with suitable example.
Answer
Why DMA instead of an array
An array has a fixed size declared at compile time. If the size is too large, memory is wasted; if too small, data cannot be stored, and it cannot be resized or freed before the function ends. Dynamic Memory Allocation (DMA) allocates memory from the heap at run time, so only the required amount is used, the size can be changed with realloc(), and the memory can be released with free().
DMA functions (in <stdlib.h>)
| Function | Purpose |
|---|---|
malloc(size) | allocates size bytes, uninitialised |
calloc(n, size) | allocates n blocks, initialised to zero |
realloc(ptr, newsize) | changes size of allocated block |
free(ptr) | releases the memory |
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *p, sum = 0;
printf("How many numbers? ");
scanf("%d", &n);
p = (int *)malloc(n * sizeof(int));
if (p == NULL) {
printf("Allocation failed\n");
return 1;
}
for (i = 0; i < n; i++) {
scanf("%d", p + i);
sum += *(p + i);
}
printf("Sum = %d\n", sum);
free(p);
return 0;
}
The size n is known only at run time, which a normal array could not handle without wasting memory.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 · 10 marks
Write a C program to store the player name, runs scored, wickets taken of 20 cricketers using structure along with pointer then arrange the players in order of maximum wickets to minimum wickets they had taken.
Answer
The program stores name, runs and wickets of 20 players in an array of structures, accesses it through a pointer, and sorts it with bubble sort so the player with most wickets comes first.
#include <stdio.h>
struct player {
char name[30];
int runs;
int wickets;
};
int main() {
struct player pl[20], temp, *p;
int i, j, n = 20;
p = pl;
for (i = 0; i < n; i++) {
printf("Enter name, runs, wickets of player %d: ", i + 1);
scanf("%s %d %d", (p + i)->name, &(p + i)->runs, &(p + i)->wickets);
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if ((p + j)->wickets < (p + j + 1)->wickets) {
temp = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = temp;
}
}
}
printf("\nPlayers in order of maximum to minimum wickets:\n");
printf("%-20s %-8s %-8s\n", "Name", "Runs", "Wickets");
for (i = 0; i < n; i++)
printf("%-20s %-8d %-8d\n", (p + i)->name, (p + i)->runs, (p + i)->wickets);
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 · 10 marks
Write a C program to generate following series using function:
Sum = 1 + x/2! + x²/3! + x³/4! + ... + xⁿ/n!
Answer
The series has terms; term is for :
Two functions are used: fact() computes a factorial and series() adds up the terms.
#include <stdio.h>
#include <math.h>
long double fact(int n) {
long double f = 1;
int i;
for (i = 2; i <= n; i++)
f *= i;
return f;
}
double series(double x, int n) {
double sum = 0;
int k;
for (k = 0; k < n; k++)
sum += pow(x, k) / fact(k + 1);
return sum;
}
int main() {
double x;
int n;
printf("Enter x and number of terms n: ");
scanf("%lf %d", &x, &n);
printf("Sum = %.4f\n", series(x, n));
return 0;
}
Check: : , which the program prints.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 · 10 marks
Define SDLC. Explain software process models.
Answer
SDLC
The Software Development Life Cycle (SDLC) is the structured series of phases through which software is planned, built, tested, delivered and maintained, so that a high-quality product is produced within time and budget.
Phases: requirement analysis, system design, implementation (coding), testing, deployment, and maintenance.
Software process models
1. Waterfall model Phases are carried out one after another in a linear order; the output of one phase is the input of the next, and there is no going back easily.
- Advantages: simple, well documented, easy to manage.
- Disadvantages: inflexible to changing requirements; working software appears only late.
2. Prototype model A quick, working prototype is built from basic requirements, shown to the user, refined repeatedly on feedback, and then the final system is built.
- Advantages: clarifies unclear requirements, early user involvement.
- Disadvantages: can lead to poor design if the prototype is carried into the product; extra cost.
3. Spiral model Combines waterfall and prototyping with risk analysis. The project passes through repeated spirals, each with planning, risk analysis, engineering and evaluation.
- Advantages: good risk management, suited to large, complex projects.
- Disadvantages: expensive; needs risk-analysis expertise.
4. Incremental / iterative model The system is built and delivered in small increments; each increment adds functionality and the first one delivers the core product.
- Advantages: early partial delivery, easier testing and change.
5. Agile model Short iterations (sprints) with close customer collaboration, continuous feedback and adaptation to change (for example Scrum, XP).
6. V-model An extension of waterfall in which each development phase has a matching testing phase.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2023 · 5 marks
Define Data Types. Explain different data types available in C Programming.
Answer
Data type
A data type specifies the kind of value a variable can hold, the amount of memory it needs and the operations allowed on it.
1. Primary (basic) data types
| Type | Size (typical) | Description | Example |
|---|---|---|---|
char | 1 byte | single character | char ch = 'A'; |
int | 2 or 4 bytes | whole numbers | int a = 25; |
float | 4 bytes | single-precision real number | float x = 3.14; |
double | 8 bytes | double-precision real number | double d = 2.718281; |
void | none | no value; used for functions without return value | void show(); |
Modifiers short, long, signed and unsigned change the range, for example unsigned int, long int.
2. Derived data types
Built from basic types: arrays (int a[10]), pointers (int *p), functions.
3. User-defined data types
Created by the programmer: structure (struct), union (union), enumeration (enum) and typedef.
Example:
struct student { int roll; char name[20]; float marks; };
enum day { MON, TUE, WED };
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2023 · 5 marks
Explain bitwise and conditional operator with example.
Answer
Bitwise operators
They work on individual bits of integer operands.
| Operator | Meaning |
|---|---|
& | bitwise AND |
| | bitwise OR |
^ | bitwise XOR |
~ | one's complement (NOT) |
<< | left shift |
>> | right shift |
Example with a = 12 (1100) and b = 10 (1010):
#include <stdio.h>
int main() {
int a = 12, b = 10;
printf("%d\n", a & b); /* 1000 = 8 */
printf("%d\n", a | b); /* 1110 = 14 */
printf("%d\n", a ^ b); /* 0110 = 6 */
printf("%d\n", a << 1); /* 11000 = 24 */
printf("%d\n", a >> 2); /* 0011 = 3 */
return 0;
}
Conditional (ternary) operator ?:
It is the only operator with three operands and is a short form of if-else.
Syntax: expression1 ? expression2 : expression3;
If expression1 is true, expression2 is evaluated, otherwise expression3.
Example:
int a = 10, b = 20, max;
max = (a > b) ? a : b; /* max = 20 */
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2023 · 5 marks
Write a program to print the following pattern using loop.
U
UN
UNI
UNIV
UNIVE
UNIVER
UNIVERS
UNIVERSI
UNIVERSIT
UNIVERSITY
Answer
Row (from 1 to 10) prints the first characters of the string "UNIVERSITY". A nested loop prints characters of the string using the index.
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "UNIVERSITY";
int i, j, n = strlen(s);
for (i = 1; i <= n; i++) {
for (j = 0; j < i; j++)
printf("%c", s[j]);
printf("\n");
}
return 0;
}
Output:
U
UN
UNI
UNIV
UNIVE
UNIVER
UNIVERS
UNIVERSI
UNIVERSIT
UNIVERSITY
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2023 · 5 marks
Define Array. Write a program to search an element from an array of 10 numbers.
Answer
Array
An array is a collection of a fixed number of elements of the same data type stored in contiguous memory locations and accessed by a common name and an index (starting from 0). Example: int a[10];.
Program: linear search
#include <stdio.h>
int main() {
int a[10], i, key, found = 0;
printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++)
scanf("%d", &a[i]);
printf("Enter the element to search: ");
scanf("%d", &key);
for (i = 0; i < 10; i++) {
if (a[i] == key) {
found = 1;
break;
}
}
if (found)
printf("%d found at position %d\n", key, i + 1);
else
printf("%d not found in the array\n", key);
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2023 · 5 marks
Define function. Write a program that find xy using function.
Answer
Function
A function is a named, self-contained block of code that performs a specific task and can be called from other parts of the program. It promotes reusability and modular design.
Program to find
The function multiplies by itself times:
#include <stdio.h>
long power(int x, int y) {
long result = 1;
int i;
for (i = 1; i <= y; i++)
result *= x;
return result;
}
int main() {
int x, y;
printf("Enter x and y: ");
scanf("%d %d", &x, &y);
printf("%d^%d = %ld\n", x, y, power(x, y));
return 0;
}
For the output is .
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2023 · 5 marks
Define pointer. Write a program to find sum of 10 numbers using pointer.
Answer
Pointer
A pointer is a variable that holds the memory address of another variable. Declaration: int *p;. & gives an address and * accesses the value at that address. For an array, a is the address of its first element, so *(p + i) is a[i].
Program
#include <stdio.h>
int main() {
int a[10], i, sum = 0, *p;
p = a;
printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++)
scanf("%d", p + i);
for (i = 0; i < 10; i++)
sum += *(p + i);
printf("Sum = %d\n", sum);
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2023 · 10 marks
What are the characteristics of a good program? Explain types of languages processor. Explain any two system design tools.
Answer
Characteristics of a good program
- Correctness: produces the right output for all valid inputs.
- Readability: meaningful names, indentation and comments make it easy to understand.
- Efficiency: uses minimum time and memory.
- Robustness: handles invalid input and errors without crashing.
- Portability: runs on different machines and systems with little change.
- Modularity: divided into small functions that can be tested and reused.
- Maintainability and documentation: easy to modify, debug and extend.
- User friendliness: clear prompts and output.
Types of language processors
A language processor translates a program written in a high-level or assembly language into machine code.
- Assembler: converts assembly language into machine language.
- Compiler: translates the whole high-level program into machine code at once and reports all errors together; the resulting program runs fast (C, C++).
- Interpreter: translates and executes the program line by line, stopping at the first error; execution is slower but debugging is easier (Python, BASIC).
Two system design tools
Flowchart: a pictorial representation of the logic of a program using standard symbols (oval for start/stop, parallelogram for input/output, rectangle for process, diamond for decision) joined by arrows. It shows the flow clearly and helps in debugging and documentation. Example: to check pass or fail, read marks, test "marks >= 40?", print "Pass" if yes, otherwise "Fail".
Algorithm: a finite sequence of well-defined steps to solve a problem, written in plain language. Example for the largest of two numbers:
- Start
- Read A, B
- If A > B, print A; otherwise print B
- Stop
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2023 · 10 marks
Define file opening modes. Write a program to delete specific record from a file.
Answer
File opening modes
| Mode | Meaning |
|---|---|
"r" | open existing file for reading; fails if the file does not exist |
"w" | create a file for writing; existing content is erased |
"a" | open for appending; data is added at the end, file is created if missing |
"r+" | open existing file for reading and writing |
"w+" | create for reading and writing; existing content erased |
"a+" | open for reading and appending |
Adding b (for example "rb", "wb") opens the file in binary mode.
Program to delete a specific record
A file cannot be shortened in place, so all records except the one to be deleted are copied to a temporary file; then the original is removed and the temporary file is renamed.
#include <stdio.h>
#include <stdlib.h>
struct student {
int roll;
char name[30];
};
int main() {
FILE *fp, *ft;
struct student s;
int delroll, found = 0;
/* create some records */
fp = fopen("student.dat", "ab");
char ch = 'y';
while (ch == 'y') {
printf("Enter roll and name: ");
scanf("%d %s", &s.roll, s.name);
fwrite(&s, sizeof(s), 1, fp);
printf("Add more (y/n)? ");
scanf(" %c", &ch);
}
fclose(fp);
printf("Enter roll number to delete: ");
scanf("%d", &delroll);
fp = fopen("student.dat", "rb");
ft = fopen("temp.dat", "wb");
if (fp == NULL || ft == NULL) {
printf("File error\n");
exit(1);
}
while (fread(&s, sizeof(s), 1, fp) == 1) {
if (s.roll == delroll)
found = 1;
else
fwrite(&s, sizeof(s), 1, ft);
}
fclose(fp);
fclose(ft);
remove("student.dat");
rename("temp.dat", "student.dat");
if (found)
printf("Record deleted\n");
else
printf("Record not found\n");
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2023 · 10 marks
Differentiate between structure and Union. Locate a structure named student with members roll no, name, address. Write a program that displays name of only those students whose addressing is Kathmandu
Answer
Structure versus union
| Basis | Structure | Union |
|---|---|---|
| Keyword | struct | union |
| Memory | Separate memory for each member; size is the sum of all members (with padding) | All members share one memory block; size equals the largest member |
| Access | All members can hold values at the same time | Only one member holds a valid value at a time |
| Value change | Changing one member does not affect others | Writing one member overwrites the others |
| Use | When all data are needed together | To save memory when only one member is used at a time |
Program
#include <stdio.h>
#include <string.h>
struct student {
int roll;
char name[30];
char address[30];
};
int main() {
struct student s[100];
int n, i;
printf("Enter number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter roll no, name, address of student %d: ", i + 1);
scanf("%d %s %s", &s[i].roll, s[i].name, s[i].address);
}
printf("\nStudents whose address is Kathmandu:\n");
for (i = 0; i < n; i++) {
if (strcmp(s[i].address, "Kathmandu") == 0)
printf("%s\n", s[i].name);
}
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2024 · 5 marks
What type of programming approach is followed by the C program? Write the necessary steps for the Program Development Life Cycle (PDLC).
Answer
Programming approach of C
C follows the procedural, structured, top-down programming approach. A problem is divided into smaller functions (modules), each doing one task, and the program is built using the three control structures: sequence, selection (if, switch) and iteration (for, while, do-while). It avoids unconditional jumps (goto) and uses a step-by-step flow of procedures.
Program Development Life Cycle (PDLC)
- Problem definition: understand clearly what the program must do, its inputs and outputs.
- Analysis: study the requirements, data and processing needed, and decide on the method of solution.
- Design: plan the solution using algorithms, pseudocode and flowcharts.
- Coding: write the program in a programming language such as C.
- Compilation and debugging: compile, find and remove syntax, logical and run-time errors.
- Testing: run the program with different test data to check that it gives correct results.
- Implementation and documentation: install the program for use and prepare user and technical documents.
- Maintenance: correct remaining errors and update the program as needs change.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2024 · 5 marks
Is the break keyword mandatory in the switch statement? List the operators used in C based on utility. Explain the concept of a logical operator with an example.
Answer
Is break mandatory in switch? No, it is not mandatory syntactically, but it is normally needed. Without break, control falls through and executes the statements of every following case until a break or the end of the switch is reached. The last case or default does not need it. Fall-through is sometimes used deliberately to let several cases share one block.
switch (day) {
case 1:
case 7: printf("Weekend"); break; /* deliberate fall-through */
default: printf("Weekday");
}
Operators in C based on utility
- Arithmetic:
+ - * / % - Relational:
< > <= >= == != - Logical:
&& || ! - Assignment:
= += -= *= /= %= - Increment/decrement:
++ -- - Bitwise:
& | ^ ~ << >> - Conditional (ternary):
?: - Special:
sizeof, comma,, address-of&, dereference*, member access.and->
Logical operators combine or invert conditions and give 1 (true) or 0 (false).
&&(AND) is true only if both operands are true.||(OR) is true if at least one operand is true.!(NOT) reverses the truth value.
int age = 25, income = 40000;
if (age > 18 && income > 30000)
printf("Eligible for loan");
if (age < 18 || age > 60)
printf("Special category");
if (!(age == 25))
printf("Not 25");
Here the first condition is true because both parts are true, so "Eligible for loan" is printed. && and || use short-circuit evaluation: the right operand is not evaluated if the left already decides the result.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2024 · 5 marks
What is nested loop? Write the difference between formatted and unformatted I/O operations in C.
Answer
Nested loop: a loop placed inside the body of another loop. For each iteration of the outer loop, the inner loop runs completely. If the outer loop runs times and the inner times, the inner body executes times. It is used for matrices, tables and patterns.
for (i = 1; i <= 3; i++) {
for (j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
Output:
*
* *
* * *
Difference between formatted and unformatted I/O
| Formatted I/O | Unformatted I/O |
|---|---|
Data is read or displayed according to a format specification such as %d, %f, %s. | No format specifier; data is handled as raw characters or strings. |
Functions: scanf(), printf(), fscanf(), fprintf(). | Functions: getchar(), putchar(), gets(), puts(), getch(). |
| Can handle all data types with conversion. | Handles only characters and strings. |
Output appearance (width, precision) can be controlled, e.g. %8.2f. | No control over appearance. |
scanf stops reading at whitespace. | gets reads a whole line including spaces. |
| Slower because of format conversion. | Faster and simpler. |
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2024 · 5 marks
What is DMA? List out the memory management function used in the C program. Using DMA, write a program to find the sum of N numbers.
Answer
DMA (Dynamic Memory Allocation): the process of allocating memory at run time from the heap, rather than fixing its size at compile time. It lets a program use exactly the memory it needs and release it when finished. The functions are declared in <stdlib.h>.
Memory management functions
malloc(size)allocates a block ofsizebytes; the memory is uninitialised.calloc(n, size)allocates space fornelements ofsizebytes each, initialised to zero.realloc(ptr, newsize)changes the size of a previously allocated block.free(ptr)releases allocated memory back to the heap.
Program: sum of N numbers using DMA
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, sum = 0;
int *p;
printf("Enter number of elements: ");
scanf("%d", &n);
p = (int *)malloc(n * sizeof(int));
if (p == NULL) {
printf("Memory not allocated\n");
return 1;
}
printf("Enter %d numbers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", p + i);
sum += *(p + i);
}
printf("Sum = %d\n", sum);
free(p);
return 0;
}
For input and numbers , the output is .
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2024 · 5 marks
Write a C program to read a string from the keyboard and reverse it without using string handling function.
Answer
The program finds the length of the string by scanning for the null character '\0' manually, then swaps characters from both ends. No library function such as strlen or strrev is used.
#include <stdio.h>
int main() {
char str[100], temp;
int i, len = 0;
printf("Enter a string: ");
scanf("%[^\n]", str); /* reads a line including spaces */
while (str[len] != '\0') /* find length manually */
len++;
for (i = 0; i < len / 2; i++) {
temp = str[i];
str[i] = str[len - 1 - i];
str[len - 1 - i] = temp;
}
printf("Reversed string: %s\n", str);
return 0;
}
Example: input Nepal gives length ; the swaps are (N,l) and (e,a), with p staying in the middle, so the output is lapeN.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2024 · 5 marks
What is the use of initgraph() in the C program? Write a C program to draw a circle, rectangle and ellipse using the graphics functions with a green background and red foreground.
Answer
Use of initgraph(): it initialises the graphics system. It loads the graphics driver from disk, puts the system into graphics mode and sets all graphics settings to their defaults.
void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
graphdriveris the driver to use;DETECTlets the system detect it automatically.graphmodeis the graphics mode; it is set by the call whenDETECTis used.pathtodriveris the folder holding the.BGIdriver files (e.g."C:\\TC\\BGI", or""for the current folder).
After drawing, closegraph() restores text mode.
Program
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(GREEN); /* green background */
cleardevice();
setcolor(RED); /* red foreground */
circle(150, 150, 60); /* centre (150,150), radius 60 */
rectangle(250, 90, 400, 210); /* left, top, right, bottom */
ellipse(520, 150, 0, 360, 80, 50); /* centre, start angle, end angle, xradius, yradius */
getch();
closegraph();
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2024 · 10 marks
Explain different method of passing argument to a function. Write a C program to print Fibonacci sequence up to n- terms using Recursive function.
Answer
Methods of passing arguments to a function
1. Call by value. A copy of the actual argument is passed to the formal parameter. Changes made inside the function do not affect the original variable.
void change(int x) { x = 50; }
/* main: int a = 10; change(a); a is still 10 */
2. Call by reference (using pointers). The address of the actual argument is passed, and the function accesses the original variable through a pointer. Changes made inside the function affect the original.
void change(int *x) { *x = 50; }
/* main: int a = 10; change(&a); a becomes 50 */
Typical use: swap(&a, &b), which needs to modify two variables of the caller.
| Call by value | Call by reference |
|---|---|
| Copy of value is passed. | Address is passed. |
| Original is not changed. | Original can be changed. |
| Extra memory for the copy. | No copy of the data is made. |
(Arrays are always passed by passing the address of the first element.)
Program: Fibonacci sequence up to n terms using recursion
#include <stdio.h>
int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n - 1) + fib(n - 2);
}
int main() {
int n, i;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci sequence: ");
for (i = 0; i < n; i++)
printf("%d ", fib(i));
printf("\n");
return 0;
}
For the output is 0 1 1 2 3 5 8, using with and .
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2024 · 10 marks
What is pointer? Write a C program that uses pointers to store cricket player's data(name, age, position and height) and display them.
Answer
Pointer: a variable that stores the memory address of another variable. It is declared with *, for example int *p;. The address-of operator & gives an address and the dereference operator * gives the value stored at that address.
int a = 10;
int *p = &a; /* p holds address of a */
printf("%d", *p); /* prints 10 */
Pointers allow dynamic memory allocation, call by reference, efficient array and string handling, and the building of data structures.
Program: cricket player data using pointers
A structure holds the data, and a pointer to the structure is used to read and display it, accessing members with the -> operator.
#include <stdio.h>
struct Player {
char name[30];
int age;
char position[20];
float height; /* in metres */
};
int main() {
struct Player pl[3], *p;
int i;
p = pl; /* pointer to the first element */
for (i = 0; i < 3; i++, p++) {
printf("Enter details of player %d\n", i + 1);
printf("Name: ");
scanf(" %[^\n]", p->name);
printf("Age: ");
scanf("%d", &p->age);
printf("Position: ");
scanf(" %[^\n]", p->position);
printf("Height (m): ");
scanf("%f", &p->height);
}
printf("\n%-20s %-5s %-15s %-8s\n", "Name", "Age", "Position", "Height");
for (p = pl; p < pl + 3; p++)
printf("%-20s %-5d %-15s %-8.2f\n", p->name, p->age, p->position, p->height);
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2024 · 10 marks
What do you mean by nested structure? Create a structure named Book with members ISBN, Title, author and price. Use this structure to read records of 10 books and display the record of book having highest and lowest price.
Answer
Nested structure: a structure that has another structure as one of its members. It groups related data in layers.
struct Date { int day, month, year; };
struct Student {
char name[30];
struct Date dob; /* nested structure */
};
/* access: s.dob.year */
Program: 10 books, show highest and lowest priced
#include <stdio.h>
struct Book {
int ISBN;
char title[50];
char author[30];
float price;
};
int main() {
struct Book b[10];
int i, max = 0, min = 0;
for (i = 0; i < 10; i++) {
printf("Enter details of book %d\n", i + 1);
printf("ISBN: ");
scanf("%d", &b[i].ISBN);
printf("Title: ");
scanf(" %[^\n]", b[i].title);
printf("Author: ");
scanf(" %[^\n]", b[i].author);
printf("Price: ");
scanf("%f", &b[i].price);
}
for (i = 1; i < 10; i++) {
if (b[i].price > b[max].price) max = i;
if (b[i].price < b[min].price) min = i;
}
printf("\nBook with highest price:\n");
printf("ISBN: %d\nTitle: %s\nAuthor: %s\nPrice: Rs %.2f\n",
b[max].ISBN, b[max].title, b[max].author, b[max].price);
printf("\nBook with lowest price:\n");
printf("ISBN: %d\nTitle: %s\nAuthor: %s\nPrice: Rs %.2f\n",
b[min].ISBN, b[min].title, b[min].author, b[min].price);
return 0;
}
The indices max and min start at the first book and are updated while scanning the remaining nine, so a single pass finds both records.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
Questions from TU BCA 2018 Second Semester C Programming (TU board exam, 2018 (scanned paper compiled by Aadim College)), TU BCA 2019 Second Semester C Programming (TU board exam, 2019), TU BCA 2020 Second Semester C Programming (TU board exam, 2020), TU BCA 2021 Second Semester C Programming (TU board exam, 2021), TU BCA 2022 Second Semester C Programming (TU board exam, 2022), TU BCA 2023 Second Semester C Programming (TU board exam, 2023), TU BCA 2024 Second Semester C Programming (TU board exam, 2024), TU BCA 2018 Second Semester English II (TU board exam, 2018 (scanned paper compiled by Aadim College)), TU BCA 2020 Second Semester English II (TU board exam, 2020), TU BCA 2021 Second Semester English II (TU board exam, 2021), TU BCA 2022 Second Semester English II (TU board exam, 2022), TU BCA 2023 Second Semester English II (TU board exam, 2023), TU BCA 2024 Second Semester English II (TU board exam, 2024), TU BCA 2018 Second Semester Mathematics II (TU board exam, 2018 (scanned paper compiled by Aadim College)), TU BCA 2022 Second Semester Mathematics II (TU board exam, 2022), TU BCA 2023 Second Semester Mathematics II (TU board exam, 2023), TU BCA 2024 Second Semester Mathematics II (TU board exam, 2024), TU BCA 2018 Second Semester Microprocessor and Computer Architecture (TU board exam, 2018 (scanned paper compiled by Aadim College)), TU BCA 2019 Second Semester Microprocessor and Computer Architecture (TU board exam, 2019), TU BCA 2020 Second Semester Microprocessor and Computer Architecture (TU board exam, 2020), TU BCA 2021 Second Semester Microprocessor and Computer Architecture (TU board exam, 2021), TU BCA 2022 Second Semester Microprocessor and Computer Architecture (TU board exam, 2022), TU BCA 2023 Second Semester Microprocessor and Computer Architecture (TU board exam, 2023), TU BCA 2024 Second Semester Microprocessor and Computer Architecture (TU board exam, 2024) and TU BCA 2018 Second Semester Financial Accounting (TU board exam, 2018 (scanned paper compiled by Aadim College)). Answers are written for this site; check them against your class notes.