Important Introduction to Programming Short Questions
Chapter: 16
Question 1:
How can command-line arguments be accessed in a C program?
Answer:
Command-line arguments can be accessed in C by using the argc and argv parameters in the main() function. argc gives the number of arguments, and argv is an array of strings containing the arguments passed from the command line.
Chapter: 16
Question 2:
What does the argc variable represent in the main() function?
Answer:
The argc variable represents the number of command-line arguments passed to the program. It counts the program name as the first argument.
Chapter: 16
Question 3:
How are command-line arguments passed to a program in C?
Answer:
Command-line arguments are passed to a C program by writing them after the program's name in the command line, e.g., program-name argument1 argument2. These arguments are accessed using argc and argv in the main() function.
Chapter: 16
Question 4:
How does the rand() function work in shuffling a deck of cards in C?
Answer:
The rand() function generates random numbers. In the card shuffling algorithm, the random number is divided by 4 for suits and 13 for card values to ensure that the random values fall within the valid range of suits and card values.
Chapter: 16
Question 5:
What is double dereferencing and how is it used in arrays?
Answer:
Double dereferencing involves using the * operator twice to access a value in a two-dimensional array. For example, **multi refers to the value at the first element of the first row of the array.
Chapter: 16
Question 6:
How do you handle a variable number of command-line arguments in C?
Answer:
You handle a variable number of command-line arguments in C using the argc and argv parameters. argc provides the number of arguments, while argv is an array of strings representing each argument. The arguments are accessed through argv[index].
Chapter: 16
Question 7:
What is the significance of the atoi() function in command-line arguments?
Answer:
The atoi() function is used to convert a command-line argument from a string to an integer. This is necessary because command-line arguments are passed as strings by default.
Chapter: 16
Question 8:
What is the role of the argv parameter in the main() function?
Answer:
The argv parameter is an array of strings containing the command-line arguments passed to the program. The first element (argv[0]) is the program name, and the following elements are the arguments.
Chapter: 16
Question 9:
How is space wasted when using a two-dimensional array to store strings?
Answer:
In a two-dimensional array, all rows must have the same number of columns. If the strings have varying lengths, the shorter strings will leave unused space in the rows, leading to wasted memory.
Chapter: 16
Question 10:
Why is the const keyword used in the declaration of an array of pointers?
Answer:
The const keyword is used to ensure that the values of the array (like strings in const char *face[]) are not modified. This is useful when the array holds constant values that should remain unchanged throughout the program.