Final Exam Study Review

Final Exam Study Review - ‘ ~

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ‘ ~ http://www-rohan.sdsu.edu/~masc0500/LECS107/f08 fessQ.txt “t7 x 2 ,4 FINAL EXAM STUDY SHEET C5107, FALL 2008 *** IMPORTANT NOTE: *** .e The Final Exam will be made by the Dept, not by me, I? and it won't be ready until shortly before the exam date. 1%.“ So, the following "sample Final Exam" is only a rough guess or approximation , L; of the Final Exam, based on previous years's exams. But, there will definitely be some Midterm—style questions Ty? 9n_ ( I 2 14 g? (6.9., what is outputted by some nested IFs, or loops, or etc.) = EL * W * vk“i g, g aw NOTE: * Since the Dept will create the Final Exam, it might have code with different whitespace usage, e.g.: while (x<=3o) { 1 instead of our usual while (x<=30} { 1 Also, you might see shortcut versions of Java variable assignment, e,g.: a+=10; instead of a=a+lo; (I use the longer way in lecture, since it works in ALL computer languages.) * The Dept MIGHT include a programming question that will NOT be fill—in—the—blank, i.e. asking you to write the entire program code. * Since we are running out of time in the Group Learning Manual, then we might not have enough time to discuss some of this Study Sheet's topics in detail (e.g., scoop and filter, or etc.)_ So, if the Final Exam ends up having some problems that we didn't have time to discuss, then I will cancel those questions. MIXED-MODE EXPRESSIONS #1. / What will be the output of the following program code? System.out.println( 9/2 1; System.out.println( 9.0/2 ); System.out println( 9/2D ); System.out.println( (doublgjg/Z 1; System.out.print1n( (doubleflS/Z) ); System.out1println( (int)9.0/2 ); Systemtout.println( (int)(9.0/2) J; NESTED "IF" STRUCTURES #2. what will be the output generated to the screen upon running the following program code? int a = 10; int b = 30; if (b+l < a*3) 1 of9 11/25/2008 11:27 AM r_______________elfi, 3 M- a. :10 ,‘ Fifi b: 301' http://www-rohan.sdsu.edu/~mascOSOO/LECS107/f08fessQ.txt d I)» 30 .c (b +| < mm) ' ~{ if (a + 50 >= b) ( System.out.println("Joe“): } else { System.out.println("Jane“); ) } ’50 V50 else if (a*3 <= b+100) { qo $0 if (b*3 >= a*3) ( System.out.println("John"); ) else if (b*3 >= a*3) ( ) System.out.println("Jim'); else { System.out.println("Kim"); } } else { if (a + 3 <= b) { System.out.println("Jessie"); } else ( System.out.println("Suzi“); ) } "FOR" LOOPS #3. What will be the out ut enerated to the screen upon running the following Program code? ’/:;;:fggifl.Sda3 5 gm wax» m :5 w" 35‘ for (int i=10;(1 >= 0;x1=1—2) ( \ System.out.println(3*i); "WHILE" LOOPS #4. jaé:' what will be the output generated to the screen upon running the following program code? 10; 5; int y int K II II while (x <= y+2) { x = x + 3; system.out.println(x*y); y——, } ' O WHILE" LOOPS #5. What ill be the output generated to the screen upon running the following program code? int x = 3; 2 of9 11/25/2008 11:27 AM qg‘ I \ "’ \J 3of9 http://www-rohan.sdsu.edu/~masc0500/LECS107/f08fessQ.txt {n1 3: t "3,“ «int y = 21; do ( Vx y=y-2: System.out.println(x+y): x = x + 3; ) while ( 2*x <= y+1 ); yfw "FOR" LOOPS #6. What will be the output generated to the screen upon running the following program code? éfisoé%; for (int x=4 ; x>=l ; x--) { int y = 30*x + 3; System.out.println("x=" + x + ", y=" + y); "FOR" LOOPS #7. What will be outputted by the following program code? int 9:9; int k=3; for (int x=10; k<g; g——) { k=k+2 ,- System.out.println(x); x++; 9=9+1i NESTED "FOR" LOOPS #8. What will be outpggteg;lnk the following program code? f” RAE ‘9 .2 Ryeficgjufi’ M‘ for (int x=l; XK=5j’ fl_fl ' Lfij ( ,.V_,/ d" for (int y=30; §>=2 ;f§—;) ( Wmumyv System.out.println("x=" + x + ", y=" + Y); NESTED "FOR" LOOPS #9. What will be outputtgfi by the following program code? Y, h «wax w for (int x=10; x<13; x++) //caution: look at the 2nd slot carefully { gnaw" for (int y=13; y>=x; y-—) //caution: look at the 2nd slot carefully { system.out.print1n("x=" + x + ", y=" + y); } } 11/25/2008 11:27 AM http://www-rohan.sdsu.edu/~mascOSOO/LECS107/f08fessQ.txt >1 wARRAYS ? #10. What will be the contents of the array arr, after the following code runs? int arr = new int[51; for (int i=0; i<=4; i++) I ( '=1\C'm§u, \WLL‘" 6% “a arr [i] = 10*1; ARRAYS #11. Write a single line of code which will do the same thing as the code in problem #10 above. (I.e., use the "braces" shortcut for initializing an array.) _ :n1 [-:] 5L _. n - “61%);DJIOJ 2:9, 3,9,tf0j/u , a :1“ 76$- 0 #12. ~ ~ W \ Create a "functio which receiyéS‘an integer parameter, and then computes andéfEtu 1%7the word "EVEN" if the parameter was an even number, or returnsfithe word "0 D" if the parameter was an odd number. smtJ’f a‘éctti Eélfnk§ check( i;# n ) if ( u m t:¥nwltwn; } else u‘ # r<$avn 093 ; } } a. FUNCTIONS AND SUBROUTIINES} swag? M§= t A»‘ Kg; #13. /em film/WW Sww‘ V or a. , r" 1" '/ greatewgiflsggrouting’/which receiyeeian integer parameter, and then,putputs)g dgmgdd ,4’(to the scréEnfipthe word "EVEN" if the parameter was an even number, “W ‘ " ; barWantputs EHETWOId "ODD" if the parameter was an odd number. “my . J Wm“ “whit wvé. checkicm n ) 1 if 1 else 1 1 FUNCTIONS AND SUBROUTINES #14, what will be the output generated to the screen upon 7 running the following program? 7 3 ,6 mo a; ’ ' ublic class Pro 1 “at l , " ,N , “HAG IE 9 7 r5.“ 1 “any? : L JCth | "NW public staticJVOidlmain (String args) ‘ ‘ 5 11V 3 1 _ “IN-«w flu.) (3,165 4 of9 11/25/2008 11:27 AM Wrenche-sdsu.,qgu:ma9é9§®/LECSImmatesstht s System.out.println(result); System.out.println( f(vl,4) ); print(2); : ,p ‘ fl., 1 IVH' :1 ‘ 5’,» public static int f(int y, int a) A ~ /,,' 1 (z , if y>=0) ‘1'»2‘ { return a; } 7 z I p; _ g 51 1 $158 7:, / 3H1 u‘SLljé’ ‘ 7f ‘11 ,1! Y = y + 6; 1” i a=a+1,- :4, 1‘,» , INK/“L , .7 EEEUEELXD . _ ‘9 ' V “1 ‘ )7 . } / , 1“ 7 public static void print(int m) { if (m =2 l) { System.out.println("Alpha"); } else { System.out.println(“Beta"); } ‘, y , , . {sf :5 q; man ,a X SAMPLE PROGRAMMING PROBLEM BMW M“, P VH4 s '. 3 3; am. i am. shim tags" is ”+ my“); "‘ e a program that will do theffollowing. First, it will input@ w ($5 whda yon” Vtsmvél Rs ! SAMPLE PROGRAMMING PROBLEM int“ #16. V, w a 1 9» , Write a Java program thatgprgmp ‘Dthe user to Qpput 100 integer data values. g ‘; After all 100 values have beenwinputted, it prompts'the'user to ‘ input a Ptarget"‘value. -&5‘/-’ >,,N_The program should count, and output, the number of data values that are , ‘1 1 ‘ >9“¥equal to the target value. } é? , ‘ ,x! 1A1 f 1 1f _ g . p g _ _ _ _ _ _ _ _ _ _ g _ 7 7 A A _ _ _ _ _ _ _ _ _ _ _ _ w _ A A A _ _ _ _ _ _ _ V _ _ v A A _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ fl 7 V ‘_ 1’11 5 um ' I A“ . , :51 5‘ M11! we, ./<<- ‘7/_« "’ ”’ *‘v . , . ‘1 LE PROGRAMMING PROBLEM ‘1 ‘4 . Stanx 1 3’1 // Wri ya,program that will do the following. * The program should ask the user to input antgdd-numbered integer‘ 521 j v‘ Whit Kaéi§3i (fauna); greater than 101 r~-eeer W.il.iiiiuv I ;w.tn .tv (Anfl"odd numberfi is a number that isn't evenly divisible by 2.) 1 _ _ J * For ease, assume that the user will always input an integer, and will ne%er input a double or a String. ,, " 7 “\ / ' _ * Your program;should compute and output theK§um,bf.all_odd numbers starting at l and ending at the user's inputted value} ‘7 * Note} if tie userksxinputted value doesn't meet the requirement of being fl‘i J greater than 10, thefi“the_program should ONLY output the message "Your number isn't great than 10". * Note} if the user's inputted value doesn't meet the requirement of being an reg; odd number, then the program should ONLY output the message "Your number is even, not odd". Example: if the user ianES"2l, then the program should compute the sum ll+l3+15+17+l9+21, which is 96. So, the program should output 96. Example: if the user inputs 11, then the program should compute the sum 11, which is 11. So, the program should output 11. 5 of9 11/25/2008 11:27 AM http://www—rohan.sdsu.edu/~InascOSOO/LECS107/f08fessQ.txt A W iInput tw _,teg§EE}from the user, then1compute and output the sum of all integers between (and including) the user 5 va ues. For ease, assume that the user will always make the second number be greater than or equal to the first number. int first = Console.readInt("What's the first number?"); int last = Console.readInt("What's the last number?"); int ca1c=ng;l, ’ - for (int 1:513,- i<= ,-i“' ) { } calc ll SAMPLE PROGRAMMING PROBLEM #19. 1 Fill in the blanks, to create a short section of code (inside a "main" routine) 1 which determines whether or notkg‘gs a divisor ‘123456Tf§ - " (A "divisor" is a number that dividesievefily‘ifitEflafioEHér’number, e.g. 2 is a divisor of 10, 3 is a divisor of 9, etc.) If it is a divisor, then output the words "9 IS a divisor". Otherwise, output the words "9 is NOT a divisor".- if ( ) 1 ) else 1 1 """"""""""""""""""" “all—433;"?Q‘Efii"""'""""””"' ...3 .M .M...WWWMWMWWW, k a“.ng 195 L, 53m 1 i SAMPLE PROGRAMMING PROBLEM E tb 59,”,fl§:_ h EV D .mawwmmwmwmmeWnfl , r J #20. .»:~"-~\ Create a Java{"fungtion9 namedglargestNTDivisv calculates and,réturn§§1as an Hafiswerhthhek\ integer n. } ‘\~\ii_// ‘ that 0/0 (A "nonetrivial divisor" of n is a number between 2 and #v hat divides evenly into n with no remainder. E.g., thef ' ' s)of 10 are; 1,, ,“fijwlo_ But, the’fiEfi “Ifidivisors of 10 are: 2, 5.) fia‘ On the other hand, if n has divisors between 2 and n—1, then the function should return the value —1. (A number which has no nonetrivial divisors, is called "prime".) For example, assume that a Progl main routine has this code- System.out.println( largestNTDivisor(lO) ); //will output the value 5 System.out.println( largestNTDivisor(9) ); //will output the value 3 System.out.println( largestNTDivisor(ll) ); //will ouptut the value —1 Hint: use Java's modulus operator, i.e. the % symbol. Remember that it returns the remainder after a division. public Slab: {fli largestNTDivisor(int n) { //code the entire body EARRAYQJ #21. Assume that a Progl "main" routine declares an array "a" as: int a 2 new int1151; 6 of9 11/25/2008 11:27 AM 70f9 scoop AND FILTER WITH OPTIMIZATION ,-'l#22 . http://www—r0h311.sdsu.edu/~masc050OILECS107/f08festhxt and also suppose that the "main" routine has code which stores a;:andonr\ integer value in each element of the array. ¢~“/ Now, complete that5main routing) by writing Java code which counts and prints \‘” "H4 g‘U’h 61/. as in depth! V“ thé‘”EfEen the number of repeated data values ~A i.e., the numbéifof times EEEE‘EEE”§aIfié“6E an eiea§fif“I§”Efi锓“““‘ same as the value of the preceding element. For example, if the array happens to contain these data values {2,3,6,4,3,3,6,7,8,4,5,5,5,9,3} then, in this situation, there are 3 sets of repeated values (indicated by underlines above). Therefore, in this situation, your program should output "3". I... A cylinder with height H and radius R has volume . Wm . ‘ given by volume = pi * (R squa ed)w*‘H and surface area, w*jpi?*éfl” tKR+H) where we use piwabovéito represent 3.14..., which exists in Java as the constant Math.PI You are working as a software engineer for Campbell's Soup Company, and your job is to decide on he shape of a new "soup can". The can must be cylindrical, with a height and radius that are an integer number of inches each. The can must have a surface area which is asvsmalliaswpossible (to decrease the amount of metal, and therefore to decrease production costs), but must hold at 1east“T00»oubig inches of soup. Fill in the blanks below to complete a Java program that computes and outputs to the screen the integer values H and R for the desired can. public class ErogTW' { public static void main (String args) ( int bestRsoFar = 'iL, int bestHsoFar = v , double smallestAreasoFar = ; for_flint h: ; h<= ; ) { .I" . .lw for (int r: ; r<= ; ) { double currentCansVol = Math.PI * * * ; double currentCansArea = 2 * Math.PI * * ; if ( && ) { _ SystemLoutzprintln{"Use radius " + System.out.println("and height " + } , SOOP AND FILTER WITHOUT OPTIMIZATION “A #23 TM,“ _ , Assume‘you have $5.00, and you walk into a school—supply store. They sell pencils for $0.30 each, pens for $0.80 each, and folders for $1.60eachi They have a very large number of folders, but they only/have 3 EEnCils and S pens remaining on the shelves. Output ALL possible purchases you canwmahe (including buying nothing). Each purcha é should fl outputted in the following format: Buy\¥‘pencf s;'# pens: # folders. for (int a— ; a<= ; ) 11/25/2008 11:27 AM http://www-rohan.sdsu.edu/~mascOSOO/LECS107/f()8fessQ.txt PROGW, PROBLEM //. ,. , 7‘ \‘ “NW. K - 1’ J 'fyhp:;blem #23 above, so that it only outputs one value: ZOTAQ$NUMBfifi of possible purchases you can make. t (I.§53¢€hstead of outputting all possible purchases, just purchases exist.) \ a y “I o pu ~hpw man possible ,kgonsi‘gr two positive (i.e., not 0, and not negative) integers named x and y. <50 ,f’How many ways ggetthererwto‘maggfithe integers multiply to a value which is \nless thanyor égga; to” a?» vvvvvv “W' a “' anks to create a program which will compute and output the answerwto,thisn estmohtw (Notegfiéiawyesg ,,v r are considered different ways, and therefore eqph¢§ OuTamffircounted separately.) 5 int count: ; for (int x: { ‘ pm ,g_,.4,x $$ . I 7te hewggdewsn $§3A above, so that it now counts only unordered pairs § ons rather than permutations). In other words, treat "1 and 50" and "50 and 1" as the same thing, and thEreere count it only once rather than as two separate ways. ‘ "' l 4 ~ int count: ; toruiint x: ; x$=, a ) 8 of9 11/25/2008 11:27 AM http://www-rohan.sdsu.edu/~masc0500/LECS107/f08fessQ.txt ( fflW/_Wuw_ww» fogjfffigr§= { 71 “WWW? (ww_ ‘SAMPLE PROGRAMMING PROBLEM l #26. Fill in the blanks, to create program code which will compute and output the following sum: .«l 5X9y¢l vb 9 11 13 25 27 3 12 14 26 {29 sum: ———— + "w + ——v~ + + —-—- + ——-- 13 15 17 29 31 11 13 15 27 29 double sum =- _Q_; . I for (int i=_[_o_,- i<= 3,-1: 1.) { } M System.out .println (5‘ ); sum = sum + Math.pow(1 ,1'!) / Math.pow(l”‘ ,16); //note: Math.pow always gives its answer as data type "double", //therefore we are not doing "int/int" division above. END OF FILE {My}. /’ . 521;,m2 . -\ ‘ 4 a 4—\ 1 A I + w W, “WWW + t ‘+3 I MIO M+I+x=13 1“ +le’5 X: (3—H y; g 0 V,/T«34X:’Sr Imwz “7 15 M“? x — tS'—-‘3 X: l 9 of9 11/25/2008 11:27 AM ...
View Full Document

Page1 / 9

Final Exam Study Review - ‘ ~

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online