pointer array

Download Pointer Array

Post on 17-Jan-2016

231 views

Category:

Documents

0 download

Embed Size (px)

DESCRIPTION

Pointer and Array in C

TRANSCRIPT

C Programming Questions and Answers Pointers and Addresses

C Programming Questions and Answers Pointers and Addresses

1. What is the output of this C code?

1. #include 2. int main()3. {4. char *p = NULL;5. char *q = 0;6. if (p)7. printf(" p ");8. else9. printf("nullp");10. if (q)11. printf("q\n");12. else13. printf(" nullq\n");14. }a) nullp nullqb) Depends on the compilerc) x nullq where x can be p or nullp depending on the value of NULLd) p qView AnswerAnswer:a

2. What is the output of this C code?

1. #include 2. int main()3. {4. int i = 10;5. void *p = &i;6. printf("%d\n", (int)*p);7. return 0;8. }a) Compile time errorb) Segmentation fault/runtime crashc) 10d) Undefined behaviourView AnswerAnswer:a

3. What is the output of this C code?

1. #include 2. int main()3. {4. int i = 10;5. void *p = &i;6. printf("%f\n", *(float*)p);7. return 0;8. }a) Compile time errorb) Undefined behaviourc) 10d) 0.000000View AnswerAnswer:d

4. What is the output of this C code?

1. #include 2. int *f();3. int main()4. {5. int *p = f();6. printf("%d\n", *p);7. }8. int *f()9. {10. int *j = (int*)malloc(sizeof(int));11. *j = 10;12. return j;13. }a) 10b) Compile time errorc) Segmentation fault/runtime crash since pointer to local variable is returnedd) Undefined behaviourView AnswerAnswer:a

5. What is the output of this C code?

1. #include 2. int *f();3. int main()4. {5. int *p = f();6. printf("%d\n", *p);7. }8. int *f()9. {10. int j = 10;11. return &j;12. }a) 10b) Compile time errorc) Segmentation fault/runtime crashd) Undefined behaviourView AnswerAnswer:a

6. Comment on the following pointer declaration?int *ptr, p;a) ptr is a pointer to integer, p is notb) ptr and p, both are pointers to integerc) ptr is a pointer to integer, p may or may not bed) ptr and p both are not pointers to integerView AnswerAnswer:a

7. What is the output of this C code?

1. #include 2. int main()3. {4. int *ptr, a = 10;5. ptr = &a;6. *ptr += 1;7. printf("%d,%d/n", *ptr, a);8. }a) 10,10b) 10,11c) 11,10d) 11,11View AnswerAnswer:d

8. Comment on the following?const int *ptr;a) You cannot change the value pointed by ptrb) You cannot change the pointer ptr itselfc) Both (a) and (b)d) You can change the pointer as well as the value pointed by itView AnswerAnswer:a

1. Which is an indirection operator among the following?a) &b) *c) ->d) .View AnswerAnswer:b

2. Which of the following does not initialize ptr to null (assuming variable declaration of a as int a=0;?a) int *ptr = &a;b) int *ptr = &a &a;c) int *ptr = a a;d) All of the mentionedView AnswerAnswer:a

3. What is the output of this C code?

1. #include 2. int x = 0;3. void main()4. {5. int *ptr = &x;6. printf("%p\n", ptr);7. x++;8. printf("%p\n ", ptr);9. }a) Same addressb) Different addressc) Compile time errord) VariesView AnswerAnswer:a

4. What is the output of this C code?

1. #include 2. int x = 0;3. void main()4. {5. int *const ptr = &x;6. printf("%p\n", ptr);7. ptr++;8. printf("%p\n ", ptr);9. }a) 0 1b) Compile time errorc) 0xbfd605e8 0xbfd605ecd) 0xbfd605e8 0xbfd605e8View AnswerAnswer:b

5. What is the output of this C code?

1. #include 2. void main()3. {4. int x = 0;5. int *ptr = &x;6. printf("%p\n", ptr);7. ptr++;8. printf("%p\n ", ptr);9. }a) 0xbfd605e8 0xbfd605ecb) 0xbfd605e8 0cbfd60520c) 0xbfd605e8 0xbfd605e9d) Run time errorView AnswerAnswer:a

6. What is the output of this C code?

1. #include 2. void main()3. {4. int x = 0;5. int *ptr = &5;6. printf("%p\n", ptr);7. }a) 5b) Address of 5c) Nothingd) Compile time errorView AnswerAnswer:d

7. What is the output of this C code?

1. #include 2. void main()3. {4. int x = 0;5. int *ptr = &x;6. printf("%d\n", *ptr);7. }a) Address of xb) Junk valuec) 0d) Run time errorView AnswerAnswer:c

C Programming Questions and Answers Pointers and Function Arguments

1. What is the output of this C code?

