Important Introduction to Programming Short Questions
Chapter: 12
Question 1:
What is a character array in C?
Answer:
A character array is a sequence of characters, similar to an integer array but used to store strings. It is declared like an integer array, for example, char name[100]; to store a name.
Chapter: 12
Question 2:
Why can't a simple variable be used to store a string?
Answer:
A simple variable can only store a single character, while a string is a sequence of characters. Hence, a character array is needed to store strings.
Chapter: 12
Question 3:
How do you declare a character array in C?
Answer:
A character array is declared similarly to an integer array. For example, char name[100]; declares an array that can hold up to 100 characters.
Chapter: 12
Question 4:
What happens to the unused spaces in a character array after a string is stored?
Answer:
The unused spaces remain in the array but are not part of the string. The string is terminated by a null character (\0), which marks the end of the string.
Chapter: 12
Question 5:
What is the role of the null character in a character array?
Answer:
The null character (\0) is used to mark the end of a string in a character array, helping the program determine where the string ends.
Chapter: 12
Question 6:
How do you input a string into a character array using cin?
Answer:
You can input a string using cin >> name;. The cin stream reads the entire string until the user presses Enter and stores it in the array, adding a null character at the end.
Chapter: 12
Question 7:
How does cin handle strings differently from single characters?
Answer:
cin reads the entire string at once when using a character array, whereas it reads only a single character when using a single char variable.
Chapter: 12
Question 8:
What is the consequence of storing a string larger than the character array size?
Answer:
Storing a string larger than the array size without a null character can cause logical errors, as the program won't know where the string ends in memory.
Chapter: 12
Question 9:
How do you initialize a character array with a string?
Answer:
A character array can be initialized with a string using either a list of characters in single quotes or a string in double quotes, e.g., char name[100] = "imran";.
Chapter: 12
Question 10:
What is the advantage of initializing a character array with a string in double quotes?
Answer:
Initializing with double quotes allows the compiler to automatically set the size of the array, including space for the null character at the end.