0% found this document useful (0 votes)
23 views16 pages

qustion_answer_in_c(2024)

Uploaded by

ranajitbarik2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views16 pages

qustion_answer_in_c(2024)

Uploaded by

ranajitbarik2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Based on the uploaded PDF containing previous years' question papers (2017–2024), I analyzed the patterns of

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.

1. MCQ Trends and High-Chance Questions 1


Common Topics in MCQs:

 Control structures: Looping, switch-case.


 Operators: Associativity, precedence, and special operators.
 Pointers: Memory addresses, arithmetic.
 Functions: Prototypes, recursion, storage classes.
 Strings: Manipulation and functions.
 File Handling: Modes and operations.

Sample High-Probability MCQs:

1. Which loop is guaranteed to execute at least once?


o (a) for
o (b) while
o (c) do-while
o (d) None of these
Answer: (c) do-while
2. What is the size of int on a 32-bit system?
o (a) 1 byte
o (b) 2 bytes
o (c) 4 bytes
o (d) 8 bytes
Answer: (c) 4 bytes
3. The function malloc() is declared in which header file?
o (a) stdio.h
o (b) stdlib.h
o (c) conio.h
o (d) iostream.h
Answer: (b) stdlib.h
4. What is the output of printf("%d", 3 + 2 * 4 / 2 - 1);?
o (a) 5
o (b) 6
o (c) 7
o (d) 8
Answer: (c) 7 ANS 6
5. Which of these is not a valid variable name in C?
o (a) _var
o (b) 2var
o (c) var2
o (d) _123var
Answer: (b) 2var
6. Which operator is used to access members of a structure through a pointer?
o (a) .
o (b) &
o (c) ->
o (d) *
Answer: (c) ->

2. SAQ Trends and High-Chance Questions

Topics Repeated for SAQs:

 Control structures and loops.


 Functions (recursion, prototypes, passing by value/reference).
 Pointers.
 Arrays and Strings.

Sample High-Probability SAQs:

1. Differentiate between while and do-while loops. Answer:


o while is an entry-controlled loop; the condition is checked before execution.
o do-while is an exit-controlled loop; the body executes at least once regardless of the condition.
2. Explain the difference between structure and union.
Answer:
o Structure: Allocates separate memory for each member.
o Union: Shares memory among all members; memory allocated equals the size of the largest
member.
3. What are the advantages of pointers?
Answer:
o Efficient memory usage.
o Enables dynamic memory allocation.
o Supports call-by-reference to modify variables directly.
4. Explain malloc() and calloc().
Answer:
o malloc(): Allocates uninitialized memory.
o calloc(): Allocates memory and initializes it to zero.
5. Write a program to check if a number is even or odd using a function.

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)

Recurring Topics in LAQs:

 Recursive functions: Fibonacci, GCD, sum of digits.


 File Handling: Copying files, separating even/odd numbers.
 Structure-based programs: Storing and displaying student data.
 String manipulation: Palindrome check, reversing a string.

Sample High-Probability LAQs:

1. Write a program to find the GCD of two numbers using recursion.


Answer:

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;
}

2. Write a program to copy the contents of one file to another.


Answer:

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;
}

3. Explain the storage classes in C with examples.


Answer:
o auto: Default for local variables.
o register: Hints the compiler to store the variable in a CPU register for faster access.
o static: Retains its value between function calls.
o extern: Declares a global variable defined elsewhere.

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;
}

4. Write a program to find all prime numbers within a given range.


Answer:

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;
}

Extended Analysis of High-Chance Questions for December 2024

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.

1. MCQs (Multiple Choice Questions) 2


High-Frequency Topics:

 Basics of C language.
 Operators and precedence.
 Pointers and arrays.
 Loops and control structures.
 Functions and storage classes.

Additional High-Chance MCQs:

1. Which keyword is used to prevent a variable from being modified?


o (a) static
o (b) const
o (c) auto
o (d) extern
Answer: (b) const
2. The default value of a global variable is:
o (a) Garbage
o (b) Undefined
o (c) 0
o (d) Compiler-dependent
Answer: (c) 0
3. How many bytes does a float occupy in a 32-bit compiler?
o (a) 2 bytes
o (b) 4 bytes
o (c) 8 bytes
o (d) None of the above
Answer: (b) 4 bytes
4. What does & operator do in C?
o (a) Multiplies two numbers
o (b) Finds the address of a variable
o (c) Logical AND operation
o (d) None of the above
Answer: (b) Finds the address of a variable
5. Which of the following is used to dynamically allocate memory in C?
o (a) malloc()
o (b) calloc()
o (c) Both a and b
o (d) free()
Answer: (c) Both a and b

