Chapter 3: Pointers
23) Write the function header for a function called exchange that takes two pointers to double-precision numbers x and y as parameters and does not return a value.
ANS: void exchange(double+ x, double+ y)
3) _________ qualifier enables the programmer to inform the compiler that the value of a particular variable should not be modified.
Ans: Const
19) Which statement would be used to declare a 10-element integer array c?
a. array c = int[10]; b. c = int[10]; c. int array c[10]; d. int c[10]; ANS d. int c[10];
18) Which of the following is not a correct way to initialize an array?
a. int n[5]={0, 7, 0, 3, 8, 2}; b. int n[]={0, 7, 0, 3, 8, 2}; c. int n[5]={7}; d. int n[5]={9, 1, 9}; ANS: a. int n[5]={0, 7, 0, 3, 8, 2};
11) Write a C++ program to display square of a number by passing pointer argument to the function.
Ans: #include<iostream> #include<conio.h> using namespace std; int square(int *); void main() { int number = 10; cout<<"The given number is "<<number<<endl; cout<<"The square of the number is "<<square(&number); _getch( ); } int square(int *n) { return +n * +n; }
12) Write a C++ program to determine the datatype size using sizeof operator.
Ans: #include<iostream> #include<conio.h> using namespace std; void main() { int a; char b; short c; double d; float e; long f; cout<<"Size of int variable is :"<<sizeof a<<endl; cout<<"Size of char variable is :"<<sizeof b<<endl; cout<<"Size of short variable is :"<<sizeof c<<endl; cout<<"Size of double variable is :"<<sizeof d<<endl; cout<<"Size of float variable is :"<<sizeof e<<endl; cout<<"Size of long variable is :"<<sizeof f<<endl; getch( ); }
10) Write a C++ program to demonstrate pointer operators & and *.
Ans: #include<iostream> #include<conio.h> using namespace std; void main() { int a; int *aptr; a=10; aptr = &a; cout<<"The value of a is "<<a<<endl; cout<<"The value of +aptr is "<<*aptr<<endl; cout<<"The value of aptr is "<<aptr<<endl; cout<<"The value of &a is "<<&a<<endl; _getch( ); }
13) Write a C++ program to print the one dimensional array elements using pointers.
Ans: #include<iostream> #include<conio.h> using namespace std; void main() { int a[]={1,2,3,4,5}; int *aptr=a; for(int i=0; i<4; i++) cout<<*(a+i)<<endl; _getch(); }
6) Explain the concept of pointers?
Ans: Pointer variables contain memory addresses as their values. A pointer contains the memory address of a variable that, in turn, contains a specific value. The pointer variable which indirectly references a value is often called as indirection.
8) What are the 3 ways to pass arguments to a function?
Ans: The 3 ways to pass argument to a function are: 1. Pass-by-value 2. Pass-by-reference with reference arguments 3. Pass-by-reference with pointer arguments
9) What are the 4 ways to pass a pointer to a function?
Ans: The 4 ways to pass a pointer to a function are: 1. A non-constant pointer to non-constant data 2. A non-constant pointer to constant data 3. A constant pointer to non-constant data 4. A constant pointer to constant data
7) What is address operator? How will you declare and assign a values to the pointer?
Ans: The address operator(&) is a unary operator that returns the memory address of its operand. Declaration: int y = 5; int *yptr; Assigning values: yptr = &y;
5) Referencing a value through a pointer is often called _________.
Ans: indirection
4) _________ operator determine the size of an array in bytes during program compilation.
Ans: sizeof
1) _________ variables contain memory address as their values.
a) Function b) Array c) Pointer d) String Ans: c) Pointer
2) _________ operator returns the memory address of its operand.
a) Increment b) Arithmetic c)Comparison d) Address Ans: d) Address
22) Given that k is an integer array starting at location 2000, kPtr is a pointer to k and each integer is stored in 4 bytes of memory, what location does kPtr + 3 point to?
a. 2003 b. 2006 c. 2012 d. 2024 ANS: c. 2012
15) Pointers may be assigned which of the following values?
a. Any integer values. b. An address. c. nullptr. d. Both (b) and (c). ANS d. Both (b) and (c).
17) Which of the following is not a valid way to pass arguments to a function in C++?
a. By reference with reference arguments. b. By value. c. By reference with pointer arguments. d. By value with pointer arguments. ANS: d. By value with pointer arguments.
14) Pointers cannot be used to:
a. Contain memory addresses. b. Reference values directly. c. Pass an argument by reference. d. Manipulate dynamic data structures. ANS: b. Reference values directly.
21) sizeof:
a. Is a binary operator. b. Returns the total number of elements in an array. c. Usually returns a double. d. Returns the total number of bytes in a variable. ANS: d. Returns the total number of bytes in a variable.
20) To prevent modification of an array's values when you pass the array to a function:
a. The array must be declared static in the function. b. The array parameter can be preceded by the const qualifier. c. A copy of the array must be made inside the function. d. The array must be passed by reference. ANS: b. The array parameter can be preceded by the const qualifier.
16) What does the following statement declare? int *countPtr, count;
a. Two int variables. b. One pointer to an int and one int variable. c. Two pointers to ints. d. The declaration is invalid. ANS: b. One pointer to an int and one int variable.