Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default 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. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #4
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    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. #5
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    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.
    It's easy to write a code that computers can understand...
    ... the challenge is to write a code that humans can understand

  6. #6
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    OOOOOOOHHHH OK. So, the a[1]-- is pretty much just there for no reason, otherwise it's not used at all?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by AWPtic View Post
    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 View Post
    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. #8
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    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. #9
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    Quote Originally Posted by AWPtic View Post
    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
    It's easy to write a code that computers can understand...
    ... the challenge is to write a code that humans can understand

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by AWPtic View Post
    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. #11
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    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. #12
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    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.
    It's easy to write a code that computers can understand...
    ... the challenge is to write a code that humans can understand

  13. #13
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    lol ok. Thanks for the help.

  14. #14
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Default

    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. #15
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    the answer for # 2 is 1234 :p hahahha
    It's easy to write a code that computers can understand...
    ... the challenge is to write a code that humans can understand

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by AWPtic View Post
    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. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

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

  19. #19
    AWPtic is offline Member
    Join Date
    Mar 2009
    Posts
    39
    Rep Power
    0

    Smile

    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. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by AWPtic View Post
    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. I need help on a practice code for java
    By Sageinquisitor in forum New To Java
    Replies: 12
    Last Post: 07-13-2010, 05:00 PM
  2. [SOLVED] Is this good programming practice?
    By porchrat in forum New To Java
    Replies: 9
    Last Post: 04-08-2009, 01:54 PM
  3. Replies: 22
    Last Post: 04-06-2009, 04:36 AM
  4. Best practice of IF loop with NOT
    By sathishranganathan in forum Advanced Java
    Replies: 13
    Last Post: 05-29-2008, 01:11 PM
  5. SCBCD 5.0 Mock Exam Questions
    By kks_krishna in forum Reviews / Advertising
    Replies: 0
    Last Post: 04-02-2008, 09:26 AM

Posting Permissions

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