1, #include <iostream>
using namespace std;
int main()
{
const int i = 20;
const int* const ptr = &i;
(*ptr)++;
int j = 15;
ptr = &j;
cout << i;
return 0;
}
Options:
a. 20
b. 21
c. 15
d. Compile error
using namespace std;
int main()
{
const int i = 20;
const int* const ptr = &i;
(*ptr)++;
int j = 15;
ptr = &j;
cout << i;
return 0;
}
Options:
a. 20
b. 21
c. 15
d. Compile error
3. What will be the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int num[5];
int* p;
p = num;
*p = 10;
p++;
*p = 20;
p = &num[2];
*p = 30;
p = num + 3;
*p = 40;
p = num;
*(p + 4) = 50;
for (int i = 0; i < 5; i++)
cout << num[i] << ", ";
return 0;
}
Options:
a. 10, 20, 30, 40, 50
b. 10, 20, 30, 40, 50,
c. compile error
d. runtime error
#include <iostream>
using namespace std;
int main()
{
int num[5];
int* p;
p = num;
*p = 10;
p++;
*p = 20;
p = &num[2];
*p = 30;
p = num + 3;
*p = 40;
p = num;
*(p + 4) = 50;
for (int i = 0; i < 5; i++)
cout << num[i] << ", ";
return 0;
}
Options:
a. 10, 20, 30, 40, 50
b. 10, 20, 30, 40, 50,
c. compile error
d. runtime error
4. What will be the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 4, 5, 6, 7 };
int* p = (arr + 1);
cout << *arr + 10;
return 0;
}
Options:
a. 12
b. 15
c. 14
d. error
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 4, 5, 6, 7 };
int* p = (arr + 1);
cout << *arr + 10;
return 0;
}
Options:
a. 12
b. 15
c. 14
d. error
5 What would be printed from the following C++ program?
#include <iostream>
using namespace std;
int main()
{
int x[5] = { 1, 2, 3, 4, 5 };
// p points to array x
int* p = x;
int i;
// exchange values using pointer
for (i = 0; i < 2; i++) {
int temp = *(p + i);
*(p + i) = *(p + 4 - i);
*(p + 4 - i) = temp;
}
// output the array x
for (i = 0; i < 5; i++)
cout << x[i] << " ";
return 0;
}
a) 5 4 3 2 1
b) 1 2 3 4 5
c)Address of the elements
d) Canβt say
#include <iostream>
using namespace std;
int main()
{
int x[5] = { 1, 2, 3, 4, 5 };
// p points to array x
int* p = x;
int i;
// exchange values using pointer
for (i = 0; i < 2; i++) {
int temp = *(p + i);
*(p + i) = *(p + 4 - i);
*(p + 4 - i) = temp;
}
// output the array x
for (i = 0; i < 5; i++)
cout << x[i] << " ";
return 0;
}
a) 5 4 3 2 1
b) 1 2 3 4 5
c)Address of the elements
d) Canβt say