# Thread: Exam Practice Questions

1. Member
Join Date
Mar 2009
Posts
39
Rep Power
0

## Exam Practice Questions

Hi, I have this big exam coming up, so my teacher gave me a practice exam to prepare for the real thing. Unfortunately, he's a terrible teacher and doesn't actually teach us anything. He just expects us to read the book and understand everything. So, I have to come to online sources like this. I have a few questions that I need help with that I don't really understand. If you could, then please explain them to me.

public int m1(int[] a)
{
a[1]--;
return (a[1] * 2);
}
If a = {5, 3, 1}, what is the value in a[1] after m1(a); is run?

1. 4
2. 2
3. 10
4. 6
5. 8

//precondition: x >=0
public void mystery (int x)
{
System.out.print(x % 10);

if ((x / 10) != 0)
{
mystery(x / 10);
}
System.out.print(x % 10);
}

What is the output from mystery(4321)?

1. 43211234
2. 1234
3. 4321
4. 12344321
5. 32144123

2. Nobody will help you unless you show some effort first. Tell us what you think the correct answers are and explain why and then we can give you pointers from there.

3. Think about the each line of the code. Take a peace of paper and pencil, and write down each values on variables and statement. You can easily find the answer. At the end run the code segment on the command prompt or on an IDE.

4. Member
Join Date
Mar 2009
Posts
39
Rep Power
0
Do you seriously think I haven't put any effort into this judas? I've spent plenty of time and put forth a lot of effort. And Eranga, I'm not really wanting to run the code segment on a command prompt or IDE for 2 reasons. (1) I can't use anything like that on the exam. (2) I'll get the answer, but I won't really know how I got it, also I already have the answer to both of the questions, provided by a friend. He tried to explain it to me, but I just didn't understand at all. For the first question, I don't understand what I'm supposed to do with the numbers 5, 3, and 1. On the second one, I get 4.321.

5. Java Code:
```public int m1(int[] a)
{
a[1]--;
return (a[1] * 2);
}```
If a = {5, 3, 1}, what is the value in a[1] after m1(a); is run?
so a is an array with a value of the ff:
a[0] = 5
a[1] = 3
a[2] = 1

a[1]--
will make a[1] equal to 2
the formula is a[1] * 2

substitute the a[1] witch is equal to 3 we will have
2 * 2
so it will return 4
Last edited by azzaiel; 04-15-2009 at 06:36 AM.

6. Member
Join Date
Mar 2009
Posts
39
Rep Power
0
OOOOOOOHHHH OK. So, the a[1]-- is pretty much just there for no reason, otherwise it's not used at all?

7. Originally Posted by AWPtic
Do you seriously think I haven't put any effort into this judas? I've spent plenty of time and put forth a lot of effort. And Eranga, I'm not really wanting to run the code segment on a command prompt or IDE for 2 reasons. (1) I can't use anything like that on the exam. (2) I'll get the answer, but I won't really know how I got it, also I already have the answer to both of the questions, provided by a friend. He tried to explain it to me, but I just didn't understand at all.
If you spend enough time on this where you stuck. Seems to me you don't have enough knowledge of basis.

Originally Posted by AWPtic
For the first question, I don't understand what I'm supposed to do with the numbers 5, 3, and 1.
This is all about arrays. On the argument list you pass an array of type int.

Java Code:
`a = {5, 3, 1}`
This is the content of array, consist of three element. Do you remember that, arrays are zero base index. That means the first element indexed as the zeroth element. In other words,

Java Code:
```a[0] = 5;
a[1] = 3;
a[2] = 1;```

8. Member
Join Date
Mar 2009
Posts
39
Rep Power
0
No, just what really got me was the decrementing part, as well as my friends explanation. But yes, I do need to review the basis, I just wish there was like one website that has all of them there.

9. Originally Posted by AWPtic
No, just what really got me was the decrementing part, as well as my friends explanation. But yes, I do need to review the basis, I just wish there was like one website that has all of them there.
life is never easy dude

10. Originally Posted by AWPtic
No, just what really got me was the decrementing part, as well as my friends explanation. But yes, I do need to review the basis, I just wish there was like one website that has all of them there.
In Java you two types of increments(decrements too).

1. Post increment - do the increment after the statement execution.
2. Pre-increment - do the increment before the statement execution.

This is common for decrement as well.

Following is what you have

Java Code:
```public int m1(int[] a)
{
a[1]--;   // At the end of this statement, a[1] = 2
return (a[1] * 2);   // So this is equal to, [B]return (2 * 2);[/B]
}```

11. Member
Join Date
Mar 2009
Posts
39
Rep Power
0
So, the actual answer is 4, not 6? Also, a post increment would be like a for loop? ex: for(i = 2; i => 0; i++)

12. ow sorry... i missed that Eranga and AWPtic hahaha a-- hahaha sory so a[1]--
meaning 3 - 1
Last edited by azzaiel; 04-15-2009 at 06:52 AM.

13. Member
Join Date
Mar 2009
Posts
39
Rep Power
0
lol ok. Thanks for the help.

14. Member
Join Date
Mar 2009
Posts
39
Rep Power
0
Also, what about the other question? I think that one confused me more than the first question. What are the steps in figuring out this problem?

15. the answer for # 2 is 1234 :p hahahha

16. Originally Posted by AWPtic
So, the actual answer is 4, not 6? Also, a post increment would be like a for loop? ex: for(i = 2; i => 0; i++)
In this case only. You can use the post/pre increment in for loops as well. Say you are starting from the maximum value and reach to the minimum. So need to decrease the count.

17. You have this,

Java Code:
```public void mystery (int x)
{
System.out.print(x % 10);

if ((x / 10) != 0)
{
mystery(x / 10);
}
System.out.print(x % 10);
}```
and x = 4321.

Here you have three operators, module(%) division(/) and inequality(!=).

Java Code:
`mystery(x / 10);`
What's the above line of code does? Recursion, since the condition is not satisfied same method calls again within the method.

First print() method prints the 1 on the console, because module(reminder) of 4321 is 1.

Output is 1.

Then check the condition and it's true. Call the method with 432. Module of 10 is 2.

Output is 12.

Then check the condition again, x = 432. Evaluates true and calls the method with 43 again. Module of it is 3.

Output is 123.

Check the condition and calls the method again with 4. Module of 10 is 4, so..

Output is 1234.

Check the condition. 4 / 10 equals to zero, and condition flies.

Now check that there is a print() method at the end. Each time you call the same method recursively this call, how? Think about the logic. In any case new x value is not assign to anything? Event to x itself. So finally,

Output is 12344321

18. Originally Posted by azzaiel
the answer for # 2 is 1234 :p hahahha
Check my previous post before this.

19. Member
Join Date
Mar 2009
Posts
39
Rep Power
0
Azzaiel, nice try again lol. Eranga, thank you very very much for helping me out. You explain everything wayyyyyy better than my stupid Java book could. I really appreciate it. Also, maybe one day I'll understand the Java language well enough to where I can come here and help people out. :)

20. Originally Posted by AWPtic
Azzaiel, nice try again lol. Eranga, thank you very very much for helping me out. You explain everything wayyyyyy better than my stupid Java book could. I really appreciate it. Also, maybe one day I'll understand the Java language well enough to where I can come here and help people out. :)

And also this must be your target, learn it well and try to help others. You feel much better lol because of that. I know it in my experience.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•