Machine-translated by Google. Questions are authored in English.
Top

Command Line Arguments Questions and Answers

1.

In linux, argv[0] by command-line argument can be occupied by _________

Answer: D

Correct answer: all of the mentioned

argv[0] holds the name by which the program was invoked, and on Linux that is whatever string the user typed to run it - whether ./a.out, ./test or anything else.

The name has no necessary connection to the source file, and a program can be renamed freely. It is not even guaranteed to be a real filename: a shell or an exec call can supply any string it likes, which is what lets a single binary behave differently under several names, as busybox does.

Please log in or register to report this question.

Answer: A

Correct answer: You cannot change the value pointed by ptr

The const applies to what ptr points at, not to ptr itself, so *ptr = 5 is rejected while ptr = &other is allowed.

Read such declarations from the identifier outwards, or apply the rule that const qualifies whatever lies to its left unless there is nothing there, in which case it qualifies what lies to its right. int * const ptr reverses the meaning - a constant pointer to a modifiable int - and const int * const ptr fixes both.

Please log in or register to report this question.

3.

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

1. $ ./output 'Hello World'

2.  $ ./output Hello World

Answer: C

Correct answer: Only 1

The shell splits on whitespace before the program ever runs, so ./output Hello World delivers two separate arguments - argv[1] is "Hello" and argv[2] is "World", with argc of 3.

Quoting is what prevents the split: 'Hello World' reaches the program as a single argv[1], and argc is 2. Escaping the space as Hello\ World has the same effect. Only the first form therefore gives one argument.

Please log in or register to report this question.

4.

What is the output of this C code?

#include <stdio.h>
    void main()
    {
        int x = 0;
        int *ptr = &x;
        printf("%p\n", ptr);
        ptr++;
        printf("%p\n ", ptr);
    }

 

Answer: A

Correct answer: 0xbfd605e8 0xbfd605ec

Pointer arithmetic is in units of the pointed-to type, so ptr++ advances by sizeof(int) - four bytes on a typical platform. The address rises from ...e8 to ...ec.

The distractor ending e9 assumes a step of one byte, which would be right only for a char *. The particular addresses are arbitrary and vary between runs; what the question tests is the size of the step, which follows from the type.

Please log in or register to report this question.

5.

What is the output of this C code?

#include <stdio.h>
    int main()
    {
        int i = 10;
        int *p = &i;
        foo(&p);
        printf("%d ", *p);
        printf("%d ", *p);
    }
    void foo(int **const p)
    {
        int j = 11;
        *p = &j;
        printf("%d ", **p);
    }

 

Answer: B

Correct answer: 11 11 Undefined-value

int **const p makes p itself constant while leaving *p modifiable, so *p = &j is legal and foo prints 11.

But j dies when foo returns, so the caller's pointer dangles. The first printf in main often still finds 11, because nothing has yet overwritten that stack slot - but the call itself uses the stack, so the second read may find anything. Every read after the return is undefined; only the timing makes the first appear to work.

Please log in or register to report this question.

6.

What is the output of this C code?

#include <stdio.h>
    int main()
    {
        int i = 10;
        void *p = &i;
        printf("%d\n", (int)*p);
        return 0;
    }

 

Answer: A

Correct answer: Compile time error

void is an incomplete type, so *p on a void * has no type and no size and the compiler rejects it. The cast in (int)*p comes too late: the dereference must be resolved before anything can be cast.

The correct form casts the pointer first, as *(int *)p, telling the compiler how to read the memory before reading it. That is exactly what the companion question does with a float *, and it is why void * can carry any object pointer but can be dereferenced through none.

Please log in or register to report this question.

Answer: B

Correct answer: Either single or double quotes

The shell splits the command line on whitespace, so an argument containing a space must be quoted to arrive intact. Both quoting styles achieve that.

They differ in what else they do: double quotes still allow the shell to expand variables and command substitutions inside them, while single quotes suppress everything and pass the text literally. For an argument containing only spaces either works; where the text also contains $ or a backtick, single quotes are the safer choice.

Please log in or register to report this question.

8.

What type of array is generally generated in Command-line argument?

Answer: C

Correct answer: Jagged Array

argv is an array of pointers, and each points to a string of its own length - so the rows are of unequal length, which is what "jagged" means.

A rectangular two-dimensional array would allocate the same width for every row, wasting space on the short ones and truncating any longer than the fixed width. The pointer-of-arrays arrangement stores each argument exactly as long as it is, which is why char *argv[] is the natural declaration rather than char argv[][N].

Please log in or register to report this question.

9.

What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog friday tuesday sunday

/* myprog.c */
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("%c", *++argv[1]);
    return 0;
}

 

Answer: A

Correct answer: r

Subscripting binds tighter than the prefix ++, so the expression is *(++(argv[1])). It is the pointer stored in argv[1] that is advanced, not argv.

argv[1] points at "friday"; advancing it by one character makes it point at the 'r', and the * yields that character. The contrast with **++argv, which gives 'f', is entirely a matter of which pointer the increment reaches.

Please log in or register to report this question.

10.

The first argument to be supplied at command-line must always be count of total arguments.

Answer: B

Correct answer: False

Nothing of the kind is supplied by the user. The shell counts the arguments itself and passes that count to the program in argc, so the count travels alongside the arguments rather than among them.

Everything the user types after the program name is an ordinary argument, available from argv[1] onwards. Requiring the user to state a count would be redundant and error-prone, and no convention on any common system does so.

Please log in or register to report this question.

Loading…
Tags: Command Line Arguments Questions and Answers || Command Line Arguments MCQ Questions and Answers || Command Line Arguments GK Questions and Answers || Command Line Arguments GK MCQ Questions || Command Line Arguments Multiple Choice Questions and Answers || Command Line Arguments GK || GK on Command Line Arguments || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming