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

Command Line Arguments Questions and Answers

Answer: B

Correct answer: A pointer to an array of character strings that contain the arguments, one per string

The second parameter is argv. Declared char *argv[] it is an array of pointers, and as a parameter it decays to a pointer to its first element, which is how char **argv comes to be an equivalent declaration.

Each element addresses one null-terminated argument, with argv[0] naming the program and argv[argc] guaranteed null. The first parameter, argc, carries the count of those strings.

Please log in or register to report this question.

22.

What will be the output of the following C statement? (assuming the input is “cool brother in city”)

printf("%s\n", argv[argc]);

 

Answer: A

Correct answer: (null)

argv[argc] is guaranteed to be a null pointer, so the call is printf("%s\n", NULL).

Passing a null pointer to %s is undefined behaviour under the standard, but the GNU C library detects it and prints the literal text (null) rather than dereferencing it - which is the behaviour this key reflects. Other implementations crash instead, so the code should not be written whatever library it happens to meet.

Please log in or register to report this question.

23.

What is the output of this C code?

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

 

Answer: D

Correct answer: Compile time error

& requires an lvalue - something with a storage location - and the literal 5 has none. It is a value, not an object, so the address-of operator has nothing to take the address of.

The compiler rejects the expression outright. Assigning the literal to a variable first, and taking the address of that, is the only way to obtain a pointer to a 5; and a string literal is the special case that does have an address, being an array object in its own right.

Please log in or register to report this question.

Answer: A

Correct answer: It is an array of character pointers

Declared as char *argv[], argv is an array whose elements are char * - one pointer per argument, each addressing a null-terminated string.

The second option describes char **argv read too literally: as a parameter the array does decay to a pointer to its first element, but the object itself is an array of pointers. "An array of strings" is loose talk, since C has no string type; what it has is an array of pointers to characters, which is precisely the first option.

Please log in or register to report this question.

25.

In Turbo C/C++ under DOS if we want that any wild card characters in the command-line arguments should be appropriately expanded, are we required to make any special provision?

Answer: A

Correct answer: Yes

Under DOS the shell does not expand wildcards - a program invoked as prog *.c receives the literal string "*.c" and must expand it itself.

Turbo C supplies that facility in a separate object module, WILDARGS.OBJ, which has to be linked in explicitly; only then does the runtime expand the pattern into matching filenames before main is entered. Unix-like shells do the expansion themselves before the program starts, so no such provision is needed there.

Please log in or register to report this question.

26.

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

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

int main(int argc, char *argv[])
{
    printf("%s\n", argv[0]);
    return 0;
}

 

Answer: C

Correct answer: sample

argv[0] holds the name by which the program was invoked, and the command line here begins with sample, so that is what is printed - whatever arguments follow.

The three invocations differ only in their arguments, and argv[0] is unaffected by them. Note that the exact contents are a platform convention: Unix-like systems generally give the string as typed, while Turbo C under DOS supplies the full uppercase path, which is what the companion question reflects.

Please log in or register to report this question.

27.

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

#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv[])
{
    int answer;

    answer=atoi(argv[1])+atoi(argv[2]);
    printf("%d",answer);
    return 0;
}

The program is run as:

C:\TC\BIN>prg_3.exe 10 20

Answer: C

Correct answer: 30

Command line arguments always arrive as strings, never as numbers. With the command line prg_3.exe 10 20, argv[1] is the two-character string "10" and argv[2] is "20". Adding those directly would add two pointers, which C does not even permit, so the program converts each with atoi - ASCII to integer, declared in <stdlib.h> - giving \[ 10 + 20 = 30. \] Two habits are worth taking from this. First, atoi reports no errors: given text that is not a number it simply returns 0, which is indistinguishable from a genuine "0", so production code uses strtol instead. Second, the program never checks argc before reading argv[1] and argv[2]; run with fewer arguments it would dereference a null pointer and crash. The command line had been lost from this record and is restored above.

Please log in or register to report this question.

28.

What is the output of this C code?

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

 

Answer: A

Correct answer: Compile time error

Read the parameter from the identifier outwards: p is a pointer to a const pointer to int. So *p - the pointer being pointed at - is qualified const, and *p = &j attempts to modify it.

The compiler rejects the assignment. The companion question uses int **const p, where the const attaches to p itself and *p remains modifiable - the same two words in the other order giving the opposite permission.

Please log in or register to report this question.

29.

What is the output of this C code?

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

 

Answer: B

Correct answer: Compile time error

int *const ptr declares a constant pointer to a modifiable int, so ptr itself may not be reassigned and ptr++ is rejected.

Contrast const int *ptr, where the const protects the pointed-to value and the pointer may still be moved. The position of the qualifier relative to the * decides which is fixed, and reading from the identifier outwards makes it clear: ptr is a const pointer to int.

Please log in or register to report this question.

30.

If the different command line arguments are supplied at different times would the output of the following program change?

#include <stdio.h>

int main(int argc, char **argv)
{
    printf("%d\n", argv[argc]);
    return 0;
}

 

Answer: B

Correct answer: No

argv[argc] is guaranteed by the standard to be a null pointer, marking the end of the vector. That is true whatever arguments are supplied, so the value printed never changes.

Note the further problem: the format is %d while the argument is a pointer, which is itself undefined behaviour and typically prints 0. The question turns on the guarantee at the end of the array - a program can therefore walk argv until it meets the null pointer without consulting argc at all.

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