Lebanese International University: CSCI 250 - Introduction To Programming - TEST-2: Student Name: Student ID
Lebanese International University: CSCI 250 - Introduction To Programming - TEST-2: Student Name: Student ID
Lebanese International University: CSCI 250 - Introduction To Programming - TEST-2: Student Name: Student ID
a) Create and initialize an array with five double values of your choice, between 10 and
18.
Answer: double [ ]a = {10, 13, 15 ,18 , 16.3};
b) The area of a cylinder is: Area= 2×π × r (r + h), where r and h are the radius and height
of the cylinder. Complete the header of the method that computes and returns the area of a
cylinder:
Answer: public static double Area( double n)
c) Assume having an array of decimal numbers named A. Write the code to print the
multiplication of the second element in A by the last element in A.
Answer: double multipl = A[1]*A[A.length-1];
d) Correct all the compile and logical errors in this method so it returns true if there
are no two equal consecutive values in the array, otherwise the method returns false.
For example, if the array contains {5, 6, 0, 0, 3} the method returns false. If the array
contains {5, 6, 4, 0, 3} the method returns true.
public static boolean CheckNonConsecutiveDuplicate(int[] A)
{ for(int i=0; i<A.length; i++)
{ if (A[i] = A[i+1])
return true;
else
return false;
}
}
Answer: { if (A[i] == A[i+1])
Return false;
e) Consider the following two methods and the main. Assume the methods are
implemented. Do NOT write code in the method’s body. Complete the main program
to invoke the two methods:
public static void absolute1(int[] A){…}
public static int[] absolute2(int[] A){…}
public static void main (String[] args){
int[] X={5, -2, 9, 10, -15, -34};
/// add code here to call absolute1 method
1|Page
Spring 2019-2020
absolute1(5);
/// add code here to call absolute2 method
Answer:
package scratch;
import java.util.Scanner;
public class Scratch {
int y = 2 * x + 1;
2|Page
Spring 2019-2020
}
}
pattern(alpha , n);
}
public static void pattern(char alpha, int n){
3|Page
Spring 2019-2020
}
}
}
public static String analyzeID(int ID){
if (ID/10000000 == 1)
{
System.out.println("Campus : Beirut");
4|Page
Spring 2019-2020
}
else if (ID/10000000 == 2)
{
System.out.println("Campus : Saida");
}
return null;
}
}
5|Page