Pointers

Question 1

Consider the following three C functions:

[PI]            int*g(void) 
             { 
                int x = 10; 
                return(&x); 
             }  
    
[P2]            int*g(void) 
             { 
                int*px; 
                *px = 10; 
                return px; 
             } 
    
[P3]            int*g(void) 
             { 
                int*px; 
                px = (int *) malloc(sizeof(int)); 
                *px = 10; 
                return px; 
             } 

Which of the above three functions are likely to cause problems with pointers?

A
Only P3
B
Only P1 and P3
C
Only P1 and P2
D
P1, P2 and P3
       Data-Structures       Pointers       GATE 2001
Question 1 Explanation: 
[P1] → May cause error because the function is returning the address of locally declared variable.
[P2] → It will cause problem because px is in int pointer that is not assigned with any address and we are doing dereferencing.
[P3] → It will work because memory will be stored in px that can be use further. Once function execution completes this will exist in Heap.
Question 2

Faster access to non-local variables is achieved using an array of pointers to activation records called a

A
stack
B
heap
C
display
D
activation tree
       Data-Structures       Pointers       GATE 1998
Question 2 Explanation: 
Properties of displays:
→ Use a pointer array to store the activation records along the static chain.
→ Fast access for non-local variables but may be complicated to maintain.
Question 3
Consider the following program Assuming required header files are included and if the machine in which this program is executed is little-endian, then the output will be
A
0
B
99999999
C
1
D
unpredictable
       Programming-for-Output-Problems       Pointers       ISRO-2018
Question 3 Explanation: 
Question 4
What is the output of this C code?
A
5 5 5
B
5 5 junk
C
5 junk junk
D
Compile time error
       Programming-for-Output-Problems       Pointers       ISRO-2016
Question 4 Explanation: 
If we are given p instead of *p then it prints the address of k but we are printing the value of k.
The same rule applies in **m.
Output is 5 5 5
Question 5
What will be the output of the following program? Assume that you are running this program in little-endian processor.
A
1
B
320
C
64
D
Compilation error
       Programming-for-Output-Problems       Pointers       ISRO-2016
Question 5 Explanation: 
Ptr = (char*) &a ; /* It is type casted to char* */
Printf (“%d”, *ptr) ; /* Here *ptr is referring a single character */
Printf (“%d”, *ptr) ;
Output: 64 /* @ ASCII value is 64 */
Note: Assume short a is 2 byte integer.
Question 6
Which of the following is true with respect to Reference?
A
A reference can never be NULL
B
A reference needs an explicit dereferencing mechanism
C
A reference can be reassigned after it is established
D
A reference and pointer are synonymous
       Programming       Pointers       ISRO CS 2014
Question 6 Explanation: 
In the C++ programming language, a reference is a simple reference data type that is less powerful but safer than the pointer type inherited from C. A reference is a general concept datatype, with pointers and C++ references being specific reference data type implementations.
We can assign NULL to pointers where as we can’t assign NULL to references.
We can re-assign pointers as many as number of times where a reference can never be re-assigned once it is established.
Question 7
What is the meaning of following declaration?
int(*P[7])();
A
P is pointer to function
B
P is pointer to such function which return type is array
C
P is array of pointer to function
D
P is pointer to array of function
       Programming       Pointers       Nielit Scientist-B IT 4-12-2016
Question 7 Explanation: 
int *ptr[7]; --This is an array of 7 int* pointers, a pointer to an array of 7 ints
int (*ptr)[7]; --This is a pointer to an array of 7 int
int(*P[7])(); --P is array of pointer to function
int(*P)() ; --​ P is pointer to function
Question 8
Prior to using a pointer variable it should be
A
declared
B
initialized
C
both declared and initialized
D
none of these
       Programming       Pointers       Nielit Scientist-B CS 2016 march
Question 8 Explanation: 
● In the programming, Before using any variable we should declare that variable.If require means initialize the variable.
● Pointer is also one variable
● So before using a pointer variable it should be both declared and initialized.
Question 9
Consider the following code fragment
void foo(int x, int y)
{
   x+=y;
   y+=x;
}
main()
{
   int x=5.5;
   foo(x,x);
}
What is the final value of x in both call by value and call by reference, respectively?
A
5 and 16
B
5 and 12
C
5 and 20
D
12 and 20
       Programming-for-Output-Problems       Pointers       ISRO CS 2015
