3.3 - Command Line Arguments.


Back Button Home

3.1.1 - Overview of command Line Arguments


1. What does argc represent in a C program that uses command-line arguments?
  • (A) Argument value
  • (B) Array of strings
  • (C) Argument count
  • (D) Array of integers
Correct Answer: (C) Argument count
2. What is stored in argv[0] when a C program is executed with command-line arguments?
  • (A) The first argument passed to the program
  • (B) The second argument passed to the program
  • (C) The number of arguments passed
  • (D) The program’s name
Correct Answer: (D) The program’s name
3. How would you compile and run a C program that uses command-line arguments?
  • (A) gcc program.c -run program arg1 arg2
  • (B) gcc program.c -output program arg1 arg2
  • (C) gcc program.c -o program; ./program arg1 arg2
  • (D) gcc program.c -execute program arg1 arg2
Correct Answer: (C) gcc program.c -o program; ./program arg1 arg2
4. If a program is executed as ./program arg1 arg2 arg3, what will be the value of argc?
  • (A) 1
  • (B) 3
  • (C) 4
  • (D) 5
Correct Answer: (C) 4
5. In the command-line argument example provided, what does the atoi function do?
  • (A) Converts a string to a character
  • (B) Converts a string to an integer
  • (C) Converts an integer to a string
  • (D) Converts a string to an array
Correct Answer: (B) Converts a string to an integer
6. Which of the following commands is used to create a new directory in the command line?
  • (A) DIR
  • (B) MD
  • (C) CD
  • (D) CLS
Correct Answer: (B) MD
7. In the context of command-line arguments, which of the following is correct for char *argv[]?
  • (A) It is a string variable that holds the number of arguments.
  • (B) It is an integer that counts the number of arguments.
  • (C) It is an array of pointers to strings representing arguments.
  • (D) It is an array of integers representing arguments.
Correct Answer: (C) It is an array of pointers to strings representing arguments.
8. What will be the output of the following program if executed as ./program 10 20?

            #include <stdio.h>
            #include <stdlib.h>
            int main(int argc, char *argv[]) {
                int num1 = atoi(argv[1]);
                int num2 = atoi(argv[2]);
                int sum = num1 + num2;
                printf("Sum: %d\n", sum);
                return 0;
            }
                
  • (A) Sum: 1020
  • (B) Sum: 30
  • (C) Sum: 15
  • (D) Sum: 0
Correct Answer: (B) Sum: 30
9. Which of the following commands is used to clear the terminal screen?
  • (A) COPY
  • (B) CLS
  • (C) CD
  • (D) MD
Correct Answer: (B) CLS
10. In a program using command-line arguments, what does the first argument (argv[0]) always contain?
  • (A) The first user-provided argument
  • (B) The program’s name
  • (C) The second user-provided argument
  • (D) The total number of arguments
Correct Answer: (B) The program’s name