CSCI250 Final Exam – v1 Fall 2017 – 2018
Question 1 [21 pts – 3 pts each] Circle the correct answer:
Answer
MCQ 1
int c=0; a) 2 3 3
for (int line = 1; line <= 3; line++) {
b) 2 3 5
for (int j = 1; j <= (-1 * line + 3); j++) {
c++; c) 2
}
d) 2 3 3 0
System.out.print(c +" ");
}
MCQ 2 What is the output of the following
codes a) 13
int b = 0; b) 7
int[][] a = {{2, 4, 7}, c) 16
{7, 3, 5}, d) 44
{1, 6, 9}};
for(int i = 0; i < a.length - 1; i++)
b += a[i+1][i];
System.out.println(b);
MCQ 3 What is the output of the following
code? a) 331
public static void main(String[] args) {
b) 439
int[] a={9,1,4,8};
int i=3;
System.out.print(a[i] ); c) 831
System.out.print(element(a,1));
} d) 841
public static int element(int[] b, int x){
System.out.print(b.length);
return b[x];
}
Page 1 of 9
CSCI250 Final Exam – v1 Fall 2017 – 2018
MCQ 4 What is the output of the following
a) 5
code? b) 3
int[][] m = new int[3][5]; c) runtime Error
System.out.println(m[3].length); d) 15
MCQ 5
A method called T accepts 3 a) public static double average(double t1,
double parameters and double t2, double t3)
prints on the screen the b) public static void T(double t1, double
average of these numbers. t2, double t3)
What will the signature of c) public static double T(double a, double
this method be: b, double c)
Page 2 of 9
CSCI250 Final Exam – v1 Fall 2017 – 2018
MCQ 6
What is the correct syntax to create an array of a) int[] A = new
type int and initialized to 1, 2 and 3 int[1,2,3];
b) int [] A=new{
1,2,3};
c) int[] A;
A = { 1,2,3};
d) int[] A = {1,2,3};
MCQ 7
What does this method do? a) Counts and returns the number
of elements in array a
public static int count (int[] a){
int c=0; b) Counts and returns the number
for(int i=0;i<a.length;i++) of even numbers in array a.
if(a[i]%2==0)
c++; c) Counts and returns the sum of
return c; all even numbers in a.
}
Page 3 of 9
CSCI250 Final Exam – v1 Fall 2017 – 2018
Question 2 [24 pts]:
Write a method called isSorted that tests if a one dimensional integer array is sorted in ascending order, the
method returns true if the array is sorted and false otherwise. Do not sort the array.
The method header is: public static boolean isSorted(int[ ] array)
Write a main program that creates an array of integer of 10 elements and initializes it using random integers
between 1 and 120 inclusive. The program tests if this array is ordered in an ascending order, by using the
above method and displays the result on the screen.
Example: the array A below is sorted so the method returns true while B is not sorted.
A 3 5 9 11 21 33 59 63 91 110
B 10 15 49 11 2 23 44 63 1 11
Sample Run1:
The generated array is: 3 5 9 11 21 33 44 63 91 110
This array is sorted
Sample Run2:
The generated array is: 10 15 49 11 2 23 44 63 1 11
This array is not sorted
Page 4 of 9
CSCI250 Final Exam – v1 Fall 2017 – 2018
Page 5 of 9
CSCI250 Final Exam – v1 Fall 2017 – 2018
Question 3 [30 pts]:
Write a method that takes as parameters an integer x and an array of integers list.
The method returns the sum of values in list that are greater than x.
Write a program that reads n integers from the user and stores them in an array list. Then, for each
number x in list, the program computes and prints the sum of numbers in list greater than x.
For example, in the sample run below, there are:
Two numbers greater than 5 (6 and 7) and their sum in 13 (6+7).
Three numbers greater than 3 (5, 6 and 7), their sum is 18.
One number greater than 6 (7), its sum is 7
Etc.…
Sample run :
Enter the number of integers: 5
Enter 5 integers 5 3 6 7 1
5: Sum:13
3: Sum:18
6: Sum:7
7: Sum:0
1: Sum:21
Page 6 of 9
CSCI250 Final Exam – v1 Fall 2017 – 2018
Page 7 of 9
CSCI250 Final Exam – v1 Fall 2017 – 2018
Question 4 [25 pts]:
A number is called a strong number if the sum of the factorial of its digits is equal to number itself
For example: 145 is strong since 1! + 4! + 5! = 1+24+125 = 145.
Write a method called factorial that takes as parameter an integer and returns its factorial.
Write a test program (main) that reads an integer from the user and prints out if the integer is Strong
or not.
Sample run1:
Enter a number: 145
145 is a Strong number
Sample run2:
Enter a number: 1501
1501 is not a Strong number
Page 8 of 9
CSCI250 Final Exam – v1 Fall 2017 – 2018
Page 9 of 9