CSCI250 Introduction To Programming - FinalExam-sample2
CSCI250 Introduction To Programming - FinalExam-sample2
int b = 0; b) 7
{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);
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
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.
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