Question 1 (25 Points) : A. What Is The Output of Each of The Following Fragments? Circle The Correct Answer
Question 1 (25 Points) : A. What Is The Output of Each of The Following Fragments? Circle The Correct Answer
Output
int[][]M = {{3,-5},{7,2}}; a. 3 -5 7 2
for(int i = 0; i < M.length; i++) b. 3 7 -5 2
for(int j = 0; j < M[i].length; j++) c. 7 2 3 -5
System.out.print(M[j][i]+"\t");
d. 7 3 2 -5
//(4 points)
//(4 points)
Page 1 of 9
CSCI250 Sample Final Exam
int a = 6, b = 12;
a. 1
while(a<b){
b. 2
System.out.println("In the loop");
c. 3
b-=2;
d. 4
}
//(4 points)
Page 2 of 9
CSCI250 Sample Final Exam
(BMIS, MATH, PHYS and TEPM students: Choose Two Problems and solve)
A pPrime number is a number where the sum of all divisors, except the number itself, is
prime number. For example n=8, the sum of all divisors of 8 is 7 (1+2+4), and 7 is prime
number, then 8 is pPrime.
Sample run 1:
8 is pPrime number.
Sample run 2:
15 is not pPrime number
Page 3 of 9
CSCI250 Sample Final Exam
Question 3 [25 points]:
A customer is loyal if he consumes 1000$ or more in our shop. A loyal customer deserves a
cash back (Bonus) on the amount that he spent as follows:
Write a method called getCashBack that takes as parameter the amount spent and returns
the cash back amount.
Write a test program (main) that reads the customer name and the amount spent by the
customer and prints out if the customer is loyal or not (using the method getCashBack).
Sample run 1:
Enter customer name and amount spent: Fadi 2800
Fadi is a loyal customer, he spent 2800.0 $ and he will get
420.0 $ cashback
Sample run 2:
Enter customer name and amount spent: Samer 850
Samer is not a loyal customer, he spent only 850.0 $
Page 5 of 9
CSCI250 Sample Final Exam
Question 4 [25 points]:
Concatenation of two arrays.
Write a program that reads two arrays of integer (A and B), puts them in a third array C as
follow then display C.
A 12 17 15 10 B 13 14 11
C 12 17 15 10 13 14 11
Sample run 1:
Enter the size of the arrays: 4 3
Enter the numbers of the first array: 12 17 15 10
Enter the numbers of the second array: 13 14 11
The third array is: 12 17 15 10 13 14 11
Sample run 2:
Enter the size of the arrays: 2 3
Enter the numbers of the first array: -3 5
Enter the numbers of the second array: 22 67 11
The third array is: -3 5 22 67 11
Page 7 of 9
CSCI250 sample Final Exam Version1 Spring 2016 – 2017
Question 4 [25 pts]:
Write a method newArray that takes as parameter an array of integers list and returns a new array of
integers newList filled with values as follows:
If the value in list is 0, then the value in newList is -1
If the value in list is even, then the value in newList is its half (divide by 2)
If the value in list is odd, then the value in newList is its third (divide by 3)
In the main method, create an array of integers of any size and any values you want using an
initializer list. Display this array as well as the new array obtained by calling the method as per the
below sample run.
Sample Run:
List New List
8 4
9 3
31 10
0 -1
0 -1
12 6
Page 6 of 7