![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
Strategy to Solve these 2 questions
Hi everyone! AP tests are coming (Start next week) and I am taking Computer Science AB's test. While I was reviewing (doing last year's ap test), I found myself have most trouble on these 2 types of question:
The first type is public static int indonesia(int p, int q)
{
if (p/q == 0)
return q;
else
return indonesia (q, p/q);
}
public static int thailand (int p, int q)
{
int temp = -1;
int value = 0;
while (temp != 0)
{
temp = p/q;
if (temp == 0)
{
value = q;
}
else
{
p = q;
q = temp;
}
}
return value;
}
For which values of p and q do indonesia (p,q) and thailand (p, q) return the same result?
(A) For all values of p and q
(B) For all values, such that p > q
(C) For all values, such that p <= q
(D) For all values, such that p > 0 and q >0
(E) For no values of p and qSecond is Boolean Algebra: a && (a || b) a || (a && b) For which values of a and b are the two expressions equivalent? (A) For all values of a and b (B) For no values of a and b (C) Only whenever a is true (D) Only whenever b is true (E) Only whenever both a is true and b is true I hope I can get some help because I couldn't get any help from school....... Thanks in Advance!
__________________
why....why u.s. citizen............. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6
![]() |
i thin kthe second one you plug in true. i think only when a is true. the other one i'm not totally sure, little too much beer to think recursively, but just think about testing for p == 0 rather than p/q == 0...makes it easier.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#3 |
|
Newbie
|
(I took AP COmputer Science A last year and recieved a 5, Iw as going to take AB this year, but too few students signed up for it so it was concelled. I know the stuff though.)
For the first problem: You will want to begin by analyzing indonesia(). Let's say p = 0, then p/q would also be zero, so indonesia would return q. If q was zero, it would through a DivideByZeroException. If p was not equal to zero, then it would go through again with p = q and q = p/q. Let's take two arbitrary numbers, p = 4 and q = 2. p/q = 2, so it calls indonesia(2, 2), 2/2 (p/q) = 1, so it calls indonesia(2, 1). p/q (2/1) is still 2, so it calls indonesia(1, 2). Now, 1/2 is 0.5, BUT since indonesia returns an int, it is truncated to zero, therefore indonesia(4, 2) returns 2. Now we should try with p<q, so let's say p=2 and q=4: step 1: p/q = 2/4 = 0.5, truncated to 0, returns 4. so there is only one step. We see that the method behaves differently for the two situations. So this is what we have found (looking at teh possible answers): - If p>q, the method returns some number (I don't think we need to delve further than that) - If p<=q... if p<q the method returns q, if p = q, the method returns p. - If both p and q are >0, it returns some number. Now for the second method thailand(). ... So, yeah, the only way is to go throgh it logically and possibly plugging in numbers. ~~~~~~~~~~~~~~~~~~~~~~~~ For the first one, if a is true, it is always true since a is true and a or b is true (since a is true) it is false in any other case. For the second statement, if a is false teh whole thing is always gfalse since a is false and a && b is false (since a is false it doesn't amytter what b is). if a is true: true || something else, it is always true. Statement 1: if a = true then true, if a = false then false Statement 2: if a = true then true, if a = false then false Therefore you can eliminate E (since they are equivalent for the above cases), D (since it doesn't matter what b is), C (they are equivalent when a is false), B, (they are equivalent for the cases above). SO I guesswe found teh answer. On the test, some questions will take longer time, some will take one or two seconds, as long as you know exactly what to do and you start doing it immediately you should be fine. And remember to write all over your test, it really helps, especially when working out recursion (you will need to trace the stack which can get really confusing). Hope that helped.
__________________
If you need help with Java, you can go to javaforum.tk and get answers quickly. Programmer YouLikeJava = new JavaProgrammer("Eyvind"); YouLikeJava.printInfo(); YouLikeJava.setWeb("You Like Java?"); |
|
|
|
|
|
#4 |
|
Programmer
|
Well the second one is very easy to solve mathematically:
let's say 1 = "true", 0 = "false". Therefore, for the first statement we have | a | b | (a||b) | a && (a||b)| |----------------------------| | 1 | 1 |___1___|____1_____| | 1 | 0 |___1___|____1_____| | 0 | 1 |___1___|____0_____| | 0 | 0 |___0___|____0_____| and for the second statement we have: | a | b | (a&&b) | a || (a&&b)| |----------------------------| | 1 | 1 |___1___|____1_____| | 1 | 0 |___0___|____1_____| | 0 | 1 |___0___|____0_____| | 0 | 0 |___0___|____0_____| So, if you compare the two tables then you will see that the result is the same for every a and b.
__________________
countdown++; Last edited by HeX; May 6th, 2005 at 8:51 AM. |
|
|
|
|
|
#5 |
|
Programmer
|
Well, for the first one you just need to check the methods for the given options.
A. For all values of p and q, the methods return a number, but we aren't sure what. So, this option fails. B. For all values, such that p > q the methods return a number, but we aren't sure what. So, this option fails. C. For all values, such that p <= q both methods return q. Therefore this is the right answer.
__________________
countdown++; |
|
|
|
|
|
#6 |
|
Newbie
|
Isn't this what I said? w/e
__________________
If you need help with Java, you can go to javaforum.tk and get answers quickly. Programmer YouLikeJava = new JavaProgrammer("Eyvind"); YouLikeJava.printInfo(); YouLikeJava.setWeb("You Like Java?"); |
|
|
|
|
|
#7 |
|
Programmer
|
No. you said that B would be the right answer. And it is not.
__________________
countdown++; |
|
|
|
|
|
#8 |
|
Newbie
|
No, I eliminated E, D, C, and B, leaving the answer as A. I jsut never explicitly stated teh answer to be A, I said, after eliminating everything but A, "SO I guesswe found teh answer."
I'm sorry for not being clear enough, I see how you can think I said that B was the answer, Glad we cleared it up anyhow.
__________________
If you need help with Java, you can go to javaforum.tk and get answers quickly. Programmer YouLikeJava = new JavaProgrammer("Eyvind"); YouLikeJava.printInfo(); YouLikeJava.setWeb("You Like Java?"); |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|