Question 9 Explanation: 
Call by Value:
The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
foo(x,x) ---->foo(5,5)
Modification of x and y values won’t effect in the main program, So still x will hold value “5” because the data type is integer.
Call by Reference:
The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.
Function call foo(x,x)---foo(5,5) calls the function
void foo(int x, int y)
{
x+=y;
y+=x;
}
Here x and y variables, both pointing to same variable “x” in the main function.
So, if any modification done in the function it automatically reflect in the main function.
x+=y means x=x+y=5+5=10 // The variable “x” of main function will hold value 10 which used by both local variables of x and y
y+=x means u=y+x= 10+10=20 // Y value is 10 because , it point to variable ”x” in the main function.
Question 10
Output of following program?
#include
int main()
{
int *ptr;
int x;
ptr=&x;
*ptr=0;
printf("x=%d\n",x);
printf("*ptr=%d\n",*ptr);
*ptr+=5;
printf("x=%d\n",x);
printf("*ptr=%d\n",*ptr);
(*ptr)++;
printf(x=%d\n",x);
printf("*ptr=%d\n",*ptr);
return 0;
}
A
x=0
*ptr=0
x=5
*ptr=5
x=6
*ptr=6
B
x=garbage value
*ptr=0
x=garbage value
*ptr=5
x=garbage value
*ptr=6
C
x=0
*ptr=0
x=5
*ptr=5
x=garbage value
*ptr=garbage value
D
x=0
*ptr=0
x=0
*ptr=0
x=0
*ptr=0
       Programming       Pointers       Nielit Scientist-B IT 22-07-2017
Question 10 Explanation: 
● ptr=&x; // Address of x variable will store in to pointer “ptr” or pointer “ptr” will point to “x”
● *ptr=0; // storing value “0” in the memory location.
● printf("x=%d\n",x); // “0” will be printed
● printf("*ptr=%d\n",*ptr); // “0” will be printed because , ptr will point to variable “x”
● *ptr+=5; // *ptr+=5; means *ptr=*ptr+5 which is nothing but value 5 will store into memory locaton
● printf("x=%d\n",x); // 5 will be printed
● printf("*ptr=%d\n",*ptr); //5 will be printed.
● (*ptr)++; // (*ptr) means 5 and it is incremented by 1 so the updated value is “6”
● printf(x=%d\n",x); // “6” will be printed
● printf("*ptr=%d\n",*ptr);// “6” will be printed
Question 11
Assume that float takes 4 bytes, predict the output of following program.
#include
int main()
{
float arr[5]={12.5,10.0,13.5,90.5,0.5};
float *ptr1=&arr[0];
float *ptr2=ptr1+3;
printf("%f",*ptr2);
printf("%d",ptr2-ptr1);
return 0;
}
A
90.500000 3
B
90.500000 12
C
10.000000 12
D
0.500000 3
       Programming       Pointers       Nielit Scientist-B IT 22-07-2017
Question 11 Explanation: 
● float *ptr1=&arr[0]; // ptr1 will point to first element of the array.
● float *ptr2=ptr1+3; // ptr2 will point to fourth element of the array
● printf("%f",*ptr2); // It will display fourth element value which is 90.500000
● printf("%d",ptr2-ptr1); // Here both pointer will point to same array . The subtraction operation gives the number of elements between the addresses.
Question 12
Consider the following declaration:
int a, *b=&a, **c=&b;
The following program fragment
a=4;
**c=5;
A
does not change the value of a
B
assigns address of c to a
C
assigns the value of b to a
D
assigns 5 to a
       Programming-for-Output-Problems       Pointers       ISRO CS 2015
Question 12 Explanation: 
In the given program fragment, three variables are there
Ordinary variable -a // value will be stored
Pointer variable -b // address of variable will be stored

Pointer to pointer variable(double pointer) -c // address of pointer variable will stored.
a=4 means storing/assigning value to “a”.
**c means value at(value at(c)) =value at(value at(&b)) (c holds address of pointer “b”)
=value at(&a) (b holds the address of “a”)
Memory location of variable “a”, value 5 is stored/assigned into the variable.
Question 13
Consider the following declaration
int a, *b=&a, **c=&b;
a=4;
**c=5;
If the statement
b=(int *)**c
is appended to the above program fragment then
A
Value of b becomes 5
B
value of b will be the address of c
C
value of b is unaffected
D
none of these
       Programming       Pointers       Nielit Scientific Assistance IT 15-10-2017
Question 13 Explanation: 
In the given program fragment, three variables are there
Ordinary variable -a // value will be stored
Pointer variable -b // address of variable will be stored
Pointer to pointer variable(double pointer) -c // address of pointer variable will stored.
a=4 means storing/assigning value to “a”.
**c means value at(value at(c)) =value at(value at(&b)) (c holds address of pointer “b”)
=value at(&a) (b holds the address of “a”)
Memory location of variable “a”, value 5 is
stored/assigned into the variable.
Given statement is b=(int *)**c. So the value of b becomes 5.
Question 14
The following statement in ‘C’
int (*f())[ ];
declares
A
a function returning a pointer to an array of integers.
B
a function returning an array of pointers to integers.
C
array of functions returning pointers to integers.
D
an illegal statement.
       Programming       Pointers       UGC NET CS 2016 July- paper-2
Question 14 Explanation: 
int (*f())[ ] declare a function returning a pointer to an array of integers.
Question 15
Consider the following declaration in C :
char a[ ];
char *p;
Which of the following statement is not a valid statement ?
A
p=a;
B
p=a+2;
C
a=p;
D
p=&a[2];
       Programming       Pointers       UGC NET CS 2008-june-Paper-2
Question 15 Explanation: 
According to pointer value assigning declarations,
p=a; /*It is correct declaration */
p=p+2; /*It is correct declaration */
p=&a[2]; /*It is correct declaration */
a=p; /* It is incorrect declaration */
Question 16
Assume that the program ‘P’ is implementing parameter passing with ‘call by reference’. What will be printed by following print statements in P?
Program P( )
{
x = 10;
y = 3;
funb (y, x, x)
print x;
print y;
}
funb (x, y, z)
{
y = y + 4;
z = x + y + z;
}
A
10, 7
B
31, 3
C
10, 3
D
31, 7
       Programming       Pointers       UGC NET CS 2016 Aug- paper-3
Question 16 Explanation: 
Here, variable x of func1 points to address of variable y.
And variable y and z of func1 points to address of variable x.
Therefore, y = y+4 ⇒ y = 10+4 = 14
and z = x+y+z ⇒ z = 14+14+3 = 31
z will be stored back in k.
Hence, x=31 and y will remain as it is (y=3).
Hence, answer is (B).
Question 17
Which of the following declares ‘pf’ as a pointer to a function, which returns an integer quantity and requires two integer arguments ?
A
int *pf(int, int);
B
int (*pf)(int, int);
C
(int *) pf(int, int);
D
int ( int *pf(int, int));
       Programming       Pointers       CIL Part - B
Question 17 Explanation: 
int (*pf)(int, int) , Here ‘pf’ declares as a pointer to a function, which returns an integer quantity and requires two integer arguments
Question 18

Which of the following statements is TRUE for the function prototype declaration given below?

Int *(*P)(char *Q[]);
A
P is a function that accepts an argument which is a character array and returns a pointer to an integer quantity.
B
P is a function that accepts an argument which is a pointer to a character array and returns a pointer to an integer quantity.
C
P is a pointer to a function that accepts an argument which is an array of character pointers, and returns a pointer to an integer quantity.
D
P is a pointer to function that accepts an argument which is a character array and returns a pointer to an integer quantity .
       Programming       Pointers       CIL Part - B
Question 18 Explanation: 
P is a pointer to a function that accepts an argument which is an array of character pointers, and returns a pointer to an integer quantity.
There are 18 questions to complete.
PHP Code Snippets Powered By : XYZScripts.com
error: Content is protected !!