Lecture 1: Introduction to Pointers #
What will be the output ?
int *ptr = 0;
int a = 10;
ptr = a;
cout << *ptr << endl;
Options:
a. 10
b. 0
c. Error
d. None
Correct Answer
Error
Cannot assign int value to an int*
What will be the output ?
int a = 7;
int b = 17;
int *c = &b;
a = *c;
*c = *c + 1;
cout << a << " " << b << endl;
Options
a. 18 18
b. 7 18
c. 17 17
d. 17 18
Correct Answer
d. 17 18
Make a variable map.
What will be the output ?
float f = 10.5;
float p = 2.5;
float* ptr = &f;
(*ptr)++;
*ptr = p;
cout << *ptr << " " << f << " " << p;
Options
a. 2.5 10.5 2.5
b. 2.5 11.5 2.5
c. 2.5 2.5 2.5
d. 11.5 11.5 2.5
Correct Answer
c.2.5 2.5 2.5
Make a variable map.
Lecture 2: Pointer Arithmetic #
What will be the output ?
int a = 7;
int *c = &a;
c = c + 1;
cout << a << " " << *c << endl;
Options
a. Garbage_value 7
b. 7 Garbage_value
c. 8 8
d. 7 7
Correct Answer
b. 7 Garbage_value
Obvious.
Assume memory address of variable ‘a’ is : 400 (and an integer takes 4 bytes), what will be the output ?
int a = 7;
int *c = &a;
c = c + 3;
cout << c << endl;
Fill in the answer
____
Correct Answer
412
400 + 3*4 = 412
Assume memory address of variable ‘a’ is : 200 and a double variable is of size 8 bytes, what will be the output ?
double a = 10.54;
double *d = &a;
d = d + 1;
cout << d << endl;
Fill in the answer
____
Correct Answer
208
200 + 1*8 = 208
Lecture 3: Arrays and Pointers #
Assume integer takes 4 bytes and integer pointer 8 bytes.
What is the output
int a[5];
int *c;
cout << sizeof(a) << " " << sizeof(c);
Options
a. 8 8
b. 5 8
c. 20 8
d. 20 4
Correct Answer
c. 20 8
sizeof(a) = size of the array, sizeof(c) = 8 bytes(pointer size)
Fill the output
int a[] = {1, 2, 3, 4};
cout << *(a) << " " << *(a+1);
Answer
____
Correct Answer
c. 1 2
*a = first element of the array,*(a+1) is the second element
Assume that address of 0th index of array ‘a’ is : 200. What is the output ?
int a[6] = {1, 2, 3};
cout << a << " " << &a;
Answer
____
Correct Answer
200 200
value of 'a' and address of 'a' is the same in case of array names.
Assume that address of 0th index of array ‘a’ is : 200. What is the output ?
int a[6] = {1, 2, 3};
cout << (a+2);
Answer
____
Correct Answer
208
jump = 4, 200 + 2*(4) = 208. Jump is decided based on data type.
Assume that address of 0th index of array ‘a’ is : 200. What is the output ?
int a[6] = {1, 2, 3};
int *b = a;
cout << b[2];
Options
a. Error
b. 3
c. 1
d. 200
e. 212
Correct Answer
b. 3
b[2] = 3rd element
Assume that address of 0th index of array ‘a’ is : 200. What is the output ?
int a[] = {1, 2, 3, 4, 5};
cout << *(a) << " " << *(a + 4);
Options
a. Error
b. 200 216
c. 1 5
d. None of these
Correct Answer
b. 1 5
1st and 5th element
Assume that address of 0th index of array ‘a’ is : 200. What is the output ?
int a[] = {1, 2, 3, 4};
int *p = a++;
cout << *p << endl;
Options
a. 1
b. 2
c. Garbage Value
d. Error
Correct Answer
d. Error
int *p = a is okay, but a = a+1 is invalid, coz it intends to make a change in the symbol table.