Page 0
German University in Cairo June 2, 2024
Media Engineering and Technology Faculty
Prof. Dr. Slim Abdennadher
Assoc. Prof. Milad Ghantous
Dr. Mohamed Hamed
CSEN 202: Introduction to Computer programming
Spring Semester 2024
Final Exam
Bar Code
Instructions: Read carefully before proceeding.
1) Please tick your major
Major
Engineering
Civil
BI
2) Duration of the exam: 3 hours (180 minutes).
3) No books or other aids are permitted for this test.
4) This exam booklet contains 15 pages, including this one. Three extra sheets of scratch paper are attached
and have to be kept attached. Note that if one or more pages are missing, you will lose their points. Thus,
you must check that your exam booklet is complete.
5) Write your solutions in the space provided. If you need more space, write on the back of the sheet containing
the problem or on the three extra sheets and make an arrow indicating that. Scratch sheets will not be
graded unless an arrow on the problem page indicates that the solution extends to the scratch sheets.
6) When you are told that time is up, stop working on the test.
Good Luck!
Don’t write anything below ;-)
P
Exercise 1 2 3 4 5 6
Possible Marks 16 12 12 10 30 8 88
Final Marks
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 1
Exercise 1 (10+6=16 Marks)
a) Write a Java method exceptSelf that takes as a parameter an array of integers arr, constructs and returns
a product array prod (of the same size) such that prod[i] is equal to the product of all the elements of
arr[] except arr[i].
Examples:
arr[] = {10, 3, 5, 6, 2}
exceptSelf(arr) returns prod[] = {180, 600, 360, 300, 900}
Explanation: 180 is the product of 3*5*6*2, 600 is the product of all
elements except 3, and so on..
arr[] = {1,3,2}
exceptSelf(arr) returns prod[] = {6,2,3}
Solution:
# total 10 marks
# 1 for method signature
# 1.5 initializing new array
# 1 outer loop
# 1 initializing prod
# 1 inner loop
# 1.5 condition i != j
# 1 updating product
# 1 for assigning result[i]
# 1 for return
public static int[] exceptSelf(int[] arr)
{
int[] res = new int[arr.length];
for(int i=0; i<arr.length; i++)
{
int prod = 1;
for(int j=0; j<arr.length; j++)
if(i!=j)
prod *= arr[j];
res[i] = prod;
}
return res;
}
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 2
b) Write a class called Tester that uses a command-line argument to test the method exceptSelf imple-
mented above.
Example:
> java Tester 10 3 5 6 2
180,600,360,300,900
Solution:
# total 6 marks
# 1 initializing new array
# 1 for loop
# 2 initializing arr[i] and Integer.parseInt
# 1 calling method
# 1 for loop and printing
public static void main(String[]args)
{
int arr[] = new int[args.length];
for(int i =0; i <arr.length; i++)
arr[i] = Integer.parseInt(args[i]);
int[] res = exceptSelf(arr);
for(int i = 0; i<res.length; i++)
System.out.print(res[i] + " " );
}
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 3
Exercise 2 (12 Marks)
Write a recursive Java method named reversedAlpha that accepts a string that consists of digits and alphabet
letters (lowercase only) as parameter, extracts the alphabet letters from the string, then returns a strings with these
alphabets reversed. If there are no alphabets, your method should return an empty string.
Examples:
s = "ab123cd4"
reversedAlpha(s) returns "dcba"
s = "123"
reversedAlpha(s) returns ""
s = "x789y"
reversedAlpha(s) returns "yx"
Solution:
# Total 12 marks
# 1 mark method signature
# 2 marks base case
# 1 mark return
# 3 marks condition ( 1 each condition, 1 &&)
# 3 marks (1 first return + 1 concatination + 1 mark right place of cancatination)
# 2 marks second return
public static String reversedAlpha(String s)
{
if(s.length()==0)
return "";
if(s.charAt(0) >= ’a’ && s.charAt(0) <= ’z’)
return reversedAlpha(s.substring(1)) + s.charAt(0);
return reversedAlpha(s.substring(1));
}
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 4
Exercise 3 (12 Marks)
Write a recursive method called maxProduct that takes an array of integers arr as input and returns the maximum
product that can be obtained by multiplying two distinct integers from the array. The input array will always have
a minimum of two elements, contains positive integers only and no duplicates.
Examples:
arr[] = {3, 7, 2, 9, 5}
maxProduct(arr) returns 63
Explanation: 63 is the maximum product that can be obtained by multiplying
9 and 7
arr[] = {1,2,6};
maxProduct(arr) returns 12
Note: You may use helper method(s)
Solution:
# Total 2+9 marks
first method:
# 1 mark signature
# 1 mark return statement
second method:
# 1 mark signature
# 1 base case
# 1 return
# 1 first if condition
# 1.5 updating first and second max
# 0.5 marks else
# 1 second if
# 0.5 updating second max
# 1.5 return statement (0.5 for ++i)
public static int maxProduct(int[] a)
{
return helper(a,0,a[0],a[0]);
}
public static int helper(int[] a, int i, int x, int y)
{
if(i == a.length)
return x*y;
if(a[i] > x)
{
y = x;
x = a[i];
}
else
if (a[i] > y)
y = a[i];
return helper(a,++i,x,y);
}
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 5
Alternative solution:
Solution:
# Total 2+9 marks
first method:
# 1 mark signature
# 1 mark return statement
second method:
# 1 mark signature
# 1 base case
# 1 return
# 1 second if condition
# 1 second return
# 1 initialize k
# 1 third if
# 1 updating second max
# 1 return statement
public static int maxProduct(int[] a)
{
return maxproduct(a,0,1,0);
}
public static int maxproduct(int[] a, int i, int j, int max)
{
if (i == a.length - 2 && j == a.length)
return max;
if (j == a.length)
return maxproduct(a, i + 1, i + 2, max);
int k = a[i] * a[j];
if (k > max)
max = k;
return maxproduct(a, i, j + 1, max);
}
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 6
Exercise 4 (10 Marks)
Write a static method called isMatrixSymmetric that takes a n × n 2D array as a parameter and returns true if the
matrix is symmetric across its main diagonal, and false otherwise. A main diagonal is the one from the upper
left element to the lower right element.
Examples:
int[][] arr = {{1, 2, 3},
{2, 4, 5},
{3, 5, 6}};
isMatrixSymmetric(arr) returns true
int[][] arr = {{1, 2, 3},
{2, 4, 5},
{3, 6, 6}};
isMatrixSymmetric(arr) returns false
int[][] arr = {{7, 2, 3, 4},
{2, 8, 5, 6},
{3, 5, 7, 9},
{4, 6, 9, 8}};
isMatrixSymmetric(arr) returns true
Solution:
#Total 10 marks
# 2 mark for method signature
# 2 marks each for loop
# 2 marks condition
# 1 mark each return
public static boolean isSymmetric(int arr[][])
{
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr.length; j++)
if (arr[i][j] != arr[j][i])
return false;
return true;
}
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 7
Exercise 5 (15+15=30 Marks)
In this exercise, we want to create the class GoalTracker which manages and monitors daily activity goals over
a week. It also tracks whether the user meets their set goals .
a) Implement a class ActivityTracker which is designed to monitor and record the user’s daily physical
activities, focusing on three primary health metrics: active calories burned, exercise minutes, and standing
hours. These metrics are crucial for assessing overall physical activity. This class will keep track of the
following information:
• activeCalories: An integer representing the number of calories burned through physical activity.
• exerciseMinutes: An integer representing the number of minutes spent exercising.
• standingHours: An integer representing the number of hours spent standing.
• moveGoal: An integer representing the goal for active calories burned.
• exerciseGoal: An integer representing the goal for exercise minutes.
• standGoal: An integer representing the goal for standing hours.
The class ActivityTracker should implement the following:
1. A constructor ActivityTracker that takes as parameters the move Goal, exercise Goal and stand
Goal goals. Initially the user had not performed any physical activities.
2. An instance method addActivity that accepts activeCalories, exerciseMinutes and standingHours,
records and updates the user’s activity for the day.
3. An instance method setGoals(moveGoal, exerciseGoal, standGoal) that sets the daily
goals for the Move, Exercise, and Stand Goals.
4. An instance method getProgress that calculates and prints the progress towards each activity goal
as a percentage.
5. A static method completedGoals that checks and returns true if the user achieved all his goals
for the day and returns false otherwise.
6. An instance method String toString() that returns information about the ActivityTracker. The
string should look like the following:
You achieved 800/950 KCAL, 28/30 MIN and 6/12 HR today!
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 8
Answer to Exercise 5 a:
Solution:
public class ActivityTracker {
# 1.5 a t t r i b u t e s ( 0 . 2 5 each )
int activeCalories ;
int exerciseMinutes ;
int standingHours ;
int moveRing ;
int exerciseRing ;
int standRing ;
# 2.5 c o n s t r u c t o r ( 0 . 5 s i g n a t u r e , 0.25 each )
p u b l i c A c t i v i t y T r a c k e r ( i n t m, i n t e , i n t s )
{
moveRing = m;
exerciseRing = e ;
standRing = s ;
activeCalories = 0;
exerciseMinutes = 0;
standingHours = 0;
}
# 1 . 2 5 marks ( 0 . 5 s i g n a t u r e , 0 . 2 5 e a c h a s s i g n m e n t )
public void r e c o r d A c t i v i t y ( i n t a c t i v e C a l o r i e s , i n t exerciseMinutes ,
i n t standingHours )
{
this . activeCalories = activeCalories ;
this . exerciseMinutes = exerciseMinutes ;
t h i s . standingHours = standingHours ;
}
# 1 . 2 5 marks ( 0 . 5 s i g n a t u r e , 0 . 2 5 e a c h a s s i g n m e n t )
p u b l i c v o i d s e t G o a l s ( i n t moveGoal , i n t e x e r c i s e G o a l , i n t s t a n d G o a l )
{
t h i s . moveRing = moveGoal ;
this . exerciseRing = exerciseGoal ;
t h i s . standRing = standGoal ;
}
# 2 . 5 marks ( 0 . 2 5 s i g n a t u r e , 0 . 2 5 e a c h c a l c u l a t i o n , 0 . 5 e a c h p r i n t )
public void get Progr ess ( )
{
d o u b l e perMove = a c t i v e C a l o r i e s * 1 0 0 / moveRing ;
double perExer = exerciseMinutes *100/ exerciseRing ;
double perHour = s ta nd in gH ou rs *100/ standRing ;
System . o u t . p r i n t l n ( " you a c h i e v e d : " + " " + perMove + "% i n move r i n g " ) ;
System . o u t . p r i n t l n ( " you a c h i e v e d : " + " " + p e r E x e r + "% i n
exercise ring ");
System . o u t . p r i n t l n ( " you a c h i e v e d : " + " " + p e r H o u r + "% i n s t a n d r i n g " ) ;
}
# 1 . 2 5 marks ( 0 . 5 s i g n a t u r e , 0 . 2 5 e a c h a s s i g n m e n t )
public void r e s e t s ( )
{
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 9
t h i s . activeCalories = 0;
t h i s . exerciseMinutes = 0;
t h i s . standingHours = 0;
}
# 2 . 7 5 marks ( 0 . 5 s i g n a t u r e , 0 . 2 5 e a c h c a l c u l a t i o n , 1 . 5 r e t u r n )
public boolean completedRings ( )
{
d o u b l e perMove = a c t i v e C a l o r i e s * 1 0 0 / moveRing ;
double perExer = exerciseMinutes *100/ exerciseRing ;
double perHour = s ta nd in gH ou rs *100/ standRing ;
r e t u r n ( perMove == 1 0 0 . 0 && p e r E x e r == 1 0 0 . 0 && p e r H o u r == 1 0 0 . 0 ) ;
}
# 2 marks ( 0 . 5 s i g n a t u r e , 1 . 5 r e t u r n )
public String toString ()
{
S t r i n g s = " You a c h i e v e d " + a c t i v e C a l o r i e s + " / " + moveRing + "KCAL , " ;
s += e x e r c i s e M i n u t e s + " / " + e x e r c i s e R i n g + "MIN , " ;
s += s t a n d i n g H o u r s + " / " + s t a n d R i n g + "HR t o d a y ! " ;
return s ;
}
}
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 10
b) Implement a class GoalTracker which monitors and manages daily activity goals over a week. Internally,
you should keep an integer array goals which stores the goals for the move, exercise and stand goals per
day. You also should keep an array of ActivityTracker called dailyActivities representing the actual
activity data tracked for each day of the week, where day at index 0 is Sunday and day at index 6 is Saturday.
We will implement the following:
1. A constructor GoalTracker that takes as parameters the move, exercise and stand goals. Initial-
izes the goals array, and updates the dailyActivities accordingly. Initially the user had not
performed any physical activities, but has his goals set.
2. A constructor GoalTracker(int[] goals, AcitivityTracker[] dailyAcitivities)
that clones the array instance variables with goals and dailyActivities. The array instance
variables should be of the same length as the arrays that are passed to the constructor. You are required
to do deep cloning in the constructor. Do not copy references of the objects.
3. An instance method recordDailyActivity(day, move, exercise, stand) that records
and updates the actual activity data for a specific day. The day is passed to the method as index.
4. An instance method getOverallCompletionRate() that calculates the number of days of the
week, in which the goals were fully met.
5. An instance method bestDay() that returns the index of the day where the user achieved best in
calories burnt activity.
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 11
Answer to Exercise 5 b:
Solution:
public c l a s s GoalTracker {
# 1 mark ( 0 . 5 e a c h )
int [] goals ;
ActivityTracker [] dailyActivities ;
# T o t a l 1 . 7 5 marks
# 0.25 s i g n a t u r e
# 0.5 each assignment
p u b l i c G o a l T r a c k e r ( i n t move , i n t e x e r c i s e , i n t s t a n d )
{
g o a l s [ 0 ] = move ;
goals [1] = exercise ;
goals [2] = stand ;
}
# t o t a l 4 . 2 5 marks
# 0.25 s i g n a t u r e
# 0.5 i n i t i a l i z i n g goals array
# 0.25 for loop
# 0.5 assigning goals
# 0.5 i n i t i a l i z i n g a c t i v i t i e s array
# 0.25 for loop
# 1 mark c r e a t i n g new A c t i v i t y
# 1 mark p a r a m e t e r s o f t h e a c t i v i t y
public GoalTracker ( i n t [ ] goals , ActivityTracker [ ] d a i l y A c t i v i t i e s )
{
t h i s . g o a l s = new i n t [ g o a l s . l e n g t h ] ;
f o r ( i n t i = 0 ; i < g o a l s . l e n g t h ; i ++)
this . goals [ i ] = goals [ i ] ;
t h i s . d a i l y A c t i v i t i e s = new A c t i v i t y T r a c k e r [ d a i l y A c t i v i t i e s . l e n g t h ] ;
f o r ( i n t i = 0 ; i < d a i l y A c t i v i t i e s . l e n g t h ; i ++)
d a i l y A c t i v i t i e s [ i ] = new A c t i v i t y T r a c k e r ( d a i l y A c t i v i t i e s [ i ] . moveRing ,
d a i l y A c t i v i t i e s [ i ] . exerciseRing , d a i l y A c t i v i t i e s [ i ] . standRing ) ;
}
# T o t a l 2 . 2 5 marks
# 0.25 s i g n a t u r e
# 1 condition
# 1 calling recordActivity
p u b l i c v o i d r e c o r d D a i l y A c t i v i t y ( i n t day , i n t move , i n t e x e r c i s e , i n t s t a n d )
{
i f ( day < d a i l y A c t i v i t i e s . l e n g t h )
{
t h i s . d a i l y A c t i v i t i e s [ day ] . r e c o r d A c t i v i t y ( move , e x e r c i s e ,
stand ) ;
}
}
# Total 2 . 2 5 marks
# 0.25 signature
# 0.25 initializing total
# 0.25 for loop
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 12
# 1 condition
# 0.25 updating t o t a l
# 0.25 r e t u r n
public int getOverallCompletionRate ()
{
i n t t o t a l = 0;
f o r ( i n t i = 0 ; i < d a i l y A c t i v i t i e s . l e n g t h ; i ++)
i f ( d a i l y A c t i v i t i e s [ 0 ] . completedRings ( ) )
t o t a l ++;
return total ;
}
# T o t a l 3 . 5 marks
# 0.25 s i g n a t u r e
# 0.25 i n i t i a l i z i n g best
# 0 . 5 i n i t i a l i z i n g max
# 0.25 for loop
# 1 condition
# 0 . 5 u p d a t i n g max
# 0.5 updating best
# 0.25 r e t u r n
p u b l i c i n t bestDay ( )
{
i n t best = 0;
i n t max = d a i l y A c t i v i t i e s [ 0 ] . a c t i v e C a l o r i e s ;
f o r ( i n t i = 1 ; i < d a i l y A c t i v i t i e s . l e n g t h ; i ++)
i f ( d a i l y A c t i v i t i e s [ i ] . a c t i v e C a l o r i e s > max )
{
max = d a i l y A c t i v i t i e s [ i ] . a c t i v e C a l o r i e s ;
best = i ;
}
return best ;
}
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 13
Exercise 6 (8 Marks)
What is the output after executing the following programs?
a) class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void mystery1(Person x) {
String tempName = this.name;
int tempAge = this.age;
this.name = x.name;
this.age = x.age;
x.name = tempName;
x.age = tempAge;
}
public void mystery2(Person x) {
this.name = x.name;
this.age = x.age;
}
public String toString() {
return this.name + " (Age: " + this.age + ")";
}
public static void main(String[] args) {
Person alice = new Person("Alice", 25);
Person bob = new Person("Bob", 30);
Person charlie = new Person("Charlie", 35);
mystery(alice, bob, charlie);
System.out.println("After the mystery:");
System.out.println(alice);
System.out.println(bob);
System.out.println(charlie);
}
public static void mystery(Person p1, Person p2, Person p3) {
p1.mystery2(p2);
p2.mystery1(p3);
p3.mystery2(p1);
p1.mystery1(p2);
}
}
Solution:
# 1 each line
After the mystery:
Charlie (Age: 35)
Bob (Age: 30)
Bob (Age: 30)
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 14
b) public class Main
{
public static void main(String[] args) {
int a = 3;
int b = 7;
int[] array = {1, 2, 3, 4, 5};
a = mystery1(b, array);
System.out.print("Array: ");
for (int i = 0; i< array.length;i++)
System.out.print(array[i] + " ");
mystery2(array, a);
System.out.print("Array: ");
for (int i = 0; i< array.length;i++)
System.out.print(array[i] + " ");
}
public static int mystery1(int num, int[] arr)
{
num += 50;
arr[1]++;
arr[3] += num;
return arr[2] * 3;
}
public static void mystery2(int[] arr, int val) {
arr[0] = val;
arr[4] *= 2;
val = arr[2] + arr[4];
int[] newArr = arr;
newArr[1] = 100;
}
}
Solution:
# 2.5 each line ( each number 0.5)
Array: 1 3 3 61 5
Array: 9 100 3 61 10
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 15
Scratch paper
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 16
Scratch paper
CSEN 202: Introduction to Computer programming, Final Exam, June 2, 2024 Page 17
Scratch paper