C Programming Multiple Choice Questions - Command Line Arguments
This section focuses on the "Command Line Arguments" in C programming. These Multiple Choice Questions (MCQ) should be practiced to improve the C programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. The command line arguments are handled using?
A. void()
B. main()
C. header files
D. macros
View Answer
Ans : B
Explanation: The command line arguments are handled using main() function arguments.
2. argc refers to the?
A. number of arguments passed
B. a pointer array
C. Both A and B
D. None of the above
View Answer
Ans : A
Explanation: The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed
3. argv[] is a?
A. a pointer array
B. It points to each argument passed to the program.
C. Both A and B
D. None of the above
View Answer
Ans : C
Explanation: argv[] is a pointer array which points to each argument passed to the program.
4. In linux, argv[0] by command-line argument can be occupied by _________
A. ./a.out
B. ./test
C. ./fun.out.out
D. All of the above
View Answer
Ans : D
Explanation: All the options mentioned (./a.out, ./test, ./fun.out.out) are simply the command without any argument. A command is always stored as argument vector zero i.e., argv[0] always contain the command where as argv[1], argv[2], etc. contains the arguments to the commands, if any.
5. What type of array is generally generated in Command-line argument?
A. Single dimension array
B. 2-Dimensional Square Array
C. Jagged Array
D. 2-Dimensional Rectangular Array
View Answer
Ans : C
Explanation: Jagged Array is generally generated in Command-line argument.
6. What is the index of the last argument in command line arguments?
A. argc – 2
B. argc – 1
C. argc
D. argc + 1
View Answer
Ans : B
Explanation: argc – 1 is the index of the last argument in command line arguments.
7. What will be output for the following code?
#include <stdio.h>
int main(int argc, char *argv[])
{
printf(""%d
"", argc);
return 0;
}
A. 0
B. 1
C. error
D. Depends on the compiler
View Answer
Ans : B
Explanation: The output for the following code : 1
8. What will be output for the following code?
#include <stdio.h>
int main(int argc, char *argv[])
{
printf(""%s
"", argv[argc]);
return 0;
}
A. Segmentation fault/code crash
B. Executable file name
C. Depends on the platform
D. Depends on the compiler
View Answer
Ans : A
Explanation: The output for the followinng code : Segmentation fault/code crash
9. What will be output for the following code?
#include <stdio.h>
int main(int argc, char *argv[])
{
printf(""%s
"", argv[-1]);
return 0;
}
A. ./a.out
B. Segmentation fault/code crash
C. -1
D. Depends on the compiler
View Answer
Ans : B
Explanation: The output for the followinng code : Segmentation fault/code crash
10. What will be output for the following code?
#include <stdio.h>
int main(int argc, char *argv[])
{
while (*argv != NULL)
printf(""%s
"", *(argv++));
return 0;
}
A. ./a.out
B. Segmentation fault/code crash
C. Depends on the platform
D. Depends on the compiler
View Answer
Ans : A
Explanation: The output for the followinng code : ./a.out
Discussion