100% found this document useful (1 vote)
64 views16 pages

7.pointers - 42

The document contains a series of programming questions and answers related to C language concepts, including pointers, memory management, and function behavior. Each question presents a code snippet and multiple-choice answers, with the correct answer indicated. The questions cover various topics such as output prediction, pointer arithmetic, and function parameter passing.

Uploaded by

harshacsds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
64 views16 pages

7.pointers - 42

The document contains a series of programming questions and answers related to C language concepts, including pointers, memory management, and function behavior. Each question presents a code snippet and multiple-choice answers, with the correct answer indicated. The questions cover various topics such as output prediction, pointer arithmetic, and function parameter passing.

Uploaded by

harshacsds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Question 1

What is the output of following program?


# include <stdio.h>
void fun(int x)
{
x = 30;
}

int main()
{
int y = 20;
fun(y);
printf("%d", y);
return 0;
}

A 30
B 20
C Compiler Error
D Runtime Error

ANS :- B

Question 2
Output of following program?
# include <stdio.h>
void fun(int *ptr)
{
*ptr = 30;
}

int main()
{
int y = 20;
fun(&y);
printf("%d", y);

return 0;
}

A 20
B 30
C Compiler Error
D Runtime Error

ANS :- B

Question 3
Output of following program?
#include <stdio.h>

int main()
{
int *ptr;
int x;

ptr = &x;
*ptr = 0;
printf(" x = %dn", x);
printf(" *ptr = %dn", *ptr);

*ptr += 5;
printf(" x = %dn", x);
printf(" *ptr = %dn", *ptr);

(*ptr)++;
printf(" x = %dn", x);
printf(" *ptr = %dn", *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

ANS :- A

Question 4
Consider a compiler where int takes 4 bytes, char takes 1 byte and pointer
takes 4 bytes.
#include <stdio.h>

int main()
{
int arri[] = {1, 2 ,3};
int *ptri = arri;

char arrc[] = {1, 2 ,3};


char *ptrc = arrc;

printf("sizeof arri[] = %d ", sizeof(arri));


printf("sizeof ptri = %d ", sizeof(ptri));

printf("sizeof arrc[] = %d ", sizeof(arrc));


printf("sizeof ptrc = %d ", sizeof(ptrc));

return 0;
}

A sizeof arri[] = 3 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4


B sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 1
C sizeof arri[] = 3 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 1
D sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4

ANS :- D

Question 5
Assume that float takes 4 bytes, predict the output of following program.
#include <stdio.h>

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

ANS :- A

Question 6
#include<stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40, 50, 60};
int *ptr1 = arr;
int *ptr2 = arr + 5;
printf("Number of elements between two pointer are: %d.",
(ptr2 - ptr1));
printf("Number of bytes between two pointers are: %d",
(char*)ptr2 - (char*) ptr1);
return 0;
}

Assume that an int variable takes 4 bytes and a char variable takes 1 byte

A Number of elements between two pointer are: 5. Number of bytes


between two pointers are: 20
B Number of elements between two pointer are: 20. Number of bytes
between two pointers are: 20
C Number of elements between two pointer are: 5. Number of bytes
between two pointers are: 5
D Compiler Error
E Runtime Error

ANS :- A

Question 7
#include<stdio.h>
int main()
{
int a;
char *x;
x = (char *) &a;
a = 512;
x[0] = 1;
x[1] = 2;
printf("%dn",a);
return 0;
}
Run on IDE
What is the output of above program?

A Machine dependent
B 513
C 258
D Compiler Error

ANS :- A

Question 8
int main()
{
char *ptr = "GeeksQuiz";
printf("%cn", *&*&*ptr);
return 0;
}

A Compiler Error
B Garbage Value
C Runtime Error
D G

ANS :- G

Question 9
#include<stdio.h>
void fun(int arr[])
{
int i;
int arr_size = sizeof(arr)/sizeof(arr[0]);
for (i = 0; i < arr_size; i++)
printf("%d ", arr[i]);
}

int main()
{
int i;
int arr[4] = {10, 20 ,30, 40};
fun(arr);
return 0;
}

A 10 20 30 40
B Machine Dependent
C 10 20
D Northing

ANS :- B

Question 10
The reason for using pointers in a Cprogram is
A Pointers allow different functions to share and modify their local
variables.
B To pass large structures so that complete copy of the structure can
be avoided.
C Pointers enable complex �linked" data structures like linked lists
and binary trees.
D All of the above

ANS :- D
Question 11
#include<stdio.h>
void f(int *p, int *q)
{
p = q;
*p = 2;
}
int i = 0, j = 1;
int main()
{
f(&i, &j);
printf("%d %d n", i, j);
getchar();
return 0;
}

A 2 2
B 2 1
C 0 1
D 0 2

ANS :- D
Question 12
Consider this C code to swap two integers and these five statements after it:
void swap(int *px, int *py)
{
*px = *px - *py;
*py = *px + *py;
*px = *py - *px;
}

S1: will generate a compilation error


S2: may generate a segmentation fault at runtime depending on the arguments
passed
S3: correctly implements the swap procedure for all input pointers referring
to integers stored in memory
locations accessible to the process
S4: implements the swap procedure correctly for some but not all valid input
pointers
S5: may add or subtract integers and pointers.

A S1
B S2 and S3
C S2 and S4
D S2 and S5

ANS :- C
Question 13
int f(int x, int *py, int **ppz)
{
int y, z;
**ppz += 1;
z = **ppz;
*py += 2;
y = *py;
x += 3;
return x + y + z;
}

void main()
{
int c, *b, **a;
c = 4;
b = &c;
a = &b;
printf("%d ", f(c, b, a));
return 0;
}

A 18
B 19
C 21
D 22

ANS :- B
Question 14
Predict the output of following program
#include<stdio.h>
int main()
{
int a = 12;
void *ptr = (int *)&a;
printf("%d", *ptr);
getchar();
return 0;
}

A 12
B Compiler Error
C Runt Time Error
D 0

ANS :- B

Question 15
#include<stdio.h>
void swap (char *x, char *y)
{
char *t = x;
x = y;
y = t;
}

int main()
{
char *x = "geeksquiz";
char *y = "geeksforgeeks";
char *t;
swap(x, y);
printf("(%s, %s)", x, y);
t = x;
x = y;
y = t;
printf("n(%s, %s)", x, y);
return 0;
}

A
(geeksquiz, geeksforgeeks)
(geeksforgeeks, geeksquiz)
B
(geeksforgeeks, geeksquiz)
(geeksquiz, geeksforgeeks)
C
(geeksquiz, geeksforgeeks)
(geeksquiz, geeksforgeeks)
D
(geeksforgeeks, geeksquiz)
(geeksforgeeks, geeksquiz)

ANS :- A

Question 16
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
++*p;
p += 2;
printf("%d", *p);
return 0;
}

