Practice AP Exam questions help

jai6638

Golden Member
Apr 9, 2004
1,790
0
0
Hey.. Am taking the practice AP Computer Science A tests to prep for the AP exam..Would appreciate it if you guys could verify if my answers are correct..:

1) When will method whatIsIt cause a stack overflow (i.e., cause computer memory to be exhausted )

Code:
public static int whatIsIt(int x,int y)
{

      if (x>y)
            return x*y;
      else 
            return whatIsIt(x-1,y);

}

A) Only when X<y
B) Only when X<=y
C) Only when X>y
D) For all values of x and y
E) The method will never cause a stock overflow

I chose E .since dont se any reason why it should overflow.......
________________________________________________________________________

2) The boolean expression a==max || !(max !=a) can be simplified to

A) a==max
B) a!=max
C) a<max || a>max
D) True
E) False

I choose A

________________________________________________________________________

3) Suppose an ArrayList list is initialized with integer values. Which of the following will not cause an IndexOutOfBoundsException to be thrown?

A) for ( int i=0;i<=list.size();i++)
list.set(i,new Integer(0));

B) list.add(list.size(),new Integer(0));

C) Integer int0b=list.get(list.size());

D) Integer int0b=list.remove(list.size());

E) list.add(-1,new Integer(0));

have no idea in this case. E and B dont seem right because the keyword New would result in a compile time error. A cant be right either. so am left with C and D
_______________________________________________________________________

4)

//<postcondition>
public static void doSomething(ArrayList a, int i, int j)

{

Object temp=a.get(i);
a.set(i,a.get(j));
a.set(j,temp);
}

Which best describes the postcondition for doSomething?

A) removes from a the objects indexed at i and j
B) resplaces in a the object indexed at i with the object indexed at j.
C) replaces in a the object indexed at j with the object indexed at i
D) replaces in a the objects indexed at i and j with temp.
E) interchanges in a the objects indexed at i and j

I choose E
________________________________________________________________________

5) What will be output as a result of the method call whatsIt(347)?

A) 74
B) 47
C) 734
D) 743
E)347

I choose A
_________________________________________________________________________

6) A large list of numbers is to be sorted in ascending order. Which of the following is a true statement?

A) if the array is initially sorted in descending order, then insertion sort will be more efficient than selection sort.

B) the number of comparisons for selection sort is independent of the initial arrangement of elements.

C) the number of comparisons for insertion sort is independent of hte initial arrangements of elements.

D) the number of data movements in selection sort depends on the initial arragenment of elements.

E) The number of data movements in insertino sort is indepent of the initial arrangement of elements.

I choose D but not sure.
________________________________________________________________________

7) Which of the following will execute without throwing an exceptoin?

A) String s=null;
String t="";
if (s.equals(t))
System.out.println("holy moly!");

B) String s="holy";
String t="moly";
if (s.equals(t))
System.out.println("holy moly!");

C) String s="holy";
String t=s.substring(4);
System.out.println(s+t);

A) A only
B) B only
C) C only
D) A and B only
E) B and C only

I choose E
______________________________________________________________________

8) Consider the array a with values as shown:

4,7,19,25,36,37,50,100,101,205,220,271,306,321

where 4 is a [0] and 321 is a [13]. Suppose that the search method is called with first=0 and last =13 to locate the key 205. HOw many iterations of the while loop must be made in order to locate it?

A) 3
B) 4
C)5
D) 10
E)13

Totally clueless on this one!


I'd really appreciate help with these. Thanks
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
Originally posted by: jai6638


3) Suppose an ArrayList list is initialized with integer values. Which of the following will not cause an IndexOutOfBoundsException to be thrown?

A) for ( int i=0;i<=list.size();i++)
list.set(i,new Integer(0));

B) list.add(list.size(),new Integer(0));

C) Integer int0b=list.get(list.size());

D) Integer int0b=list.remove(list.size());

E) list.add(-1,new Integer(0));

