Programming in C, Test 1

Ace your homework & exams now with Quizwiz!

Two ways to invoke functions

- call by value - call by reference

Four scopes for an identifier

-Function scope -File scope -Block scope -Function-prototype scope

What could not be produced by the program below? (The numbers after the prompt "Which would you like?" are entered from the keyboard.) #include <stdio.h> void main(void) { int choice = 0; do { printf(" 1. coffee\n"); printf(" 2. tea\n"); printf(" 3. Mountain Dew\n"); printf("Which would you like? "); scanf("%d", &choice); } while( choice != 1 && choice != 2 && choice != 3 ); switch(choice) { case 1: case 2: printf("Cream or sugar?\n"); break; case 3: printf("Would you like it in a glass, or is a can okay?\n"); break; default: printf("I'm not familiar with that beverage.\n"); } }

1. coffee 2. tea 3. Mountain Dew Which would you like? 4 I'm not familiar with that beverage.

What number does the following code output? (Notice that in this case, there's no need for function prototypes because each function is defined before it is called.) #include <stdio.h> void f3(int num){ int n; num = 1000; n = 1000; } int f2(int num){ int n = 20; f3(num); return n; } int f1(int n){ int num = 5; num = f2(n); return n; } void main(void){ int num = 10; num = f1(num); printf("%d", num); }

10

Which integer could not be assigned to variable num by the following C instruction? num = 7 + rand() % 5; 7 8 10 11 12

12

What output is produced by the code below? #include <stdio.h> void main(void) { printf("1"); printf("%d",2); printf("%d,%d", 3, 4); }

123, 4

What output is produced by the code below? (Express your answer as a number only.) #include <stdio.h> int myFunc2(int x){ x += 10; return x; } int myFunc1(int x){ x += 10; myFunc2(x); return x; } void main(void){ int x; x = 3; x = myFunc1(x); printf("%d", x); }

13

What output is produced by the code below if the inputs from the keyboard are 50 and 3? #include <stdio.h> int mysteryFunc(int, int); void main(void) { int int1, int2, result; printf("Enter two positive integers: "); scanf("%d%d", &int1, &int2); result = mysteryFunc(int1, int2); printf("%d", result); } int mysteryFunc(int n1, int n2) { if(n2 < 1) return 0; else if(n2 == 1) return n1; else return n1 + mysteryFunc(n1, n2 - 1); }

150

What output will be produced by the code below? (Express your answer numerically.) #include <stdio.h> void main(void) { int toAdd = 3; int sum = 0; int i = 0; while(i < 10) { sum += toAdd; ++i; } printf("%d", sum); }

30

RAND_MAX

32767

What output is produced by the following code? (Watch for trickery!) #include <stdio.h> int main(void) { int i; for(i = 4; i < 11; i += 2) if(i != 6) printf("%d", i); printf("---"); return 0; }

4810---

How many times will 'x' be printed by the code below? (Please express your answer as a number only.) #include <stdio.h> void main(void) { int i; for(i = 100; i > 50; i -= 10) printf("x"); }

5

The following program will print 10 integers. What is the last integer that it will print? #include <stdio.h> void myAdder(int); void main(void) { int amountToAdd = 5; int count; for(count = 0; count < 10; ++count) myAdder(amountToAdd); } void myAdder(int toAdd) { int mySum = 0; mySum += toAdd; printf("%d\n", mySum); }

5

What output is produced by the code below? #include <stdio.h> void main(void) { int choice; choice = 5; printf("%d / 2 = %d", choice, choice / 2); }

5/2=2

What output will be produced by the code below? (Express your answer numerically.) #include <stdio.h> void main(void) { int i = 0; int count = 0; while(i < 100) { if(i % 2 == 1) count++; i += 1; } printf("%d", count); }

50

If the value 9 is passed into the function defined below, what value does the function return? (Please give your answer as a number only.) int myFunc(int n) { int i, j = 1; for(i = 0; i < n; i++) j *= 2; return j; }

512

What is the largest value printed by the code below? (Please answer with a number only.) int i; for(i = 1; i < 1000; i *= 2) { printf("%d\n", i); }

512

How many times will 'x' be printed by the following code? (Please answer with a number only.) #include <stdio.h> void main(void) { int i; for(i = 1; i <= 10; ++i) { if(i == 4) continue; if(i == 8) break; printf("x"); } }

6

Parameter list

A comma-separated list that specifies the parameters received by the function when it is called

Recursive function

A function that calls itself either directly or indirectly through another function

Local variables

All variables defined in function definitions

Functions

Allow the programmer to modularize a program

Math library function

Allows the programmer to perform certain common mathematical calculations

The conditional operator (?:)

C's only ternary operator

By default global variables and function names are of storage class:

Extern (as opposed to static)

The code below is a complete C program. #include <stdio.h> void myProgram(void) { int i; for(i = 0; i < 10; i++) { printf("i is now %d,", i); printf("an %s number.\n", i % 2 ? "odd" : "even"); } }

False

Scope

Is the portion of the program in which the identifier can be referenced

The printf() conversion specifier %10.4f produces which effect?

It prints a floating point number with 5 digits/spaces to the left of the decimal point and 4 digits to the right of the decimal point.

Static

Local variables with this keyword are still known only in the function in which they are defined, but unlike automatic variables, these local variables retain their value when the function is exited.

Parameters

Provide the means for communicating information between functions

Function prototype

Tells the compiler the type of data returned by the function, the number of parameters the function expects to receive, the types of parameters, and the order in which these parameters are expected

Which statement about the code is accurate? #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> void main(void) { double num; printf("Enter a number, and I'll tell you the square root: "); scanf("%lf", &num); printf("The square root of %lf is %lf.\n", num, sqrt(num)); }

The code will not run because it does not have #include <math.h>

Postincrementing(i++)/Postdecrementing(i--)

The current variable is used in the expression and then incremented by 1

Preincrement (++i)/predecrement opperators (--i)

The variable is incremented by 1, then the new value of the variable is used in the expression in which it appears

The following output: 31415 3.14 A This is a string. Will be produced by the code below: #include <stdio.h> int main(void) { int myInt = 31415; float myFloat = 3.1415; char myChar = 'A'; printf("%d\n%.2f\n%c\n%s\n", myInt, myFloat, myChar, "This is a string."); }

True

Call by value

Used whenever the called function does not need to modify the value of the caller's original variable

Call by reference

Used with trusted call functions that need to modify the original variable

#include <stdio.h> int main(void) { int num = 5; int sum1 = 0; int sum2 = 0; sum1 = num++; printf("sum1 = %d, num = %d.\n", sum1, num); sum2 = ++num; printf("sum2 = %d, num = %d.\n", sum2, num); return 0; }

Value of sum1: 5 Value of num output by 1st printf: 6 Value of sum2: 7 Value of num output by 2nd printf: 7

What output is produced by the following code? #include <stdio.h> void main(void) { int a = 10, b = 20, c; c = b /= a; printf("a = %d, b = %d, c = %d\n", a, b, c); }

a = 10, b = 2, c = 2

(if c = 3) c+=7

c = 10

for ( counter = 1; counter <= 10; ++counter)

counter = 1; while (counter <=10) ++counter

(if d = 5) d-=4

d = 1

Show the basic form of a do ... while loop

do { i++ } while(i <=a);

(if e = 4) e *= 5

e = 20

(if f = 6) f /= 3

f = 2

What function prototype could be used for the function defined below? (The function header has been removed so that this question is not ridiculously easy.) --> This is where the function header would appear. <-- { int localInt1 = 4; int localInt2; float localFloat; localInt2 = m + n + localInt1; // addition of 3 ints localFloat = localInt2 * f; // multiplication of 2 floats return localFloat; }

float myFunction(int, int, float);

(if g = 12) g %= 9

g = 3

Which of the following is not one of the three control structures of structured programming? -sequence -repetition -goto -selection

goto

What output is produced by the code below? #include <stdio.h> void main(void) { int myNum = 7; if(myNum > 5) printf("greater than 5\n"); else if(myNum < 10) printf("less than 10\n"); if(myNum > 6) printf("greater than 6\n"); if(myNum < 9) printf("less than 9"); }

greater than 5 greater than 6 less than 9

The switch case statement can replace what other statement

if/if else statement

if statement

single selection, single entry/single exit structure

Show basic form of a switch case

switch(rand) case'a': printf("%d",a); a++; break; default: "That's not valid"; break; (this break is optional)

% (mod operator)

takes the remainder of a divided number i.e. 5 % 4 = 1


Related study sets

NSG 330 Ch 49- Assessment and Management of Hepatic Disorders

View Set

Psychological Disorders (Chapter 11 book & online)

View Set

CELLS CH 3: ATP and Mitochondria

View Set