A 2
B 3
C 4
D Compiler Error

ANS :- B

Question 17
What does the following program print?
#include
void f(int *p, int *q)
{
p = q;
*p = 2;
}
int i = 0, j = 1;
int main()
{
f(&i, &j);
printf("%d %d n", i, j);
getchar();
return 0;
}

A 2 2
B 2 1
C 0 1
D 0 2

ANS :- D

Question 18
#include <stdio.h>
void f(char**);
int main()
{
char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" };
f(argv);
return 0;
}
void f(char **p)
{
char *t;
t = (p += sizeof(int))[-1];
printf("%sn", t);
}

A ab
B cd
C ef
D gh

ANS:- D

Question 19
What does the following C-statement declare? [1 mark]
int ( * f) (int * ) ;

A A function that takes an integer pointer as argument and returns an


integer.
B A function that takes an integer as argument and returns an integer
pointer.
C A pointer to a function that takes an integer pointer as argument and
returns an integer.
D A function that takes an integer pointer as argument and returns a
function pointer

ANS :- C

Question 20
Consider the C program shown below.
#include <stdio.h>
#define print(x) printf("%d ", x)
int x;
void Q(int z)
{
z += x;
print(z);
}
void P(int *y)
{
int x = *y + 2;
Q(x);
*y = x - 1;
print(x);
}
main(void)
{
x = 5;
P(&x);
print(x);
}

The output of this program is

A 12 7 6
B 22 12 11
C 14 6 6
D 7 6 6

ANS :- A

Question 21
Suppose that in a C program snippet, followings statements are used.
i) sizeof(int);
ii) sizeof(int*);
iii) sizeof(int**);
Assuming size of pointer is 4 bytes and size of int is also 4 bytes, pick the
most correct answer from the given options.

A Only i) would compile successfully and it would return size as 4.


B i), ii) and iii) would compile successfully and size of each would be
same i.e. 4
C i), ii) and iii) would compile successfully but the size of each
would be different and would be decided at run time.
D ii) and iii) would result in compile error but i) would compile and
result in size as 4.

ANS :- B

Question 22
Assume int is 4 bytes, char is 1 byte and float is 4 bytes. Also, assume that
pointer size is 4 bytes (i.e. typical case)
char *pChar;
int *pInt;
float *pFloat;

sizeof(pChar);
sizeof(pInt);
sizeof(pFloat);

What�s the size returned for each of sizeof() operator?

A 4 4 4
B 1 4 4
C 1 4 8
D None of the above

ANS :- A
Question 23
In the below statement, ptr1 and ptr2 are uninitialized pointers to int i.e.
they are pointing to some random address that
may or may not be valid address.
int* ptr1, ptr2;

A TRUE
B FALSE

ANS :- B

Question 24
Pick the best statement for the following program snippet:
#include <stdio.h>

int main()
{
int var; /*Suppose address of var is 2000 */

void *ptr = &var;


*ptr = 5;
printf("var=%d and *ptr=%d",var,*ptr);

return 0;
}

A It will print �var=5 and *ptr=2000�


B It will print �var=5 and *ptr=5�
C It will print �var=5 and *ptr=XYZ� where XYZ is some random address
D Compile error

ANS :- D

