Machine-translated by Google. Questions are authored in English.
Top

Pointers Questions and Answers

1.

What is the output of the following 'C' language statements?

#include\(\lt\)stdio.h\(\gt\)
int main(){
int a=10,b=20,*p1=&a,*p2=&b;
printf("Before : **p1=%d **p2=%d",*p1,*p2);
*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
printf("\nAfter : **p1=%d **p2=%d",*p1,*p2);
return 0;
}

Exam Year(s)

DSSSB TGT - 2023,

Answer: A

The output is Before : **p1=10 **p2=20 and then After : **p1=20 **p2=10. The pointers p1 and p2 point to a and b, so the three statements swap the values without a temporary variable. \[ *p1 = 10 + 20 = 30 \] \[ *p2 = 30 - 20 = 10 \] \[ *p1 = 30 - 10 = 20 \] The strings '**p1=' and '**p2=' are printed as plain text. The second option reverses the values, the third drops the asterisks that the format string prints, and the fourth gives the wrong second value.

Please log in or register to report this question.

2.

What is the output of the following C program?

#include \(\lt\)stdio.h\(\gt\)
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;}

Exam Year(s)

GATE CS - 2024,

Answer: B

The output is 1,5.

p points to a[0] and q to a[1]. Subtracting two pointers into the same array gives the number of elements between them, not the number of bytes, so q - p is 1 even though a double occupies 8 bytes.

*q - *p subtracts the values stored there, \(25.0 - 20.0 = 5.0\), and the cast to int gives 5. Both arguments are ints, so the %d conversions print 1,5.

8 (in 8,5) is the byte distance, which pointer subtraction does not return. 4,8 and 1,8 mix up the element and byte counts, and the value difference is 5, never 8.

Please log in or register to report this question.

3.

Consider the following C program:

#include\(\lt\)stdio.h\(\gt\)

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 is

Exam Year(s)

GATE CS - 2018,

Answer: A

The program prints Hi Bye Bye Hi.

fun1 receives copies of the two pointers. It swaps its own copies, but str1 and str2 in main are unchanged, so the first printf prints 'Hi Bye '.

fun2 receives the addresses of str1 and str2. Writing through *s1 and *s2 swaps the pointers in main itself, so str1 now points to "Bye" and str2 to "Hi", and the second printf prints 'Bye Hi'.

'Hi Bye Hi Bye' assumes neither function swaps anything. The options starting 'Bye Hi' assume fun1's swap reaches main, which C's pass-by-value rules out.

Please log in or register to report this question.

4.

What is printed by the following ANSI C program?

#include\(\lt\)stdio.h\(\gt\)
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;
}

Exam Year(s)

GATE CS - 2022,

Answer: D

The program prints 10, 10, 14.

  1. p = &x; *p = 10; writes 10 into x through the pointer.
  2. p = &z[1]; only moves p; nothing is written.
  3. &z[0] + 1 is the address of z[1], so *(&z[0] + 1) += 3 adds 3 to z[1], making it 14. z[0] stays 10.

So x = 10, z[0] = 10, z[1] = 14. '1, 10, 11' and '1, 10, 14' forget that x was changed through p. '10, 14, 11' applies the += 3 to z[0] instead of to the element one past it.

Please log in or register to report this question.

5.

Consider the case that a record is read by another, which is moved or deleted and it contains a pointer, then that pointer is called as :

Exam Year(s)

MH SET - 2018, SET Exam - 2018,

Answer: C

Such a pointer is called a dangling pointer.

If a record holding a pointer to another record keeps that pointer after the target has been moved or deleted, the pointer now refers to a location that no longer holds the intended record. That is a dangling pointer, and following it gives wrong data or an error. Database systems avoid it with tombstones or indirection through record identifiers.

A dynamic pointer is just one allocated at run time, a pointer to pointer holds the address of another pointer, and 'unlinked pointer' is not a standard term.

Please log in or register to report this question.

6.

Consider the following 'C' program fragment :

int main( )
{
char *a, *b, c[10], d[10];
a=b;
b=c;
c=d;
d=a;
return φ;
}

Choose the statement having error from the following :

