Array and Pointer · Programming in C
54 questions · asked in 22 of 27 years (2000–2026)
MCQ 39 · MSQ 1 · NAT 141-mark 34 · 2-mark 20
2000Questions per year2026
Questions 16–20 of 54
Consider the following C program:
#include <stdio.h>
void fun1(char *s1, char *s2){
char *tmp;
tmp = s1;
s1 = s2;
s2 = tmp;
}
void fun2(char **s1, char **s2){
char *tmp;
tmp = *s1;
*s1 = *s2;
*s2 = tmp;
}
int main(){
char *str1 = "Hi", *str2 = "Bye";
fun1(str1, str2);
printf("%s %s ", str1, str2);
fun2(&str1, &str2);
printf("%s %s", str1, str2);
return 0;
} The output of the program above isAnswer: (A)
Going through each call:
- fun1 gets only copies of the pointers and swaps those copies. So str1 and str2 in main stay the same prints "Hi Bye".
- fun2 gets the addresses of str1 and str2. It swaps the pointers themselves prints "Bye Hi".
Answer: Hi Bye Bye Hi
- fun1 gets only copies of the pointers and swaps those copies. So str1 and str2 in main stay the same prints "Hi Bye".
- fun2 gets the addresses of str1 and str2. It swaps the pointers themselves prints "Bye Hi".
Answer: Hi Bye Bye Hi
Consider the following C code:
#include <stdio.h>
int * assignval (int *x, int val) {
*x = val;
return x;
}
void main ( ) {
int * x= malloc (sizeof (int));
if (NULL == x) return;
x = assignval (x,0);
if(x) {
x=(int *) malloc (sizeof (int));
if (NULL == x) return;
x = assignval (x, 10);
}
printf("%d\n", *x);
free (x);
} The code suffers from which one of the following problems:Answer: (D)
The first malloc block, which holds 0, is never freed. The pointer x is given a new address by the second malloc, so the first block cannot be reached any more memory leak. The code compiles without error (C lets a void* be used as an int* without a cast).
Answer: compiles successfully but execution may result in memory leak
Answer: compiles successfully but execution may result in memory leak
Consider the following C program.
#include <stdio.h>
#include <string.h>
void printlength (char *s, char *t) {
unsigned int c = 0;
int len = ((strlen(s) - strlen (t)) > c) ? strlen(s): strlen(t);
printf ("%d\n", len);
}
void main ( ) {
char *x = "abc";
char *y ="defgh";
printlength (x,y);
} Recall that strlen is defined in string.h as returning a value of type size_t, which is an unsigned int. The output of the program is _____________.Answer: 3
strlen returns an unsigned value, so in 3 5 the result is not 2. It wraps around and becomes a very large positive number, which is 0. So len = strlen(s) = 3.
Answer: 3
Answer: 3
Match the following:
(P) static char var; | (i) Sequence of memory locations to store addresses |
(Q) m = malloc(10);m = NULL; | (ii) A variable located in data section of memory |
(R) char *ptr[10]; | (iii) Request to allocate a CPU register to store data |
(S) register int var1; | (iv) A lost memory which cannot be freed |
Answer: (A)
P: a static variable lives for the whole run of the program, so it is kept in the data section, not on the stack → (ii).
Q: malloc(10) returns the only pointer to a new 10-byte block, and m = NULL overwrites it. The block stays allocated but can no longer be reached or freed: lost memory (a memory leak) → (iv).
R: char *ptr[10] is an array of 10 pointers, that is, 10 consecutive memory locations that hold addresses → (i).
S: register asks the compiler to keep var1 in a CPU register (the compiler may ignore the request) → (iii).
Answer: (A)
Q: malloc(10) returns the only pointer to a new 10-byte block, and m = NULL overwrites it. The block stays allocated but can no longer be reached or freed: lost memory (a memory leak) → (iv).
R: char *ptr[10] is an array of 10 pointers, that is, 10 consecutive memory locations that hold addresses → (i).
S: register asks the compiler to keep var1 in a CPU register (the compiler may ignore the request) → (iii).
Answer: (A)
Consider the following function implemented in C:
void printxy (int x, int y) {
int *ptr ;
x = 0;
ptr = &x;
y = * ptr;
*ptr = 1;
printf ("%d, %d,", x, y);
} The output of invoking printxy (1,1) isAnswer: (C)
First x is set to 0, and ptr gets the address of x: ptr = x. Then y = *ptr gives y = 0. Next, *ptr = 1 writes through the pointer, so x becomes 1.
The program prints x, y = 1, 0.
Answer: 1,0
The program prints x, y = 1, 0.
Answer: 1,0
Keep practising