Chapter 2
C Programming
TU BIM past exam questions
Past questions and answers
22 questions set from this chapter. Most repeated first.
- 2022 AD Model · 1 mark
Define algorithm.
Answer
An algorithm is a finite, step-by-step set of well-defined instructions for solving a problem or accomplishing a task. It takes input, produces output, and should be clear, unambiguous and terminate after a finite number of steps.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 1 mark
What is preprocessor directive?
Answer
A preprocessor directive is an instruction in C that begins with # and is processed by the preprocessor before actual compilation begins. Examples are #include <stdio.h> to include a header file and #define PI 3.14 to define a macro.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 1 mark
Differentiate between scanf() and gets()?
Answer
scanf() | gets() |
|---|---|
| Reads formatted input of any data type using format specifiers. | Reads only a string (line of text). |
Stops reading at whitespace (space, tab, newline), so it cannot read a string with spaces using %s. | Reads the whole line including spaces until Enter is pressed. |
Example: scanf("%s", name); | Example: gets(name); |
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 1 mark
What is conditional operator?
Answer
The conditional operator ?: is the only ternary operator in C (it takes three operands). It is a short form of if-else.
Syntax: expression1 ? expression2 : expression3;
If expression1 is true (non-zero), expression2 is evaluated; otherwise expression3 is evaluated.
max = (a > b) ? a : b;
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 1 mark
Differentiate between break and exit().
Answer
break | exit() |
|---|---|
A keyword (statement) that terminates only the nearest enclosing loop or switch. | A library function (stdlib.h) that terminates the whole program. |
| Control moves to the statement after the loop/switch. | Control returns to the operating system. |
Used as break; | Used as exit(0); |
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 1 mark
How to initialize two-dimensional matrix?
Answer
A two-dimensional array is initialized by giving the values row by row inside braces, with the data type, name and both sizes declared.
int a[2][3] = { {1, 2, 3}, {4, 5, 6} };
It may also be written as int a[2][3] = {1, 2, 3, 4, 5, 6};. The first dimension may be omitted when initializing, e.g. int a[][3] = {{1,2,3},{4,5,6}};. Elements can also be assigned or read with nested loops.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 1 mark
What is function prototype?
Answer
A function prototype is a declaration of a function that tells the compiler its name, return type and the number and types of its parameters before the function is used or defined. It ends with a semicolon.
int add(int, int);
It allows the compiler to check that calls match the function's definition.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 1 mark
What is the use of double pointer?
Answer
A double pointer (pointer to pointer), declared as int **pp;, stores the address of another pointer variable. Its uses are:
- To modify a pointer itself from inside a function (pass the pointer's address).
- To create dynamic 2D arrays and arrays of strings (e.g.
char **names). - To handle command-line arguments (
char **argv).
int x = 5, *p = &x, **pp = &p;
printf("%d", **pp); /* 5 */
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 1 mark
Differentiate between array and structure.
Answer
| Array | Structure |
|---|---|
| Collection of elements of the same data type. | Collection of variables of different data types under one name. |
Elements are accessed by index, e.g. a[2]. | Members are accessed by the dot (or arrow) operator, e.g. s.name. |
Declared with int a[10]; | Declared with the struct keyword. |
| Not a user-defined type. | User-defined data type. |
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 1 mark
List out the different modes of file in file handling.
Answer
The modes used with fopen() are:
r- open an existing file for reading.w- open for writing; creates the file or erases an existing one.a- open for appending at the end of the file.r+- open an existing file for reading and writing.w+- open for reading and writing; erases existing content.a+- open for reading and appending.- Adding
b(e.g.rb,wb) opens the file in binary mode.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 3 marks
Write a program to check if the input given by the user is divisible by 5 or 7.
Answer
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n % 5 == 0 || n % 7 == 0)
printf("%d is divisible by 5 or 7.\n", n);
else
printf("%d is not divisible by 5 or 7.\n", n);
return 0;
}
A number is divisible by 5 or 7 if the remainder (%) on division by either is zero.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 3 marks
Write a program that uses a while loop to compute and prints the sum of given numbers of cubes. For example, if 3 is input, then the program should print 36, which is equal to .
Answer
#include <stdio.h>
int main() {
int n, i = 1, sum = 0;
printf("Enter n: ");
scanf("%d", &n);
while (i <= n) {
sum = sum + i * i * i;
i++;
}
printf("Sum of cubes = %d\n", sum);
return 0;
}
For : , which is printed.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 3 marks
Write a program to read a line of text and display the number of vowels in it.
Answer
#include <stdio.h>
#include <ctype.h>
int main() {
char line[200];
int i, count = 0;
printf("Enter a line of text: ");
fgets(line, sizeof(line), stdin);
for (i = 0; line[i] != '\0'; i++) {
switch (tolower(line[i])) {
case 'a': case 'e': case 'i': case 'o': case 'u':
count++;
}
}
printf("Number of vowels = %d\n", count);
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 3 marks
Write a program that takes string as input and displays the length of it.
Answer
#include <stdio.h>
int main() {
char str[100];
int len = 0;
printf("Enter a string: ");
scanf("%99[^\n]", str);
while (str[len] != '\0')
len++;
printf("Length of the string = %d\n", len);
return 0;
}
The loop counts characters until the null character '\0'. (The library function strlen(str) gives the same result.)
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 3 marks
Write a program to compute the factorial of number entered by the user using recursion.
Answer
#include <stdio.h>
long fact(int n) {
if (n == 0 || n == 1)
return 1;
return n * fact(n - 1);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n < 0)
printf("Factorial is not defined for negative numbers.\n");
else
printf("Factorial of %d = %ld\n", n, fact(n));
return 0;
}
The function calls itself with until the base case (or ) is reached, since .
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 3 marks
Write a program to draw a triangle using graphics function.
Answer
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
line(200, 100, 100, 250); /* left side */
line(200, 100, 300, 250); /* right side */
line(100, 250, 300, 250); /* base */
getch();
closegraph();
return 0;
}
initgraph() starts graphics mode, line(x1, y1, x2, y2) draws a line between two points, and closegraph() returns to text mode.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 5 marks
Explain the different logical operators in detail.
Answer
Logical operators combine or negate conditions (relational expressions) and give a result of 1 (true) or 0 (false). C has three logical operators.
(a) Logical AND (&&) - true only if both operands are true.
(b) Logical OR (||) - true if at least one operand is true.
(c) Logical NOT (!) - unary operator that reverses the truth value of its operand.
| A | B | A && B | A || B | !A |
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 0 | 0 | 0 | 0 | 1 |
int a = 10, b = 20, c = 5;
if (a > c && b > a) printf("both true\n"); /* printed */
if (a > b || c > 1) printf("one true\n"); /* printed */
if (!(a > b)) printf("not (a>b)\n"); /* printed */
&& and || use short-circuit evaluation: in A && B, B is not evaluated if A is false; in A || B, B is not evaluated if A is true. Any non-zero value is treated as true.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 5 marks
How array can be passed as function arguments? Illustrates with suitable example.
Answer
An array is passed to a function by passing its name, which is the address of its first element (call by reference). The formal parameter can be written as an array (int a[], int a[5]) or a pointer (int *a). The size is usually passed as another argument. Because the address is passed, changes made in the function affect the original array.
#include <stdio.h>
int sum(int a[], int n) {
int i, s = 0;
for (i = 0; i < n; i++)
s += a[i];
return s;
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("Sum = %d\n", sum(arr, 5)); /* Sum = 15 */
return 0;
}
For a 2D array, all dimensions except the first must be given in the parameter, e.g. void show(int m[][3], int r).
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 5 marks
What do you mean by dynamic memory allocation? Explain the use of malloc() and free() functions with suitable example.
Answer
Dynamic memory allocation is allocating memory at run time (from the heap) as needed, instead of fixing the size at compile time. It is declared in <stdlib.h>.
malloc() - allocates a block of the given number of bytes and returns a void pointer to it (or NULL on failure). The memory is not initialized.
ptr = (int *) malloc(n * sizeof(int));
free() - releases memory previously allocated, returning it to the system and avoiding memory leaks.
free(ptr);
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, *p;
printf("How many numbers? ");
scanf("%d", &n);
p = (int *) malloc(n * sizeof(int));
if (p == NULL) {
printf("Memory not available\n");
return 1;
}
for (i = 0; i < n; i++)
scanf("%d", p + i);
for (i = 0; i < n; i++)
printf("%d ", *(p + i));
free(p);
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 5 marks
Write a program to create and write N numbers in a file "Number.txt". Open this file to then read its contents and write all positive numbers in the file "Positive.txt" and all negative numbers in the file "Negative.txt."
Answer
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fn, *fp, *fneg;
int n, i, x;
fn = fopen("Number.txt", "w");
if (fn == NULL) { printf("Cannot open file\n"); exit(1); }
printf("How many numbers? ");
scanf("%d", &n);
printf("Enter %d numbers: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &x);
fprintf(fn, "%d\n", x);
}
fclose(fn);
fn = fopen("Number.txt", "r");
fp = fopen("Positive.txt", "w");
fneg = fopen("Negative.txt", "w");
if (fn == NULL || fp == NULL || fneg == NULL) {
printf("Cannot open file\n");
exit(1);
}
while (fscanf(fn, "%d", &x) == 1) {
if (x > 0)
fprintf(fp, "%d\n", x);
else if (x < 0)
fprintf(fneg, "%d\n", x);
}
fclose(fn);
fclose(fp);
fclose(fneg);
return 0;
}
Zero is neither positive nor negative, so it is written to neither file.
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 10 marks
Why looping is used in programming? Explain different looping statement with appropriate example.
Answer
Why looping is used: A loop repeats a block of statements as long as a condition holds. It avoids writing the same code many times, makes programs shorter and easier to maintain, and is needed for processing arrays, files, tables and menus.
C provides three looping statements.
(a) while loop (entry-controlled) - the condition is tested first; the body runs only while it is true.
i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
(b) do-while loop (exit-controlled) - the body runs first, then the condition is tested, so it executes at least once.
i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
(c) for loop - initialization, condition and update are written together in one line.
for (i = 1; i <= 5; i++)
printf("%d ", i);
Each of these prints 1 2 3 4 5.
Syntax summary
| Loop | Syntax | Condition checked |
|---|---|---|
| while | while (cond) { ... } | before the body |
| do-while | do { ... } while (cond); | after the body |
| for | for (init; cond; update) { ... } | before the body |
Loops can be nested (a loop inside another), e.g. printing a pattern:
for (i = 1; i <= 3; i++) {
for (j = 1; j <= i; j++)
printf("*");
printf("\n");
}
Jump statements break (leave the loop) and continue (skip to the next iteration) control loop execution. An infinite loop such as for(;;) runs until a break or exit().
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
- 2022 AD Model · 10 marks
What is structure? How is it different from union? Create a structure "Student" having data members name, roll_nmber and percentage. Write a program to display the name of all students having percentage greater than or equal to 80.
Answer
Structure: A structure is a user-defined data type that groups variables of different data types under one name, declared with the struct keyword. Each variable is a member, and each member has its own memory.
struct Student {
char name[30];
int roll_number;
float percentage;
};
Difference between structure and union
| Structure | Union |
|---|---|
Declared with struct. | Declared with union. |
| Each member has separate memory. | All members share the same memory location. |
| Size is the sum of the sizes of all members (plus padding). | Size is the size of the largest member. |
| All members can be used at the same time. | Only one member holds a valid value at a time. |
| Changing one member does not affect others. | Changing one member overwrites the others. |
Program
#include <stdio.h>
struct Student {
char name[30];
int roll_number;
float percentage;
};
int main() {
struct Student s[100];
int n, i;
printf("Enter number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Student %d name: ", i + 1);
scanf("%29s", s[i].name);
printf("Roll number: ");
scanf("%d", &s[i].roll_number);
printf("Percentage: ");
scanf("%f", &s[i].percentage);
}
printf("\nStudents with percentage >= 80:\n");
for (i = 0; i < n; i++) {
if (s[i].percentage >= 80)
printf("%s\n", s[i].name);
}
return 0;
}
Answer checked by Claude Sonnet · Oct 21, 2026
Test yourself · 2 MCQs
Loading…
Questions from TU BIM First Semester Model Questions 2022 (IT 231, C Programming, Basic Mathematics, English I, MGT 231) (Faculty of Management model question set, 2022, via EducateNepal), TU BIM 2016 First Semester IT 211 Computer Information System (model question) (Faculty of Management model question, 2016 (old course), via EducateNepal), TU BIM Computer Information System (ITC 211) 2009 AD (Board exam paper, 2009 AD), TU BIM Computer Information System (ITC 211) 2010 AD (Board exam paper, 2010 AD), TU BIM Computer Information System (ITC 211) 2011 AD (Board exam paper, 2011 AD), TU BIM Computer Information System (ITC 211) 2013 AD (Board exam paper, 2013 AD), TU BIM Computer Information System (ITC 211) 2014 AD (Board exam paper, 2014 (one short-answer question is cut off between the pages of the scan and is omitted)), TU BIM Computer Information System (ITC 211) 2015 AD (Board exam paper, 2015 AD), TU BIM Computer Information System (ITC 211) 2016 AD (Board exam paper, April 2016), TU BIM Computer Information System (ITC 211) 2017 AD (Board exam paper, March-April 2017), TU BIM Computer Information System (ITC 211) 2018 AD (Board exam paper, April 2018), TU BIM Computer Information System (ITC 211) 2019 AD (Board exam paper, April 2019), TU BIM Computer Information System (ITC 211) 2021 Jan (FM 60) (Board exam paper, January 2021, full marks 60, answer any six; this paper is listed as the 2020 batch paper on the source site), TU BIM Computer Information System (ITC 211) 2021 Jan (FM 40) (Board exam paper, January 2021, full marks 40), TU BIM Basic Mathematics (MTH 201) 2009 AD (Board exam paper, 2009 AD), TU BIM Basic Mathematics (MTH 201) 2010 AD (Board exam paper, 2010 AD), TU BIM Basic Mathematics (MTH 201) 2011 AD (Board exam paper, 2011 AD), TU BIM Basic Mathematics (MTH 201) 2013 AD (Board exam paper listed as 2013 on the source site (the scanned header reads 2009)), TU BIM Basic Mathematics (MTH 201) 2014 AD (Board exam paper, 2014 AD), TU BIM Basic Mathematics (MTH 201) 2015 AD (Board exam paper, 2015 AD), TU BIM Basic Mathematics (MTH 201) 2017 AD (Board exam paper, 2017 AD), TU BIM Basic Mathematics (MTH 201) 2018 AD (Board exam paper, April 2018), TU BIM Basic Mathematics (MTH 201) 2019 AD (Board exam paper, April 2019), TU BIM Digital Logic (ITC 212) 2009 AD (Board exam paper, 2009 AD), TU BIM Digital Logic (ITC 212) 2010 AD (Board exam paper, 2010 AD), TU BIM Digital Logic (ITC 212) 2011 AD (Board exam paper, 2011 AD), TU BIM Digital Logic (ITC 212) 2012 AD (Board exam paper, 2012 AD), TU BIM Digital Logic (ITC 212) 2013 AD (Board exam paper, 2013 AD), TU BIM Digital Logic (ITC 212) 2014 AD (Board exam paper, 2014 AD), TU BIM Digital Logic (ITC 212) 2015 AD (Board exam paper, 2015 AD), TU BIM Digital Logic (ITC 212) 2016 AD (Board exam paper, 2016 AD), TU BIM Digital Logic (ITC 212) 2017 AD (Board exam paper, 2017 AD), TU BIM Digital Logic (ITC 212) 2018 AD (Board exam paper, April 2018), TU BIM Digital Logic (ITC 212) 2019 AD (Board exam paper listed as 2019 on the source site (scanned header reads April 2018)), TU BIM Digital Logic (ITC 212) 2021 Jan (Board exam paper, January 2021, full marks 60, answer any six; listed as 2020 on the source site), TU BIM Digital Logic (ITC 212) 2021 Oct (Board exam paper, October 2021), TU BIM Principles of Management (MGT 201) 2009 AD (Board exam paper, 2009 AD), TU BIM Principles of Management (MGT 201) 2010 AD (Board exam paper, 2010 AD), TU BIM Principles of Management (MGT 201) 2011 AD (Board exam paper, 2011 AD), TU BIM Principles of Management (MGT 201) 2012 AD (Board exam paper, 2012 AD), TU BIM Principles of Management (MGT 201) 2014 AD (Board exam paper, 2014 AD), TU BIM Principles of Management (MGT 201) 2015 AD (Board exam paper, 2015 AD), TU BIM Principles of Management (MGT 201) 2016 AD (Board exam paper, 2016 AD), TU BIM Principles of Management (MGT 201) 2017 AD (Board exam paper, 2017 AD), TU BIM Principles of Management (MGT 201) 2018 AD (Board exam paper, April 2018), TU BIM Principles of Management (MGT 201) 2019 AD (Board exam paper, April 2019), TU BIM Principles of Management (MGT 201) 2021 Jan (Board exam paper, January 2021, full marks 60, answer any six; listed as 2020 on the source site), TU BIM Principles of Management (MGT 201) 2021 Oct (Board exam paper, October 2021), TU BIM English Composition January 2013 (Board exam paper, January 2013 (scanned images on neupanenirmal.com.np)) and TU BIM English Composition 2014 (Board exam paper, 2014 (scanned images on neupanenirmal.com.np)). Answers are written for this site; check them against your class notes.