C++ Programming Questions and Answers – Command Line Arguments

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Command Line Arguments”.

1. What are command line arguments?
a) Arguments passed to main() function
b) Arguments passed to any function
c) Arguments passed to class functions
d) Arguments passed to structure functions
View Answer

Answer: a
Explanation: Command line arguments are the arguments that passed to the main function when the program is starting its execution.

2. To use command line arguments in C++, how many parameters are passed to the main function?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: b
Explanation: 2 arguments are needed to be passed to main() function while using command line arguments. The first one represents a number of strings in the argument list and the second list represents the list of string arguments.

3. What is the signature of math in function using command line arguments?
a) int main(int argc, char const *argv[]);
b) int main(int argc, char const **argv);
c) int main(int argc, char **argv);
d) all of the mentioned
View Answer

Answer: d
Explanation: Any of the above signature can be used while using command line arguments in C++ programs.
advertisement
advertisement

4. What does the first parameter of the main function represent?
a) Number of command line arguments
b) List of command line arguments
c) Dictionary of command line arguments
d) Stack of command line arguments
View Answer

Answer: a
Explanation: The first argument of the main() function represents the number of command line arguments that are passed.

5. What does the second parameter of the main function represent?
a) Number of command line arguments
b) List of command line arguments
c) Dictionary of command line arguments
d) Stack of command line arguments
View Answer

Answer: b
Explanation: The second argument of the main() function represents the list of command line arguments that are passed.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. Which of the following is correct about the first parameter of the main function?
a) First argument is of int type
b) Stores the count of command line arguments
c) First argument is non-negative
d) All of the mentioned
View Answer

Answer: d
Explanation: All of the statements about the first parameter is correct. The first parameter is of non-negative integer type and stores the count of command line arguments.

7. Which of the following is correct about the second parameter of the main function?
a) Second parameter is an array of character pointers
b) First string of the list is the name of the program’s output fle
c) The string in the list are separated by space in the terminal
d) All of the mentioned
View Answer

Answer: d
Explanation: All of the statements about the second parameter is correct. It is the collection of character pointers which of which the first represents the name of the program file.
advertisement

8. Which of the following gives the name of the program if the second parameter to the main fucntion is char **argv?
a) argv[3]
b) argv[1]
c) argv[0]
d) argv[2]
View Answer

Answer: c
Explanation: The first string in the list of command line arguments represents the name of the program which can be accessed by using argv[0].

9. What will be the output of the following C++ code if the following arguments are executed on terminal?

advertisement
================program.cpp================
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
	for(int i=0;i<argc;i++)
		cout<<argv[i]<<"\n";
}
=======================================
================commands===============
$ g++ program.cpp -o output
$ ./output Hello World
=======================================

a)

./output
Hello
World

b)

Hello
World

c)

program.cpp
Hello

d)

program.cpp
Hello
World
View Answer
Answer: a
Explanation: In this program we are trying to print all the command line arguments. Hence as the list contains [“./output”, “Hello”, “World”] so the output is as shown. The first string is not program.cpp because the first string represents the name of the output file of the program.
 
 

10. Which character is used to separate different arguments?
a) #
b) $
c) space
d) |
View Answer

Answer: c
Explanation: Command line arguments are separated by space. So if you write
./output This is a single parameter
then they will interpreted as 5 command line arguments as shown : [“./output”, “This”, “is”, “single”, “parameter”].

11. Which is the correct way of handling arguments with spaces?
a) Use single quotes
b) Either single or double quotes
c) Use double quotes
d) There is no way of handling arguments with space
View Answer

Answer: b
Explanation: One can use either single or double quotes to handle command line argument with spaces in-between. For example, ./output “Hello World” has 2 command line argument “./output” and “Hello World”.

12. Which of the following is correct to interpret Hello World as a single argument?

1) $ ./output 'Hello World'
2) $ ./output Hello World

a) Only 1
b) Only 2
c) Both 1 and 2
d) Neither 1 nor 2
View Answer

Answer: c
Explanation: To interpret space separated words as single argument one can use single or double quotes.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.