Command Line Arguments Questions
Practice Command Line Arguments MCQs with answers and explanations. Page 1 of 2.
Category
C Programming
Topic
Command Line Arguments
Page
1 / 2
Mode
Practice
Questions
Open any question to view the answer and explanation.
In C command-line arguments, the program below prints argv[argc - 1]. If it is executed as: cmd> sample (where argv[0] numerically refers to "sample"), what will it print?
#include<stdio.h>
int main(int argc, char **argv)
{
printf("%s
", argv[argc-1]);
return 0;
}
Open
View answer
In C, argv points to the command-line argument vector. For the program below executed as: cmd> myprog one two three, what does the statement printf("%s
", *++argv); print?
/* myprog.c */
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv)
{
printf("%s
", *++argv);
return 0;
}
Open
View answer
In C, reversing traversal via argv index. Given the program below and execution: cmd> sample friday tuesday sunday, what exact contiguous output does it produce (note: printed without spaces or extra newlines)?
/* sample.c */
#include<stdio.h>
int main(int sizeofargv, char *argv[])
{
while(sizeofargv)
printf("%s", argv[--sizeofargv]);
return 0;
}
Open
View answer
Command-line wildcards and argument types in C. For the corrected program below executed as: cmd> sample "*.c", what will be printed (assume DOS/Turbo C style where the shell does not expand wildcards)?
/* sample.c (corrected) /
#include<stdio.h>
int main(int argc, char argv[])
{
int i;
for(i = 1; i < argc; i++)
printf("%s
", argv[i]);
return 0;
}
Open
View answer
In C, **++argv moves to the first user argument and then dereferences twice. For the program below executed as: cmd> sample friday tuesday sunday, what character is printed?
/* sample.c */
#include<stdio.h>
int main(int argc, char argv[])
{
printf("%c", ++argv);
return 0;
}
Open
View answer
In C, the code below reverses the first user argument character-by-character using strlen and a countdown index. If executed as: cmd> sample one two three, what does it print?
/* sample.c */
#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[])
{
int i = 0;
i += strlen(argv[1]);
while(i > 0)
{
printf("%c", argv[1][--i]);
}
return 0;
}
Open
View answer
In DOS/Turbo C style execution, argv[0] typically contains the full path of the running program. If the file myproc.c is in "C:\TC" and you run MYPROC.EXE from that directory, what does this program print?
/* myproc.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
printf("%s", argv[0]);
return 0;
}
Open
View answer
In Turbo C, consider main defined with three parameters to also receive the environment vector. After correcting argv's type, what will this program print when executed (it attempts to print env variables)?
/* corrected Turbo C style */
#include<stdio.h>
int main(int argc, char *argv[], char *env[])
{
int i;
for(i = 1; env[i] != 0 && i < argc; i++)
printf("%s
", env[i]);
return 0;
}
Open
View answer
In C, printing the first character of each user argument. For the program below executed as: cmd> myprog one two three, what does it print (characters are contiguous, no spaces)?
/* 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;
}
Open
View answer
C command-line args: sum argv values with atoi, including argv[0] which converts to 0 if non-numeric.
/* myprog.c */
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int i, j = 0;
for (i = 0; i < argc; i++)
j = j + atoi(argv[i]);
printf("%d
", j);
return 0;
}
Executed as: myprog 1 2 3
Open
View answer
Recursively calling main via a helper: trace argc from 1 up to 4 and print each value.
#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);
}
Open
View answer
Pointer to argument string and prefix increment: what character is printed?
/* myprog.c /
#include <stdio.h>
int main(int argc, char argv[])
{
/ Invoked as: myprog friday tuesday sunday /
printf("%c", ++argv[1]);
return 0;
}
Open
View answer
Double dereference on incremented argv pointer: which character prints first?
/* myprog.c */
#include <stdio.h>
int main(int argc, char *argv)
{
/ Invoked as: myprog one two three */
printf("%c
", ++argv);
return 0;
}
Open
View answer
Pointer arithmetic on argv elements (Turbo C / DOS): adding pointers is invalid and causes a compile-time error.
/* 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;
}
Invoked as: sample 1 2 3
Open
View answer
Printing argv[0] only: observe program name output regardless of the numeric arguments.
/* sample.c */
#include <stdio.h>
int main(int argc, char argv[])
{
/ Invocations: sample 1 2 3 | sample 2 2 3 | sample 3 2 3 */
printf("%s
", argv[0]);
return 0;
}
Open
View answer
Dereference of incremented argument pointer: identify the printed character from argv[2].
/* sample.c /
#include <stdio.h>
int main(int argc, char argv[])
{
/ Invoked as: sample friday tuesday sunday /
printf("%c", ++argv[2]);
return 0;
}
Open
View answer
Addresses of argv elements (pointers) and their contiguity: predict subsequent &argv[i] values.
/* myprog.c /
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char argv)
{
int i;
for (i = 1; i <= 3; i++)
printf("%u
", (unsigned)&argv[i]);
return 0;
}
Given that the first printed address is 65517, find the next two values.
Open
View answer
Iterating over command-line arguments with pre-increment on argv pointer: what strings print?
/* sample.c */
#include <stdio.h>
int main(int argc, char argv[])
{
/ Invoked as: sample monday tuesday wednesday thursday */
while (--argc > 0)
printf("%s", *++argv);
return 0;
}
Open
View answer
Print every argv element including program name: what is the full output order?
/* myprog.c */
#include <stdio.h>
int main(int argc, char *argv)
{
/ Invoked as: myprog 10 20 30 */
int i;
for (i = 0; i < argc; i++)
printf("%s
", argv[i]);
return 0;
}
Open
View answer
C (Turbo C under DOS) — determine the program's output when executed with command-line arguments.
Command used:
cmd> sample Good Morning
/* sample.c */
#include<stdio.h>
int main(int argc, char *argv[])
{
printf("%d %s", argc, argv[1]);
return 0;
}
What will be printed to the screen?
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.