Programing with C Final

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Consider the following if statement: if (a <= b || a >= b) { printf("Yes\n"); } else { printf("No\n"); } Assuming that a and b have been previously declared to be int: A. This program fragment will always print Yes B. This program will fragment always print No. C. I cannot tell what this program fragment will do unless you tell me what is in a and b D. This program fragment will not compile.

A. This program fragment will always print Yes

True or False: Anything that I can compute using a while loop, I can also compute using a for loop? A. True B. False

A. True

True or False: In using a multiway if decision structure, the order in which the conditions are listed may matter. A. True B. False

A. True

Let a and b be two positive ints with a > b What is the value of b % a? A. 0 B. 1 C. b D. No way to know

C. b

Consider the following main program and subprogram: #include <stdlib.h> #include <stdio.h> double square(double x) { double retval = x * x; x = 3.1415927; return retval; } void main(void) { double z = 12.0; printf("before squaring, z = %lf ", z); double q = square(z); printf("after squaring, z = %lf and q = %lf\n", z, q); } What would this program print? A. before squaring z = 12.0 after squaring z = 144.0 and q = 144.0 B. before squaring z = 12.0 after squaring z = 144.0 and q = 12.0 C. before squaring z = 12.0 after squaring z = 12.0 and q = 144.0 D. before squaring z = 12.0 after squaring z = 3.1415927 and q = 144.0 E. The program would crash

C. before squaring z = 12.0 after squaring z = 12.0 and q = 144.0

I want to write a program to find the prduct of the first n positive integers. 1 * 2 * 3 * 4 * ... * n What will be the result of the following program: #include <stdlib.h> #include <stdio.h> int n, j, factorial; void main(void) { printf("Enter n:"); scanf("%d", &n); factorial = 0; for(j = 1; j < n; j++) { factorial = factorial * j; } printf("The answer is %d\n", factorial); } If I run this program and enter 3 when prompted for n: A. correctly print 6 B. incorrectly print 0 C. incorrectly print 2 D. none of the above

C. incorrectly print 2

Given the declaration: char x = 'A'; What is the difference between: printf("%c", x); printf(%d", x); A. they will print the same thing B. the second printf is illegal because d means decimal, but x is a letter C. the first will print a and the second will print 65 (ASCII code for a) D. none of the above

C. the first will print a and the second will print 65 (ASCII code for a)

If the print statement in question 20 had read printf("%d", j), how would the results differ? A. It would print the ASCII codes for the letters of the reversed alphabet' B. It would print the ASCII codes for the letters of the alphabet. C. It would still generate a compile error D. It would still print garbage because subtracting one from a letter still makes no sense.

A. It would print the ASCII codes for the letters of the reversed alphabet

Given the following program fragment: int *a = { 1, 2, 3, 4, 5}; int *b = a; a [0] = 4; printf("%d", b[0]); A. this will print 1 B. this will print 2 C. this will print 3 D. this will print 4 E. none of the above

D. this will print 4

Let a and b be two positive ints with a > b What is the value of b / a? A. 0 B. 1 C. b D. No way to know

A. 0

What will be the value of x following the statement below: int x = '6' - '3'; A. 3 B. garbage because you cannot subtract letters of the alphabet C. error message D. you haven't given enough information to answer this questions

A. 3

What would be the value of x following the statement below: int x = '6' - '0'; A. 6 B. garbage, because you cannot subtract letters of the alphabet C. compiler error D. You haven't given enough information to answer this question

A. 6

Consider the following two loops: Loop 1 int j = 0; while(j < 5) { printf("Hello\n"); j++; } Loop 2 int j; for (j = 0; j < 5; j++) { printf("Hello\n"); } A. Loop1 and loop2 will always behave identically B. Loop1 and loop2 will sometimes behave identically and sometimes not C. Loop 1 contains a syntax error D. Loop 2 contains a syntax error

A. Loop1 and loop2 will always behave identically

Consider the following main program and subprogram: #include <stdlib.h> #include <stdio.h> double mystery(double x) { double retail = x + x; x = 3.1415927; return retval; } int main(void) { int i; double a[ ] = {1.0, 2.0, 3.0, 4.0, 5.0} double b[5] for(i = 0; i < 5; i++) { b[i] = mystery(a[i]); } } After the loop completes: A. a contains 1.0, 2.0, 3.0, 4.0, 5.0 and b contains 2.0, 4.0, 6.0, 8.0, 10.0 B. a and b both contain 2.0, 4.0, 6.0, 8.0, 10.0 C. a and b both contain 1.0, 2.0, 3.0, 4.0, 5.0 D. none of the above

A. a contains 1.0, 2.0, 3.0, 4.0, 5.0 and b contains 2.0, 4.0, 6.0, 8.0, 10.0

The following code fragment will: char a[ ] = { 'a', 'b', 'c', 'd', 'e' }; char temp; int lcv; for(lcv = 0; lcv < 5; lcv++) { temp = a[lcv]; a[lcv] = a[4-lcv]; a[4-lcv] = temp; } A. result in no changes to the array a[ ] B. reverse the elements of the array a[ ] C. accidentally change the contents of a memory cell that is not part of the array D. none of the above

A. result in no changes to the array

Consider the following program fragment: int a = 10; int b = 5; if (b % a == 0); { printf("%d is divisible by %d\n", b, a); } printf("Have a nice day!\n") A. this will print 5 is divisible by 10 Have a nice day! B. this will only print Have a nice day! C. This will crash because of trying to divide by zero D. None of the above

A. this will print 5 is divisible by 10 Have a nice day!

If p contains 1, meaning "true" and q contains 1 meaning "true", will the expression p || q mean "true" or "false?" A. true B. false C. neither

A. true

True or false: Anything you that I can compute using a while loop, I can also compute using a for loop A. true B. false

A. true

True or false: Even though I declare an array int arr[10], I am able to use fewer than 10 elements of the array A. true B. false

A. true

True or false: Any thing I can write using a for loop I can also write using a while loop. a) true b) false