2. SAQs (Short Answer Questions)

Trending Topics for SAQs:

 Differentiating between constructs and concepts.


 Explaining core functionalities (e.g., recursion, file handling).
 Writing small snippets and logical explanations.

Additional High-Chance SAQs:

1. Explain the differences between break and continue.


Answer:
o break: Terminates the nearest enclosing loop or switch.
o continue: Skips the remaining statements in the current iteration and moves to the next
iteration.

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);
}

2. What are the advantages of using recursion?


Answer:
o Simplifies code for problems like factorials, Fibonacci, and GCD.
o Avoids the need for complex looping constructs.
o Useful in divide-and-conquer algorithms like quicksort and mergesort.
3. Explain the difference between fopen and freopen.
Answer:
o fopen: Opens a file for reading, writing, or appending.
o freopen: Reopens an existing file stream with a new file or mode.
4. What is the use of #define in C?
Answer:
o Used for defining macros or symbolic constants.
o Example: #define PI 3.14.
5. Write a C code snippet to check if a number is prime.

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;
}

3. LAQs (Long Answer Questions)

Trending Topics for LAQs:

 Programs involving arrays, pointers, strings, and files.


 Complex functionality using recursion or structures.
 Real-world simulations, like student data or file handling.

Additional High-Chance LAQs:

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;
}

2. Write a C program to reverse a string without using string.h.


Answer:

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;
}

4. Write a program to implement your own version of strlen().


Answer:

c
Copy code
int customStrlen(const char *str) {
int len = 0;
while (str[len] != '\0')
len++;
return len;
}

5. Write a program to handle even and odd numbers in files.


Answer:

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;
}

Strategy for December 2024

1. Expanded MCQs (Multiple Choice Questions) 3


Additional Likely Topics:

 Advanced usage of control statements.


 Nuances in pointer operations.
 Memory allocation and string operations.
 Common errors in logical expressions and code snippets.

Additional High-Chance MCQs:


1. What is the ASCII value of the null character ('\0')?
o (a) 1
o (b) 0
o (c) 255
o (d) Undefined
Answer: (b) 0
2. Which of the following is not a storage class in C?
o (a) auto
o (b) register
o (c) dynamic
o (d) extern
Answer: (c) dynamic
3. What is the output of the following code?

c
Copy code
int a = 5, b = 3;
printf("%d", a+++b);

o (a) Compilation error


o (b) 8
o (c) 5
o (d) Undefined behavior
Answer: (b) 8
4. What is the size of a double data type on a 64-bit system?
o (a) 4 bytes
o (b) 8 bytes
o (c) 16 bytes
o (d) 32 bytes
Answer: (b) 8 bytes
5. Which operator is used to access the value stored at the address pointed to by a pointer?
o (a) *
o (b) &
o (c) ->
o (d) .
Answer: (a) *
6. What is the output of the following code?

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

2. Expanded SAQs (Short Answer Questions)


Trending Areas for SAQs:

 Common errors in programs.


 Differences between similar concepts.
 Code snippets requiring analysis and explanation.

Additional High-Chance SAQs:

1. Explain the difference between a pointer and a reference.


Answer:
o Pointer: A variable that stores the address of another variable.
o Reference: An alias for an existing variable (not available in C but in C++).
2. What are macros in C, and how are they different from functions?
Answer:
o Macros: Preprocessor directives (#define) used to replace code at compile time.
o Functions: Code blocks executed at runtime.
o Example:

c
Copy code
#define SQUARE(x) (x*x) // Macro

3. Write a program to calculate the factorial of a number using recursion.


Answer:

c
Copy code
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

4. What is a segmentation fault in C? How can it be avoided?


Answer:
A segmentation fault occurs when a program tries to access memory that it is not allowed to.
o Avoidance: Ensure proper pointer initialization and bounds checking.
5. Explain the difference between strcpy() and strcat() with examples.
Answer:
o strcpy(dest, src): Copies the string src to dest.
o strcat(dest, src): Appends the string src to dest.
Example:

c
Copy code
char dest[20] = "Hello ";
char src[] = "World!";
strcat(dest, src); // Result: dest = "Hello World!"

6. What are the advantages of using a pointer to a function?


Answer:
o Dynamic invocation of functions.
o Efficient code reuse, e.g., passing functions as arguments.
3. Expanded LAQs (Long Answer Questions)

High-Priority Programming Questions:

1. Write a program to check if a string is a palindrome without using string.h.


Answer:

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;
}

