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

Functions Questions and Answers

21.

Which of the following is the correct value returned to the operating system upon the successful completion of a program ?

Exam Year(s)

UGC NET - 2014,

Answer: A

The value 0 is returned to the operating system when a program completes successfully. By convention a return value of zero from main, or a call exit(0), means success, and a non-zero value means some kind of error. The values 1 and -1 are used for failure, and a program does return a value, since the operating system looks at it. Shell scripts and make files test this value to decide whether the program ran correctly.

Please log in or register to report this question.

22.

Find the output of the program
# include \(\lt\)iostream.h\(\gt\)
set_val (int, int);
void main( )
{ int a = 20, b = 10;
   set_val (a++, b++);
   printf ("\n" %d %d, a, b);
}
set_val (int x, int y)
{ x = x + y;
   y = y - x;
   x = x - y;
}

Exam Year(s)

UGC NET - 2014,

Answer: B

Following the official key, the answer is the pair 11 and 21. The call is by value: the arguments are the old values of a and b, and the function only changes its own copies x and y. The post-increment then leaves \(a = 21\) and \(b = 11\) in main, so the printed values are 21 and 11, the same two numbers that option B lists (in the other order).

The inner swap of x and y cannot change a and b. Options A and C are values that could occur only if a and b were not incremented or if the swap acted on them, and D adds b to a in a way no statement here does. The program as printed is not valid C or C++ (it mixes iostream.h with printf and has a malformed format string), so the answer is read as the pair of values a and b take.

Please log in or register to report this question.

Answer: A

Correct answer: Declared within the scope of a block, usually a function

An automatic variable is one declared inside a block. It comes into existence when the block is entered and disappears when it is left. Declaring it outside all functions would make it static; writing auto is optional, since it is the default.

Please log in or register to report this question.

Answer: D

Correct answer: Both (a) and (b)

An automatic variable is visible from its declaration to the end of the block that contains it, and that includes any blocks nested inside - unless one of them declares another variable of the same name, which shadows it.

Please log in or register to report this question.

25.

Functions can be called either by value or reference

Answer: A

Correct answer: True

C passes everything by value, but passing a pointer by value gives the function the caller’s address and so the effect of call by reference. Both styles are therefore available, which is why arrays - whose names decay to pointers - behave as though passed by reference.

Please log in or register to report this question.

26.

What is the output of the below code snippet?

#include <stdio.h>

main() 
{
   unsigned x = 5, y=&x, *p = y+0; 

   printf("%u",*p);
}

 

Answer: D

Correct answer: 5

y holds the address of x, and p is set to that same address, so *p is x itself - 5. (The code stores an address in an unsigned int, which is poor practice and provokes warnings; it happens to work only where a pointer and an unsigned int are the same width.)

Please log in or register to report this question.

27.

What is the output of this C code?
 

void m();
void n()
{
m();
}
void main()
{
void m()
{
printf("hi");
}
}

 

Answer: B

Correct answer: Compile time error

Two faults. Standard C does not allow a function to be defined inside another, and m is defined inside main here. And n, which calls m, is never called at all - so even if it compiled, nothing would be printed.

Please log in or register to report this question.

28.

Is an assignment statement that gives a value to a local static variable executed only once?

Answer: B

Correct answer: false

The initialisation of a static variable happens once, before the program starts. But an ordinary assignment to it inside the function is an executable statement like any other, and runs every time the function is called - which would defeat the whole point of making the variable static.

Please log in or register to report this question.

Answer: A

Correct answer: return-type function-name(argument type);

A declaration (prototype) states the return type, the name and the parameter types, and ends with a semicolon - no body. Adding a body makes it a definition, which is a different thing.

Please log in or register to report this question.

30.

What is the output of this C code?

void foo();
int main()
{
void foo();
foo();
return 0;
}
void foo()
{
printf("2 ");
}

 

Answer: B

Correct answer: 2

The function is declared at file scope and again inside main. Repeating a declaration is harmless as long as the two agree, which they do, so the program compiles and prints 2.

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Functions Questions and Answers || Functions MCQ Questions and Answers || Functions GK Questions and Answers || Functions GK MCQ Questions || Functions Multiple Choice Questions and Answers || Functions GK || GK on Functions || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming