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

Command Line Arguments Questions and Answers

61.

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 post-increment is the trap. The condition tests *argv, which is argv[0] and non-null, and then advances the pointer - so by the time the body runs, *argv is argv[1].

With no arguments argv[1] is the terminating null pointer, so the very first printf is handed NULL for %s. The loop tests one element and prints the next, which is exactly one element out of step; moving the increment into the printf instead, as the companion question does, corrects it.

Please log in or register to report this question.

62.

What is the output of this C code?

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

 

Answer: A

Correct answer: 10

p++ is a post-increment, so the value passed to foo is p as it was - the address of i - and only afterwards does p advance.

foo therefore dereferences a valid pointer and prints 10. The increment leaves p pointing one past i, which is never used, so no harm follows. Had the pre-increment ++p been written, the pointer passed would have been one past the object and the dereference undefined.

Please log in or register to report this question.

Answer: B

Correct answer: The number of command-line arguments, including the program name

argc counts the strings in argv, and argv[0] holds the name by which the program was invoked. So running a program with two arguments gives argc a value of 3.

Please log in or register to report this question.

64.

Which of the following does not initialize ptr to null (assuming variable declaration of a as int a=0;

Answer: A

Correct answer: int *ptr = &a;

That initialiser gives ptr the address of a, which is a real object and therefore not a null pointer.

The other two do yield null. &a - &a is zero, and a - a is zero because a is 0 - and a constant expression evaluating to 0 is a null pointer constant, so it converts. Both are legal but poor style: NULL states the intention, and a subtraction that happens to give zero conceals it.

Please log in or register to report this question.

65.

What is the output of this C code?

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

 

Answer: D

Correct answer: 0

ptr holds the address of x, and *ptr reads the object there, which is 0 - the value x was initialised with.

The %d conversion prints it as an integer. Printing the pointer itself would need %p and would show an address, which is the first distractor. There is nothing uninitialised here, so no junk value arises, and dereferencing a pointer to a live object is entirely well defined.

Please log in or register to report this question.

66.

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[])
    {
        printf("%s\n", argv[argc]);
        return 0;
    }

 

Answer: A

Correct answer: Segmentation fault/code crash

With no arguments argc is 1, so argv[argc] is argv[1] - the guaranteed null pointer at the end of the vector.

Passing a null pointer to %s is undefined behaviour, and the crash the key reports is what an implementation that simply dereferences it produces. The GNU C library instead detects the case and prints (null), which is why the same construct appears elsewhere with that answer - the code is unsafe either way and its result cannot be relied on.

Please log in or register to report this question.

67.

Every time we supply new set of values to the program at command prompt, we need to recompile the program.

Answer: B

Correct answer: False

Command-line arguments are read at run time, which is the whole point of them. A different set of values is supplied simply by typing a different command; the executable is untouched.

Recompilation would be needed only if the values were written into the source. Separating what the program does from the data it operates on is exactly what argc and argv provide, and it is why utilities can be written once and applied to any file named on the command line.

Please log in or register to report this question.

68.

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

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

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

 

Answer: C

Correct answer: u

Subscripting binds tighter than the prefix ++, so this is *(++(argv[2])): the pointer held in argv[2] is advanced by one character.

argv[2] is "tuesday", so advancing past the 't' leaves it pointing at the 'u', which the * yields. The pattern is worth learning as a family: **++argv gives the first letter of the first argument, while *++argv[n] gives the second letter of argument n.

Please log in or register to report this question.

69.

What will be the output of this program (prg_1.c)?

#include <stdio.h>
int main(int argc,char* argv[])
{
    printf("%d",argc);
    return 0;
}

The program is run as:

C:\TC\BIN>prg_1.exe 1 2 3 4

Answer: B

Correct answer: 5

The runtime hands main two things: argc, the number of command line arguments, and argv, an array of pointers to those arguments as strings. The crucial point is that the program’s own name counts. argv[0] holds the name the program was invoked by, and the arguments the user typed follow from argv[1] onwards. Here the command line is prg_1.exe 1 2 3 4: one program name plus four arguments, so \[ \text{argc} = 1 + 4 = 5. \] It is also worth knowing that argv[argc] is guaranteed to be a null pointer, so the array always has argc + 1 elements - which is what lets a loop walk argv without consulting argc at all. The command line had been lost from this record and is restored above; without it the question has no determinate answer.

Please log in or register to report this question.

70.

What is the output of this C code?

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

 

Answer: D

Correct answer: 0.000000

The cast is on the pointer this time, so the compiler accepts it and reads the four bytes holding the integer 10 as though they were an IEEE single-precision float.

The bit pattern 0x0000000A interpreted that way is a subnormal number of about \( 1.4 \times 10^{-44} \), which %f prints to six decimal places as 0.000000. Nothing is converted - the same bits are simply given a different meaning, which is what type punning through a pointer does.

Please log in or register to report this question.

Loading…

Other Subjects

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