{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# AG_Test_2_Answers-1 - CIS 15AG 1 Test 2 Questions Chapters...

This preview shows pages 1–5. Sign up to view the full content.

CIS 15AG Test 2 Questions: Chapters 1-8, emphasizing 6, 7, and 8. "Computers are good at following instructions, but not at reading your mind." - Donald Knuth _____________________________________________________________________________________ 1. Loops (A) Predict the output: (B) Predict the output. The initial value of x is 5638 1 for( i = 1; i < 4; i++ ) { printf( "Iteration number %d ", i ); printf( "Hello again!\n" ) } printf( "All done!\n" ) Answer: Iteration number 1 Hello again! Iteration number 2 Hello again! Iteration number 3 Hello again! All done! int guess (int x) { int rx; int d; rx = 0; while( x > 0 ) { d = x % 10; rx = rx*10 + d; x /= 10; } return rx; } Answer: Iteration# d rx initial value: 0 x initial value: 5638 x > 0 ? True 1 8 8 563 True 2 3 83 56 True 3 6 836 5 True 4 5 8365 0 False Returned value: 8365 (this function reverses its input parameter).

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

View Full Document
CIS 15AG Test 2 Questions: Chapters 1-8, emphasizing 6, 7, and 8. (C) Rewrite the following function using the correct indentation. int fun( int num ){ int d;int p;int s;p=1;d=2; s=(int)sqrt(num);while(d<=s && p) {if(num%d==0) p=0;d++;} return p;} Answer: int fun( int num ) { int d; int p; int s; p = 1; d = 2; s = (int)sqrt(num); { if( num % d == 0 ) p = 0; d++; // note that this statement is not part of if } return p; } 2
CIS 15AG Test 2 Questions: Chapters 1-8, emphasizing 6, 7, and 8. (D). Nested loops. Predict the output 3 for( i = 5; i <= 7; i++ ) for( j = 1; j <= 3; j++ ) printf( "%2d%2d\n", i,j ); Answer: set i to 5 check if i is less than or equal to 7, and if it is, execute the inner loop (controlled by j ) completely, then add 1 to i ; now i is 6 check if i is less than or equal to 7, it is, then execute the inner loop (controlled by j ) completely, then add 1 to i ; now i is 7 check if i is less than or equal to 7… and so on! i i <= 7? j j <= 3? Output 5 5 <= 7 True 1 1 <= 3 True 5 1 2 2 <= 3 True 5 2 3 3 <= 3 True 5 3 4 4 <= 3 False 6 6 <= 7 True 1 1 <= 3 True 6 1 2 2 <= 3 True 6 2 3 3 <= 3 True 6 3 4 4 <= 3 False 7 7 <= 7 True 1 1 <= 3 True 7 1 2 2 <= 3 True 7 2 3 3 <= 3 True 7 3 4 4 <= 3 False 8 8 <= 7 False

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

View Full Document
CIS 15AG Test 2 Questions: Chapters 1-8, emphasizing 6, 7, and 8. _____________________________________________________________________________________ 2 . (A). Determine which one of the following assignments is not valid, given the declarations: char listA[50]; int listB[50]; int listC[50]; char ch = 'X'; int num = 25; a. listC[0] = num; // OK b. listB[ 50 ] = 99; // index 50: out of range c. listA[0] = ch ; // OK d. listA[2*num – 1] = ch + 1; // OK: list[49] = 'Y’ e. listA = listB; // listA: not a left value // the address of its first element cannot be changed (B). What is displayed after execution of the following program fragment? Assume the input data is 85 AB 4 2 6 9519CD ^^^^^^^^^^^^^^^^^^^^^^ // to help you keep track of the number of spaces int list[4]; // 85, 4, 6, 19 char word[4]; // 'B', ' ', '5', 'D'
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 14

AG_Test_2_Answers-1 - CIS 15AG 1 Test 2 Questions Chapters...

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

View Full Document
Ask a homework question - tutors are online