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

Command Line Arguments Questions and Answers

71.

What will be the output of the following C code (run without any command line arguments)?

#include <stdio.h>
    int main(int argc, char *argv[])
    {
        while (*argv  !=  NULL)
        printf("%s\n", *(argv++));
        return 0;
    }

 

Answer: B

Correct answer: Executable file name

Here the increment is in the body rather than the condition, and that makes the difference. The test examines *argv - argv[0], non-null - and the printf then prints that same element before advancing the pointer.

So the program name is printed. The next test finds argv[1], which with no arguments is the terminating null pointer, and the loop ends cleanly. Testing and printing the same element is what keeps this version correct where the companion is one out of step.

Please log in or register to report this question.

72.

What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample one two three

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

int main(int argc, char *argv[])
{
    int i=0;
    i+=strlen(argv[1]);
    while(i>0)
    {
        printf("%c", argv[1][--i]);
    }
    return 0;
}

 

Answer: C

Correct answer: eno

argv[1] is "one", so i becomes 3. The loop then prints argv[1][--i] for i = 2, 1 and 0 - that is 'e', 'n', 'o' - and stops when i reaches 0.

The pre-decrement is what makes the indexing correct: it steps to 2 before the first access, so the terminating null at index 3 is never read. Only the first argument is touched; "two" and "three" are ignored entirely.

Please log in or register to report this question.

73.

What will be the output of the following C code?

#include <stdio.h>
main()
{
    typedef int a;
    a b=2, c=8, d;
    d=(b*2)/2+8;
    printf("%d",d);
}

 

Answer: A

Correct answer: 10

The typedef is a distraction: a is simply another name for int, so b, c and d are ordinary integers.

Precedence settles the arithmetic. * and / bind tighter than + and associate left to right:

\[ (2 \times 2)/2 + 8 = 4/2 + 8 = 2 + 8 = 10 \]

The distractor 16 comes from adding before dividing. Note that c is declared and never used, which is a further piece of misdirection.

Please log in or register to report this question.

74.

What is the output of this C code?

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

 

Answer: B

Correct answer: 0.000000

The address of an int is passed to a function expecting a float *, so the four bytes holding 10 are read as though they were an IEEE single-precision float.

The bit pattern 0x0000000A read that way is a subnormal number of about \( 1.4 \times 10^{-44} \), which %f prints as 0.000000. No conversion takes place - the same bits are simply reinterpreted. Note that the mismatched pointer types would draw a diagnostic in any modern compiler.

Please log in or register to report this question.

75.

What is the output of this C code?

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

 

Answer: A

Correct answer: Same address

ptr holds the address of x, and incrementing x changes the value stored there, not where it is stored. An object does not move during its lifetime.

So both printf calls print the same pointer value. The distinction is the one beginners most often blur: x++ alters the contents of the box, ptr++ would alter which box is pointed at, and only the second changes what %p reports.

Please log in or register to report this question.

76.

What will be the output of the following C code (run without any command line arguments)?

#include <stdio.h>
    int main(int argc, char *argv[])
    {
        while (argv != NULL)
        printf("%s\n",  *(argv++));
        return 0;
    }

 

Answer: A

Correct answer: Segmentation fault/code crash

The condition tests argv rather than *argv, and argv is a pointer into the array - it is never itself null however far it is advanced.

The loop therefore never terminates. It prints argv[0], then the terminating null pointer, then whatever lies beyond the array, walking off the end of the block until it reaches unmapped memory and the program is killed. One * separates this from the working version: the null to test for is the one stored in the array.

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