Question:
What will be the output of the following program?
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;
printf("%d %d %d", *ptr, *(ptr+2), *(ptr+4));
return 0;
}
Options: A) 10 30 50
B) 10 20 30
C) 10 40 50
D) Compilation error
Answer & Explanation:
Correct Answer: A) 10 30 50
Explanation: The pointer ptr
points to the first element of the array. *(ptr+2)
accesses the third element (30), and *(ptr+4)
accesses the fifth element (50).
Question:
What is a dangling pointer in C, and how can it be avoided?
Answer:
A dangling pointer is a pointer that still refers to a memory location that has been freed or deallocated.
It can be avoided by:
free(ptr); ptr = NULL;
)Question:
Write a recursive function to calculate the factorial of a number.
Solution:
#include <stdio.h>
long factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %ld", num, factorial(num));
return 0;
}
Concepts Covered: Recursion, Base Condition, Function Calls
Question:
What will be the output of the following C program?
#include <stdio.h>
void fun() {
printf("Hello, World!\n");
}
int main() {
void (*ptr)();
ptr = fun;
(*ptr)();
return 0;
}
Answer:
Output:Hello, World!
Explanation:ptr
is a function pointer that stores the address of fun()
, which is later executed using (*ptr)();
.
Question:
Write a program to swap two numbers without using a third variable.
Solution:
#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Concepts Covered: Bitwise operations, arithmetic tricks
Question:
Consider the following struct definition:
#include <stdio.h>
struct test {
char a;
int b;
char c;
};
int main() {
printf("%lu", sizeof(struct test));
return 0;
}
What will be the output?
Options:
A) 6
B) 8
C) 12
D) 16
Answer:
Correct Answer: C) 12
Explanation:
Due to structure padding, the memory layout will be:
char a
→ 1 byte + 3 bytes paddingint b
→ 4 byteschar c
→ 1 byte + 3 bytes paddingTotal memory occupied = 12 bytes.
Question:
What is the key difference between malloc()
and calloc()
in C?
Answer:
malloc(size)
allocates uninitialized memory.calloc(n, size)
allocates zero-initialized memory (all bits set to 0).Example:
int *arr1 = (int *)malloc(5 * sizeof(int)); // Contains garbage values
int *arr2 = (int *)calloc(5, sizeof(int)); // Contains all zeros
Question:
What will be the output of the following program?
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
int **q = &p;
printf("%d %d %d", a, *p, **q);
return 0;
}
Answer:
Output: 10 10 10
Explanation:
p
stores the address of a
q
stores the address of p
*p
dereferences to a
, and **q
also dereferences to a
Question:
What will be the output of the following program?
#include <stdio.h>
int main() {
int a = 5, b = 9;
printf("%d", a ^ b);
return 0;
}
Options:
A) 12
B) 4
C) 10
D) 8
Answer:
Correct Answer: C) 12
Explanation:
XOR Operation:
5 → 0101
9 → 1001
----------------
XOR → 1100 (Decimal: 12)
Question:
Write a function to reverse a singly linked list in C.
Solution:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void reverse(struct Node** head) {
struct Node* prev = NULL;
struct Node* current = *head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
Concepts Covered: Linked lists, pointers, iteration
Question:
What is wrong with this program?
#include <stdio.h>
int main() {
char *str;
str = "Hello";
str[0] = 'M';
printf("%s", str);
return 0;
}
Answer:
This will cause a segmentation fault because "Hello"
is stored in read-only memory. To modify it, we should use:
char str[] = "Hello"; // Stored in writable memory
str[0] = 'M'; // Now valid
Question:
What is the key difference between struct
and union
?
Answer:
struct
: Each member has its own memory space.union
: All members share the same memory location, and only one member is stored at a time.Example:
struct Example {
int a; // 4 bytes
char b; // 1 byte (Padding: 3 bytes)
}; // Total = 8 bytes
union Example {
int a; // 4 bytes
char b; // 1 byte (Uses same memory as `a`)
}; // Total = 4 bytes
Concepts Covered: Memory allocation, storage optimization
Question:
Write a C program to find the missing number in an array of size N-1
containing numbers from 1
to N
.
Solution:
#include <stdio.h>
int findMissing(int arr[], int n) {
int total = (n * (n + 1)) / 2;
int sum = 0;
for (int i = 0; i < n - 1; i++) {
sum += arr[i];
}
return total - sum;
}
int main() {
int arr[] = {1, 2, 4, 5, 6}; // Missing number = 3
int n = 6;
printf("Missing number is %d", findMissing(arr, n));
return 0;
}
Concepts Covered: Mathematics, arrays, loops
Question:
How would you detect a loop in a linked list using Floyd’s cycle detection algorithm?
Solution:
int detect Loop(struct Node* head) {
struct Node *slow = head, *fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
return 1; // Loop found
}
return 0; // No loop
}
Concepts Covered: Pointers, Floyd’s cycle detection
Question:
Write a recursive C function to check if a string is a palindrome.
Solution:
#include <stdio.h>
#include <string.h>
int isPalindrome(char str[], int start, int end) {
if (start >= end) return 1;
if (str[start] != str[end]) return 0;
return isPalindrome(str, start + 1, end - 1);
}
int main() {
char str[] = "madam";
if (isPalindrome(str, 0, strlen(str) - 1))
printf("Palindrome");
else
printf("Not a Palindrome");
return 0;
}
Concepts Covered: Strings, recursion