Download Meeting

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

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

Document related concepts
no text concepts found
Transcript
Faculty of Computer Studies
M180
Data Structures and Algorithms in Java
Make-up MTA
Spring 2012-2013
Tuesday May 7, 2013
Number of MTA Pages:
(including this cover sheet)
( 3 ) Time Allowed:
Instructions:
1- Write all your answers on the same exam paper.
2- Electronic devices (especially calculators) are not allowed.
( 1.5 ) Hour
M180
Make-up MTA Spring 2012- 2013
PART 1: ALL QUESTIONS ARE REQUIRED [5 Marks]
Question 1: Choose the correct answer: (5marks, one mark each)
1)
The value table.length in the following code equals:
inti, j;
int sum = 0;
for(i=0; i<table.length; i++)
{
for(j=0; i< table[i].length; j++)
{
sum += table[i][j];
}
}
a.
b.
c.
d.
Number of columns in a two-dimensional array
Number of rows in a two-dimensional array
Size of a two-dimensional array
None of the above
2) --------- are rows of a two-dimensional arrays that are not all the same length
a. Nagged Arrays
b. Ragged arrays
c. Dimensional Arrays
d. Irregular Arrays
3)
A node’s successor in a linked list is the ……….
a. Last node in the list
b. Next node in the list
c. First node in the list
d. previous node in the list
4)
In linked list each entry contains?
a. Data
b. Link
c. Data + Link
d. None of the above
5)
Which of the following expressions is asymptotically the largest?
a. 1000000n3
b. nlogn2/1000
c. 1000log n
d. n2log n
PART 2: ALL QUESTIONS ARE REQUIRED [10 Marks]
Question 1:
What is the problem with the following recursive method to compute an, for positive integer n? [2 marks]
public static double X (double a, int n) {
return a*X(a,n+1);
}
Answer: The program will go into an infinite loop with n getting more and more negative //1 mark since no stopping conditions are applied
here//1 mark.
2/4
M180
Make-up MTA Spring 2012- 2013
Question 2:
What is the running time (Big O) of each of the following two code fragments? Justify your answer. [3 marks]
public long sum(int start, int stop)
{
Int bigseries = stop*(stop+1)/2;
start--;
int littleseries = start*(start+1)/2;
return bigseries-littleseries;
}
Answer: This method returns the result for any start and stop values in a fixed amount of time //1 mark, regardless of how many numbers
are summed. Therefore the method takes O(1) //2 mark.
Question 3:
What is the running time (Big-O) of the following operations applied on Arrays? [5 marks]
Operations
Insert an item in Unsorted Array
Insert an item in Sorted Array
Calculate the number of items in an Array
Print the items of an Array
Linear Search for an item in Array
Big-O
O(1) //1 mark
O(N) //1 mark
O(1) //1 mark
O(N) //1 mark
O(N) //1 mark
PART 3: ALL QUESTIONS ARE REQUIRED [15 Marks]
Question 1:
Write a recursive method (sum_num) that should take n and returns Sum where: [3 marks]
1 1
1
π‘†π‘’π‘š = 1 + + + β‹― . +
2 3
𝑛
Answer:
Public int sum_num(int num)//0.5 mark
{
int sum; //0.5 mark
if (num == 1) //0.5 mark
sum = 1; //0.5 mark
else
sum = 1/num + sum_num(num-1); //0.5 mark
return sum; //0.5 mark
}
Question 2:
Write a Java method (sum_ragged( )) which returns the summation of the elements of Ragged array a. [6 marks]
Answer:
Public int sum_ragged(int[] a){ //1 mark
int sum = 0; //1 mark
for (int i = 0; i < a.length) { //1 mark
for (int j = 0; j < a[i].length; j++) {//1 mark
sum += a[i][j]; //1 mark
}
return sum; //1 mark
}
3/4
M180
Make-up MTA Spring 2012- 2013
Question 3:
Write a java method (add_node( )) which insert a new list node of value (int w) directly after the current
position in a linked list. [6 marks]
Answer:
Public void add_node(int w){ //1 mark
node_w = new ListNode<int>(); //1 mark
node_w.data = w; //1 mark
node_w.next = current.next; //2 mark
current.next = node_w; //1 mark
}
4/4
Related documents