0% found this document useful (0 votes)
115 views7 pages

CSCI250 - Sample Final Exam - Solution

The document is a sample final exam for an Introduction to Programming course. It contains 3 parts: 1) multiple choice questions, 2) a question to write a method to check if circles intersect, and 3) a question to write a program to check if an array of integers is balanced. The exam allows 120 minutes and does not allow documents or calculators. It has a total of 100 points.

Uploaded by

medo.miso.2000
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)
115 views7 pages

CSCI250 - Sample Final Exam - Solution

The document is a sample final exam for an Introduction to Programming course. It contains 3 parts: 1) multiple choice questions, 2) a question to write a method to check if circles intersect, and 3) a question to write a program to check if an array of integers is balanced. The exam allows 120 minutes and does not allow documents or calculators. It has a total of 100 points.

Uploaded by

medo.miso.2000
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/ 7

CSCI250 Sample Final Exam Fall 2020 – 2021

[Solution]

Lebanese International University


School of Arts and Sciences - Department of Computer Science

Course Name : Introduction to Programming Course Code : CSCI250


Date : Section : All
Instructor : Time :
Auditorium : Seat Number :

Number of pages : 14 Allowed Time : 120 minutes


Documents : Not Allowed Calculators : Not Allowed

Problem# Grade Total Grade


Part I
Question 1 /20
Part II
Question 1 /25
/100
Question 2 /25
Question 3 /30

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

What will the output of the following code be?


int x=2,y=3; a. A 5 B F
System.out.print("A "+x+y);
b. A5 C
if(x>y && y<3)
System.out.print(" B"); c. A 23 C
else d. A 23 C F
System.out.print(" C");

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?

a. int sum = 0, i = 0; b. int sum = 0, i = 100;


while ( i >= 0 ) { while ( i != 0 ) {
sum += i; sum += i;
System.out.println(“Hello”); i--;
} }

Which one of the following will declare an array and initialize it with five integer numbers?

a. Array a = new Array(5);


b. int [ ] a = {23,22,21,20,19};
c. int a [ ] = new int[6];
d. int [5] array;

Page 4 of 7
CSCI250 Sample Final Exam Fall 2020 – 2021
[Solution]
Part II: Write a program

Question 1: [25 pts]

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.

The header of the method is as follows:

public static void checkIntersection(double x1, double y1, double r1, double x2, double y2, double r2)

Hint:

Distance between centers C1 and C2 is calculated as follows:


d = Math.sqrt((x1 - x2)2 + (y1 - y2)2).

There are three conditions that arise:

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:

public static void checkIntersection(double x1,double y1,double r1,double x2,


double y2, double r2) {
double d = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
if(d == r1 + r2)
System.out.println(“Circles touch each other”);
else if(d > r1 + r2)
System.out.println(“Circles do not touch each other”);
else
System.out.println(“Circles intersect”);
}

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

Use the following method header:


public static void evenToZero(int limit)

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:

Enter a positive integer: 5


The even numbers from 5 down to 0 are 4 2 0

Sample Run 2:

Enter a positive integer: 9


The even numbers from 9 down to 0 are 8 6 4 2 0

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();

System.out.print("The even numbers from “+number+” down to 0


are: "+ evenToZero(number));
}
}

Page 6 of 7
CSCI250 Sample Final Exam Fall 2020 – 2021
[Solution]

Question 3: [30 pts]


An array listA is called balanced if it stores the same count of even and odd integers. Write a program to
test an array listA filled with 10 integers from an initializer list as follow:
{22,5,18,4,9,12,13,17,23,24}. Then the program will output the integers stored in listA and
checks if the array listA is balanced or not.

Sample Run:

listA store: {22 5 18 4 9 12 13 17 23 24}


listA is a balanced array

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("}");

for(int i=0; i<listA.length; i++)


if(listA[i]%2 ==0)
even++;
else
odd++;

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

You might also like