Curioustab
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Command Line Arguments Questions
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
int main(int argc, char **argv) { printf("%s\n", argv[argc-1]); return 0; }
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\n", *++argv); print? /* myprog.c */ #include
#include
int main(int argc, char **argv) { printf("%s\n", *++argv); return 0; }
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
int main(int sizeofargv, char *argv[]) { while(sizeofargv) printf("%s", argv[--sizeofargv]); return 0; }
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
int main(int argc, char argv[]) { int i; for(i = 1; i < argc; i++) printf("%s\n", argv[i]); return 0; }
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
int main(int argc, char argv[]) { printf("%c", ++argv); return 0; }
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
#include
int main(int argc, char *argv[]) { int i = 0; i += strlen(argv[1]); while(i > 0) { printf("%c", argv[1][--i]); } return 0; }
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
int main(int argc, char *argv[]) { printf("%s", argv[0]); return 0; }
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
int main(int argc, char *argv[], char *env[]) { int i; for(i = 1; env[i] != 0 && i < argc; i++) printf("%s\n", env[i]); return 0; }
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
int main(int argc, char *argv[]) { int i; for(i = 1; i < argc; i++) printf("%c", argv[i][0]); return 0; }
C command-line args: sum argv values with atoi, including argv[0] which converts to 0 if non-numeric.\n\n/* myprog.c */\n#include
\n#include
\n\nint main(int argc, char **argv)\n{\n int i, j = 0;\n for (i = 0; i < argc; i++)\n j = j + atoi(argv[i]);\n printf("%d\n", j);\n return 0;\n}\n\nExecuted as: myprog 1 2 3
Recursively calling main via a helper: trace argc from 1 up to 4 and print each value.\n\n#include
\nvoid fun(int);\n\nint main(int argc)\n{\n printf("%d ", argc);\n fun(argc);\n return 0;\n}\n\nvoid fun(int i)\n{\n if (i != 4)\n main(++i);\n}\n
Pointer to argument string and prefix increment: what character is printed?\n\n/* myprog.c /\n#include
\n\nint main(int argc, char argv[])\n{\n / Invoked as: myprog friday tuesday sunday /\n printf("%c", ++argv[1]);\n return 0;\n}\n
Double dereference on incremented argv pointer: which character prints first?\n\n/* myprog.c */\n#include
\n\nint main(int argc, char *argv)\n{\n / Invoked as: myprog one two three */\n printf("%c\n", ++argv);\n return 0;\n}\n
Pointer arithmetic on argv elements (Turbo C / DOS): adding pointers is invalid and causes a compile-time error.\n\n/* sample.c /\n#include
\n\nint main(int argc, char argv[])\n{\n int j;\n j = argv[1] + argv[2] + argv[3];\n printf("%d", j);\n return 0;\n}\n\nInvoked as: sample 1 2 3
Printing argv[0] only: observe program name output regardless of the numeric arguments.\n\n/* sample.c */\n#include
\n\nint main(int argc, char argv[])\n{\n / Invocations: sample 1 2 3 | sample 2 2 3 | sample 3 2 3 */\n printf("%s\n", argv[0]);\n return 0;\n}\n
Dereference of incremented argument pointer: identify the printed character from argv[2].\n\n/* sample.c /\n#include
\n\nint main(int argc, char argv[])\n{\n / Invoked as: sample friday tuesday sunday /\n printf("%c", ++argv[2]);\n return 0;\n}\n
Addresses of argv elements (pointers) and their contiguity: predict subsequent &argv[i] values.\n\n/* myprog.c /\n#include
\n#include
\n\nint main(int argc, char argv)\n{\n int i;\n for (i = 1; i <= 3; i++)\n printf("%u\n", (unsigned)&argv[i]);\n return 0;\n}\n\nGiven that the first printed address is 65517, find the next two values.
Iterating over command-line arguments with pre-increment on argv pointer: what strings print?\n\n/* sample.c */\n#include
\n\nint main(int argc, char argv[])\n{\n / Invoked as: sample monday tuesday wednesday thursday */\n while (--argc > 0)\n printf("%s", *++argv);\n return 0;\n}\n
Print every argv element including program name: what is the full output order?\n\n/* myprog.c */\n#include
\n\nint main(int argc, char *argv)\n{\n / Invoked as: myprog 10 20 30 */\n int i;\n for (i = 0; i < argc; i++)\n printf("%s\n", argv[i]);\n return 0;\n}\n
C (Turbo C under DOS) — determine the program's output when executed with command-line arguments.\n\nCommand used:\ncmd> sample Good Morning\n\n/* sample.c */\n#include
\n\nint main(int argc, char *argv[])\n{\n printf("%d %s", argc, argv[1]);\n return 0;\n}\n\nWhat will be printed to the screen?
1
2