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.

Direction for [ Question No: 23 To 23 ] :

Directions : The following items consists of two statements, one labelled the “Assertion (A)” and the other labelled the “Reason (R)”. You are to examine these two statements carefully and decide if the Assertion (A) and Reason (R) are individually true and if so, whether the reason is a correct explanation of the Assertion. Select your answer to these items using the codes given below and mark your answer accordingly.

23.

Assertion (A) : To avoid repetition of code and bulky programs functionally related statements are isolated into a function.

Reason (R) : Function definition defines the body of the function.

Exam Year(s)

UGC NET - 2014,

Answer: A

Both statements are true and (R) explains (A). Functionally related statements are collected into a function so that they need not be written again each time they are needed, which avoids repeated code and keeps programs short, so (A) is true.


A function definition is the place where the body of the function is written, so (R) is true, and since this body is the code written once and reused, it gives the way in which the repetition is avoided. Hence option (A).

Please log in or register to report this question.

24.

What is the notation for the following functions?

1.  int f(int a, float b)
    {
        /* Some code */
    }

2.  int f(a, b)
    int a; float b;
    {
        /* Some code */
    }

 

Answer: C

Correct answer: 1. ANSI Notation 2. KR Notation

The first form declares each parameter’s type inside the parentheses - the ANSI (prototype) style, introduced by the 1989 standard. The second lists only the names in the parentheses and declares their types between the parentheses and the body - the old Kernighan and Ritchie style. The ANSI form matters because it lets the compiler check the arguments at every call; the K&R form does not, and is now obsolete.

Please log in or register to report this question.

25.

What is the output of the following program?

#include <stdio.h>

void swap(int m, int n)
{
   int x = m;
   
   m = n;
   n = x;
}
main()
{
   int x=5, y=3;

   swap(x,y);
   printf("%d %d", x, y);
}

 

Answer: B

Correct answer: 5 3

C passes arguments by value, so swap receives copies and exchanges the copies. The originals in main are untouched, and the output is 5 3. To swap the caller’s variables the function must take pointers and be called as swap(&x, &y).

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 will happen after compiling and running following code?

main()
{
  printf("%p", main);
}

 

Answer: C

Function names are just addresses (just like array names are addresses).
main() is also a function. So the address of function main will be printed. %p in printf specifies that the argument is an address. They will be printed as hexadecimal numbers.

Please log in or register to report this question.

Answer: E

Correct answer: All of the above

Functions do all four. They let a block of code be written once and used many times; they make a program clearer by giving each task a name; they can be collected into libraries and reused across programs; and because each can be tested on its own, they make debugging far easier.

Please log in or register to report this question.

29.

The keyword used to transfer control from a function back to the calling function is

Answer: D

The keyword return is used to transfer control from a function back to the calling function.

Example:

#include
int add(int, int); /* Function prototype */

int main()
{
    int a = 4, b = 3, c;
    c = add(a, b);
    printf("c = %d\n", c);
    return 0;
}
int add(int a, int b)
{
/* returns the value and control back to main() function */
   return (a+b);
}

Output: c = 7

Please log in or register to report this question.

30.

The keyword used to transfer control from a function back to the calling function is

Answer: D

Correct answer: return

return ends the function and hands control - and optionally a value - back to the caller. goto can only jump within the same function, and there is no go back keyword.

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