Sample questions
Question 1Medium
What will be the output of the program
#include<stdio.h>
void fun(int);
int main(int argc)
{
printf("%d ", argc);
fun(argc);
return 0;
}
void fun(int i)
{
if(i!=4)
main(++i);
}
- 1 2 3
- 1 2 3 4
- 2 3 4
- 1
Answer: B. 1 2 3 4
Explanation
Run without arguments, argc = 1. main prints 1, then fun calls main(2), which prints 2, then 3, then 4. When i is 4 the recursion stops, so the output is 1 2 3 4.
Step-by-step solution
- Main is first called with argc = 1, so it prints 1 followed by a space.
- Main calls fun(1), and since 1 is not 4, fun calls main(2).
- main(2) prints 2 and calls fun(2), which calls main(3), which prints 3.
- main(3) calls fun(3), which calls main(4), which prints 4.
- fun(4) does nothing because i is 4, so the output is 1 2 3 4.
Why the other options are wrong
- A. This stops at 3, but the call main(4) still prints 4.
- C. This misses the first call, which prints 1.
- D. This ignores the recursive calls to main that print 2, 3 and 4.
Tip: Trace recursive calls one by one and keep track of the argument at each level.
Question 2Medium
What do the 'c' and 'v' in argc and argv stand for?
- 'c' means argument control, 'v' means argument vector
- 'c' means argument count, 'v' means argument vertex
- 'c' means argument count, 'v' means argument vector
- 'c' means argument configuration, 'v' means argument visibility
Answer: C. 'c' means argument count, 'v' means argument vector
Explanation
argc is the argument count and argv is the argument vector (array of argument strings).
Step-by-step solution
- Argc stands for argument count: the number of command-line arguments, including the program name.
- Argv stands for argument vector: an array of strings holding those arguments.
- So 'c' stands for count and 'v' stands for vector.
- So the answer is: 'c' means argument count, 'v' means argument vector.
Why the other options are wrong
- A. 'c' is count, not control, and 'v' is vector, not control.
- B. 'v' is vector, not vertex.
- D. Configuration and visibility are not the meanings of these letters.
Tip: Argc is the count and argv is the vector of strings.
Question 3Medium
Which of the following is TRUE about argv?
- It is an array of character pointers
- It is a pointer to an array of character pointers
- It is an array of strings
- None of above
Answer: A. It is an array of character pointers
Explanation
char *argv[] is an array of character pointers, each pointing to one argument string.
Step-by-step solution
- Argv is an array of character pointers.
- Each pointer points to a string argument.
- Option A correctly describes argv.
Why the other options are wrong
- B. It's not a pointer to an array; it's the array itself.
- C. It's an array of pointers, not strings directly.
- D. Option A is correct, so this can't be true.
Tip: Remember: argv is an array of char*.
Question 4Medium
What will be the output of the program (sample.c) given below if it is executed from the command line (Turbo C in DOS)?
cmd> sample 1 2 3
/* sample.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
int j;
j = argv[1] + argv[2] + argv[3];
printf("%d", j);
return 0;
}
- 6
- sample 6
- Error
- Garbage value
Answer: C. Error
Explanation
Here argv[1], argv[2] and argv[3] are string type. We have to convert the string to integer type before perform arithmetic operation.
Example:
j = atoi(argv[1]) +atoi(argv[2]) + atoi(argv[3]);
Step-by-step solution
- Argv is an array of char pointers, so argv[1], argv[2] and argv[3] are pointers to the strings '1', '2' and '3'.
- Adding two pointers, as in argv[1] + argv[2], is not a valid operation in C.
- So the expression argv[1] + argv[2] + argv[3] does not compile.
- The result is a compile-time error, so the answer is: Error.
Why the other options are wrong
- A. 6 would need the strings converted to numbers, and C does not do that when adding pointers.
- B. The program never prints the name sample; it only tries to print j, which is never assigned a valid value.
- D. J is assigned the result of an invalid expression, so the program does not produce a garbage value.
Tip: You cannot add pointers in C; convert strings to numbers first, for example with atoi.
Question 5Medium
The maximum combined length of the command-line arguments including the spaces between adjacent arguments is
- 128 characters
- 256 characters
- 67 characters
- It may vary from one operating system to another
Answer: D. It may vary from one operating system to another
Explanation
The limit on command-line length depends on the operating system and shell, so it varies.
Step-by-step solution
- The C standard does not fix a maximum total length for command-line arguments.
- The limit comes from the operating system and the shell, such as the size of the argument buffer.
- Different systems have different limits, so no single fixed number is correct.
- The answer is It may vary from one operating system to another.
Why the other options are wrong
- A. 128 is not a fixed limit in C; it depends on the system.
- B. 256 is not a fixed limit in C; it depends on the system.
- C. 67 is not a fixed limit in C; it depends on the system.
Tip: Limits set by the operating system are implementation-defined, not fixed by C.
Question 6Medium
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample monday tuesday wednesday thursday
/* sample.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
while(--argc>0)
printf("%s ", *++argv);
return 0;
}
- sample monday tuesday wednesday thursday
- monday tuesday wednesday thursday
- monday tuesday thursday
- tuesday
Answer: B. monday tuesday wednesday thursday
Explanation
argc is 5. The loop runs 4 times, each time moving argv forward and printing the next argument, so it prints monday tuesday wednesday thursday (the program name is skipped).
Step-by-step solution
- The loop checks
--argc > 0 first, then prints *++argv. Argc starts at 5, so the loop prints argv[1] to argv[4].- Each word is printed followed by a space: monday tuesday wednesday thursday.
- The program name is not printed, so the output is monday tuesday wednesday thursday, which is option B.
Why the other options are wrong
- A. The program name sample is not printed; the loop starts at argv[1].
- C. Wednesday is missing, but the loop prints every argument.
- D. Only tuesday is printed, but the loop prints all four arguments.
Tip: Argc counts the program name, so the loop prints argc minus 1 words.
Question 7Medium
What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three
/* myprog.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
int i;
for(i=1; i< argc; i++)
printf("%c", argv[i][0]);
return 0;
}
- oot
- ott
- nwh
- eoe
Answer: B. ott
Explanation
The loop prints the first character of argv[1], argv[2] and argv[3]: 'o' from one, 't' from two and 't' from three, giving ott.
Step-by-step solution
- The loop runs for i from 1 to argc − 1, which covers the words one, two and three.
- argv[i][0] is the first character of each word, which is o, t and t.
- The printed characters are o, t and t one after another.
- So the output is ott.
Why the other options are wrong
- A. The first letters are o, t and t, not o, o and t.
- C. The first letters are o, t and t, not n, w and h.
- D. The code prints the first letter of each word, not the last letters e, o and e.
Tip: argv[0] is the program name, so the words start at argv[1].
Question 8Medium
What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three
/* myprog.c */
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv)
{
printf("%s\n", *++argv);
return 0;
}
- myprog
- one
- two
- three
Answer: B. one
Explanation
The ++argv increments the pointer to point to the next element in the argv array, which is argv[1]. Therefore, *++argv dereferences this pointer to get the string 'one'.
Step-by-step solution
- The command is myprog one two three, so
argv[0] is myprog and argv[1] is one. *++argv first moves the pointer to argv[1].- printf prints that string, which is one.
- So the output is one, which is B.
Why the other options are wrong
- A. argv[0] is myprog, but ++argv moves past it before printing.
- C. Two is argv[2], which needs a second increment.
- D. Three is argv[3], which needs three increments.
Tip: *++argv moves to the next argument before reading it.
Question 9Medium
What will be the output of the program (sample.c) given below if it is compiled with Turbo C and executed from the command line?
cmd> sample Jan Feb Mar
/* sample.c */
#include<stdio.h>
#include<dos.h>
int main(int arc, char *arv[])
{
int i;
for(i=1; i<_argc; i++)
printf("%s ", _argv[i]);
return 0;
}
- No output
- sample Jan Feb Mar
- Jan Feb Mar
- Error
Answer: C. Jan Feb Mar
Explanation
In Turbo C, dos.h declares the global variables _argc and _argv, which hold the same values as main's argc and argv. The loop starts at index 1, so it prints Jan Feb Mar.
Step-by-step solution
- Turbo C provides the global variables _argc and _argv, which hold the argument count and argument array.
- _argc is 4 for the command sample Jan Feb Mar, because it counts the program name.
- The loop starts at i = 1 and runs while i is less than 4, so it prints _argv[1], _argv[2] and _argv[3].
- Those are Jan, Feb and Mar, so the output is Jan Feb Mar, which is option C.
Why the other options are wrong
- A. The loop prints the arguments, so the output is not empty.
- B. The program name is not printed, since the loop starts at index 1.
- D. Turbo C provides _argc and _argv, so there is no error.
Tip: argv[0] is the program name, so arguments start at index 1.
Question 10Medium
Which of the following statements is FALSE about the code below?
int main(int ac, char *av[])
{
}
ac contains the count of arguments supplied at the command lineav[] contains the addresses of the arguments supplied at the command line- In place of
ac and av, argc and argv must be used - The variables
ac and av are always local to main()
Answer: C. In place of ac and av, argc and argv must be used
Explanation
The parameter names are arbitrary; argc and argv are only a convention, so the third statement is false.
Step-by-step solution
- Ac and av are ordinary parameter names chosen by the programmer.
- The names argc and argv are a convention only; any valid identifiers work.
- So ac and av can be used in place of argc and argv, which makes the claim that they must be used false.
- The false statement is option C: In place of ac and av, argc and argv must be used.
Why the other options are wrong
- A. This is true: ac holds the count of command-line arguments.
- B. This is true: av is an array of pointers to the argument strings.
- D. This is true: ac and av are parameters of main, so they are local to it.
Tip: Argc and argv are only names; the rule is about types and meaning, not spelling.
Practise all 13 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →