CSCI250 - Sample Final Exam - Solution
CSCI250 - Sample Final Exam - Solution
[Solution]
Good Luck
Page 1 of 7
CSCI250 Sample Final Exam Fall 2020 – 2021
[Solution]
Part I: Multiple Choice Questions
Question 1: [20 pts – 4 pts each] Circle the correct answer:
Answer
public static void main(String[ ] args) { a- 2, 1, -2
int [ ]a = {2, 1, -2}; b- -2,
1,
for (int i =a.length - 1 ; i >= 0 ; i--)
2,
System.out.print(a[i] + "," );
} c- -2,1,2,
d- 2
System.out.print(" F");
Page 2 of 7
CSCI250 Sample Final Exam Fall 2020 – 2021
[Solution]
public class CSCI250Final {
public static void main(String[ ] args){
int x = 0;
method1(x);
System.out.print(x + " " );
}
public static void method1(int x){ a- 1 2 3
x++; b- 1 2 0
System.out.print(x + " " ); c- 0 1 2
method2(x);
d- 2 1 0
}
public static void method2(int x){
x*=2;
System.out.print(x + " " );
}
}
Page 3 of 7
CSCI250 Sample Final Exam Fall 2020 – 2021
[Solution]
Which of the following is an infinite loop?
Which one of the following will declare an array and initialize it with five integer numbers?
Page 4 of 7
CSCI250 Sample Final Exam Fall 2020 – 2021
[Solution]
Part II: Write a program
Write a method that prints on the screen a message stating whether 2 circles touch each other, do not
touch each other or intersect. The method accepts the coordinates of the center of the first circle and its
radius, and the coordinates of the center of the second circle and its radius.
public static void checkIntersection(double x1, double y1, double r1, double x2, double y2, double r2)
Hint:
1. If d == r1 + r2
Circles touch each other.
2. If d > r1 + r2
Circles do not touch each other.
3. If d < r1 + r2
Circles intersect.
ANS:
Page 5 of 7
CSCI250 Sample Final Exam Fall 2020 – 2021
[Solution]
Question 2: [25 pts]
a- Write a method called evenToZero that takes as parameter an integer ‘limit’. The method prints on the
screen all the even numbers from ‘limit’ down to 0 (inclusive).
b- Use the above method to write a test program which reads from the user a positive integer and prints all
the even numbers from this integer down to 0 via invoking the method evenToZero.
Note: Assume that the user will enter a positive integer. (Do not check if the input is positive or not)
Sample Run 1:
Sample Run 2:
ANS:
import java.util.Scanner;
public class PrintEven {
public static void evenToZero(int limit) {
for (int i=limit; i >= 0; i--)
if (i%2 == 0)
System.out.print(i);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int number = input.nextInt();
Page 6 of 7
CSCI250 Sample Final Exam Fall 2020 – 2021
[Solution]
Sample Run:
ANS:
package javaapplication345;
public class JavaApplication345 {
public static void main(String[] args){
int listA[] = {22,5,18,4,9,12,13,17,23,24};
int even =0, odd=0;
System.out.print("listA stores:{");
for(int i=0; i< listA.length; i++)
System.out.print(listA[i]+" ");
System.out.println("}");
if(even == odd)
System.out.println("listA is a balanced array");
else
System.out.println("listA is not a balanced array");
}
}
Page 7 of 7