4. Explain different modes for file opening in C with a program.


Answer:
o Modes: "r", "w", "a", "r+", "w+", "a+".
o Example Program:
c
Copy code
FILE *file = fopen("test.txt", "r");
if (file == NULL) {
printf("File does not exist.\n");
return 1;
}
fclose(file);

5. Write a program to implement the bubble sort algorithm.


Answer:

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;
}
}
}
}

Final Summary: High-Probability Questions for December 2024 Semester Exam

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

Key Topics for MCQs:

 Control structures: Loops, switch-case, goto.


 Operators: Associativity, precedence, bitwise operators.
 Pointers: Basics, arithmetic, and memory allocation.
 Functions: Prototypes, recursion, storage classes.
 File Handling: Modes, standard functions (fopen, fprintf, fscanf).

Likely MCQs:

1. Which loop is exit-controlled?


o (a) for
o (b) while
o (c) do-while
o (d) None of these
Answer: (c) do-while
2. The default storage class for local variables is:
o (a) auto
o (b) register
o (c) extern
o (d) static
Answer: (a) auto
3. The size of a pointer on a 64-bit system is:
o (a) 4 bytes
o (b) 8 bytes
o (c) 2 bytes
o (d) Depends on compiler
Answer: (b) 8 bytes
4. The function strcpy(dest, src) in <string.h>:
o (a) Copies src into dest.
o (b) Appends src to dest.
o (c) Compares src and dest.
o (d) None of these.
Answer: (a) Copies src into dest.
5. The ASCII value of 'A' is:
o (a) 65
o (b) 97
o (c) 48
o (d) None of the above
Answer: (a) 65

2. High-Probability SAQs

Key Topics for SAQs:

 Conceptual differences between constructs.


 Code snippets requiring error detection or explanation.
 Short programs for specific tasks.

Likely SAQs:

1. Differentiate between while and do-while loops.


Answer:
o while: Entry-controlled loop. Executes only if the condition is true.
o do-while: Exit-controlled loop. Executes at least once regardless of the condition.
2. Explain the difference between malloc and calloc.
Answer:
o malloc: Allocates uninitialized memory.
o calloc: Allocates memory and initializes it to zero.
3. Write a program to find the factorial of a number using recursion.
Answer:

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

5. Explain dynamic memory allocation in C.


Answer:
Allows memory to be allocated during runtime using functions like malloc, calloc, realloc, and
free.

3. High-Probability LAQs

Key Topics for LAQs:

 Programs involving arrays, strings, and file handling.


 Advanced recursion problems.
 Complex tasks requiring structure-based programming.

Likely LAQs:

1. Write a program to copy contents from one file to another.


Answer:

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;
}

2. Write a program to find the GCD of two numbers using recursion.


Answer:

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;
}

4. Write a program to reverse a string without using string.h.


Answer:

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;
}
}

5. Explain storage classes in C with examples.


Answer:
o auto: Default for local variables.
o register: Suggests storing variable in a CPU register for faster access.
o static: Retains value between function calls.
o extern: Declares global variables defined elsewhere.

Example:

c
Copy code
void func() {
static int count = 0; // Retains value across calls
count++;
printf("%d\n", count);
}

4. Actionable Study Tips

1. Prioritize Frequently Asked Questions:


o MCQs: Revise control structures, file handling, pointers, and operator precedence.
o SAQs: Focus on conceptual differences and small programs.
o LAQs: Practice recursion, file handling, and array-based programs.
2. Simulate Exam Conditions:
o Solve past papers under timed conditions to improve speed and accuracy.
3. Master Core Topics:
o File Handling: Reading, writing, and copying files.
o Pointers: Pointer arithmetic, dynamic memory allocation.
o Recursion: Fibonacci, GCD, factorial.
4. Practice Snippets:
o Debugging and predicting outputs for tricky snippets.

If you want detailed solutions for more questions or additional practice material, let me know!

You might also like