You are on page 1of 11

# CECS 100 Logic Design and Java

## FINAL EXAMINATION SAMPLE

1. Write Java statement that does the following. a. Declare a variable named distance that is used store a real number. Answer double distance; b. Declare and initialize a variable named age with a value of 30. Answer int age = 30; c. Declare three integer variables ! " and z in one Java statement. Answer int ! "! z; #. Assume the variables a = #! b = \$! and c = %. &ndicate b" circling the ' or ( if each of the following condition is true or false. a = = \$ )) b * # 1 += b ,, c +=3 +-a * #. T T T ( ( (

3. Write an if statement that dis/la"s the message 0 'he number is valid1 if the variable s/eed is within the range 0 through #00. Answer if - s/eed *0 ,, 2 #00. 3"stem.out./rintln-0'he number is valid1.; \$. Write an if statement that dis/la"s the message 0'he number is invalid1 if the variable s/eed is outside the range 0 through #00. Answer if-s/eed 2 0 )) s/eed *#00. 3"stem.out./rintln-0'he number is invalid1.; 4. Write an if5else statement that assigns #0 to the variable " if the variable 100. 6therwise! it should assign 0 to the variable ". Answer if - * 100. " = #0; else is greater than

" = 0; %. 7se the following if 8 else if statement to answer 9uestions a! b and c. if = = 0. 3"stem.out./rintln-0 else if - * 10. 3"stem.out./rintln-0 else if - 2 4 ,, * 0. 3"stem.out./rintln-0 else 3"stem.out./rintln-0 is 01.; is greater than 101.; is less than 41.; is negative1.;

a. What is the out/ut if Answer is greater than 10 b. What is the out/ut if Answer is less than 4 c. What is the out/ut if Answer is negative

is 30:

is \$:

is 510:

;. <ow man" times will 0<ello World1 be /rinted in the following /rogram segment: int count =1; while -count 2= 4. =3"stem.out./rintln-0<ello World1.; count>>; ? Answer 4 times @. Write a for loo/ that dis/la"s "our name 10 times. Answer for-int n = 0; n2 10; n>>. 3"stem.out./rintln-0John1.;

A. Write a com/lete Java /rogram with the following re9uirementsB a. Define a method sum#Cumbers with two /arameters named num1 and num#. 'he method returns the sum of num1 and num#. b. Dall the function sum#Cumbes in the main method. Eass two values 10 and #0 to the function sum#Cumbers. Dis/la" the returned value from the function sum#Cumbers. Answer im/ort Fava .swing.G; /ublic class test =/ublic static void main-3tring HI args. = int total; total = sum#Cumbers-10!#0.; 3"stem.out./rintln-0'he sum is 0 > total.; ? /ubic static int sum#Cumbers-int num1! int num#. =int sum; sum = num1 >num#; return sum; ?

10. Write a com/lete Java /rogram with the following re9uirementsB a. Define a method called showJessage that dis/la"s a message 0<ave great holida"s1 b. Define a the main method that called showJesssage method 10 times using a for loo/. Answer im/ort Fava .swing.G /ublic class test =/ublic static void main-3tring HI args. = for -int n = 0; n 2 10; n>>. showJessage- .; ? /ublic static void showJessage- . = 3"stem.out./rintln-0<ave great holida"s1.;

? ?

11. &ntialize a one dimensional arra" called D with the following valuesB 10! \$! 3\$! %;! @A and #4. Answer int DH I = = 10! \$! 3\$! %;! @A! #4?; 7sing the arra" D above! answer the following 9uestionsB a. What is the value of DH3I; Answer %; b. 7se a for loo/ to dis/la" all the elements in the arra" D Answer for-int n = 0; n24; n>>. 3"stem.out./rintln-DHnI.;

c. 7se a for loo/ to iterate the arra" D in order to find the smallest element. Answer int smallest = DH0I; for-int n = 1; n 2 4; n >>. if - smallest * DHnI. smallest = DHnI; 1#. Assume that arra" A and K are defined belowB int AH4I==10!#0!30!\$0!40?; int KHI = new intH4I; a. H4 /ointsI Write Java statements that co/" the arra" A to the arra" K. <intB use a for loo/. Answer for-int = 0; 24; >>. KH I = AH I; b. H4 /ointsI Write Java statements that calculates the average of all elements in the A arra" using a for loo/. Assign the average to a variable name avg. Answer int sum = 0! average; for-int = 0; 2 4; >>.

sum = sum > A H I; avg = sum L4; 13. Write a com/lete Java /rogram that creates a one dimensional arra" called num with the following valuesB 10!34!\$%!%;!A@!#0!%; and \$4. 'he /rogram then accumulates onl" the even values in the arra" num and dis/la"s the accumulated value. im/ort Fava .swing.J6/tionEane; /ublic class 3um = /ublic static void main-3tring argsHI. = int numHI = =10!34!\$%!%;!A@!#0!%;!\$4?; int sum =0; for-int i = 0; i2 @;i>>. if-numHiIM# == 0. sum = sum > numHiI; 3"stem.out./rintln-N3umB N > sum.; 3"stem.e it-0.; ? ? 1\$. Write a com/lete Java /rogram that meets the following re9uirementsB a. Dreate a method called calculate3ales. 'he methods acce/ts two arguments. 1st argumentB 9uantit" 5 t"/e integer #nd argumentB /rice/erunit 8 t"/e double 'he method calculates and returns the sale where sales = /rice/erunit times 9uantit". b. Write the main method that asOs the user to enter 9uantit" and /rice/erunit! calls the method calculate3ales and then dis/la"s the sales. Answer im/ort Fava .swing.G; /ublic class 'est =/ublic static void main -3tring HI args. = 3tring s; int 9; double /rice!sales; s = J6/tionEane.show&n/utDialog-NPnter /rice /er unitN.; /rice = Double./arseDouble-s.; s = J6/tionEane.show&n/utDialog-NPnter 9uantit"N.; 9 = &nteger./arse&nt-s.; sales = calculate3ales-9!/rice.; 3"stem.out./rintln-N3alesB N > sales.;

