Here are the fully answered exam questions for Introduction to Programming and
Algorithms.
EXAM QUESTIONS & ANSWERS –
INTRODUCTION TO PROGRAMMING AND
ALGORITHMS
SECTION A: MULTIPLE CHOICE QUESTIONS (MCQs) – 1
Mark Each
1. Which of the following is a valid variable name in C?
2. a) 2number
3. b) float
4. c) my_variable
5. d) #define
6. What is the size of an int variable in C (on a 32-bit system)?
a) 4 bytes
b) 2 bytes
c) 8 bytes
d) 1 byte
7. Which operator is used to allocate memory dynamically in C?
a) malloc
b) new
c) Both A and B
d) calloc
8. Which of the following is NOT a data type in C?
a) char
b) boolean
c) float
d) double
9. Which of the following loops in C is exit-controlled?
a) for
b) while
c) do-while
d) switch
10. Which of the following statements is true about arrays in C?
a) The size of an array must be defined at runtime.
b) Arrays can store elements of different data types.
c) Array indexing starts from 0.
d) Arrays are always passed by value to functions.
SECTION B: SHORT ANSWER QUESTIONS – 3 Marks Each
7. What is a pointer in C?
a. A pointer is a variable that stores the memory address of another variable.
b. Example: int x = 10;
int *ptr = &x; // ptr now holds the address of x
printf("%d", *ptr); // Outputs 10
8. Explain the difference between global and local variables in C.
a. Global Variables: Declared outside any function and accessible throughout
the program.
b. Local Variables: Declared inside a function and only accessible within that
function.
9. What is recursion? Provide an example.
a. Recursion is a function calling itself repeatedly until a base condition is met.
b. Example: int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
10. Differentiate between call by value and call by reference in C.
Method Description Example
void func(int a)
Call by Value Sends a copy of the argument
{ a++; }
Call by Sends the actual address of the void func(int *a)
Reference argument { (*a)++; }
SECTION C: PROGRAMMING QUESTIONS – 5 Marks Each
11. Write a C program to find the largest of three numbers using if-else.
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c)
printf("Largest number: %d", a);
else if (b > c)
printf("Largest number: %d", b);
else
printf("Largest number: %d", c);
return 0;
}
12. Write a C program that reverses a number using a while loop.
#include <stdio.h>
int main() {
int num, reversed = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
printf("Reversed Number: %d", reversed);
return 0;
}
13. Write a program that finds the sum of all elements in an array.
#include <stdio.h>
int main() {
int arr[5], sum = 0;
printf("Enter 5 numbers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum: %d", sum);
return 0;
}
SECTION D: LONG ANSWER QUESTIONS – 10 Marks Each
14. Write a C program that reads student marks and prints their grade using a
switch-case statement.
#include <stdio.h>
int main() {
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
switch (marks / 10) {
case 10:
case 9: printf("Grade: A"); break;
case 8: printf("Grade: B"); break;
case 7: printf("Grade: C"); break;
case 6: printf("Grade: D"); break;
default: printf("Grade: F");
}
return 0;
}
15. Write a C program that reads a string from the user and counts the number of
vowels in it.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, count = 0;
printf("Enter a string: ");
gets(str);
for (i = 0; i < strlen(str); i++) {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i]
== 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i]
== 'O' || str[i] == 'U') {
count++;
}
}
printf("Number of vowels: %d", count);
return 0;
}