Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Help Reviewing For Exam  RSS feed

 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I been going over some review question for a exam I going to be having in java and I would like to check with you guys if what I have is right:

Q.Write a method that takes two int parameters and return he smaller of the two values. Call your method getSmaller

My Answer:

public int getSmaller(int num1, int num2)
{
if(num1 < num2)
{
return num1;
}
else
return num2;
}
---------

Assume that boolean done = false , int x = 10, int y = 6, String s = �Help� and String t = �Me�

True or False?

Q.The expression (s.concat(t).length() < y) is True(True is part of the question)

I am not sure what it is like s is �Help� so that is 4character or 3characters and t is �Me� so that is either 2characters or 1character.

So it is either 6 charcters or 5characters. I don�t know what it is like I know a lot of things like to start counting at zero like enum types(can you list other ones what start counting at zero or is there some rule I can follow to determine if they count at 1 or zero).

So if it is 6 then 6 is not < y(6) so this would be False
But if it is 5 then 5 is < y(6) so it would be True.

----------
Q.How many times will the System.out.println(*); statement execute inside of the following nested for-loops show you calculation:

for(int j=0; j< 5; j++)
for(int k=1; k<=j; k++)
System.out.println(�*�);

So I ran this in netBeans and it gets I think it was 10 stars I keep getting 5 stars when I do it in my head so something is wrong.

First I don�t know how to approach these very well like to me this is how it runs down

for(int j =0; 0 <5; j++)
for(int k = 1; 1<=0; k++)
This does not happen since 1 is not less then 0 so now does it increment k by 1 or what happens?

So I really need a step by step break down how everything works. Like I know you first start with the int to start it off then you do the middle(0<5) and you increment it but what happens if the middle failed? Does it stops there and does not go to the increment?

If that is true then I only could 5 stars.

So I need a good way of doing this.

---------

I really don�t even know what this question is asking.

Q.What is wrong with the following assignment statement? Assume x and y are both String objects String z = x.equals(y);

That�s all I have for now.
 
Jeremy Tartaglia
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first question is right, although it could be condensed a bit:



Q.The expression (s.concat(t).length() < y) is True(True is part of the question)

You're confusing length with index. Length is always the number of characters present, so in this case, 4+2=6 so the answer is False. The max indices on s and t are 3 and 1 respectively, since they start from zero: 0, 1, 2, 3 = H, e, l, p.



This is a simple math puzzle. You're right about the first loop. When i=0, the inner loop does nothing. From there, it goes:



The sum comes down to 1 + 2 + 3 + 4 = 10 since the internal loop prints i stars. Once you see that, all you have to do is sum all the i's: 0+1+2+3+4=10.


Sorry, missed the last question crammed in there. To understand what's wrong with String z=x.equals(y), look at a simpler problem of the same nature:



That should make it more clear.
[ February 11, 2006: Message edited by: Jeremy Tartaglia ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a method that takes two int parameters and return he smaller of the two values...

It looks good to me, but have you tested this code?

I am not sure what it is like s is "Help" so that is 4 character or 3 characters...

Again, writing some test code is the best approach.

The String "Help" is 4 characters, so it has a length of 4. You can verify this by calling System.out.println(s.length()). What might be confusing is String's indexing of these characters, because the index starts at zero. So s.charAt(0) = 'H' and s.charAt(3) = 'p'.

I ran this in netBeans and it gets I think it was 10 stars I keep getting 5 stars when I do it in my head so something is wrong...

In a for loop, the boolean test is done at the beginning ("top") of each loop. If true, the body of the loop executes. If false, the body is skipped and execution continues outside that loop. Note that the incrementing of the variable is done at the end ("bottom") of each iteration.

To help see what's happening here, compile and run this modified version of the code with the added println's...

What is wrong with the following assignment statement? Assume x and y are both String objects String z = x.equals(y);

On the left of the assignment (=), we have a variable of type String. On the right, we have an expression. What is the type of this expression? (What is returned by String's equals method?) Can this be assigned to a String variable?
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok now I get it with the for loop. Same thing happened to me with a while loop question. Like I keep thinking that once the int is initialized it is initialized for good.

So like I thought it goes int j=0 then 0<5 j++
then it goes to int k = 1, k<=j k++

then I just continued with the original so the 2nd time around it was 1 time then it get incremented to 2. stops then it goes back to the out loop and once it gets to that other loop it has k as 2 so 2<=2 is another star.

I got to remember that it get reinitialized I gotten in my head for some reason I think ones it get�s initialized that it never gets reinitialized again.

I get it now with the length I just got to review that stuff so I remember that but I don't still get the last question it still does not make sense to me.

code:

int x,y;
int z=(x==y);

This also does not make to much sense to be like I know you are trying to see if x is equal to y then I guess you put in Z.

And going back to the original part of the equation I would not know what to put down as an answer.
 
Jeremy Tartaglia
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does x==y mean? What are the possible values for x==y? To what (intrinsic) data type does that relate in Java? Take your time, answer all three.
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeremy Tartaglia:
What does x==y mean? What are the possible values for x==y? To what (intrinsic) data type does that relate in Java? Take your time, answer all three.



1st: asks if x is equal to y
2nd: Not sure what you mean by possible values? like what kind of types they could be? like x could be int?
3rd: not either what you want.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Hubele:
...1st: asks if x is equal to y
2nd: Not sure what you mean by possible values? like what kind of types they could be? like x could be int? ...

Yes, the question is what data type is "x==y"? And then: Can this data type be assigned to the variable z (considering z's declared type)?
 
Michael Hubele
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well in the orignal question they say x and y are Strings. So I think you can compaire a string to a string. And I think they could be applied to z since it is also a string.
 
Jeremy Tartaglia
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so you answered number one correctly. x==y means "is x equal to y". Now, how could you respond if someone asked you "is x equal to y"? Those are the possible values for "x==y".
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

More precisely, what is "it"? (The result of x==y.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!