list.add(1,new Integer(0) );

will complie and run.

B: is your anwser.


Originally posted by: jai6638

8) Consider the array a with values as shown:

4,7,19,25,36,37,50, 100,101,205,220,271,306,321

where 4 is a [0] and 321 is a [13]. Suppose that the search method is called with first=0 and last =13 to locate the key 205. HOw many iterations of the while loop must be made in order to locate it?

A) 3
B) 4
C)5
D) 10
E)13

Totally clueless on this one!
Its a simple search
int i =0;
while( array.length> i && array[0]!=key ){
i++;
}
so it would be 10 times loop would run.


if they wanted a binary search it would be 3 iterations.
 

jai6638

Golden Member
Apr 9, 2004
1,790
0
0
Originally posted by: Cooler
Originally posted by: jai6638

Its a simple search
int i =0;
while( array.length> i && array[0]!=key ){
i++;
}
so it would be 10 times loop would run.


if they wanted a binary search it would be 3 iterations.

Wow! that was stupid of me...

list.add(1,new Integer(0) );

will complie and run.

B: is your anwser.

if thats the case, then why wouldn't the others work?

Are the rest of my answers correct?


Thanks much.. appreciate it.
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
1) When will method whatIsIt cause a stack overflow (i.e., cause computer memory to be exhausted )

Code:
public static int whatIsIt(int x,int y)
{

if (x>y)
return x*y;
else
return whatIsIt(x-1,y);

}

A) Only when X<y
B) Only when X<=y
C) Only when X>y
D) For all values of x and y
E) The method will never cause a stock overflow

I chose E .since dont se any reason why it should overflow.......




Answer is A. If x < y then it will go to the recursive function whatisit(x-1,y).since x will again be smaller than y during the second call it will just keep recursing until all memory is gone.
 

totalcommand

Platinum Member
Apr 21, 2004
2,487
0
0
Originally posted by: KB
1) When will method whatIsIt cause a stack overflow (i.e., cause computer memory to be exhausted )

Code:
public static int whatIsIt(int x,int y)
{

if (x>y)
return x*y;
else
return whatIsIt(x-1,y);

}

A) Only when X<y
B) Only when X<=y
C) Only when X>y
D) For all values of x and y
E) The method will never cause a stock overflow

I chose E .since dont se any reason why it should overflow.......




Answer is A. If x < y then it will go to the recursive function whatisit(x-1,y).since x will again be smaller than y during the second call it will just keep recursing until all memory is gone.

if x=y the same wil happen. answer is B
 

jai6638

Golden Member
Apr 9, 2004
1,790
0
0
I see... B makes sense I guess since the x=y will do the same thing as x<y. However, are the rest of my answers correect?


Thanks
 

SaturnX

Diamond Member
Jul 16, 2000
3,415
0
76
Originally posted by: jai6638
Originally posted by: Cooler
Originally posted by: jai6638

Its a simple search
int i =0;
while( array.length> i && array[0]!=key ){
i++;
}
so it would be 10 times loop would run.


if they wanted a binary search it would be 3 iterations.

Wow! that was stupid of me...

list.add(1,new Integer(0) );

will complie and run.

B: is your anwser.

if thats the case, then why wouldn't the others work?

Are the rest of my answers correct?


Thanks much.. appreciate it.

Because an arraylist is 0 based.

So for an array list of 10 items, you index from 0 to 9

For A, it loops through 0 upto, and including the size, so when it trys to set the item in position 10, it'll go out of bounds.

For C, same principle, you trying to execute, list.get(10), but that doesn't exist, only indexes are from 0-9

For D, again the same principle, you can't remove item 10, because item 9 is the highest index.

Also, add behaves differently from set, remove, and get. The add method will increase the size of the arraylist.

--Mark
 

elkinm

Platinum Member
Jun 9, 2001
2,146
0
71
It should be B beet me to it. Don't remember this anymore. So simple just if you remember the proper syntax.