Download Question 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Question 1
Draw the state of the stack after invoking each method in the following sequence of
methods. Assume that the stack is initially empty
push(2) , push(1), Pop(), push(4), push(5), Peek(), Pop(), Pop()
Answer
5
4
1
2
2
2
2
4
2
5
4
2
4
2
2
Question 2
What is the final state of the Stack after applying the following sequence of methods
in an initialized empty stack?
Push("ALi"), Push("Mohammed"), Peek(),Push("Ahmed"), pop(), pop()
Answer
Ali ,
Question 3
Draw the state of the queue after invoking each method in the following sequence of
methods. Assume that the queue is initially empty
add(1), add(2), remove(), add(4), add(5), remove(), remove()
Answer
5
4
1
2
2
5
1
2
4
4
2
5
Question 4:
Write a program that reads 10 integers number and prints them in reverse order. Use
a stack.
Answer
public class Reverse{
public static void main (String [] arg) {
Scanner input=new Scanner(System.in);
int x;
Stack<int> s = new Stack<int>();
System.out.println("Enter 10 numbers");
for(int i=1;i<=10;i++)
x=input.nextInt();
s.push(x);
}
System.out.println("the numbers in reverse order is");
while(!s.empty())
{
}}
System.out.println(s.pop()); }
Question 5
Write a method indexOf() that takes one argument, an element target, and returns the
index of the first occurrence of target in the ArrayList, or -1 if the target does not
appear
Answer
public int indexOf( E target)
{
for(int i=0;i<size;i++)
if(get(i)==target)
return i;
return -1;
}
Question 6
What is the order of the expression?
-
5 n log 3n
-
3 n2 + n log n
-
5 n4 + 1000 n2 + 105
answer
-
O ( n log n)
-
O (n2)
-
O (n4 )
Related documents