https://www.liquidpoker.net/


LP international    Contact            Users: 673 Active, 2 Logged in - Time: 04:33

YO JAVA XPERTS

New to LiquidPoker? Register here for free!
Forum Index > Poker Blogs
fira   United States. Feb 10 2009 16:58. Posts 6345
I need halps, and my midterm is in an hour!!

So I have access to a practice midterm, but not the answers. I don't know why the prof decided not to release them. Anyways it doesn't matter much since I can just compile and run the code for answers.

A few questions:


  1. What is the output of this program fragment?

double X = 123.321;
String Y = "Hi!";
System.out.format("%7.3f%s", X, Y);



I dunno what System.out.format is. Wtf is %7.3f%s?

(spoiler = solved)
+ Show Spoiler +




  15. Suppose you wanted to use a function to initialize three (already declared) variables that are not fields of a class. Further suppose you are doing this in a language that allows passing parameters either by value or by reference. To initialize the variables,

A. you should pass them to the function by reference and set the values in the function; the variables will be initialized when the function returns.
B. you should pass them to the function by value and set the values in the function; the variables will be initialized when the function returns.
C. you should pass them to the function by value, set the values in the function, then return these values via a return statement.
D. define these variables so they are global to the function, then set their values within the function; that is the preferred approach.
E. you should adopt another approach: none of the above methods is a reasonable approach.



I mentioned that I don't have the answers and this is one question I can't "test." I was thinking A but am not sure. You're supposed to pass variables to functions by reference, right?


  16.Here is a list of steps to be taken when a function with a parameter is called:

I. execute the function (and use the formal parameter during its execution)
II. delete the temporary storage associated with the formal parameter
III. do whatever work is necessary to determine the actual parameter‘s value
IV. do whatever work is necessary to resolve the actual parameter to a location in memory
V. create temporary storage with the name of the formal parameter
VI. copy the value of the actual parameter to the temporary storage location
VII. copy the memory location of the actual parameter to the temporary storage location

Now suppose a function is called with a parameter that is passed by reference. In order, what steps are taken to execute the function, from the start of the call through its completion, that involve this parameter? (The lists below give steps in order when read left to right.)

A. III, V, VI, I, II
B. IV, V, VII, I, II
C. V, VII, III, II, I
D. V, VI, IV, II, I
E. IV, V, VI, II, I



LOL NO CLUE ABOUT THIS ONE.

But yea, since I only have an hour please just whoever respond with whatever answer you have asap! Thanx a lot

0 votes

Facebook Twitter
 Last edit: 10/02/2009 17:14

danken   United States. Feb 10 2009 17:04. Posts 19

edit: sry i was wrong about #2. it should be the 1st 4 letters like u said

 Last edit: 10/02/2009 17:08

fira   United States. Feb 10 2009 17:08. Posts 6345


  On February 10 2009 16:04 danken wrote:
#2 is hmm changed names becuz (0,4) goes from 1st letter to 5th letter (0,1,2,3,4)


does this mean that nickName is actually "Smedl"? I did System.out.print(nickName) and it printed out Smed

/confused


danken   United States. Feb 10 2009 17:10. Posts 19

yea my mistake. i think the reason it isnt working is becuz when u use strings u have to use the .equals() function to compare them.


fira   United States. Feb 10 2009 17:12. Posts 6345


  On February 10 2009 16:10 danken wrote:
yea my mistake. i think the reason it isnt working is becuz when u use strings u have to use the .equals() function to compare them.


sweet, that was it.

so .equals() is for strings, and == is for ints and doubles?


Muhweli   Finland. Feb 10 2009 17:12. Posts 10663

1) %7.3f tells (afaik) that it'll shows the float in #######.### (three decimals), %s means string.
3) D works but it's generally iffy. I'd personally pass them as references AND initialize them in the function, but that's not an option -> so I guess that'd be the other. B does not work. C doesn't work.

4) I think it can be deduced from the first step. I'm KINDA sure it starts with a V. So it's proobably D! Because when you call a function with a parameter, it does create a temporary parameter and copies the given parameter to it.

Floofy says: my dick is easily bigger than 90% of guys i checked it on the net | Floofy says: i im also doing movements  

danken   United States. Feb 10 2009 17:12. Posts 19

yes im pretty sure for numbers u use == and for strings u use .equals(). rest of the questions im not going to attempt becuz i haven't done java in a while


Muhweli   Finland. Feb 10 2009 17:13. Posts 10663


  On February 10 2009 16:12 fira wrote:
Show nested quote +


sweet, that was it.

so .equals() is for strings, and == is for ints and doubles?


Yeah, I'm pretty sure == for strings compares memory locations instead of string content. I could be confusing it to C++ though.

Floofy says: my dick is easily bigger than 90% of guys i checked it on the net | Floofy says: i im also doing movements  

tec27   United States. Feb 10 2009 17:13. Posts 173

1) Format is basically used to put strings together with variables. Chracters after '%' specify different variable types for the function to expect as parameters. In this case, its passing in a floating point value, and the numbers (7.3) specify width and precision of the end string result ( http://tinyurl.com/ccdnz6 )

4) The output of this one is going to be sort of unexpected, but thats because you have to understand how Java works with non-simple types. Strings are objects, which means that any time you have a String variable, what you actually have is a reference to a string. Because of this, you cannot compare strings properly using the '==' operator, since with object references, this merely compares their addresses. The proper way is to do myString.equals('whatever'), which will compare the actual characters contained within rather than the address of each string.