?LLend main /ublic static double calculate3ales-int 9uantit"! double /riceEer7nit. =double sales; sales = /riceEer7nitG9uantit"; return sales; ?LLend method ?LLend class 14. What is the out/ut /roduced b" the following: int n = 10; while- n * 0. =3"stem.out./rintln-n.; n = n 53; ? Answer 10 ; \$ 1 1%.. Donvert the following while loo/ to a for loo/B int count = 0; while-count>> 2 40. =3"stem.out./rintln-NAN.; ? Answer for-int count = 0; count 240; count>>. 3"stem.out./rintln-NAN.; 1;. Write a for loo/ that dis/la" the following set of numbersB 0! 10! #0! 30! \$0! 40 . . . 1000 Answer for-int i=0; i 2=1000; . = 3"stem.out./rintln-i > N! N.; i = i > 10; ? or

for-int i=0; i 2=1000; i = i > 10. 3"stem.out./rintln-i > N! N.; 1@. Donvert the following while loo/ to a for loo/B int count = 14; while- count55 * 4. =3"stem.out./rintln-1A1.; ? Answer for-int count =14; count * 4; count55. 3"stem.out./rintln-1A1.; 1A. Write a com/lete Java /rogram that dis/la"s the smallest value of the following #5D arra"B 10 5 2 12 4 4 1 ! " ! 5

Answer im/ort Fava.util.G; /ublic class 'est =/ublic static void main-3tringH I arg. = 3canner in/ut = new 3canner-3"stem.in.; int AHIHI = ==10! 4! #!1#?!=\$! A! 1! 3?! = \$! @! 3! 4??; LLDetermine the smallest value in the arra" int smallestQalue = AH0IH0I; for-int r = 0; r2 A.length; r>>. for-int c=0; c2AHrI.length; c>>. if-AHrIHcI 2 smallestQalue. smallestQalue = AHrIHcI; 3"stem.out./rintln-smallestQalue.; ?LLend method ?LLend class

#0. Draw a Ra/tor flowchart that reads ten integer numbers and dis/la" the smallest and largest of the in/uts.

#1. Draw a Ra/tor flowchart that dis/la"s a table showing the numbers 1 through 10 and their s9uares. (or the out/ut s"mbol! "ou are allowed to use once in "our solution. 6ut/ut sam/leB

1 # 3 \$ 4 % ; @ A 10

1 \$ A 1% #4 3% \$A %\$ @1 100

##. H; /ointsI Dom/lete the Java code fragment below b" filling in blanOs. for-int i = 1; ##########; i=i>1. = for-int; \$ %###### ;###########.

3"stem.out./rint-\$.; 3"stem.out./rintln-.; ? &f "ou fill in blanOs correctl"! "our code fragment should /roduce the following out/utB 1 1# 1#3 1#3\$ 1#3\$4 1#3\$4% 1#3\$4%; 1#3\$4%;@ 1#3\$4%;@A NOTEB 'he method print does not move the cursor to the first column of the ne t line! but method /rintln does. Answer for-int i = 1; i&10; i=i>1. = for-int \$%1; \$&%i ;\$% \$'1. 3"stem.out./rint-\$.; 3"stem.out./rintln-.; ? C(i)ica* T+in,ing E-e(cises 1. <ow man" different license /lates are available if each /late contains a se9uence of three letters followed b" three digits: Answer #% #% #% 10 10 10 = 1;!4;%!000

#. A student can choose a com/uter /roFect from one of three lists. 'he three lists contain 10! #0! 4 /ossible /roFects! res/ectivel". Co /roFect is more than one list. <ow man" /ossible /roFects are there to choose from: Answer 10 > #0 > 4 = 34

3. What is the /robabilit" that when two dice are rolled! the sum of numbers on the two dices is ;: Answer 'here are si successful outcomes! namel"! -%!1.! -1!%.! -#!4.! -4!#.!-3!\$.!-\$!3.. 'here are a total of 3% e9uall" outconme when two dices are rolled. 'he /robabilit" that seven comes u/ when two fair dice are rolled is %L3% = 1L%. \$. &n the discrete mathematics class ever" student is a maFor in com/uter science or mathematics! or both. 'he number of students having com/uter science as a maFor -/ossibl" along with mathematics. is #4; 'he number of students having mathematics as a maFor -/ossibl" along with com/uter science. is 13; and the number of students maForing in both mathematics and com/uter science is @. <ow man" students are in this class: Answer 'he nunmber of students in this class = #4 > 13 5 @ = 30