MCMASTER UNIVERSITY HAMILTON, ONTARIO, CANADA
DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING
COE2SH4 - PRINCIPLES OF PROGRAMMING
INSTRUCTOR: SORINA DUMITRESCU2ND M
IDTERM EXAMINATION (2 HOURS) FALL, 2007
Instructions:
1. Write your name, student ID, section number and signature in the spaces indicated below.
2. The examination booklet has 13 pages in total. 3. All answers should be written in pen.
4. This is a closed book examination. No reference notes and books are allowed. No calculators are allowed.
5. Return the whole examination booklet after the test.
N a m e ( p l e a s e p r i n t ) : _ _ _ _ _ _ _ _ _ _ _ _ _
S t u d e n t I D : _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
S i g n a t u r e : _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
I Fill in the blanks Write the output A1)-A3), B1),B2)
25 points II C Programming questions
D1), D2)
14 points III C Programming question
D3)
26 points IV Java
J1) + J2)
35 points
Total 100
FILL IN THE BLANKS
A1) – A3) Evaluate the following expressions in C, and write the values in the blanks.
A1)(1 point)int x[5] = {2,4,6,8,10};
x[1] + x[4]/x[0] = 9
A2)(2 points) typedef struct{ int x; int y; }Point; Point p1={ 2, 10 } ; Point *pp=&p1;
(p1.y - p1.x)/2 = 4 pp -> x -1 = 1
A3)(2 points) typedef struct{ int x; int y; } Point; Point array[ ] = {{-1,1}, {3,6}, {-2,0}};
array[1].y = 6 (array+2) -> x = -2
WRITE THE OUTPUT
B1) (10 points) Read the following C program and write the output on the screen:
#include <stdio.h> typedef struct {
int id;
int grades[3]; int final; } Student;
int main(){
Student s[3]={{340,{80,60,70},0},{204,{80,80,80},0}, {230,{100,100,70},0}};
int i; int a=0;
for(i=0; i<3; i++) {
function2( &s[i] );
printf(“%d\n”, s[i].final ); a+=s[i].final;
}
a=a/3;
printf("%d", a); return 0;
}/* end main */
void function2( Student* s ){
s->final=(s->grades[0]+s->grades[1]+s->grades[2])/3; }/* end function2 */
ANSWER: 70
80
90
80
B2) (10 points) Read the following C program and write the output on the screen:
#include <stdio.h> int recursive(int x){ if(x==0)
return 0; else
return (x%10 + recursive(x/10)); } /* end recursive */
main(){
int number = 4321;
ANSWER: 10
C
PROGRAMMING QUESTIONS
(40 points)Exercises D1)-D3) deal with linked lists containing nodes of the following type:
struct node{ char c;
struct node *next; };
The linked lists are assumed to have a dummy node as the head.
D1)(8 points) Write a recursive function in C to print a linked list backwards. Its prototype should be
void recursive_backwards(struct node *headp); where headp is a pointer to the head of the list.
Solution:
D2)(6 points) Write a C function to insert a node at the beginning of a linked list. Its prototype should be
void insert_front(struct node *headp, struct node *p);
where headp is a pointer to the head of the list and p is a pointer to the node to be inserted.
D3)(26 points)
Write a C function with the prototype void extend_list(struct node *headp);
which extends a linked list by attaching at the end a copy of each node, in reverse order. For example, if the linked list contains three nodes representing, respectively, the characters ‘a’,’b’,’c’, then the extended list should contain six nodes representing, respectively, the characters ‘a’,’b’,’c’, ‘c’,’b’,’a’.
headp is a pointer to the linked list. Division of mark:
- 4 points: Explanation of the algorithm.
- 2 points: The use of comments in the code to identify the major steps of the algorithm.
- 20 points: Function definition
Solution:
void extend_list(struct node *headp){
}
struct node* recursive_extend( struct node *p){
if(p->next != NULL ){
q=recursive_extend( p -> next );
Java Questions
J1) (15 points) The short Java programs in this exercise (J1.1-10) use the Java classes PosInt and Point defined below. These classes’ definitions are correct.
//class to model positive integers public class PosInt{
private int i;
private static int count=0;
public PosInt (){ set(7); count ++; }
public PosInt ( int n ){ set(n); count ++; }
public void set( int n ){ this.i = n>=0?n:-n; }
public int getI( ){ return i; }
public static int getCount( ){ return count; }
public static PosInt add(PosInt a, PosInt b){ return new PosInt(a.i+b.i); }
public void scale( int n ){ set( i*n ); }
public String toString(){ return “i=” + i; }
} //end class PosInt
// class to model points in the plane, with positive integer //coordinates
public class Point{ private PosInt x; private PosInt y;
{ set( someX, someY ); }
public void set( int someX, int someY ){ x = new PosInt( someX ); y = new PosInt( someY ); }
public int getX(){ return x.getI(); } public int getY(){ return x.getI(); }
} //end class
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.
J1.1) (1.5 points) public class Test1{
public static void main(String[] args){ PosInt a = new PosInt( -5 );
PosInt b = new PosInt( );
System.out.println( b.i ); } //end main } //end class
ANSWER: ERROR (i has private access in PosInt)
J1.2) (1.5 points) public class Test2{
public static void main(String[] args){ PosInt a = new PosInt( -5 );
PosInt b = new PosInt( );
System.out.println( a.getI() ); } //end main } //end class
ANSWER: 5
J1.3) (1.5 points) public class Test3{
public static void main(String[] args){ PosInt a = new PosInt( 4 );
PosInt.scale(10);
ANSWER: ERROR (method scale() is an instance method; it cannot be invoked without an object name)
J1.4) (1.5 points) public class Test4{
public static void main(String[] args){ PosInt a = new PosInt( 4 );
PosInt b = new PosInt( -3 ); PosInt c = a;
a.scale( 10 );
System.out.println( c.getI() ); }//end main } //end class
ANSWER: 40
J1.5) (1.5 points) public class Test5{
public static void main(String[] args){ PosInt a = new PosInt( 4 );
PosInt b = new PosInt( -3 );
System.out.println(PosInt.add(a,b).getI());}//end main } //end class
ANSWER: 7
J1.6) (1.5 points) public class Test6{
public static void main(String[] args){ PosInt a = new PosInt( 4 );
PosInt b = new PosInt( -3 );
System.out.println( b.toString() );} //end main } //end class
J1.7) (1.5 points) public class Test7{
public static void main(String[] args){ PosInt a = new PosInt( 4 );
Point p = new Point( 2,8 );
System.out.println( PosInt.getCount() );}//end main } //end class
ANSWER: 3
J1.8) (1.5 points) public class Test8{
public static void main(String[] args){ PosInt a = new PosInt( 4 );
Point p = new Point( 2,8 );
System.out.println( p.x.getI() );} //end main } //end class
ANSWER: ERROR (x has private access in Point)
J1.9) (1.5 points) public class Test9{
public static void main(String[] args){ Point p= new Point( 0,0);
p.set( 6 , -4 );
System.out.println( p.getX() );}//end main } //end class
ANSWER: 6
J1.10) (1.5 points) public class Test10{
public static void main(String[] args){ PosInt a = new PosInt( 4 );
a = PosInt.add( a, new PosInt( 5 ) );
System.out.println( a.getI() );}//end main } //end class
J2)(20 points )
The Java class Account, whose incomplete definition is written below, is intended to model bank accounts. Each Account object has an id and a balance.
Complete the definition of the methods of class Account according to the description provided. Further, complete method main( ) of the class TestAccount according to the specification in the comments.
public class Account{ private int id;
private double balance;
//initializes id to anId and balance to 0 (1 point) public Account( int anId ){
this.id = anId;
this.balance = 0;
}
//returns the id of the account on which the method is called (0.5 point)
public int getId( ){
return this.id;
}
//returns the balance of the account on which the method is called (0.5 point)
public double getBalance( ){
return this.balance;
}
//deposits amount into the account on which the method is called //(i.e., balance is increased by the quantity specified by amount); //the method has to check if amount is larger than 0; if yes it //deposits; otherwise it does nothing. (2 points)
public void deposit( double amount ){
this.balance += amount;
}// end deposit
//withdraws amount from the account on which the method is called //(i.e., the balance is decreased accordingly); precisely the //method does the following: if amount is larger or equal to 0 and //smaller or equal to balance it withdraws,if amount is negative, //it does nothing; if amount is larger than balance it prints the //message: Insufficient funds. (3 points)
public void withdraw( double amount ){
if(amount > this.balance)
System.out.println(“Insufficient funds.”);
else if(amount >= 0)
this.balance -= amount;
}
}//end class Account
public class TestAccount{
public static void main( String args[ ] ){
//create account1(object of the class Account) with id 100 (1 point)
Account account1 = new Account(100);
//deposit 1500 into account1; then print the new balance (2 points)
account1.deposit(1500);
System.out.println( account1.getBalance());
//an array of 10 accounts is created; the ids are all integers between //1 and 10
Account array[ ] = new Account[ 10]; for(int i=0; i<10; i++)
array[i] = new Account( i+1 );
//is covered by the total in your accounts; if not print
//the message "Amount not covered"; if yes withdraw this amount (no //less, no more); you may need to empty some accounts. Briefly explain //the algorithm. (10 points)
double sum=0;
for(int i=0; i<10; i++)
sum += array[i].getBalance(); if(sum < 4000)
System.out.println(“Amount not covered”); else
{
sum = 4000; double temp; for(int i=0; i<10; i++) {
temp = array[i].getBalance(); if(sum < temp)
{ array[i].withdraw(sum); break;
} else {
array[i].withdraw(temp); sum –= temp;
} } //end for }
}//end main