15) You are correct, the answer is A. The reason behind this stems from proper design, and limitations of pass-by-value. Using answer D would be bad design, since you should attempt to avoid global variables. C is fairly impossible, since you can't return 3 values with a return statement without creating a new object, pretty much. If you used B, you would not be able to affect the values of the *actual* variables, you could only set the values of the variables local to the initialization function. So A is the only possible answer, since by passing them in by reference, you allow the function to modify the values of the actual variables.

16) We can get rid of B and E right away, since they signal a pass-by-value parameter, rather than pass-by-reference. C doesn't make any sense because it is deleting storage associated with the formal parameter before the function is even executed. Same with D. So it would have to be A. (I think anyway, I'm not 100% sure about this one and I can't really explain myself too well on it )


fira   United States. Feb 10 2009 17:22. Posts 6345

Thx so much all

GG midterm


Muhweli   Finland. Feb 10 2009 17:25. Posts 10663

tec27,

15) The only possible correct answer is either A or a different solution. But in A, it says the variables are initialized once the function returns, when they're in fact being initialized IN the function. So I guess it's sort of how you look at it, but I think it should be A except with the initialization happening in the function, no?

16) Myea I didn't see the rest of the phases through, but I guess it could be A. Too if it actually does first determine the actual variable's value. Just sounded kinda weird. But yea, now that you said it, it can't be C, D or E.

Doesn't A do the pass by value thing? I mean it says it first finds out the value, but Java passes parameters as references. Isn't B the only one that actually just handles the memory locations as it should. A copies the actual value.

Floofy says: my dick is easily bigger than 90% of guys i checked it on the net | Floofy says: i im also doing movements  

KingKory   United States. Feb 10 2009 17:25. Posts 2083

#1 System.Out.Format:

%7.3f formats the parameter to a floating point number with 7 integers, and 3 decimal places.

%s formats the parameter as a string.

You can figure out the output.

#15

A. Proper coding says you should never pass a variable by reference unless absolutely necessary because you have multiple places in which the variable itself can be updated.

B. You cannot initialize a variable passed by value within a function.

C. This is your most code-sound option. Although you're taking up more memory on the disk by creating another array to return response, you're limiting the variables' exposure.

D. Global variables are discouraged unless absolutely necessary. You usually don't want your variable exposed to every faucet of your application.

E. Since you are initializing the variable, why does it need to be involved in the parameter? It doesn't mention anything about the output being dependent upon what the variable is set to. I would say that you use a parameter-less function, which initializes a variable, then returns it. You can then save this response to your original declared variables. Note: you need to call this function once for each variable, but I still believe this is your most efficient option.

#16

Douchebagsayswhat?


fira   United States. Feb 10 2009 17:29. Posts 6345

Another question regarding #1, what if Y was an int?

Would the code look like:

double X = 123.321;
int Y = 25;
System.out.format("%7.3f%i", X, Y);

?

 Last edit: 10/02/2009 17:29

tec27   United States. Feb 10 2009 17:33. Posts 173


  On February 10 2009 16:25 Muhweli wrote:
tec27,

15) The only possible correct answer is either A or a different solution. But in A, it says the variables are initialized once the function returns, when they're in fact being initialized IN the function. So I guess it's sort of how you look at it, but I think it should be A except with the initialization happening in the function, no?

16) Myea I didn't see the rest of the phases through, but I guess it could be A. Too if it actually does first determine the actual variable's value. Just sounded kinda weird. But yea, now that you said it, it can't be C, D or E.

Doesn't A do the pass by value thing? I mean it says it first finds out the value, but Java passes parameters as references. Isn't B the only one that actually just handles the memory locations as it should. A copies the actual value.




With regard to 15, I think it has something to do with the underlying mechanisms of pass-by-reference (IE: maybe it temporarily creates a copy of the variable in memory, then assigns whatever the value is to the actual variable upon function end? I don't think thats how it works, but it could be), or its just a misstatement.

With 16, I think thats fine. When you pass by reference, you're basically passing a pointer. The only difference is that the function that receives that pointer automatically dereferences it to a normal variable type when it starts. B uses IV, which should never be used in a pass-by-reference function, I think, since like I said, pass-by-reference is already passing in the address. I'm not sure though, I don't recall every really looking at the stack-manipulation stuff for pass-by-reference calls.


tec27   United States. Feb 10 2009 17:35. Posts 173


  On February 10 2009 16:29 fira wrote:
Another question regarding #1, what if Y was an int?

Would the code look like:

double X = 123.321;
int Y = 25;
System.out.format("%7.3f%i", X, Y);

?


%d would be the delimiter you want rather than %i, but it would print 25 directly after the formatted double.


Muhweli   Finland. Feb 10 2009 18:13. Posts 10663

Yeah, it's been a while since I worked on such low level stuff. I'm all about bigger abstraction level, lulz. Haven't taken the course in compiler programming yet.

Floofy says: my dick is easily bigger than 90% of guys i checked it on the net | Floofy says: i im also doing movements  

tec27   United States. Feb 10 2009 18:24. Posts 173


  On February 10 2009 17:13 Muhweli wrote:
Yeah, it's been a while since I worked on such low level stuff. I'm all about bigger abstraction level, lulz. Haven't taken the course in compiler programming yet.


Yeah, we covered a fair amount of low-level stack stuff in my Computer Organization class (and I've seen it from reverse engineering SC ), but most of that coverage stops before you ever get into OO stuff.


 



Poker Streams

















Copyright © 2024. LiquidPoker.net All Rights Reserved
Contact Advertise Sitemap