Q4 1-40 Flashcards

1
Q

What is wrong with the following code? #include /This is a directive line/ main /* This function will be automatically called/ (void/ I do not have arguments to pass/) { / This is the first line in the program /* I am so excited to write this program / printf(“Hello world\n”)} / Finally I am done with it /* But I am not sure why it does not work :-( */ }

A

Extra curly bracket. Wrong quotations marks, should be “ not “

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which of the following are legal C identifier? 100_bottles, _100bootles, one-hundred-bottles, one_hundred_bottles, one_hundred_bottles, one__hundred__bottles, one__hundred__bottles__, If, main, Printf, and while.

A

Identifier may contain digits, letters and underscores but must only begin with letter or underscore. Keywords cannot be used as identifiers: 100bootles,one_hundred_bottles,one_hundred_bottles,one_hundred_bottles,one_hundred_bottles,main, Printf, are legal apparently :P

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How many token are there in the following C statement? x=answer=(a*b/++c+6-a%f)/(k);

A

24 tokensx: Identifier, =: operator, answer: Identifier, =: operator, (: punctuation, a: identifier, *: operator, b: identifier, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the output of the following C statement? printf(“%6d, %4d\n” , 86, 1040);

A

%d is a placeholder for integers.%6d indicates 6 figures for the integer.%4d indicates 4 figures for the integer.86, 1040

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the output of the following C statement? printf(“%12.5e\n”, 30.253) ;

A

%e placeholder for exponent.%12.5 indicates 12 figures and 5 decimal places.3.02530e+01

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the output of the following C statement? printf(“%.4f\n”, 83.162) ;

A

%f is a placeholder for float.%.4f indicates 4 decimal places83.1620

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the output of the following C statement? printf(“%-6.2g\n”, .0000009979);

A

1e-06 %g converts it into exponential or fixed decimal. if it is more than 4 figures, it will show as exponent. if it is less than 4 decimal places of zeros, it will also show as exponent. in the example, it has gone back 7 decimal places.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write calls to printf that display a float variable x in the following formats: (a) Exponential notation; left-justified in a field of size 8; one digit after the decimal point. (b) Exponential notation; right-justified in a field of size 10; six digits after the decimal point. (c) Fixed decimal notation; left-justified in a field of size 8; three digits after the decimal point. (d) Fixed decimal notation; right-justified in a field of size 6; no digits after the decimal point.

A

(a) %-8.1e (b) %10.6e (c) %-8.3f (d) %6.f(inclusion of minus sign indicates left-justified)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the output of the following C statement? printf(“%d\n%-d\n%d\n%-d\n”,123456,123456,-123456,-123456);

A

123456 123456 -123456 -123456

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the output of the following C statement? printf(“%.10d\n%-.10d\n%.10d\n%-.10d\n”, 123456,123456,-123456,-123456);

A

0000123456 0000123456 -0000123456 -0000123456

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the output of the following C statement? printf(“%20d\n%-20d\n%20d\n%-20d\n”, 123456,123456,-123456,-123456);

A
  • 123456* 123456 * * -123456 *-123456 *
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the output of the following C statement? printf(“%20.10d\n%-20.10d\n%20.10d\n%-20.10d\n”, 123456,123456,-123456,-123456);

A
  • 0000123456* 0000123456 * * -0000123456 *-0000123456 *
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the output of the following C program? #include main(void) { float m = 6, n = 6, o = 6, p = 6, q = 6; int i = 3, j = 3; m ++; ++ m; m += n += o -= p *= q /= i %= ++j ; printf(“%f\n%f\n %f\n %f\n %f\n %d\n%d”, m, n, o, p, q, i, j); m += 3 + (n += 3 + (o -= 3 + (p *= 3 + (q /= 3 + (i %= j++))))); printf(“%f %f\n%f %f\n%f %d %d\n”, ++m, n++, –o, p–, ++q, i–, –j); }

A

Should include otherwise won’t compile. once included. It should produce: 8.000000 0.000000 -6.000000 12.000000 2.000000 3 4-34.000000 -46.000000 -50.000000 40.000000 1.333333 3 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Suppose that we call scanf as follow: scanf (“%d%f%d” , &i, &x, &j); If the user entered 10.3 5 6 what will be the values of i, x, and j after the call? (Assume that i and j are int variables and x is a float variable.)

A

i will be 10, x will be 5.000000 and j will be 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Suppose that we call scanf as follow: scanf(“%f%d%f”, &x, &i, &y); If the user entered 12.3 45.6 789 what will be the values of x, i, and y after the call? (Assume that x and y are float variables and i is an int variable.)

A

x will be 12.300000, i will be 45, 789 will be 789.000000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

For each of the following pairs of scanf format strings, indicate whether or not the two strings are equivalent. If they’re not, show how they can be distinguished. (a) “%d” versus “ %d” (b) “%d-%d-%d” versus “%d -%d -%d” (c) “%f” versus “%f “ (d) “%f,%f” versus “%f, %f”

A

(a) equivalent (b) equivalent (c) Different, space after %f seems to prompt for another entry, and but only execute the first entry (d) Equivalent

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Write a program that formats products information entered by the user. A program session should be as follow: Enter item number: 583 Enter unit price: 13.5 Enter purchase date (mm/dd/yyyy): 09/24/2008 Item Unit Purchase Price Date 583 $ 13.50 09/24/2008 The item number and date should be left justified; the unit price should be right justified. Allow dollar amounts up to $9999.99. Hint: Use tabs (i.e., “\t”) to line up the columns.

A

include main(void) { int item, mm, dd, yyyy; float price; printf(“Enter item number: “); scanf(“%d”, &item); printf(“Enter unit price: “); scanf(“%f”, &price); printf(“Enter purchase date (mm/dd/yyyy): “); scanf(“%d/%d/%d”, &mm, &dd, &yyyy); printf(“Item\tUnit\tPurchase\n\tPrice\tdate\n%-d\t%4.2f\t%-.2d/%-.2d/%-.2d\n”, item, price, mm, dd, yyyy); }

18
Q

Write a program that prompts the user to enter a ten digit telephone number in the form xxxxxxxxxx and then displays the number in the form (xxx) xxx-xxxx.

A

include main(void) { int phone, phone1, phone2, phone3; printf(“Enter telephone number: “); scanf(“%d”, &phone); phone1 = phone0.0000001; phone2 = (phone - (phone110000000))0.0001; phone3 = phone - (phone110000000) - (phone210000); while(phone1 > 999 || phone1 < 100 || phone2 > 999 || phone2 < 100 || phone3 > 9999 || phone3 < 100) { printf(“Invalid number, enter number again: “); scanf(“%d”, &phone); phone1 = phone0.0000001; phone2 = (phone - (phone110000000))0.0001; phone3 = phone - (phone110000000) - (phone210000); } printf(“(%d) %d-%d\n”, phone1, phone2, phone3); }

19
Q

Write a program that asks the user to enter the numbers from 1 to 16 (in any order) and then displays the numbers in the same order in a 4 by 4 row-wise arrangement, followed by the sums of each rows, each columns, and each diagonals.

A

?

20
Q

Show the output produced by the following program fragment. int i, j; i = 7; j = 8; i *= j + 1; printf(“%d %d\n”, i, j);

A

63 8

21
Q

Show the output produced by the following program fragment. int i; int j, k; i = j = k = 1; i += j += k; printf(“%d %d %d\n”, i, j, k);

A

3 2 1

22
Q

Show the output produced by the following program fragment. int i; int j; int k; i = 1; j = 2; k = 3; i -= j -= k; printf(“%d %d %d\n”, i, j, k);

A

2 -1 3

23
Q

Show the output produced by the following program fragment. int i, j, k; i = 2; j = 1; k = 0; i *= j *= k; printf(“%d %d %d\n”, i, j, k);

A

0 0 0

24
Q

Show the output produced by the following program fragment. int i, j; i = 6 ; j = i += i; printf(“%d %d”, i, j);

A

12 12

25
Q

Show the output produced by the following program fragment. int i, j; i = 5 ; j = (i -= 2) + 1; printf(“%d %d”, i, j);

A

3 4

26
Q

Show the output produced by the following program fragment. int i, j; i = 7 ; j = 6 + (i = 2.5) ; printf(“%d %d”, i, j);

A

2 8

27
Q

Show the output produced by the following program fragment. int i, j; i = 2; j = 8; j = (i = 6) + (j = 3); printf(“%d %d”, i, j);

A

6 9

28
Q

Show the output produced by the following program fragment. int i; i = 1 ; printf(“%d “ i++ - 1); print f(“%d”, i);

A

0 2

29
Q

Show the output produced by the following program fragment. int i, j; i = 10; j = 5; printf(“%d “ i++ - ++j); printf(“%d %d”, i, j),

A

4 11 6

30
Q

Show the output produced by the following program fragment. int i, j; i = 7 ; j = 8 ; printf(“%d “ i++ - –j); printf(“%d %d”, i, j);

A

0 8 7

31
Q

Show the output produced by the following program fragment. int i, j, k; i = 3; j = 4; k = 5; printf(“%d “, i++ - j++ + –k); printf(“%d %d %d”, i, j, k);

A

3 4 5 4

32
Q

. Show the output produced by the following program fragment. int i, j; i = 5; j = ++i * 3 - 2; printf(“%d %d”, i, j);

A

6 16

33
Q

Show the output produced by the following program fragment. int i, j; i = 5 ; j = 3 - 2 * i++; printf(“%d %d”, i, j);

A

6 -7

34
Q

Show the output produced by the following program fragment. int i, j; i = 7 ; j = 3 * i– + 2; print f(“%d %d”, i, j);

A

6 23

35
Q

Show the output produced by the following program fragment. int i, j; i = 7 ; j = 3 + –i * 2; printf(“%d %d”, i, j);

A

6 15

36
Q

Write a program that computes the volume of a sphere (4/3 π r3), where r is the radius of the sphere. Your program should ask the user to enter the radius of the sphere.

A

include#includeint main(){float r;float volume;printf(“Enter radius of the sphere : “);scanf(“%f”,&r);volume = (4.0/3) * M_PI * r * r * r;printf(“\nVolume of sphere is : %.3f”,volume);return 0;}

37
Q

Write a program that asks the user to enter a value for x and then displays the value of the following polynomial: 3x^5 + 2x^4 – 5x^3 + x^2 – x – 6

A

include#includeint main(){float x;float polynomial;printf(“Enter value for x: “);scanf(“%f”,&x);polynomial = (xxxxx3)+(xxxx2)+(xxx5)+(x*x)-x-6;printf(“\npolynomial value is: %.3f”,polynomial);return 0;}

38
Q

Write a program that asks the user to enter a dollar amount and then shows how to pay that amount using the smallest number of $20, $10, $5, $2, and $1.

A

?

39
Q

Write a program that asks the user to enter a two-digit number, then prints the number with its digits reversed. A session with the program should have the following appearance: Enter a two-digit number: 28 The reversal is: 82 Hint: Read the number using %d, then break it into two digits. If n is an integer, then n % 10 is the last digit in n and n / 10 is n with the last digit removed.

A

?

40
Q

Write a program to read a float representing a number of degrees Celsius, and print as a float the equivalent temperature in degrees Fahrenheit. Print your results in a form such as Enter a temperature is Celsius degrees: 100 100.0 degrees Celsius is equivalent to 212.0 degrees Fahrenheit

A

include#includeint main(){floattemp_c, temp_f;printf (“Enter the value of Temperature in Celcius: “);scanf (“%f”, &temp_c);temp_f = (1.8 * temp_c) + 32;printf (“The value of Temperature in Fahreinheit is: %f”, temp_f);return 0;}