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

Command Line Arguments Questions and Answers

51.

To use command line arguments in C++, how many parameters are passed to the main function?

Answer: B

2 arguments are needed to be passed to main() function while using command line arguments. The first one represents a number of strings in the argument list and the second list represents the list of string arguments.

Please log in or register to report this question.

52.

What is the output of this C code?

#include <stdio.h>
    int main()
    {
        int *ptr, a = 10;
        ptr = &a;
        *ptr += 1;
        printf("%d,%d/n", *ptr, a);
    }

 

Answer: D

Correct answer: 11,11

ptr holds the address of a, so *ptr and a are two names for the same object. *ptr += 1 increments that object, and both expressions therefore read 11.

Note the format string ends in /n, a forward slash, not the escape \n. So the two characters are printed literally and no newline is emitted - a typographical slip that changes the output's appearance without affecting the values, which are what the question asks for.

Please log in or register to report this question.

Answer: A

Correct answer: The name by which the program was invoked

argv[0] holds the program name as invoked, which is why argc is 1 rather than 0 when no arguments follow.

The exact contents are a platform convention: Unix-like systems generally supply the string the user typed, while Turbo C under DOS supplies the full uppercase path. Nor is it necessarily a real filename - an exec call may pass any string - which is what allows one binary to behave differently according to the name it is called by.

Please log in or register to report this question.

54.

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);
    return 0;
}

 

Answer: B

Correct answer: f

++argv advances the pointer to argv[1], so the first * gives the string "friday" and the second gives its first character, 'f'.

Compare with *++argv[1], which increments the pointer inside the array rather than the array pointer itself and yields 'r'. Which pointer moves is decided by precedence: subscripting binds tighter than the prefix ++, so ++argv[1] increments argv[1], while ++argv increments argv.

Please log in or register to report this question.

55.

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

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

The program is run as:

C:\TC\BIN>prg_2.exe Help .Com

Answer: A

Correct answer: (null)

The program prints argv[argc]. With the command line prg_2.exe Help .Com there are three entries in use - argv[0] is the program name, argv[1] is Help, argv[2] is .Com - so argc is 3 and the program is asking for argv[3]. That is one past the last argument, and the standard requires it to be a null pointer: argv[argc] is always null, which is what allows a program to iterate over its arguments until it meets the null rather than counting them. Passing a null pointer to %s is strictly undefined behaviour, but every widely used C library detects it and prints the literal text (null) rather than crashing, and that is the output the question expects. The distractors are the values one index lower or higher in the reader’s mental model: .Com is argv[argc-1], Help is argv[1], and the command line itself is never a value of argv at all - the shell splits it before the program sees it. The program listing and its command line had both been lost from this record and are restored above.

Please log in or register to report this question.

56.

What is the output of this C code?

#include <stdio.h>
    int *f();
    int main()
    {
        int *p = f();
        printf("%d\n", *p);
    }
    int *f()
    {
        int *j = (int*)malloc(sizeof(int));
        *j = 10;
        return j;
    }

 

Answer: A

Correct answer: 10

The storage is obtained from malloc, so it lives on the heap and its lifetime is independent of the function that allocated it. Returning the pointer is therefore legitimate, and the caller reads 10.

The contrast with returning the address of a local is the whole point: an automatic variable's lifetime ends when the function returns, leaving a dangling pointer. The only fault here is the missing free, which leaks the block - a leak rather than an error in the value printed.

Please log in or register to report this question.

57.

Which of the following is correct about the first parameter of the main function?

Answer: D

Correct answer: All of the mentioned

All three statements hold. argc is declared int; it stores the count of command-line arguments; and it is non-negative, since a count cannot be less than zero.

In practice it is at least 1, because the program name occupies argv[0] and is counted. The standard does allow 0, in which case argv[0] is a null pointer, but that arises only from an exec call supplying no name at all - never from an ordinary shell.

Please log in or register to report this question.

58.

What is the output of this C code?

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

 

Answer: A

Correct answer: 2 97

foo receives a copy of the pointer. Assigning p = &j inside it changes only that copy, so foo prints 2 and the caller's pointer is untouched.

Back in main, p still points at i, and 97 is printed. C passes everything by value, pointers included: to let a function change the caller's pointer it must be given the address of that pointer, which is what the companion question does with int **.

Please log in or register to report this question.

59.

A program that has no command line arguments will have argc _________

Answer: C

Correct answer: One

The program name occupies argv[0] and is counted, so argc is 1 when nothing else is supplied.

That is why a program testing for arguments writes if (argc > 1) rather than if (argc). The standard permits argc to be 0, in which case argv[0] is a null pointer, but this happens only where a program is started by an exec call that supplies no name at all - never from an ordinary shell.

Please log in or register to report this question.

60.

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

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

int main(int argc, char **argv)
{
    printf("%c\n", **++argv);
    return 0;
}

 

Answer: C

Correct answer: o

Take the expression from the inside out. ++argv advances the pointer so it points at argv[1], and the pre-increment yields the new value.

The first * then gives argv[1] itself, which is the string "one" - a char *. The second * dereferences that, giving the first character, 'o'. The %c conversion prints it. Note that a single * with %s would have printed the whole word, which is what the companion question 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