Consider the following two C++ programs P1 and P2 and two statements S1 and S2 about the programs:

P1

P2

void f(int a, int*b, int &c)

{     a = 1;

       *b =2;

       c = 3;

}

int main ()

      int i = 0;

      f(i, &i, i):

      cout ≪ i;

}

double a = 1, b =2;

double &f(double &d)

{       d = 4;

         return b;

}

int main ()

{

      f(a) = 5;

      cout ≪ a ≪ “:” ≪b;

}

 

S1: P1 prints out 3

S2: P2 prints out 4:2

What can you say about the statement S1 and S2

This question was previously asked in
UGC NET Computer Science (Paper 2) Dec 2018 Paper
View all UGC NET Papers >
  1. Neither S1​ nor S2​ is true
  2. Only S​1​ is true
  3. Only S2​ is true
  4. Both S1​ and S​2​ are true

Answer (Detailed Solution Below)

Option 2 : Only S​1​ is true
Free
UGC NET Paper 1: Held on 21st August 2024 Shift 1
10.8 K Users
50 Questions 100 Marks 60 Mins

Detailed Solution

Download Solution PDF

Code P1:

In function f, there are three parameters, one is integer, second is pointer , third is reference variable. When there is call to function f from main in which initially value of i = 0

Int main ()

{int i= 0; f(i, &i, i); cout<< i;}           /it calls the function f

void f (int a, int *b, int &c)

{

a =1;      /it changes the value of i to 1, but it is call by value, change will not reflect back to main

*b = 2;      / it changes the value of i to 2 and change will reflect to main function

c = 3;         / it changes the value of i again and final value becomes 3.

}

So, Code p1 prints out 3.

Code P2:

In this, f(a) calls the function f which is:

double &f(double &d)

{

d =4;          / it will update the value of a to 4 and returns reference to double b.

return b;             / here it returns the value of b as 5.

}

So, in main

int main ()

{

f(a) = 5;            / a becomes 4

cout<< a<< “:”<< b;        / it prints 4: 5

}

Latest UGC NET Updates

Last updated on Jun 11, 2025

-> The UGC NET Exam Schedule 2025 for June has been released on its official website.

-> Check UPSC Prelims Result 2025, UPSC IFS Result 2025, UPSC Prelims Cutoff 2025, UPSC Prelims Result 2025 Name Wise & Rollno. Wise

-> The UGC NET Application Correction Window 2025 is available from 14th May to 15th May 2025.

-> The UGC NET 2025 online application form submission closed on 12th May 2025.

-> The June 2025 Exam will be conducted from 21st June to 30th June 2025

-> The UGC-NET exam takes place for 85 subjects, to determine the eligibility for 'Junior Research Fellowship’ and ‘Assistant Professor’ posts, as well as for PhD. admissions.

-> The exam is conducted bi-annually - in June and December cycles.

-> The exam comprises two papers - Paper I and Paper II. Paper I consists of 50 questions and Paper II consists of 100 questions. 

-> The candidates who are preparing for the exam can check the UGC NET Previous Year Papers and UGC NET Test Series to boost their preparations.

More Call By Reference Questions

Get Free Access Now
Hot Links: teen patti gold new version 2024 teen patti joy mod apk teen patti lucky