A. true

What is the value stored in x? double x = 5.0 + 3.0 * 4.0; A. 1.0 B. 17.0 C. 32.0 D. Error message

B. 17.0

3. How many times will this loop repeat? #include <stdlib.h> #include <stdio.h> void main(void) { int i = 0; while(i <= 255) { printf("%d", i); i++; } } A. It will never stop B. 256 times C. 1 time D. 0 times

B. 256 times

Consider the following program fragment: int a = 10; int b = 0; if (b > 0 && a / b < 12) { printf("OK\n"); } else { printf("Not OK\n"); } A. This will print OK B. This will print Not OK C. This will crash because of trying to divide by 0 D. This will generate a compiler error

B. This will print Not OK

You have a valuable named t which indicates a struct triangles (from question 16 above). How do you decide whether to write t -> side1 instead of t. side1? A. if t is a pointer, use a . B. if t is a pointer, use -> C. it doesn't matter, they mean the same thing D. neither is correct, use <-

B. if t is a pointer, use ->

I want to write a program to find the prduct of the first n positive integers. 1 * 2 * 3 * 4 * ... * n What will be the result of the following program: #include <stdlib.h> #include <stdio.h> int n, j, factorial; void main(void) { printf("Enter n:"); scanf("%d", &n); factorial = 0; for(j = 1; j <= n; j++) { factorial = factorial * j; } printf("The answer is %d\n", factorial); } If I run this program, and enter 3 when prompted for n, this program will: A. correctly print 6 B. incorrectly print 0 C. incorrectly print 2 D. this program will not compile

B. incorrectly print 0

Consider the following code fragment: int j, k; int count = 0; for (j = 0; j < 5; j++) { for (k = 0; k < 6; k++) { count++; } } What will be printed? A. j = 0, k = 0, count = 11 B. j = 5, k = 6, count = 30 C. j = 4, k = 5, count = 30 D. j = 0, k = 0, count = 11 E. none of the above

B. j = 5, k = 6, count = 30

Consider the following loop. int j; for(j = 'a'; j <= 'm'; j++) { printf("%c", j + 1); } A. this will print abcdefghijklm B. this will print bcdefghijklmn C. this will generate a runtime error D. this will print garbage because adding 1 to a number is not defined

B. this will print bcdefghijklmn

Consider the following loop. int j; for(j = 'z'; j >= 'a'; j= j - 1) { printf("%c", j); } A. This will print abcdeftjijklmnopqrstuvwxyz B. This will print zyxwvutsrqponmlkjihgfedcba C. This will generate a compile error D. This will print garbage because subtracting one from a letter is not defined.

B. this will print zyxwvutsrqponmlkjihgfedcba

Given the following loop, what will be printed? int j; int sum = 0; for (j = 0; j < 10; j++) { sum = sum + j; } printf("Now j is %d\n", j); A. Nothing because that is an infinite loop B. 11 C. 10 D. 9

C. 10

4. Which variable name is illegal? A. aardvark B. je77yro77 C. 2B D. not2B E. all are legal

C. 2B