Question 25
Consider the following C program.
#include<stdio.h>
void mystery(int *ptra, int *ptrb)
{
int *temp;
temp = ptrb;
ptrb = ptra;
ptra = temp;
}
int main()
{
int a=2016, b=0, c=4, d=42;
mystery(&a, &b);
if (a < c)
mystery(&c, &a);
mystery(&a, &d);
printf("%dn", a);
}

The output of the program _____________ Note : This question was asked as
Numerical Answer Type.

A 2016
B 0
C 4
D 8

ANS :- A

Question 26
The value printed by the following program is
void f(int* p, int m)
{
m = m + 5;
*p = *p + m;
return;
}
void main()
{
int i=5, j=10;
f(&i, j);
printf("%d", i+j);
}

A 10
B 20
C 30
D 40

ANS :- C
Question 27
Consider the C program below. What does it print?
# include <stdio.h>
# define swapl (a, b) tmp = a; a = b; b = tmp
void swap2 ( int a, int b)
{
int tmp;
tmp = a; a = b; b = tmp;
}
void swap3 (int*a, int*b)
{
int tmp;
tmp = *a; *a = *b; *b = tmp;
}
int main ()
{
int num1 = 5, num2 = 4, tmp;
if (num1 < num2) {swap1 (num1, num2);}
if (num1 < num2) {swap2 (num1 + 1, num2);}
if (num1 >= num2) {swap3 (&num1, &num2);}
printf ("%d, %d", num1, num2);
}
/* Add code here. Remove these lines if not writing code */
A 5, 5
B 5, 4
C 4, 5
D 4, 4

ANS :-C

Question 28
What will be the output produced by the following C code:
int main()
{
int array[5][5];
printf("%d",( (array == *array) && (*array == array[0]) ));
return 0;
}
A 1
B 0
C 2
D -1

ANS :- A

Question 29
Consider the following C code
int main()
{
int a = 300;
char *b = (char *)&a;
*++b = 2;
printf("%d ",a);
return 0;
}

Consider the size of int as two bytes and size of char as one byte. Predict
the output of the following code .
` Assume that the machine is little-endian.

A 556
B 300
C Runtime Error
D Compile Time Error

ANS:- A

Question 30
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 the printxy(1,1) is

A 0,0
B 0,1
C 1,0
D 1,1

ANS :- C
Question 31
Consider the following snippet of a C program. Assume that swap(&x, &y)
exchanges the contents of x and y.
int main()
{
int array[] = {3, 5, 1, 4, 6, 2};
int done = 0;
int i;

while (done == 0)
{
done = 1;
for (i = 0; i <= 4; i++)
{
if (array[i] < array[i+1])
{
swap(&array[i], &array[i+1]);
done = 0;
}
}
for (i = 5; i >= 1; i--)
{
if (array[i] > array[i-1])
{
swap(&array[i], &array[i-1]);
done = 0;
}
}
}

printf("%d", array[3]);
}
The output of the program is _____. Note: This question appeared as Numerical
Answer Type.

A 1
B 2
C 3
D 4

ANS :- C

Question 32
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

ANS :-D

Question 33
�ptrdata� is a pointer to a data type. The expression *ptrdata++ is evaluated
as (in C++) :

A *(ptrdata++)
B (*ptrdata)++
C *(ptrdata)++
D Depends on compiler

ANS :- A

Question 34
Consider the following table
A. Activation record p. Linking loader
B. Location counter q. Garbage collection
C. Reference counts r. Subroutine call
D. Address relocation s. Assembler
Matching A, B, C, D in the same order gives :

A p, q, r, s
B q, r, s, p
C r, s, q, p
D r, s, p, q

ANS :- C

Question 35
What does the following C-statement declare? int (*f) (int*);

A A function that takes an integer pointer as argument and returns an


integer
B A function that takes an integer as argument and returns an integer
pointer
C A pointer to a function that takes an integer pointer as argument and
returns an integer
D A function that takes an integer pointer as argument and returns a
function pointer

ANS :- C

Question 36
What is the output of this C code?
#include
void main()
{
int k=5;
int *p=&k;
int **m=&p;
printf("%d %d %d",k,*p,**m);
}

A 5 5 5
B 5 5 junk
C 5 junk junk
D Compile time error

ANS :-A

Question 37
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

ANS :- D

Question 38
The following �C� statement : int * f [ ] ( ); declares:

A A function returning a pointer to an array of integers.


B Array of functions returning pointers to integers.
C A function returning an array of pointers to integers.
D An illegal statement.

ANS :- B

Question 39
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

ANS :- A

Question 40
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

ANS :-A

Question 41
What does the following expression means ? char *(*(* a[N]) ( )) ( );

A a pointer to a function returning array of n pointers to function


returning character pointers.
B a function return array of N pointers to functions returning pointers
to characters
C an array of n pointers to function returning pointers to characters
D an array of n pointers to function returning pointers to functions
returning pointers to characters.

ANS :- A

Question 42
What will be the output of following C program?
main()
{
char g[] = "geeksforgeeks";
printf("%s", g + g[6] - g[8]);
}
A geeks
B rgeeks
C geeksforgeeks
D forgeeks

ANS :- A

You might also like