Quantcast

Maximum PC

It is currently Tue Jul 22, 2014 4:49 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 16 posts ] 
Author Message
 Post subject: 3 Multiple Choice C++ Questions
PostPosted: Fri Oct 08, 2004 9:04 am 
Coppermine
Coppermine
User avatar

Joined: Sat Jul 03, 2004 10:56 am
Posts: 709
When parameters are passed between the calling code and the called function, parameters and arguments are matched by:

a. their data type
b. their relative positions in the parameter and argument lists
c. their names
d. they are not matched up at all



Which of the following are valid function calls to the tabs function?

a. fabs(3.5);
b. cout << fabs(3.5);
c. cin >> fabs(3.5);
d. fabs(cin >> x);
e. a and b



What is the value of i after the following function call?

//function definition
int doSomething(int value)
{
value = 35;
return value;
}

//fragment of main program
int i=0;
cout << doSomething(i);

a. 13
b. 35
c. 48
d. 0


Top
  Profile  
 
 Post subject:
PostPosted: Fri Oct 08, 2004 3:39 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
I believe:
B
E
B

Not sure about the first one, though, and I think it might depend on the calling convention.


Top
  Profile  
 
 Post subject: Re: 3 Multiple Choice C++ Questions
PostPosted: Fri Oct 08, 2004 4:02 pm 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
Gigahertz17 wrote:
When parameters are passed between the calling code and the called function, parameters and arguments are matched by:

a. their data type
b. their relative positions in the parameter and argument lists
c. their names
d. they are not matched up at all



Which of the following are valid function calls to the tabs function?

a. fabs(3.5);
b. cout << fabs(3.5);
c. cin >> fabs(3.5);
d. fabs(cin >> x);
e. a and b



What is the value of i after the following function call?

//function definition
int doSomething(int value)
{
value = 35;
return value;
}

//fragment of main program
int i=0;
cout << doSomething(i);

a. 13
b. 35
c. 48
d. 0


Ah, what the heck:

1) A
2) E
3) B

As for the first question, I'm guessing on this one as I totally forgot.
As for the second question, the reason why it's A and B is because:
if the function fabs returns nothing, then a call to fabs(3.5); would indeed be legal. Otherwise, if it returned something, a variable of some sorts has to receive the returned value (like x = fabs(3.5); ). Hence, the cout << fabs(3.5) ; is legal.

The second one, duh, it's 35. You pass a value to the parameter "value" yet you initialize the value of "value" to 35, regardless of what number you pass into the function doSomething(). Hence, it's B.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Oct 08, 2004 4:23 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Yeah, I'm only really interested in finding what the answer to the first question is, although it seems pretty useless knowledge :)


Top
  Profile  
 
 Post subject:
PostPosted: Sun Oct 10, 2004 3:59 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
B - E - D. ;)


Top
  Profile  
 
 Post subject: Re: 3 Multiple Choice C++ Questions
PostPosted: Sun Oct 10, 2004 4:02 am 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
DJSPIN80 wrote:
The second one, duh, it's 35. You pass a value to the parameter "value" yet you initialize the value of "value" to 35, regardless of what number you pass into the function doSomething(). Hence, it's B.

You might want to review "pass by value".


Top
  Profile  
 
 Post subject: Re: 3 Multiple Choice C++ Questions
PostPosted: Sun Oct 10, 2004 8:31 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
Gadget wrote:
DJSPIN80 wrote:
The second one, duh, it's 35. You pass a value to the parameter "value" yet you initialize the value of "value" to 35, regardless of what number you pass into the function doSomething(). Hence, it's B.

You might want to review "pass by value".[/quote

Yeah, it's pass by value. If the function would have been int doSomething(int &value); then it would have been different. Regardless, since the function parameters pass the value not the reference, it will always amount to 35.


Top
  Profile  
 
 Post subject: Re: 3 Multiple Choice C++ Questions
PostPosted: Sun Oct 10, 2004 1:45 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
DJSPIN80 wrote:
Yeah, it's pass by value. If the function would have been int doSomething(int &value); then it would have been different. Regardless, since the function parameters pass the value not the reference, it will always amount to 35.

Let's walk-thru this problem:

Question: What is the value of i after the following function call?
The valueof i. Not the value that is sent to stdout.

//fragment of main program
int i=0;

At this point, the value of i is 0.

out << doSomething(i);
Here we pass a copy of the value of i to the function doSomething, which has a seperate integer variable value, and output the return value of doSomething to stdout. Notice that we have not changed the value of i though. Only passed a copy of the value of i. Therefore, i = 0 regardless of what happens in the function. If this was a pass by reference, then i would be = 35. The output sent to the screen is from the variable value which is equal to 35.


Top
  Profile  
 
 Post subject: Re: 3 Multiple Choice C++ Questions
PostPosted: Mon Oct 11, 2004 5:26 pm 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
Yeah, I didn't pay attention to the question. Oh well, i = 0, no matter, yes. I just didn't read the question correctly. Oh well.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Oct 11, 2004 7:41 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Then can someone explain this to me?

My code:
Code:

#include <iostream> //function definition

int doSomething(int value)
{
value = 35;
return value;
}

//fragment of main program
int main()
{

   int i=0;
   std::cout << doSomething(i);
   std::cin.get();
}


My output:
Quote:
35

?
Compiled with VC++ 2005.

Did I make an error in the code?


Top
  Profile  
 
 Post subject:
PostPosted: Mon Oct 11, 2004 7:45 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
It is outputting the value returned by the function, not the value of i. Add a cout << i << endl; below the doSomething to get the value of i.


Top
  Profile  
 
 Post subject: Re: 3 Multiple Choice C++ Questions
PostPosted: Mon Oct 11, 2004 7:45 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
DJSPIN80 wrote:
Yeah, I didn't pay attention to the question. Oh well, i = 0, no matter, yes. I just didn't read the question correctly. Oh well.


Yeah, I figured you weren't paying attention to the question. :)


Top
  Profile  
 
 Post subject: Re: 3 Multiple Choice C++ Questions
PostPosted: Mon Oct 11, 2004 7:46 pm 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
Gadget wrote:
DJSPIN80 wrote:
Yeah, I didn't pay attention to the question. Oh well, i = 0, no matter, yes. I just didn't read the question correctly. Oh well.


Yeah, I figured you weren't paying attention to the question. :)



hehehe... :oops:


Top
  Profile  
 
 Post subject:
PostPosted: Mon Oct 11, 2004 7:48 pm 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
Gadget wrote:
B - E - D. ;)



Isn't it that monosyllabic, three letter word that programmers never see, but have only heard from significant others/spouses? Wow, I don't even remember what a "B E D" is...


Top
  Profile  
 
 Post subject:
PostPosted: Mon Oct 11, 2004 7:52 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
DJSPIN80 wrote:
Gadget wrote:
B - E - D. ;)



Isn't it that monosyllabic, three letter word that programmers never see, but have only heard from significant others/spouses? Wow, I don't even remember what a "B E D" is...

Exactly my thoughts when I was answering it. Check the time stamp. :)


Top
  Profile  
 
 Post subject:
PostPosted: Mon Oct 11, 2004 7:59 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Oh, sorry, I didn't read the question carefully enough!
I was just looking at the output.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group