# Exam Practice Questions

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-14-2009, 03:54 PM
AWPtic
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
• 04-14-2009, 04:46 PM
xcallmejudasx
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.
• 04-14-2009, 05:57 PM
Eranga
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.
• 04-15-2009, 05:48 AM
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. 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.
• 04-15-2009, 06:01 AM
azzaiel
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
• 04-15-2009, 06:09 AM
AWPtic
OOOOOOOHHHH OK. So, the a[1]-- is pretty much just there for no reason, otherwise it's not used at all?
• 04-15-2009, 06:12 AM
Eranga
Quote:

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.

Quote:

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.

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,

Code:

```a[0] = 5; a[1] = 3; a[2] = 1;```
• 04-15-2009, 06:17 AM
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.
• 04-15-2009, 06:26 AM
azzaiel
Quote:

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
• 04-15-2009, 06:28 AM
Eranga
Quote:

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

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] }```
• 04-15-2009, 06:33 AM
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++)
• 04-15-2009, 06:35 AM
azzaiel
ow sorry... i missed that Eranga and AWPtic hahaha a-- hahaha sory so a[1]--
meaning 3 - 1
• 04-15-2009, 06:48 AM
AWPtic
lol ok. Thanks for the help.
• 04-15-2009, 06:56 AM
AWPtic
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?
• 04-15-2009, 07:06 AM
azzaiel
the answer for # 2 is 1234 :p hahahha
• 04-15-2009, 07:38 AM
Eranga
Quote:

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.
• 04-15-2009, 08:24 AM
Eranga
You have this,

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(!=).

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
• 04-15-2009, 08:24 AM
Eranga
Quote:

Originally Posted by azzaiel
the answer for # 2 is 1234 :p hahahha

Check my previous post before this.
• 04-15-2009, 03:44 PM
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. :)
• 04-16-2009, 05:03 AM
Eranga
Quote:

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. :)