L2 Pointers - MCQs

Answer the following multiple choice questions to test your understanding of the Pointers lecture

1. C++ uses ____ as the address operator
a) asterisk (*)
b) dot (.)
c) ampersand (&)
d) the hash (#)

2. If a variable int i = 7 is declared and is stored at memory location 70001 then the value of &i is ____
a) 7
b) 70001
c) 0
d) none of the above

For questions 3, 4 and 5 use the code given below

int i = 7;
int *iPtr = &i;
cout <<  iPtr;
cout << *iPtr;

3. Which of the following statements can change the value of i to 7000
a) *i = 7000;
b) 7000 = i;
c) i = &7000;
d) *iPtr = 7000;

4. cout « iPtr; will print ___
a) some integer value which is address of i
b) some hexadecimal value which is address of i
c) 7
d) none of the above

5. cout « *iPtr; will print ___
a) some integer value which is address of i
b) some hexadecimal value which is address of i
c) 7
d) none of the above

6. A pointer refers to __
a) location of the object in the program on disk
b) location of the object in memory
c) value of CPU registers
d) none of the above

7. Statically declared program variables are stored on the __
a) heap
b) stack
c) linked list
d) memory

8. Dynamically allocated objects are stored on the ___
a) heap
b) stack
c) linked list
d) memory

9. The first function on the stack is always the _____
a) the last function
b) cout
c) system("pause")
d) the main()

10. Which one of the following is true about the stack and the heap?
a) As the program executes stack grows downwards and heap grows upwards
b) As the program executes both the stack and heap grow upwards
c) As the program executes both the stack and heap grow downwards
d) As the program executes stack grows uppwards and heap grows downwards

11. Assume that you have class Time with a constructor that takes three integer arguments and a method printTime()
Identify which line is wrong in the code given below:

Time t(10,10,10);
Time *t1 = new Time(12,0,0);
t.printTime();
t1.printTime();

a) first line because we do not use new to create the Time t object
b) second line because we use new to create a Time t1 object
c) third line because we use the . (dot) operator to access the printTime() method on a Time object
d) fourth line because we the . (dot) operator to access the printTime() method on a pointer to a Time object

12. Which one of the following is true about the above code given in question 11?
a) Object t is created on the heap
b) Object pointed by t1 is created on the stack
c) Both the objects are created on the stack
d) t is created on the stack and object referred by t1 is created on the heap

13. What will be the output of the following program?

int a = 7;
int b = 17;
int *c = &b;
*c = 7;
cout << "a=" << a << " b= " << b << endl;

a) a=7 b=17
b) a=7 b=7
c) a=17 b=17
d) a=17 b=7

14. What will be the output of the following program?

int a = 7;
int b = 17;
int *c = &b;
a = *c;
*c = *c + 1;
cout << "a=" << a << " b= " << b << endl;

a) a=7 b=18
b) a=7 b=7
c) a=17 b=18
d) a=18 b=7

15. Which of the following is true (select more than one) about the following program?

    float a = 0.3; float b = 1.17;
    float *fptr1 = &a; float *fptr2 = &b;
 
    fptr1 = fptr2;
 
    cout << "a=" << *fptr1;
    cout << " b=" << *fptr2 << endl;
 
    cout << "a=" << a;
    cout << " b=" << b << endl;
 
    cout << "fptr1=" << (unsigned long) fptr1 << endl;
    cout << "fptr2=" << (unsigned long) fptr2 << endl;

a) the statements printing a and b will print different values
b) both the pointers point to different locations in memory
c) addresses printed by the last two lines are the same
d) all the above

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License