In the following code fragment, there is a ; that does not belong. What will its presence cause? int j; int sum = 0; for(j = 0; j < 3; j++); { sum += j; } printf("%d\n", sum); A. It will generate a syntax error while compiling B. It will print nothing because the loop will never end C. It will print 3 D. It will print 0

C. It will print 3

How many times will this loop repeat? #include <stdlib.h> #include <stdio.h> void main (void) { int i=0; for(i = 0; i < 10; i--) printf("%d", i); } A. 10 times B. 1 time C. It will run forever D. 0 times

C. It will run forever

Consider the following code fragment: int sum = 0; int where; for (where = 5; where; where--) { sum += where; } printf("%d", sum); A. this program fragment will not work because the second thing in the for statement (where) is not a condition B. this program will fragment print something, but it is impossible to know what C. the program fragment will print 15 D. none of the above

C. the program fragment will print 15

Given the following declarations: int x; int y [10]; Consider the two statements: scanf("%d, &x"); scanf("%d, y"); A. the first scanf is legal because the x is followed by and &, but the second is illegal because the y has no & B. they are both legal because the name of an array is itself an address, all 10 values of the array will be read in C. y is the address of the first element of the array, so y [0] will be read in D. None of the above

C. y is the address of the first element of the array, so y[0] will be read in

2. What will be the output of this program? #include <stdlib.h> #include <stdio.h> int main() { int a = 100, b = 200, c = 300; if(!a >= 500) b = 300; c = 400; printf("%d,%d,%d",a, b, c); return 0; } A. 100 200 300 B. 100 200 400 C. 100 300 300 D. 100 300 400

D. 100 300 400

You can omit the curly braces in a loop if A. It is a for loop B. You obey the rules of indenting C. They can never be omitted D. If the loop body consists of exactly one statement

D. If the loop body consists of exactly one state

Even though Dr. LaFollette says it is not a good idea, you can omit the curly braces in a loop if A. It is a for loop B. You obey the rules of indenting C. This is a trick question. They can never be omitted. D. If the loop body consists of exactly one statement.

D. If the loop body contains of exactly one statement

What is the value assigned to xyzzy? int xyzzy = 2 % 3 * 2 - 4 + 5; A. 1 B. 2 C. 3 D. None of the above

D. None of the above

What can be said about the following two expressions? First expression: a < b && b < c Second expression: a < b < c A. They both mean the same thing B. They both mean different things C. Sometimes they are the same, and sometimes different D. One of the expressions is illegal in the C language

D. One of the expressions is illegal in the C language

What will the following program fragment print? int sum = 0; int j; for(j = 0; j < 100; j++) { if(j % 2 == 0) { sum = sum + j; } } printf("%d\n", sum); A. The sum of the numbers from 1 to 100 B. The sum of the numbers from 0 to 99 C. The sum of the odd positive numbers less than 100 D. The sum of the even positive numbers less than 100

D. The sum of the even positive numbers less than 100

Which function will find the sum of all of elements of an array that have even subscripts. a[0] + a[2] and so on. int sumEvenI(int a[], int size) { int retval = 0; int lcv; for(lcv = 0; lcv < size; lcv += 2) { retval += a[lcv]; } return retval; } int sumEvenII(int a[], int size) { int retval = 0; int lcv; for(lcv = 0; lcv < size; lcv++) { if(a[lcv] % 2 == 0) retval += a[lcv]; } return retval; } int sumEvenIII(int a[], int size) { int retval = 0; int lcv = 0; while (lcv < size) { if(lcv % == 0) retval += a[lcv]; lcv++; } return retval; } Which function(s) will solve the stated problem: a) sumEvenI only b) sumEvenII only c) sumEvenIII only d) SumEvenI and sumEvenIII e) none

D. sumEvenI and sumEvenIII

Consider the following typedef: typedef struct triangle { double side1, side2, side3; } Triangle; What is the difference between the two arrays declared here: Triangle tri[10]; Triangle *ptri[10]; A. tri is an array of triangles, ptri is the address of an array of triangles B. tri is an array of triangles, ptri is an illegal declaration C. tri and ptri are both arrays of pointers to triangles D. tri is an array of triangles, ptri is an array of the addresses of triangles E. none of the above

D. tri is an array of triangles, ptri is an array of the addresses of triangles


Ensembles d'études connexes

Chapter 11: Reproductive Behaviors

View Set

AFJROTC // Chapter 8 Lesson 3 Seeking Feedback and Promotions

View Set

Accounting Test: Unit 13.12 (multiple choice)

View Set

Electricity (3rd Unit) Lesson: Physics B

View Set

WORLD HISTORY SVHS unit 2 quizzes

View Set