1. #include 2. void foo(int*);3. int main()4. {5. int i = 10;6. foo((&i)++);7. }8. void foo(int *p)9. {10. printf("%d\n", *p);11. }a) 10b) Some garbage valuec) Compile time errord) Segmentation fault/code crashView AnswerAnswer:c

2. What is the output of this C code?

1. #include 2. void foo(int*);3. int main()4. {5. int i = 10, *p = &i;6. foo(p++);7. }8. void foo(int *p)9. {10. printf("%d\n", *p);11. }a) 10b) Some garbage valuec) Compile time errord) Segmentation faultView AnswerAnswer:a

3. What is the output of this C code?

1. #include 2. void foo(float *);3. int main()4. {5. int i = 10, *p = &i;6. foo(&i);7. }8. void foo(float *p)9. {10. printf("%f\n", *p);11. }a) 10.000000b) 0.000000c) Compile time errord) Undefined behaviourView AnswerAnswer:b

4. What is the output of this C code?

1. #include 2. int main()3. {4. int i = 97, *p = &i;5. foo(&i);6. printf("%d ", *p);7. }8. void foo(int *p)9. {10. int j = 2;11. p = &j;12. printf("%d ", *p);13. }a) 2 97b) 2 2c) Compile time errord) Segmentation fault/code crashView AnswerAnswer:a

5. What is the output of this C code?

1. #include 2. int main()3. {4. int i = 97, *p = &i;5. foo(&p);6. printf("%d ", *p);7. return 0;8. }9. void foo(int **p)10. {11. int j = 2;12. *p = &j;13. printf("%d ", **p);14. }a) 2 2b) 2 97c) Undefined behaviourd) Segmentation fault/code crashView AnswerAnswer:a

6. What is the output of this C code?

1. #include 2. int main()3. {4. int i = 11;5. int *p = &i;6. foo(&p);7. printf("%d ", *p);8. }9. void foo(int *const *p)10. {11. int j = 10;12. *p = &j;13. printf("%d ", **p);14. }a) Compile time errorb) 10 10c) Undefined behaviourd) 10 11View AnswerAnswer:a

7. What is the output of this C code?

1. #include 2. int main()3. {4. int i = 10;5. int *p = &i;6. foo(&p);7. printf("%d ", *p);8. printf("%d ", *p);9. }10. void foo(int **const p)11. {12. int j = 11;13. *p = &j;14. printf("%d ", **p);15. }a) 11 11 11b) 11 11 Undefined-valuec) Compile time errord) Segmentation fault/code-crashView AnswerAnswer:b

8. What is the output of the code below?

1. #include 2. int main()3. {4. int i = 10;5. int *const p = &i;6. foo(&p);7. printf("%d\n", *p);8. }9. void foo(int **p)10. {11. int j = 11;12. *p = &j;13. printf("%d\n", **p);14. }a) 11 11b) Undefined behaviourc) Compile time errord) Segmentation fault/code-crashView AnswerAnswer:a

9. Which of the following are correct syntaxes to send an array as a parameter to function:a) func(&array);b) func(array);c) func(*array);d) func(array[size]);View AnswerAnswer:a & b

1. Which of the following can never be sent by call-by-value?a) Variableb) Arrayc) Structuresd) Both (b) and (c)View AnswerAnswer:b

2. Which type of variables can have same name in different function:a) global variablesb) static variablesc) Function argumentsd) Both (b) and (c)View AnswerAnswer:d

3. Arguments that take input by user before running a program are called?a) main function argumentsb) main argumentsc) Command-Line argumentsd) Parameterized argumentsView AnswerAnswer:c

4. The maximum number of arguments that can be passed in a single function are_____________a) 127b) 253c) 361d) No limits in number of argumentsView AnswerAnswer:b

5. What is the output of this C code?

1. #include 2. void m(int *p, int *q)3. {4. int temp = *p; *p = *q; *q = temp;5. }6. void main()7. {8. int a = 6, b = 5;9. m(&a, &b);10. printf("%d %d\n", a, b);11. }a) 5 6b) 6 5c) 5 5d) 6 6View AnswerAnswer:a

6. What is the output of this C code?

1. #include 2. void m(int *p)3. {4. int i = 0;5. for(i = 0;i < 5; i++)6. printf("%d\t", p[i]);7. }8. void main()9. {10. int a[5] = {6, 5, 3};11. m(&a);12. }a) 0 0 0 0 0b) 6 5 3 0 0c) Run time errord) 6 5 3 junk junkView AnswerAnswer:b

7. What is the output of this C code?

1. #include 2. void m(int p, int q)3. {4. int temp = p;5. p = q;6. q = temp;7. }8. void main()9. {10. int a = 6, b = 5;11. m(a, b);12. printf("%d %d\n", a, b);13. }a) 5 6b) 5 5c) 6 5d) 6 6View AnswerAnswer:c

8. What is the output of this C code?

1. #include 2. void m(int p, int q)3. {4. printf("%d %d\n", p, q);5. }6. void main()7. {8. int a = 6, b = 5;9. m

Recommended

View more >