CSCI250 Final Exam – v1 – solution 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 5
CSCI250 Final Exam – v1 – solution 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)
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 2 of 5
CSCI250 Final Exam – v1 – solution Fall 2017 – 2018
Question 2 [24 pts]:
package checksorting;
public class CheckSorting {
public static void main(String[] args) {
int[] numbers = new int[10]; // 2pts
for (int i = 0; i < 10; i++) { // 2pts
numbers[i] = (int)(Math.random()*120)+1; // 2pts
}
System.out.print("The generated array is : "); // 1pt
for(int j=0;j<10;j++) // 2pts
System.out.print(numbers[j]+" "); // 1pt
if (isSorted(numbers)) // 4pts
System.out.print("\n_BThis array is sorted.\n"); // 1pt
else
System.out.print("\nThis array is not sorted.\n"); // 1pt
}//end main
public static boolean isSorted(int[] numbers) {
for (int i = 0; i < numbers.length - 1; i++) { // 2pts
if (numbers[i] > numbers[i + 1]) // 4pts
return false; { // 1pts
}
return true; // 1pts
}
Page 3 of 5
CSCI250 Final Exam – v1 – solution Fall 2017 – 2018
Question 3 [30 pts]:
public class Exercise {
public static int sum(int[] list, int x){ // 3pts
int sum=0; // 2pts
for(int i=0;i<list.length; i++) // 2pts
if(list[i] > x) // 2pts
sum += list[i]; // 2pts
return sum; // 2pts
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of integers:"); // 1pt
int n = input.nextInt();// 1pt
int[] list = new int[n]; // 1pts
System.out.print("Enter " + n + " integers:"); // 1pt
for(int i=0; i<n ; i++) // 2pts
list[i] = input.nextInt();// 2pts
int s; // 1pt
for(int i=0; i<n ; i++){ // 2pts
s = sum(list, list[i]); // 4pts
System.out.println(list[i] + ": sum: " + s); // 2pts
}
Page 4 of 5
CSCI250 Final Exam – v1 – solution Fall 2017 – 2018
Question 4 [25 pts]:
import java.util.Scanner;
public class Strong {
public static int factorial(int n) { // 2pts
int fact = 1; // 2pts
for (int i = 1; i <= n; i++) {// 2pts
fact *= i; // 2pts
}
return fact; // 2pts
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: "); // 0.5pt
int n = input.nextInt();// 0.5pts
int m=n; // 1pts
int sum = 0; // 1pts
while (n != 0) { // 2pts
int digit = n % 10; // 1pts
sum += factorial(digit); // 4pts
n=n/10; // 1pts
}
if(sum==m) // 2pts
System.out.println(m+" is a Strong number"); // 1pts
else
System.out.println(m+" is not a Strong number"); // 1pts
Page 5 of 5