0% found this document useful (0 votes)
229 views6 pages

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

This document contains a sample final exam for a computer science course. It includes 4 multiple choice questions testing knowledge of Java arrays and methods. It also includes 4 longer, open-ended programming problems involving generating random numbers, calculating customer cash back amounts, concatenating arrays, and creating a new array based on modifying existing array values. The problems provide sample inputs and outputs to demonstrate expected program behavior.

Uploaded by

Karim Abdallah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
229 views6 pages

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

This document contains a sample final exam for a computer science course. It includes 4 multiple choice questions testing knowledge of Java arrays and methods. It also includes 4 longer, open-ended programming problems involving generating random numbers, calculating customer cash back amounts, concatenating arrays, and creating a new array based on modifying existing array values. The problems provide sample inputs and outputs to demonstrate expected program behavior.

Uploaded by

Karim Abdallah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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 9
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 9
CSCI250 Sample Final Exam
(BMIS, MATH, PHYS and TEPM students: Choose Two Problems and solve)

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

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:

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 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

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 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

You might also like