Professional Documents
Culture Documents
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
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 7
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 7
CSCI250 sample Final Exam
Question 2 [25 points]:
Write a java program that generates a random integer number N between 1 and 80, using
Math.random() method, and displays a message stating if N is a pPrime number or not.
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
Solution:
public class Problem2 {
public static void main(String[] args) {
// 1 point
int i, n, s=0;
boolean prime = true;
Page 3 of 7
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 4 of 7
CSCI250 sample Final Exam
import java.util.Scanner; // 1 point
public class Problem3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // 1 point
if(amount<1000){ // 3 points
System.out.println(name + " is not a loyal customer, "+
"he spent only " + amount + " $");
}
else{ // 3 points
System.out.println(name +
" is a loyal customer, he spent " + amount + " $
and he will get "+ getCashBack(amount) + " $ cashback");
} // 4 points (calling method)
}
Page 5 of 7
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 6 of 7
CSCI250 sample Final Exam
import java.util.Scanner;
public class Program4 {
public static void main(String[] args){
Scanner input = new Scanner (System.in); // 1 point
int i, j, sizeA, sizeB;
Page 7 of 7
CSCI250 Final Exam – Solution (V1 & V2) Spring 2016 – 2017
SOLUTION: Creating a newArray out of an existing one using a defined scheme [25 pts]
Please Subtract marks if the order of the statements in incorrect
Page 7 of 7