Exam Year(s)

MH SET - 2020, SET Exam - 2020,

Answer: C

The statements with errors are c=d; and d=a;.

In C, an array name such as c or d is not a modifiable lvalue: it stands for the fixed address of the array, so an array cannot be assigned to. Both c=d (array to array) and d=a (pointer to array) are therefore compilation errors.

a=b assigns one char pointer to another, and b=c assigns the address of c's first element to a pointer (the array name decays to a pointer), so both are legal. Every option that names a=b or b=c as an error is wrong.

Please log in or register to report this question.

7.

Which of the following are fundamental operation on pointers ?

(i) implicit dereferencing
(ii) indirection
(iii) explicit referencing
(iv) addressing

Exam Year(s)

MH SET - 2021, SET Exam - 2021,

Answer: A

The key's answer is (i), (ii) and (iii). I could not trace the three-term list to a source, so this follows the key.

A pointer is used through two basic actions: setting it to point at an object, and following it to reach the object it points at. Following a pointer is dereferencing or indirection, which a language may do implicitly (as with Ada's access types and Fortran 90 pointers) or explicitly with an operator such as the * of C. Referencing, the counterpart that produces a pointer to an object (the & of C), is the explicit-referencing item. The standard treatments I found (Sebesta) name two operations, assignment and dereferencing, so the grouping in (i) to (iii) is the paper's own.

Addressing, item (iv), is how a machine locates a memory cell and is not a pointer operation in the language sense, so (iv) only is out, and the pairs that leave out (i) or (iii) are incomplete on the key's reading.

Please log in or register to report this question.

8.

Consider the following 'C' program fragment :

if(tPTR ! = NULL)
    * tPTR = NULL;
else
    * tPTR = NULL;

Select the following statements that are having same effect :

(i) if (tPTR) * tPTR = NULL;
else * tPTR = NULL
(ii) *tPTR = NULL;
(iii) if (!tPTR) * tPTR = NULL;
else * tPTR = NULL;
(iv) if (tPTR = = NULL) * tPTR = NULL;
else * tPTR = NULL;

Exam Year(s)

MH SET - 2021, SET Exam - 2021,

Answer: B

All four have the same effect, so the answer is (i), (ii), (iii) and (iv).

In the given fragment both branches of the if do the same thing, *tPTR = NULL;, so whatever the condition, the effect is just that assignment, which is statement (ii).

Statements (i), (iii) and (iv) also have identical branches. They test different conditions (tPTR non-null, tPTR null, tPTR equal to NULL), but since the same assignment runs either way, the condition changes nothing. The paper sets the operators != and == with a space inside them and leaves the semicolon off the end of (i); read as the intended C, every option naming fewer than four statements leaves out an equivalent one.

Please log in or register to report this question.

9.

Predict the output of the following source code in C language :

int main( )
{
  int i, x[5];
  for(i=0; i\(\lt\)5; i++) * (x+i) = i;
  for(i=0; i\(\lt\)5; i++) printf("%d", x[i]);
  return 0;
}

Exam Year(s)

MH SET - 2024, SET Exam - 2024,

Answer: A

The program prints the values 0 1 2 3 4.

In C, *(x+i) is the same element as x[i], since an array name decays to a pointer to its first element. The first loop stores 0, 1, 2, 3, 4 in the five elements, and the second loop prints them in order.

Because the format string is "%d" with no separator, the actual output is the digits run together, 01234; option A lists those values. The code is valid, so there is no syntax or semantic error, and every element is assigned before it is printed, so no garbage value appears.

Please log in or register to report this question.

10.

What is the output of this C code?

void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **m);
}

 

Answer: A

Correct answer: 5 5 5

Each dereference removes one level. k is 5; p holds the address of k, so *p is 5; and m holds the address of p, so *m is p and **m is 5. All three names reach the same object by paths of different length, which is the whole point of the double pointer.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Pointers Questions and Answers || Pointers MCQ Questions and Answers || Pointers GK Questions and Answers || Pointers GK MCQ Questions || Pointers Multiple Choice Questions and Answers || Pointers GK || GK on Pointers || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming