Sample Final Exam 3 PDF

Title Sample Final Exam 3
Author Fadi Shamtia
Course introduction to java
Institution Lebanese International University
Pages 4
File Size 189.1 KB
File Type PDF
Total Downloads 98
Total Views 175

Summary

Download Sample Final Exam 3 PDF


Description

CSCI250

Sample Final Exam

Spring 2015 – 2016

Problem #1: [27 pts. 3 pts. for each] Circle the correct answer QUESTIONS

ANSWERS

What would be the output of the method call: m( 1324)

public static void m( int number){ int x = number; int count = 0; while ( x > 0) { x = x / 10; count++; } for( int i=0; i 0) { System.out.print(message); n--; } } a. 10 Given the following statement int[ ] list = new int[10];

b.

9

c.

The value depends on how many integers are stored in list.

list.length has the value d.

Which of the following is correct?

a. b. c. d.

P

double double double double

1 f4

[] [] [] []

None of the above a1= a2= a3= a4=

new double(2); new {5,2}; new {5.1,2.8}; {5,2};

CSCI250

The header of a method for testing whether a

number is even or not. It returns true if the number is even and false otherwise.

Sample Final Exam

Spring 2015 – 2016

a. public static boolean isEven(int n1, int n2) b. public static void isEven(int n1) c. public static boolean isEven(int n) d. public static int isEven(int n1, int n2)

What is the output of the following code? public static void main(String[] args) { System.out.println(method(6)); } public static int method(int x){ while(x > 3) System.out.print(x--); return x; } What is the output of the following code? int[] list = {2, 4, 6, 8}; list[list[0]] = 0; list[1] = -1; list[list.length - 1] = 9; for(int i = 0; i < list.length; i++) System.out.print(list[i] + " "); What is the output of the following code? public static void main(String[] args) { int[][] m = new int[3][5]; System.out.println(m[1].length); }

a. 654 b. 6543 c. 543 d. 5432

a. 2 -1 0 9 b. 2 -1 9 8 c. -1 0 9 8 d. 0 -1 6 9

a. b. c. d.

a. b. c. d.

5 3 syntax error 15

What is the output of the following code? public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4}; int[] y = new int[5]; for (int i = 0, j = 3; i < y.length-1; i++, j--) { y[i] = x[j] + i; System.out.print(y[i] + " "); } } } P

2 f4

a. b. c. d.

4321 4444 1234 error

CSCI250

Sample Final Exam

Spring 2015 – 2016

(BMIS, MATH and PHYS students: CHOOSE TWO PROBLEMS and solve) Problem #2: [27 pts.] Write a program to enable the user to input the basic salary of 10 employees and store them in a 1D array of integers of size 10. The program should then prompt the user to choose from the following menu: A. Calculate and print on the screen the highest basic salary. B. Find the number of basic salaries that are less than the average. C. Calculate and print on the screen the new salary of all employees by deducting the income tax of 3%, each 5 salaries on a separate line (without updating the array’s contents). Sample Run1: Enter 10 basic salaries: 613 590 984 1290 586 230 1069 491 120 1005 Choose one of the following: A to display the highest basic salary B to display the number of basic salaries that are less than the average C to display the new salary after deducting the income tax A 1290

Sample Run2: Enter 10 basic salaries: 613 590 984 1290 586 230 1069 491 120 1005 Choose one of the following: A to display the highest basic salary B to display the number of basic salaries that are less than the average C to display the new salary after deducting the income tax B 6

Sample Run3: Enter 10 basic salaries: 613 590 984 1290 586 230 1069 491 120 1005 Choose one of the following: A to display the highest basic salary B to display the number of basic salaries that are less than the average C to display the new salary after deducting the income tax C 594.61 572.3 954.48 1251.3 568.42 223.1 1036.93 476.27 116.4 974.85

P

3 f4

CSCI250

Sample Final Exam

Spring 2015 – 2016

Problem #3: [23 pts] Write a program that reads a sequence of 10 positive integers, and then computes the alternating sum. For example, if the program is executed with the input data {1, 4, 9, 16, 9, 7, 4, 9, 11, 5} then it computes 1 − 4 + 9 − 16 + 9 − 7 + 4 − 9 + 11 – 5 and displays the result. Your program should also compute and display the factorial (denoted as !) of the last number entered. Sample Run: Enter 10 integer numbers: 1 4 9 16 9 7 4 9 11 5 The alternate sum is: -7 5! = 120

Recall: x! = x * (x-1) * (x-2) * …. * 2 *1

Problem #4: [23 pts] 1. Write a method findDistanceInt that returns the positive distance between two integers a and b. For example findDistanceInt(4, 6) should return 2, findDistanceInt(12, 3) should return 9. 2. Write a method findDistanceString that returns the positive distance between two integers a and b, however, as a String. For example findDistanceString(4, 6) should return "d(4, 6) = 2", findDistanceString (12, 3) should return "d(12, 3) = 9". 3. Write a program that reads 6 integers from the user. The program should then display the distance between each two consecutive numbers in the sequence as well as the maximum distance using the above two methods. Sample Run1: Enter 6 integer numbers: 1 4 9 16 8 7 d(1,4) = 3 d(4,9) = 5 d(9,16) = 7 d(16,8) = 6 d(16,8) = 6 The maximum distance is 7

P

4 f4...


Similar Free PDFs