Important Introduction to Programming Short Questions
Chapter: 15
Question 1:
What is the result of subtracting two pointers pointing to elements of the same array?
Answer:
Subtracting two pointers pointing to elements of the same array gives the number of elements between them.
Chapter: 15
Question 2:
Can two pointers be added in C?
Answer:
No, adding two pointers in C is not allowed and will result in a compiler error.
Chapter: 15
Question 3:
What is the purpose of the null character \0 in a character array?
Answer:
The null character \0 is used to indicate the end of a character string in C. It is essential for functions like cout to properly print the string.
Chapter: 15
Question 4:
How does incrementing a pointer to an integer affect its memory address?
Answer:
Incrementing a pointer to an integer increases its address by the size of an integer, typically 4 bytes.
Chapter: 15
Question 5:
What is the effect of the expression (*yptr)++ in C?
Answer:
The expression (*yptr)++ increments the value at the memory location to which yptr points.
Chapter: 15
Question 6:
What happens if a pointer is incremented beyond the bounds of an array?
Answer:
If a pointer is incremented beyond the bounds of an array, it points to a memory location that may contain garbage data, potentially causing undefined behavior.
Chapter: 15
Question 7:
How can you initialize a character array with a string in C?
Answer:
You can initialize a character array with a string in C using double quotes, e.g., char name[20] = "Amir";. The compiler automatically appends a null character at the end.
Chapter: 15
Question 8:
What does the statement yptr++ do when yptr is a pointer to an array?
Answer:
The statement yptr++ moves the pointer yptr to the next element in the array, incrementing its address by the size of the element type.
Chapter: 15
Question 9:
How do you print the memory address to which a pointer points in C?
Answer:
You can print the memory address to which a pointer points using the cout statement, e.g., cout << yptr;.
Chapter: 15
Question 10:
What is the result of comparing two pointers using relational operators in C?
Answer:
Comparing two pointers using relational operators determines their relative positions in memory. For example, yptr1 < yptr2 checks if yptr1 points to a lower memory address than yptr2.