Nothing Special   »   [go: up one dir, main page]

Questions-Fundamentals of Data Structures-Pointers-MCQ-Set 1-20240624-0002

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

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) Compile Time Error
D) Run Time Error
ANSWER: B
Predict the output of the following code
snippet#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", i, j); return 0; }
A) 2 2
B) 2 1
C) 0 1
D) 0 2
ANSWER: D
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 errorS2: may generate a segmentation
fault at runtime depending on the arguments passedS3: correctly implements the swap
procedure for all input pointers referring to integers stored in memory locations
accessible to the processS4: implements the swap procedure correctly for some but
not all valid input pointersS5: may add or subtract integers and pointers.
A) S1
B) S2 and S3
C) S2 and S4
D) S2 and S5
ANSWER: B
What is the output of the following code snippet?
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
ANSWER: B
Predict the output of following program#include<stdio.h>int main()
{ int a = 12; void *ptr = (int *)&a; printf("%d", *ptr); return 0;}
A) 12
B) Compiler Error
C) Runt Time Error
D) 0
ANSWER: B
What will be the output of the following code snippet?#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) Compile Time Error
ANSWER: B
What will be the output of the following code snippet?
#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
ANSWER: D
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
ANSWER: C
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; pr
int(x);}int 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
ANSWER: A
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.
ANSWER: B
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 these
ANSWER: A
What will be the 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) Compile Time Error
D) Run Time Error
ANSWER: B
What is the output of following program?#include <stdio.h>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 = 0x = 5*ptr = 5x = 6*ptr = 6
B) x = garbage value*ptr = 0x = garbage value*ptr = 5x = garbage value*ptr = 6
C) x = 0*ptr = 0x = 5*ptr = 5x = garbage value*ptr = garbage value
D) x = 0*ptr = 0x = 0*ptr = 0x = 0*ptr = 0
ANSWER: A
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 a
rrc[] = %d ", sizeof(arrc)); printf("sizeof ptrc = %d ", sizeof(ptrc)); retu
rn 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
ANSWER: D
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
ANSWER: A
Predict the output of the following program.#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
ANSWER: A
#include<stdio.h> int main() { int a; char *x; x = (char *) &a; a = 512
; x[0] = 1; x[1] = 2; printf("%d",a); return 0; }What is the output
of above program?
A) Machine dependent
B) 513
C) 258
D) Compiler Error
ANSWER: B
Predict the following output.#include<stdio.h>int main()
{ char *ptr = "LearningSaga Technologies"; printf("%c", *&*&*ptr); return 0;}
A) Compiler Error
B) Garbage Value
C) Runtime Error
D) L
ANSWER: D
Predict the output of the following program.#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) Nothing
ANSWER: B

You might also like