CIS 1068 Progrm Design nd Astrction Spring2015 Midterm Exm 1 Nme SOLUTION Pge Points Score 2 15 3 8 4 18 5 10 6 7 7 7 8 14 9 11 10 10 Totl 100 1 P ge
1. Progrm Trces (41 points, 50 minutes) Answer the questions elow out ech snippet of code. For the ske of revity, I hve left off the lines "pulic clss <ClssNme> {" nd "pulic sttic void min(string [] rgs) {" from most of these questions. Pretend they re there.. (4 points) Numer Clcultion: wht re the vlues of w, x, y, nd z fter these lines execute? int w; int x = 3, y = 8; int z = y % x; y += 1 + z; w x y z? 3 11 2. (3 points) Vlue of oolen vrile nd precedence order: wht re the vlues of,, nd c fter these lines execute? oolen ; oolen = 2 < 5 / 2 && true; oolen c = (! && 1 > 6 % 2); c. (3 points) Type Comptiility: wht re the vlues of,, nd c fter these lines execute? int = 2 + 3 / 2; doule = 2 + 3 / 2; doule c = 2 + 3 / 2.0;? 3 flse true d. (1 point) String: wht does this code disply on the screen? c c 3.0 3.5 int = 0; oolen = flse; String s = ""; System.out.println( + s + ); 0flse e. (4 points) Arry: wht re the vlues of,, c, nd d fter these lines execute? int [ ] ; int [ ] = null; int [ ] c = new int[4]; int [ ] d = {2, 3, 4; c[2] = 1; d[0] = d[1] d.length; c d? null 0 3 4 0 0 1 0 2 P ge
f. (8 points) For ech column, show wht the code displys on the screen. Code Output: int x = 2; int y = 3; int z = 4; z=5 if(x + y > z) z ++; System.out.println("z=" + z); int x = 1; int y = 2; oolen = true; true oolen c = flse; if( && x >= y) { System.out.println(x); else if (c x < y) { System.out.println(); else System.out.println(c); int [] x = {1, 2, 3; int [] y = {1, 3, 5, 7; if(x.length>y.length) { x[y.length-1] = y[y.length-1]; 17 else if (x[2] > y[2]) { y[3] = x[2]; else { y[1] = x[1] + x[2]; System.out.println(y[1]+y[2]+y[3]); oolen = true; int i = 1; 2 while () { true = i ++ <2; 3 System.out.println(i); flse System.out.println(); 3 P ge
g. (18 points) Wht re the vlues of,, nd c t the end of the code in ech column? Code Vlue of,, nd c int [] = {3, -2, 4, 5; int = 0, c = 0; for( =0; <.length; ++) { 10 5 3 2 c = c + []; 4 for( =0; <.length; ++) { [] = c / (+1); c 10 int [] = {1, -2, 4, 3; int = [0]; int c = 0; for( c=0; c <.length; c++) { 1 1 4 4 if ([c] > ) { = [c]; [c] = ; c 4 4 String = "You-see-you- drive-you."; String u = "you"; String v = "they"; int =.indexof(u); String c = ""; -1 "." while(>=0) { String word =.sustring(0,); c = c + word + v; =.sustring(+u.length()); =.indexof(u); c = c + ; c "You-see-they-drive-they." 4 P ge
2. Fill the lnks (8 points, 10 minutes) For the requirement specified in the elow, complete the corresponding loop progrm so tht the computer cn print out the expected result.. Write progrm to print out the result of 1+2+3+4+ +99. int totl = 1 ; int i = 2; while (i<100){ totl=totl+i; i++; System.out.print(totl);. Write progrm to print out the result of 1+2+4+8+ +4096. int totl = 0 ; int i = 1; while (i <= 4096 ){ totl=totl+i; i *= 2; System.out.print(totl); c. Write progrm to print out the result of 1+2+4+8+ +4096. int totl = 1 ; int i = 1; while (i<3000){ i *= 2; totl=totl+ i ; System.out.print(totl); d. Write progrm tht reds in n integer n from the keyord, nd displys whether n is "perfect numer" or not.a numer is "perfect" if it is equl to the sum of ll of its fctors (not including itself s fctor, ut including 1 s fctor). 6 is the first perfect numer, ecuse its fctors re 1, 2, nd 3, nd 1+2+3 = 6. int n = keyord.nextint(); int totl = 1; for( int fctor = 2 ; fctor < n ; fctor++ ) if (n%fctor ==0) totl += fctor; System.out.println(n == totl); 5 P ge
3. Writing short progrms (39 points, 40 minutes) For ech prolem elow, write some Jv code to solve it. You do not need to define clss or the min method, or import ny clsses. For this question, you should ssume tht ll necessry clsses (e.g., Scnner, Arrys) hve een imported. Furthermore, ssume tht the following line precedes every question elow, for you to use if you wnt: Scnner keyord = new Scnner(System.in);. (7 points) Write code tht reds in word W nd numer N from the user (i.e., vi keyord). Your code should disply W on N lines. For exmple, if the user enters "hello" for W nd 3 for N, your code should disply: String W = keyord.nextline(); int N = keyord.nextint(); for(int i = 0; i< N; i++) System.out.println(W);. (7 points) Write code tht reds in n integer N vi the keyord, nd displys ll the products of 7, from 1 x 7 up to N x 7. For instnce, if the user enters 4, the progrm should disply: int N = keyord.nextint(); for( int i = 1; i<=n; i++) System.out.println(i+ x7= +i*7); c. (11 points) Write code tht reds in string, nd displys the result of true or flse whether this word (in string) is dollr word. A dollr word is one whose totl vlue is exctly 100 when ech of its chrcter is ssigned vlue ccording to its position in the lphet: =1, =2,, z=26. Some exmples of dollr word re: contented, cookout, mittens, nd shdowing. String = keyord.nextline(); int totl=0; =.tolowercse(); for (int i = 0; i<.length(); i++) totl +=.chrat(i)- +1; System.out.println(totl==100); c. (14 points) Write code tht reds in numer C nd numer R from the user (i.e., keyord), nd displys figure with R rows nd 2xC columns of "$" nd chrcters. For instnce, if the user enters 5 for C nd 4 for R, your progrm should disply the following shpe of "$"s: int C = keyord.nextint(); int R = keyord.nextint(); for( int i = 0; i < R; i++){ int j; for(j = 0; j < C-i; j++) 6 P ge
System.out.print( $ ); for(j = 0; j < 2*i; j++) System.out.print( ); for(j = 0; j < C-i; j++) System.out.print( $ ); System.out.println(); 4. Chllenge: Writing full Jv progrm (12 points, 20 minutes) Write complete Jv progrm clled SecondMAX.jv tht reds in numer of ints from the keyord, nd store them in n rry. After tht, find the second minimum vlue in this rry, nd disply it on the screen. The numer of ins, i.e., the length of the rry, must e entered y the user, efore the rry element input strts. import jv.util.scnner; pulic clss SecondMx{ pulic sttic void min(string [] rgs){ Scnner k = new Scnner (System.in); int size = k.nextint(); int [] rr = new int[size]; int iggest, iggest2nd; for (int i = 0; i<size; i++) rr [i] = k.nextint(); if (rr[0]<rr[1]){ iggest = rr[1]; iggest2nd = rr[0]; else{ iggest = rr[0]; iggest2nd = rr[1]; for (int i =2; i<size; i++){ if(rr[i]>iggest){ iggest2nd = iggest; iggest = rr[i]; else if (rr[i]>iggest2nd) { iggest2nd = rr[i]; System.out.println(iggest2nd); 7 P ge