Student number:
MCMASTER UNIVERSITY
Department of Electrical and Computer Engineering COE 2SH4 (Principles of Programming)
Instructor: Dr. Sorina Dumitrescu MIDTERM TEST 2
November 18, 2013 Duration: 1 hour
This is a closed book exam. No reference material of any kind is permitted. No calculators or other electronic devices are permitted.
This examination paper consists of 7pages and questions. Please bring any discrepancy to the attention of an invigilator. You are responsible for ensuring that your copy of the test is complete. The number in brackets at the start of each question is the number of points the question is worth. Please write your name and student number on this exam.
Answer all questions.
Answer all questions on the Answer Sheet. Note that Q4 is a bonus question.
Q1. [16] Read the following program segments and write the output on the screen. There are no errors. Assume that<stdio.h> is included.
(a) int main (void ){
unsigned int x=13, y=6;
printf( "%d; %d; %d; %d; %d", x|y, x^y, x&y, x>>3, y<<1 ); return 0; } //end main
Note: 13 = 23+ 22+ 20 and 6 = 22+ 21. (b) int main( void ){
int i, j=0; unsigned x = 31;
unsigned int mask = 1;
for( i = 0; i < 8*sizeof(unsigned int); i++ ){ j += x & mask ;
x >>= 2; } //end for printf(%d, j);
Recall that sizeof(dataType)returns the number of bytes used to store a vari-able of typedataType. Note that 31 = 24+ 23+ 22+ 21+ 20.
(c) typedef struct { int a;
int b; } Complex;
int main(){
Complex cc[3]={{30,40},{70,80},{100,200}};
printf("%d; %d; %d; %d", cc[2].b, cc->a, (cc+1)->a, (*cc).b ); return 0; } //end main
(d) void fun( int n ){ if ( n <= 1 )
printf( "*" ); else
{
fun( n/3 ); fun( n/3 );
printf( "%d", n ); }
}//end fun
int main (void ){ fun( 9 );
return 0; } //end main
Q2. [12] Write a C function allocateList() with prototype Complex** allocateList( int n );
The function allocates memory for a list of complex numbers. Each complex number is represented using a structure variable of type Complexdefined in exercise Q1.(c). The list is represented in memory using an array of pointers, where each pointer points to a structure variable of typeComplex. The size of the array equals the value of parameter n. The structure variables have to be initialized as follows. For the structure variable corresponding to indexi in the array, both members a and b have to be initialized to 2*i+1. Finally, the function returns a pointer to the beginning of the array.
(a) int binSearch1(int array[], int n, int key) {
1. int middle, low=0, high=n-1; 2. while( low <= high )
3. {
4. middle = (low + high)/2; 5. if( key == array[middle] ) 6. return middle;
7. if( array[middle] > key ) 8. low = middle +1;
9. else
10. high = middle -1; 11. }
12. return -1; }
(b) int binSearch2(int array[], int n, int key) {
1. int middle, low=0, high=n-1; 2. while( low < high )
3. {
4. middle = (low + high)/2; 5. if( array[middle] > key ) 6. high = middle-1;
7. else
8. low = middle +1; 9. }
10. if( key == array[low] ) 11. return low;
12. return -1; }
(c) int binSearch3(int x[], int key, int low, int high) {
1. int mid= (low + high)/2 ; 2. if( x[mid] > key && mid > low)
3. return binSearch3(x, key, low, mid-1); 4. if( x[mid] < key && high > mid )
5. return binSearch3(x, key, mid+1, high); 6. return -1;
}
for every i)? What about the case when the array is sorted in non-decreasing order (in other words, whenarr[i]≤arr[i+1]for everyi)? Justify your answer. To justify your answer, if your answer is YES, you have to explain how the algorithm applies and to justify that it produces the correct result. If your answer is NO, it is sufficient if you provide a counterexample (in other words, an example of input for which a binary search-like algorithm would not produce the correct result).
Q5. [20] The shortJavaprograms in exercise (Q5.(a)-(j)) use theJavaclassesStudentand Student2SH4 declared next. The declarations of these two classes are correct. Note that all classes involved in this exercise are declared in the same package, therefore, no import statement is needed.
//class to model students public class Student{
private int idNumber; private int yearOfStudy;
private static int count=0; //some tricky count
public Student(int id, int year){ set(id,year); count = count+2; } public Student(int n){ set(n,1); count = count+2; }
public void set(int id, int year){
this.idNumber = id; this.yearOfStudy = year; } public int getId(){ return idNumber; }
public int getYear(){ return yearOfStudy; } public static int getCount(){ return count; } public void promote(){ yearOfStudy++; }
} //end class Student
// class to model 2SH4 students public class Student2SH4{
private Student s; private int grade;
public Student2SH4( int id, int year, int g ) { s = new Student( id, year ); setGrade(g); }
public void setGrade( int g ){ if(g<0)
g = 0; else if(g>100)
g = 100;
public int getGrade(){ return grade; } public int getYear(){ return s.getYear(); }
public Student getStudent(){ return new Student( s.getId(), getYear()); } } //end class Student2SH4
For each of the following Java applications write the output on the screen if the code is correct, or answer ERROR if there is any error in the code. In other words, if the code contains syntax errors your answer has to be ERROR. If the code causes an exception to be thrown at run-time, your answer again has to be ERROR.
(a) public class Test1{
public static void main(String[] args){ System.out.println(Student.getCount()); Student a = new Student(100, 2);
Student b = new Student(101);
System.out.println(Student.getCount());} //end main } //end class
(b) public class Test2{
public static void main(String[] args){ Student a = new Student( 100, 2 ); Student b = new Student( 101 ); System.out.println( a.getId() );
System.out.println( b.getYear() );} //end main } //end class
(c) public class Test3{
public static void main(String[] args){ Student a = new Student( 100, 2 ); Student b = new Student( 101 ); b.promote();
System.out.println( b.yearOfStudy );
System.out.println( b.getId() );} //end main } //end class
(d) public class Test4{
public static void main(String[] args){ Student a = new Student( 100, 2 ); Student b = new Student( 101 ); b = a;
b.promote();
System.out.println( a.getYear() );
System.out.println( b.getId() );} //end main } //end class
(e) public class Test5{
Student b = null; b.promote();
System.out.println( a.getYear() );
System.out.println( b.getId() );} //end main } //end class
(f) public class Test6{
public static void main(String[] args){
Student2SH4 a = new Student2SH4( 105, 3, 103 ); Student b = new Student( 200, 3);
System.out.println( a.getId() );
System.out.println( a.getGrade() );} //end main } //end class
(g) public class Test7{
public static void main(String[] args){
Student2SH4 a = new Student2SH4( 105, 3, 98 ); Student b = new Student( 100, 4);
Student c = a.getStudent();
System.out.println( Student.getCount() ); System.out.println( c.getId() );} //end main } //end class
(h) public class Test8{
public static void main(String[] args){ Student[] arr = new Student[4]; for(int i=1; i<=4; i++){
arr[i-1]=new Student(100+i);
System.out.print( arr[i-1].getId() + "; " ); } //end for
} //end main } //end class
(i) public class Test9{
public static void main(String[] args){ Student[] arr = new Student[4]; for(int i=1; i<4; i++)
arr[i]=new Student(100+i); for(int i=1; i<4; i++)
System.out.print( arr[i-1].getId() + "; " ); } //end main
} //end class
(j) public class Test10{
public static void main(String[] args){ Student a = new Student( 200 ); Student b = a;
System.out.println( a==b );
System.out.println( a==c ); } //end main } //end class
Q6. [12] This exercise refers to Javaclasses Student and Student2SH4from exercise Q5. (a) Write the code for method sumGrades()in class Student2SH4:
public int sumGrades(Student2SH4 that){...}
This method returns the sum of the grades of this 2SH4 student andthat 2SH4 student.
(b) Write the code for method hasHigherId() in classStudent2SH4: public boolean hasHigherId(Student2SH4 that){...}
This method returnstrueif this2SH4 student has a higheridNumberthanthat 2SH4 student, whereidNumber is a field of the Studentobject referenced by the field s of aStudent2SH4 object.
(c) Assume that in methodmain()in some classTestwe have two reference variables aRefand bRef of type Student2SH4, each referencing a Student2SH4 object.
(i) Write a statement to print the sum of the grades of the two 2SH4 students using methodsumGrades(). You are not allowed to use methodgetGrade(). (ii) Write a piece of code that declares another reference variable cRef of type Student2SH4, determines which of the two 2SH4 students has a higher ID number and sets cRef to reference the corresponding Student2SH4 object. You have to use method hasHigherId() and you are not allowed to use any other method.