Jump to content

Java (someboddy plz :P)


Recommended Posts

Well, the ap test for my java class is in 2 months...., I'm screwed :thumbsdown:'. My teacher tells me the test revolves around general questions (as in, no coding, just assumptions...).

 

Ex: Bob and Garry are working on a class named myDoggyCounts. myDoggyCounts has an arraylist of 10. The arraylist is filled with only positive integers and no even numbers. What can you conclude of this?

 

I. The arraylist will always have a positive integers (to easy compared to the real test)

 

II. The arraylist will be sorted

 

III. The arraylist will never have the number 0

 

A.I only

B.II only

C.III only

D.III and II

E. None

 

Most of the questions are like this... and my teacher started to give us practices similier to this and I haven't passed a single one. I have no logic in my head, can you show me how to stick it in my head?

 

Ps. I'll edit this post in a few with a real question from one of the sheets.

Link to comment
Share on other sites

public class Person{

private String firstName;

private String lastName;

private int age;

 

public Person(String gn, String ln, int a) {

firstName = fn;

lastName = ln;

age = a;

}

 

public String getFirstName(){return firstName;}

public String getLastName(){return lastName;}

public int getAge(){return age;}

 

 

Assume that Variable p1 and p2 have been declared as follows.

 

Person p1,p2;

 

Which of the following is the best way to test whether the people represented by p1 and p2 have the same first name?

 

a.p1 == p2

b.p1.getFirstName().equals(p2.getFirstName())

c.p1.getFirstName() == p2.getFirstName()

d.p1.equals(p2)

e.p2.equals(p1)

This is a question that struck me a while back... I put down B so but I never found out if it was correct or not.

 

Three Algorithms are being considered to look for a given value in an unsorted array of integers.

    Algorithm 1:Use binary search

    Algorithm 2:Use sequential search

    Algorithm 3:Sort the array, then use binaray search.

 

Which of the following statements about the three algorithms is true?

 

a.All three will work; Algorithm  1 will be most efficient.

b.Only Algorithms 1 and 2 will work; Algorithm 1 will be most efficient.

c.Only Algorithms 1 and 3 will work; Algorithm 1 will be most efficient.

d.Only Algorithms 2 and 3 will work;Algorithm 2 will be most efficient.

e.Only Algorithms 2 and 3 will work;Algorithm 3 will be most efficient.

 

Thats the type of question given most of the time. I put A on that question.

Edited by Drake
Link to comment
Share on other sites

The most efficient way is for the list to be already sorted, and then doing the binary-find. However as the list is unsorted, you will need to sort it before doing the binary-find.

 

As usual with school questions, you need to make assumptions with insufficient specifications.

 

The "most efficient" depends on what sort technique is used, how many members in the array, and how much memory is available.

 

If we assume that the sort happens instantly, then answer (e) is correct. If the array only has 2 members, obviously answer (d) will be correct.

Link to comment
Share on other sites

For the first question, you are right - B is the correct answer.

 

 

As for the second question, algorithm 1 will definitely not work, since the array is not sorted. Therefore, answers 1,2&3 are wrong.

 

Any sorting algorithm must compare every memeber of the array to at least one value(this could be the value of another member, or a support variable). Otherwise, Any value could be put inside it, and the algorithm will not change it's position. The best possible case for sorting an array in a given size a sorting algorithm can get is one checking action for each memeber when the array is already sorted.

 

The worst case for a sequential search of a given array is when the value you need to search for is not found. In that case, every member have to be checked once. So the worst case for a sequential search is checking action for each memeber.

 

 

So, for a given array size, the worst case of scanning it equals to the best case of sorting it. This is convincing enough, but there is more - after the sorting in algorithm 3, you still need to do the binary search. Binary search is fast, but in algorithm 2, you have already found the answer.

 

 

Conclusion:

For a given array size, squential search is always faster than sorting and binary searching. The correct answer is d.

 

 

Notice that if you plan to do many searches in the same array, it will be more efficient to sort it first. However, this is not the case here.

 

 

 

 

BTW, if you want us to really help you, show us how you solve(or trying to solve. If you succeed, you don't need our help.) those questions.

Link to comment
Share on other sites

Well for the first question I remembered what each of the dots ment. (IE:.Equal). So I went in order from left to right to see which one made sense or had been typed correctly. The second one I was just dumbstrucked since I don't know the speeds of array searching since(now I do after I googled it). I will post up more questions later since I have to go type up an essay and am expecting another questinare on monday.

 

Btw: Thanks for the help, I really hope I pass this test if not I get stuck in Health class instead of programing next year >.>

Edited by Drake
Link to comment
Share on other sites

Rethinking about it, a sorting algorithm might do n-1 checks on an already sorted array - if it checks every member against the next one, the last member does not need to be checked. However, there still is the binary search action, so best case for sorting+binary-search is equal to worst case for sequential-search, and we are talking about an hipotetical sorting algorithm, and I did not take into accound the control actions, which are heavier in sorting+binary. So I still think d is the answer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...