qustion_answer_in_c(2024)
qustion_answer_in_c(2024)
MCQ, short answer questions (SAQ), and long answer questions. Below is the categorized list of questions
that have a high probability of being asked in your December 2024 exam, along with their answers.
c
Copy code
#include <stdio.h>
void checkEvenOdd(int num) {
if (num % 2 == 0)
printf("%d is Even\n", num);
else
printf("%d is Odd\n", num);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
checkEvenOdd(n);
return 0;
}
3. Long Answer Questions (LAQ)
c
Copy code
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("GCD is %d\n", gcd(num1, num2));
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
FILE *source, *destination;
char ch;
source = fopen("source.txt", "r");
destination = fopen("destination.txt", "w");
if (source == NULL || destination == NULL) {
printf("Error opening file!");
return 1;
}
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
}
fclose(source);
fclose(destination);
printf("File copied successfully.");
return 0;
}
Example:
c
Copy code
#include <stdio.h>
void func() {
static int count = 0; // Retains value across calls
count++;
printf("Count: %d\n", count);
}
int main() {
func();
func();
return 0;
}
c
Copy code
#include <stdio.h>
int isPrime(int num) {
if (num <= 1) return 0;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return 0;
}
return 1;
}
int main() {
int lower, upper;
printf("Enter lower and upper limits: ");
scanf("%d %d", &lower, &upper);
for (int i = lower; i <= upper; i++) {
if (isPrime(i)) printf("%d ", i);
}
return 0;
}
Here’s a deeper analysis of MCQs, SAQs, and LAQs based on trends in the question papers (2017–2024). This
includes additional probable questions with solutions, and the breakdown is expanded for thorough preparation.
Basics of C language.
Operators and precedence.
Pointers and arrays.
Loops and control structures.
Functions and storage classes.
Example:
c
Copy code
for (int i = 0; i < 5; i++) {
if (i == 2)
continue; // Skips the rest of the loop when i = 2
printf("%d ", i);
}
c
Copy code
int isPrime(int n) {
if (n <= 1) return 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
1. Write a program to find the sum of the series: 1 + (1+2) + (1+2+3) + ... up to n terms.
Answer:
c
Copy code
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
sum += j;
}
}
printf("Sum: %d\n", sum);
return 0;
}
c
Copy code
#include <stdio.h>
void reverseString(char str[]) {
int len = 0;
while (str[len] != '\0')
len++;
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - 1 - i];
str[len - 1 - i] = temp;
}
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
reverseString(str);
printf("Reversed String: %s\n", str);
return 0;
}
3. Write a program to count the number of lines, words, and characters in a file.
Answer:
c
Copy code
#include <stdio.h>
#include <ctype.h>
int main() {
FILE *file = fopen("input.txt", "r");
if (!file) {
printf("Error opening file.");
return 1;
}
int lines = 0, words = 0, chars = 0;
char c, prev = '\0';
while ((c = fgetc(file)) != EOF) {
chars++;
if (c == '\n')
lines++;
if (isspace(c) && !isspace(prev))
words++;
prev = c;
}
fclose(file);
printf("Lines: %d, Words: %d, Characters: %d\n", lines, words, chars);
return 0;
}
c
Copy code
int customStrlen(const char *str) {
int len = 0;
while (str[len] != '\0')
len++;
return len;
}
c
Copy code
#include <stdio.h>
int main() {
FILE *numbers = fopen("numbers.txt", "r");
FILE *even = fopen("even.txt", "w");
FILE *odd = fopen("odd.txt", "w");
int num;
while (fscanf(numbers, "%d", &num) != EOF) {
if (num % 2 == 0)
fprintf(even, "%d\n", num);
else
fprintf(odd, "%d\n", num);
}
fclose(numbers);
fclose(even);
fclose(odd);
return 0;
}
c
Copy code
int a = 5, b = 3;
printf("%d", a+++b);
c
Copy code
int x = 2, y = 6;
printf("%d", y >> x);
o (a) 1
o (b) 2
o (c) 6
o (d) 1.5
Answer: (b) 2
c
Copy code
#define SQUARE(x) (x*x) // Macro
c
Copy code
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
c
Copy code
char dest[20] = "Hello ";
char src[] = "World!";
strcat(dest, src); // Result: dest = "Hello World!"
c
Copy code
#include <stdio.h>
int isPalindrome(char str[]) {
int len = 0;
while (str[len] != '\0') len++;
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i]) return 0;
}
return 1;
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
if (isPalindrome(str)) printf("Palindrome\n");
else printf("Not a palindrome\n");
return 0;
}
2. Write a program to separate even and odd numbers into two files.
Answer: Refer to the earlier provided code for separating even/odd numbers.
3. Write a program to dynamically allocate memory for an array and compute its average.
Answer:
c
Copy code
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, *arr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
arr = (int *)malloc(n * sizeof(int)); // Dynamic memory allocation
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Average: %.2f\n", (float)sum / n);
free(arr); // Free allocated memory
return 0;
}
c
Copy code
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
Based on the detailed analysis of question papers (2017–2024), here is the final compilation of high-
probability questions categorized into MCQs, SAQs, and LAQs, along with actionable tips to maximize your
preparation.
1. High-Probability MCQs
Likely MCQs:
2. High-Probability SAQs
Likely SAQs:
c
Copy code
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
4. What is the use of #define in C? Provide an example.
Answer:
o Used to define symbolic constants or macros.
Example:
c
Copy code
#define PI 3.14159
3. High-Probability LAQs
Likely LAQs:
c
Copy code
#include <stdio.h>
int main() {
FILE *source = fopen("source.txt", "r");
FILE *dest = fopen("dest.txt", "w");
char ch;
if (source == NULL || dest == NULL) {
printf("Error opening file.");
return 1;
}
while ((ch = fgetc(source)) != EOF) {
fputc(ch, dest);
}
fclose(source);
fclose(dest);
printf("File copied successfully.");
return 0;
}
c
Copy code
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
3. Write a program to find the sum of the series 1 + (1+2) + (1+2+3) + ... up to n terms.
Answer:
c
Copy code
int main() {
int n, sum = 0;
printf("Enter the number of terms: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
sum += j;
}
}
printf("Sum of the series: %d\n", sum);
return 0;
}
c
Copy code
void reverseString(char str[]) {
int len = 0;
while (str[len] != '\0') len++;
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
Example:
c
Copy code
void func() {
static int count = 0; // Retains value across calls
count++;
printf("%d\n", count);
}
If you want detailed solutions for more questions or additional practice material, let me know!