Assignment 1
Assignment 1
Assignment#1
Important Instructions for Students
1. Write your name and ID.
2. Answer all the questions in the provided space.
3. All the questions have to solved step by step.
4. Total marks are 3.
Student ID: Student Name:
1) Show the content of the array nums after tracing the following java code.
int nums[]={5,3,7,9,1,6,4};
for(int i = 0, start = 0; i < nums.length; i++){
if(i == 0)
start = nums[i];
if(i == (nums.length -1)){
nums[i] = start;
break;
}
nums[i] = nums[i + 1];
}
Answer
2) What are the Time efficiency and Big-O for the following method?
void myFun(int [][]t, int n) {
int val=4;
int i,j;
for(i=0;i<=n;i*=2)
for(j=0;j<=n;j++)
t[i][j] = t[i][j]+ val*i;
for(i=0;i<=n;i+=val)
System.out.println(t[i][j]);
}
Answer
5) Show the content of the array M after execution the following code
int M[ ]={5,4,7,8,1,2};
int c, i, n=6;
c=M[0]
for(i=0;i<n-1;i++)
M[i]=M[i+1]
M[n-1]=c;
Answer
6) Suppose that you have these elements {2, 9, 1, 3, 6} show how you can perform the
following operations:
a. Create an ordered linked list for these elements.
b. Insert the element 5 to this linked list.
c. Delete the element 6 from this linked list.
Answer
7) Suppose that you have these elements {10, -40, 20, -30, 60} show how you can perform the
following operations:
a. Create an ordered linked list for these elements.
b. Insert the element 30 to this linked list.
c. Delete the element 40 from this linked list.
Answer
8) Convert these expressions to postfix using stack operations:
a) Z = a + ( x + 5 ) / 3
b) K = 9 * 7 * 2 – 3 + 6
c) M = a - ( c + 2 ) / ( b – 1) + 3
Answer
9) Consider an empty stacks S1 and S2 with size = 4, what is the content of the stacks and its
top after every push and pop operations.
int a[7]={4,3,8,6,2,1,10};
for(i=0;i<7;i++)
if(a[i]%2==0)
S1.push(a[i]);
else
S2.push(a[i])
for(i=0;i<3;i++){
S1.pop();
S2.pop();
}
Answer
head 10 5 20 30 null
Trace the following pseudo-code and show the change in the pointers
p=head
while(p.link!=null)
{
r=p;
p=p.link;
}
r.link=null;
Answer
11) Suppose that you went to Panda (super market) in order to buy some things, and after you go to
the cashier to pay the account, you were surprised by the presence of a waiting line for your
number was the fourth. What is the mechanism that will be used for this scenario? And
simulate it.
Answer
12) Consider a full queue Q1 of size 4 and empty queues Q2 and Q3 of size 4. What are the
contents of the queues and their front and rear after each iteration?
while(!Q1.empty()) 3 7
if(!Q2.full()) 2 4
Q2.enqueue(Q1.dequeue());
while(!Q2.empty())
1 6
if(!Q3.full()) 0 3
Q3.enqueue(Q2.dequeue()); Q1 front=0 , rear=3
Answer
13) If you have a word that contains some characters, show how you can use the stack to
reverse this word. [ Hint: Use the word “madam” for your operation]
Answer