Question 1 · Programming and Data Structures · 2026_Set2
MCQ
In C runtime environment, which one of the following is stored in heap?
- A.
A static variable declared inside a function
- B.
An array of integers declared inside a function
- C.
A dynamically allocated array of integers created using malloc() function call
- D.
Return address of a function
Step-by-Step Solution
Insight: This is a memory layout classification question, recognizable by asking where specific C constructs reside in the runtime environment.
Exam route: Eliminate options based on standard C memory segments. Static variables go to Data/BSS. Local arrays and return addresses go to Stack. Only malloc targets the Heap.
Learning route:
- Static variable inside a function: Stored in the Data segment (or BSS if uninitialized), not the Heap.
- Array of integers declared inside a function: This is a local variable, allocated on the Stack.
- Dynamically allocated array using
malloc(): Explicitly requests memory from the Heap at runtime.
- Return address of a function: Pushed onto the Stack as part of the function call frame.
Therefore, only the dynamically allocated array resides in the Heap.
Question 2 · Programming and Data Structures · 2026_Set2
NAT
Consider the following ANSI-C program.
#include <stdio.h>
int main(){
int *ptr, a, b, c;
a=5; b=11; c=20;
ptr=&a; *ptr=c; ptr=&c;
a=*(&b); c=*ptr-a;
printf("%d",c);
return(0);
}The output of this program is ____________.
(answer in integer)Note: Assume that the program compiles and runs successfully.
Step-by-Step Solution
Insight: This is a pointer tracing question, recognizable by sequential pointer assignments and dereferences that modify variable states in place.
Exam route: Track the values of a, b, c, and ptr line by line. a becomes 20, then is overwritten to 11. c remains 20 until the final subtraction 20 - 11 = 9.
Learning route:
- Initial state:
a = 5, b = 11, c = 20.
ptr = &a: ptr now holds the address of a.
*ptr = c: Dereferencing ptr accesses a, so a is updated to the value of c, which is 20.
ptr = &c: ptr is reassigned to hold the address of c.
a = *(&b): The address-of and dereference operators cancel out, so a is assigned the value of b, which is 11 (overwriting the previous 20).
c = ptr - a: ptr evaluates to the current value of c (which is 20). Thus, c = 20 - 11 = 9.
- The program prints
9.
Question 3 · Programming and Data Structures · 2025_Set2
NAT
Consider the following C program:
#include <stdio.h>
int main(){
int a;
int arr[5] = {30,50,10};
int *ptr;
ptr = &arr[0] + 1;
a = *ptr;
(*ptr)++;
ptr++;
printf("%d", a + (*ptr) + arr[1]);
return 0;
}The output of the above program is ___________. (Answer in integer)
Step-by-Step Solution
Insight: This is a pointer tracing question with mixed updates, recognizable by sequential pointer assignments, dereferences, and increments on an array.
Exam route: ptr points to arr[1] (50). a becomes 50. (*ptr)++ makes arr[1] 51. ptr++ moves ptr to arr[2] (10). Sum is 50 + 10 + 51 = 111.
Learning route:
- Initial state:
arr = {30, 50, 10, 0, 0}.
ptr = &arr[0] + 1: &arr[0] is the address of the first element. Adding 1 scales by sizeof(int), so ptr now points to arr[1] (value 50).
a = *ptr: a is assigned the value at ptr, so a = 50.
(*ptr)++: The parentheses force dereferencing first. The value at ptr (which is arr[1]) is incremented to 51. ptr still points to arr[1].
ptr++: The pointer itself is incremented. It now points to the next integer, arr[2] (value 10).
printf("%d", a + (*ptr) + arr[1]):
a is 50.
*ptr is the value at arr[2], which is 10.
arr[1] was modified in step 4 and is now 51.
- Sum = 50 + 10 + 51 = 111.
Question 4 · Programming and Data Structures · 2025_Set2
MCQ
Consider the following C program:
#include <stdio.h>
void stringcopy(char *, char *);
int main(){
char a[30] = "@#Hello World!";
stringcopy(a, a + 2);
printf("%s\n", a);
return 0;
}
void stringcopy(char *s, char *t) {
while(*t)
*s++ = *t++;
}Which ONE of the following will be the output of the program?
- A.
@#Hello World!
- B.
Hello World!
- C.
ello World!
- D.
Hello World!d!
Step-by-Step Solution
Insight: This is a string copy with overlap question, recognizable by passing a and a + 2 to a custom stringcopy function.
Exam route: The source string starts at index 2 ("Hello World!", length 12). It overwrites the first 12 characters of a. The original characters at indices 12, 13, and 14 ('d', '!', '\0') are never reached by the destination pointer and remain untouched. Result: "Hello World!d!".
Learning route:
- Initial state:
a contains "@#Hello World!\0".
stringcopy(a, a + 2) is called. s points to a[0] ('@'), t points to a[2] ('H').
- The
while(t) loop copies characters from t to s until t is '\0'.
- "Hello World!" has 12 characters. The loop runs 12 times, overwriting
a[0] through a[11].
- The loop terminates when
t points to a[14] ('\0'). The assignment s++ = t++ does not execute for the null terminator in this specific loop condition (it stops before copying '\0', but wait, the standard while(t) stops when t is '\0', so '\0' is NOT copied. However, the original '\0' at a[14] remains intact).
- Final array content: "Hello World!" (indices 0-11) + "d!\0" (indices 12-14).
printf("%s\n", a) prints "Hello World!d!".
Question 5 · Programming and Data Structures · 2025_Set1
NAT
#include <stdio.h>
void foo(int *p, int x){
*p=x;
}
int main(){
int *z;
int a = 20, b = 25;
z = &a;
foo(z,b);
printf("%d",a);
return 0;
}
The output of the given C program is __________. (Answer in integer)
Step-by-Step Solution
Insight: This is a pointer dereferencing and pass-by-reference simulation question, recognizable by a pointer being passed to a function that modifies the pointed-to value.
Exam route: z holds &a. foo(z, b) passes &a and 25. Inside foo, *p = x modifies a to 25. printf prints 25.
Learning route:
a is initialized to 20, b to 25.
z = &a makes z point to a.
foo(z, b) is called. The arguments passed are the address of a and the value 25.
- Inside
foo, p receives &a and x receives 25.
*p = x dereferences p (accessing a) and assigns it the value of x (25). Thus, a becomes 25.
- Back in
main, printf("%d", a) prints the updated value of a, which is 25.
Question 6 · Programming and Data Structures · 2024_Set2
MSQ
Consider the following C function definition.
int fX(char *a){
char *b = a;
while(*b)
b++;
return b - a;}
Which of the following statements is/are TRUE?
- A.
The function call fX(”abcd”) will always return a value
- B.
Assuming a character array c is declared as char c[] = ”abcd” in main(), the function call fX(c)will always return a value
- C.
The code of the function will not compile
- D.
Assuming a character pointer c is declared as char *c = ”abcd” in main(), the function call fX(c)will always return a value
Correct Answer: ["A","B","D"]
Step-by-Step Solution
Insight: This tests string length calculation via pointer subtraction and the validity of passing different string representations to a char * parameter in C.
Exam route: The function correctly computes length by advancing b until \0 and returning b - a. In C, string literals ("abcd"), character arrays (char c[]), and character pointers (char c) all decay to or are char , making all calls valid and returning 4.
Learning route:
- The function
fX takes a char a. It sets b = a and increments b until b is '\0' (false).
- It returns
b - a, which is the number of characters traversed (the string length).
- Option A:
fX("abcd"). In C, a string literal is of type char[] (not const char[] as in C++), so it decays to char *. The function reads it safely and returns 4. TRUE.
- Option B:
char c[] = "abcd"; fX(c);. The array c decays to char * pointing to its first element. The function reads it safely and returns 4. TRUE.
- Option C: The code uses standard C pointer arithmetic and dereferencing. It compiles without error. FALSE.
- Option D:
char c = "abcd"; fX(c);. c is already a char pointing to the string literal. The function reads it safely and returns 4. TRUE.
Question 7 · Programming and Data Structures · 2024_Set2
MCQ
What is the output of the following C program?
#include <stdio.h>
int main() {
double a[2]={20.0, 25.0}, *p, *q;
p = a;
q = p + 1;
printf(”%d,%d”, (int)(q – p), (int)(*q – *p));
return 0;}
Step-by-Step Solution
Insight: This tests pointer arithmetic rules, specifically that pointer subtraction yields the element count, while dereferenced subtraction yields the value difference.
Exam route: q - p is the difference between &a[1] and &a[0], which is 1 element. q - p is 25.0 - 20.0 = 5.0, cast to int is 5. Output is 1,5.
Learning route:
p = a assigns p to the base address of the array, so p points to a[0] (value 20.0).
q = p + 1 advances the pointer by one double element, so q points to a[1] (value 25.0).
q - p: Pointer subtraction automatically divides the byte difference by sizeof(double), yielding the number of elements between them. Since they are adjacent, this is 1.
q - p: Dereferences both pointers to get their values: 25.0 - 20.0 = 5.0.
- The
(int) cast truncates 5.0 to 5.
- The
printf outputs 1,5.
Question 8 · Programming and Data Structures · 2022
MCQ
What is printed by the following ANSI C program?
#include<stdio.h>
int main(int argc, char *argv[])
{
int a[3][3][3] =
{{1, 2, 3, 4, 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24, 25, 26, 27}};
int i = 0, j = 0, k = 0;
for( i = 0; i < 3; i++ ){
for(k = 0; k < 3; k++ )
printf("%d ", a[i][j][k]);
printf("\n");
}
return 0;
}
- A. 1 2 3
10 11 12
19 20 21
- B. 1 4 7
10 13 16
19 22 25
- C. 1 2 3
4 5 6
7 8 9
- D. 1 2 3
13 14 15
25 26 27
Step-by-Step Solution
Insight: This is a 3D array traversal question with a fixed middle index, recognizable by the nested loop structure where the middle index variable j is initialized to 0 and never incremented.
Exam route: Since j=0 always, the loops only access a[i][0][k]. For i=0, k=0,1,2 yields 1, 2, 3. For i=1, yields 10, 11, 12. For i=2, yields 19, 20, 21.
Learning route:
- The array
a is initialized as a 3x3x3 block. The first 2D slice (i=0) contains 1-9, the second (i=1) contains 10-18, the third (i=2) contains 19-27.
- The outer loop iterates
i from 0 to 2.
- The inner loop iterates
k from 0 to 2.
- Crucially,
j is initialized to 0 and never changes.
- For
i=0: prints a[0][0][0], a[0][0][1], a[0][0][2] → 1, 2, 3.
- For
i=1: prints a[1][0][0], a[1][0][1], a[1][0][2] → 10, 11, 12.
- For
i=2: prints a[2][0][0], a[2][0][1], a[2][0][2] → 19, 20, 21.
- This matches the output in Option A.
Question 9 · Programming and Data Structures · 2022
MCQ
What is printed by the following ANSI C program?
#include<stdio.h>
int main(int argc, char *argv[])
{
int x = 1, z[2] = {10, 11};
int *p = NULL;
p = &x;
*p = 10;
p = &z[1];
*(&z[0] + 1) += 3;
printf("%d, %d, %d\n", x, z[0], z[1]);
return 0;
}
- A.
1, 10, 11
- B.
1, 10, 14
- C.
10, 14, 11
- D.
10, 10, 14
Step-by-Step Solution
Insight: This is a pointer tracing question with array indexing, recognizable by mixed pointer assignments and arithmetic on array base addresses.
Exam route: Track x, z[0], and z[1]. p = 10 changes x to 10. (&z[0] + 1) is equivalent to z[1], so z[1] becomes 11 + 3 = 14. z[0] remains 10.
Learning route:
- Initial state:
x = 1, z[0] = 10, z[1] = 11.
p = &x: p points to x.
*p = 10: The value at p (which is x) is updated to 10.
p = &z[1]: p is redirected to point to z[1]. (This line does not change z[1]'s value, only p's target).
*(&z[0] + 1) += 3:
&z[0] is the address of the first element.
- Adding 1 scales by the size of
int, yielding the address of the next element, &z[1].
- Dereferencing this gives
z[1].
z[1] += 3 updates z[1] from 11 to 14.
- Final values:
x = 10, z[0] = 10, z[1] = 14.
- Output matches option D.
Question 10 · Programming and Data Structures · 2021_Set2
MCQ
Consider the following ANSI C program.
#include <stdio.h>
int main(){
int arr[4][5];
int i, j;
for (i=0; i<4; i++){
for (j=0; j<5; j++){
arr[i][j] = 10*i + j;
}
}
printf("%d", *(arr[1] + 9));
return 0;
}
What is the output of the above program?
Step-by-Step Solution
Insight: This is a multidimensional array pointer arithmetic question, recognizable by the expression *(arr[1] + 9).
Exam route: arr[1] is an int pointing to arr[1][0]. Adding 9 moves the pointer 9 integers forward. Since each row has 5 elements, this lands on arr[1 + 1][4] = arr[2][4]. Value is 102 + 4 = 24.
Learning route:
arr is a 2D array of size 4x5, stored in contiguous row-major memory.
arr[1] is the name of the second row. In an expression, it decays to a pointer to its first element, i.e., &arr[1][0]. Its type is int *.
- The expression
arr[1] + 9 performs pointer arithmetic. It adds 9 to the int * pointer.
- Since each row has 5 elements, moving 5 steps forward reaches the start of the next row:
arr[1] + 5 is equivalent to &arr[2][0].
- Moving 4 more steps reaches
&arr[2][4]. Thus, arr[1] + 9 is equivalent to &arr[2][4].
- Dereferencing this with
* gives the value of arr[2][4].
- The initialization rule is
arr[i][j] = 10i + j. For i=2, j=4, the value is 102 + 4 = 24.