Question 1 (25 Points) : A. What Is The Output of Each of The Following Fragments? Circle The Correct Answer

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CSCI250 sample Final Exam

Question 1 [25 points]:


a. What is the output of each of the following fragments? Circle the correct answer.

Output

int [] a; a. Syntax error


a = new int[10]; b. 10
a[0] = a[9] = 10;
c. 2
System.out.println(a[2]);
//(3 points) d. 0

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)

public static String m(int x, int y){


return ("sum=" + (x + y)); a. Lsum=9
} b. sum=9L
public static void main(String []args){ c. sum=45
System.out.println("L"+m(4,5));
} d. Sum=9
//(4 points)

public static void main(String[] X){


System.out.print(m1(1));}
a. Syntax error
public static int m1(int a) b. 5
{ return m2(a*a -3);} c. 4

public static int m2(int b) d. None of the above


{return b+6;}

//(4 points)

Page 1 of 7
CSCI250 sample Final Exam

b. Circle the correct answer.


How many times is the phrase "In the loop" printed?

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)

a. double [3] nums = {3.5, 35.1, 32.0};


b. char [] charArray = new char[26];
Which of the following are valid array declaration?
(2 points) c. int [] words = new words[10];
d. char [] charArray = "CSCI";

a. public static double method (int x, y)


Which of the following method headers is b. public static boolean method (int a)
syntactically correct? c. public static int break (double a)
(2 points) d. public static non_void (int x)

A method can be called/invoked from the main


a. True
method or from another method.
b. False
(2 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.

P.S: A Prime Number can be divided only by 1 or itself.

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;

n = (int) (Math.random()*80); // 2 points

//Calculate the sum of divisors of n


for (i=1; i<=n/2; i++) // 4 points
if(n%i==0) // 3 points
s+=i; // 2 points

//Check if the sum of divisors is prime or not


for (i=2; i<=s/2; i++) // 4 points
if(s%i==0){ // 3 points
prime = false; // 2 points
break;
}
if(prime) // 1 point
System.out.println(n+" is pPrime number"); // 1 point
else // 1 point
System.out.println(n+" is not pPrime number"); //1 pt
}
}

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:

Amount spent Cash back


0 $ to 999 $ 0%
1000 $ to 1999 $ 10%
2000 $ and more 15%

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

System.out.print("Enter customer name and amout spent: ");


String name = input.next();// 1 point
double amount = input.nextDouble();// 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)
}

public static double getCashBack(double amount){ // 3 points


if(amount<2000) // 2 points
return amount * 0.1; // 3 points

return amount * 0.15; // 3 points


}
}

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

Note: Do not print A and B.

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;

System.out.print("Enter the size of the arrays: ");


sizeA = input.nextInt();// 1 point
sizeB = input.nextInt();// 1 point

int []A = new int [sizeA]; // 1 point


int []B = new int [sizeB]; // 1 point
int []C = new int [sizeA + sizeB]; // 2 points

System.out.print("\nEnter the numbers of the first array: ");


for (i=0; i<A.length; i++) // 2 points
A[i] = input.nextInt();// 2 points

System.out.print("\nEnter the numbers of the second array: ");


for (i=0; i<B.length; i++) // 2 points
B[i] = input.nextInt();// 2 points

for (i=0; i<A.length; i++) // 2 points


C[i] = A[i]; // 2 points

for (j=0; j<B.length; j++) // 2 points


C[i++] = B[j]; // 2 points

System.out.print("\nThe third array is: ");


for (i=0; i<C.length; i++) // 1 point
System.out.print(C[i]+"\t"); // 1 point
}
}

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

public class CreatingNewArray { //½


public static void main(String[] args) { //½
int[] list = {8, 9, 31, 0, 0, 12}; //2
int[] newList = newArray(list); //3
System.out.println("List\tNew List"); //1
for(int i = 0; i < list.length; i++) //1
System.out.println(list[i] + "\t" + newList[i]); //2
}
public static int[] newArray(int[] list){
int[] newList = new int[list.length]; //2
for(int i = 0; i < list.length; i++) //1
if(list[i] == 0) //1 ½
newList[i] = -1; //1 ½
else if(list[i] % 2 == 0) //1 ½
newList[i] = list[i] / 2; //1 ½
else //1 ½
newList[i] = list[i] / 3; //1 ½
return newList; //3
}
}

Page 7 of 7

You might also like