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?

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.

Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.

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.

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.

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,

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,

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

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

It's pleasure to help you.

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.