Adv C Programming Questions

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 15

Advanced “C” Programming Questions – Folder 37123

Note: Correct answers are marked with “ * * ” Folder 37123

1. If there are two occurrences of the structure in memory, how many variables are
actually declared by the statements in lines 4 through 8?

1. #include "stdio.h"
a. 4 2. void main( )
b. 5 3. {
c. 6 ** 4. struct {
5. char initial; /* last name initial */
6. int age; /* child's age */
7. int grade; /* child's test score */
8. } boy,girl;

9. boy.initial = 'R';
10. boy.age = 15;
11. boy.grade = 75;

12. girl.age = boy.age - 1; /*one year younger*/


13. girl.grade = 82;
14. girl.initial = 'h';
15. printf("%c is %d years old and got a grade
of %d\n", girl.initial, girl.age girl.grade);
16. printf("%c is %d years old and got a grade
of %d\n", boy.initial, boy.age, boy.grade);
17. }

2. What is the child's initial in the fourth array element, ____________________.

1. #include "stdio.h"
a. A 2. void main( )
b. C 3. {
c. D ** 4. struct {
5. char initial;
6. int age;
7. int grade;
8. } kids[6];
9. int index;
10. for (index = 0;index < 6;index++) {
11. kids[index].initial = 'A' + index;
12. kids[index].age = 16;
13. kids[index].grade = 84;
14. }
15. kids[3].age = kids[5].age = 17;
16. kids[2].grade = kids[5].grade = 92;
17. kids[4].grade = 57;
18. kids[1] = kids[4];
19. for (index = 0;index < 6;index++)
20. printf("%c is %d years old and got a
grade of %d\n",kids[index].initial,
kids[index].age, kids[index].grade);
21. }
3. Which one of the following grid combinations represents the structure element that
will be specified by point when index == 1? (Select one answer)

a. a, 3
b. b,1 **
c. d, 1
d. f, 2

4. If index==5, what value is accessed by the following?

d. 84
e. 17 **
f. 16
g. 57

5. Using the sample program as a guide, enter the code to assign an age of 87 to the
structure sub. Do not forget your semicolon.

1. #include "stdio.h"
2. #include "string.h"
SUB.DISCRIP.AGE = 87; 3. void main( )
4. {
5. struct person {
6. char name[25];
7. char status;/* M = married, S = single */
8. int age;
9. };

10. struct alldat {


11. int grade;
12. struct person descrip;
13. char lunch[25];
14. } student[53];

15. struct alldat teacher,sub;


16. teacher.grade = 94;
17. teacher.descrip.age = 34;
18. teacher.descrip.status = 'M';
19. strcpy(teacher.descrip.name,"Mary Smith");
20. strcpy(teacher.lunch,"Baloney sandwich");

21. student[1].descrip.age = 15;


22. student[1].descrip.status = 'S';
23. strcpy(student[1].descrip.name,"Billy
Boston");
24. strcpy(student[1].lunch,"Peanut Butter");
25. student[1].grade = 77;
6. Using the sample program as a guide, enter the code to assign a status of M for
married to the structure sub. Be careful with the punctuation of this one. 1.
#include "stdio.h"
2. #include "string.h"
3. void main( )
4. {
SUB.DISCRIP.STATUS = “M” ; 5. struct person {
6. char name[25];
7. char status;/* M = married, S = single
*/
8. int age;
9. };

10. struct alldat {


11. int grade;
12. struct person descrip;
13. char lunch[25];
14. } student[53];

15. struct alldat teacher,sub;


16. teacher.grade = 94;
17. teacher.descrip.age = 34;
18. teacher.descrip.status = 'M';
19. strcpy(teacher.descrip.name,"Mary
Smith");
20. strcpy(teacher.lunch,"Baloney
sandwich");

21. student[1].descrip.age = 15;


22. student[1].descrip.status = 'S';
23. strcpy(student[1].descrip.name,"Billy
Boston");
24. strcpy(student[1].lunch,"Peanut Butter");
25. student[1].grade = 77;

7. Which of the following are true about named structures? (Select multiple answers)

a. The absence of structure variables in named structures definition means that


storage is reserverd at the beginning of the structure.
b. The structure name and type are coded before the opening brace in the
structure definition.
c. Named structures can be used as the data type of other variables
declared in a program.
d. A structure can be defined as a data type by using a named structure.

8. Type a line of code that will reference an element, period, in a nested structure
with a structure name of classroom and a nested structure name of subject.

a. CLASSROOM . SUBJECT . PERIOD **


b. CLASSROOM. PERIOD.SUBJECT
c. SUBJECT. CLASSROOM.PERIOD
9. Which one of the following lines of code defines year and color as variables with type
struct model? (Select one answer)

a. Struct Model Year, Color **


b. Model. Year. Color
c. Struct Year Color, model;
d. Struct model char color, int year;

10. How can stack overflow errors be avoided when large structures exist? (Select
multiple answers)

a. Use Compound variables names to refer to elements of structures.


b. Make structures defined static **
c. Use a structure type to define separate, non-nested structure variables.
d. Move structure definition outside the main ( ) functions * *

11. Which of the following show a value being assigned to a structure element using
literals, characters, or variables? (Select multiple answers)

a. Cars[4]
b. Cars[8].year = Cars[7].year = 5;
c. Cars[10] = Cars[11] ;
d. Cars[0] = Cars[1] ;

12. In the second part of this program, a control variable, index, is declared. A for loop
is used to assign values to the union, then display them. What does the %8x in the
printf( ) statement mean? (Select one answer)

1. union {
2. unsigned int value;
3. struct {
a. The value will be displayed 4. char first;
as hexadecimal. 5. char second;
b. The value is multiplied times 6. } parts;
eight. 7. } number;
c. The value is left justified in
the field. 8. int index;
9. for (index = 15;index <10000;index +=
3000) {
10. number.value = index;
11. printf("%8d %8x %6x %6x\n",number.value,
number.value,number.parts.first,
number.parts.second);
12. }

13. Where is the name of the union placed when writing lines of code? (Select one
answer)

a. After the word union and before the opening curly brace {
b. After the opening curly brace {
c. Just before the closing curly brace }
d. Just after the closing curly brace }.
14. Match each term on the left with its description on the right.

C 1. Union A. The data storage method that stores the low order
Byte first and the higher order byte second.

A 2. Little endian B. The data storage is which all the elements


occupies the same storage locations.

B 3. Big endian C. The data storage method that stores the high
Order first and the low order byte second.

15. Which of the following are true for unions? (Select multiple answers)

a. Unions provides a way to look at the same data with different names and
types.
b. Elements of the unions are referenced using compond variables names.
c. The way in which the computer hardware stores data will not have an effect
on the way in which mixed data unions can be used.
d. Not ever union has a name.

16. The compound element name ford.vehicle is used to assign a value to the Ford's
vehicle field. Here is the assignment statement: ford.vehicle = AUTO;
What value will appear in the vehicle field after this assignment statement? (Enter
the number.)

a. 1
b. 0

17. The 1 appears in the vehicle field. Enter the statement to assign a weight of 2472 to
the appropriate field in the ford structure. ___________________________

a. Ford.Weight = 24 72
b. Ford.Weight 24 72
18. Line 3 is the function prototype of the mix_up( ) function. The prototype shows that
mix_up will receive data from the calling function, but will not return data to it.
What kind of data will mix_up receive? (Select one answer)

a. An Array.
b. A Character.
c. A Line.

19. What will happen if there is no file that exists for the name contained in the
filename? (Select one answer)

a. It will not be created.


b. It will casuse an error.

20. What do you think will happen if the string contains a character such as !, %, #, or
~?? (Select one answer)
1. #include "stdio.h"
2. #include "ctype.h"
a. It will cause an error. 3. void count_data(char line[ ]);
b. It will test FALSE and
terminate the loop. 4. void main( )
c. It will not be counted 5. {
but program will 6. FILE *fp;
continue. 7. char line[80], filename[24];
8. char *c;
9. printf("Enter filename -> ");
10. scanf("%s",filename);
11. fp = fopen(filename,"r");
12. do {
13. c = fgets(line,80,fp);
14. if (c != NULL) {
15. count_data(line);
16. }
17. } while (c != NULL);
18. fclose(fp);
19. }

20. void count_data(char line[ ])


21. {
22. int whites, chars, digits;
23. int index;
24. whites = chars = digits = 0;
25. for (index = 0;line[index] != 0;index++){
26. if (isalpha(line[index]))
27. chars++;
28. if (isdigit(line[index]))
29. digits++;
30. if (isspace(line[index]))
31. whites++;
32. } /* end of counting loop */
33. printf("%3d%3d%3d %s",whites,chars,
digits,line);
34. }
21. The first time through the for loop, the values of mask and number[0] are the
hexadecimal values shown. However, the logical operators calculate binary values.
The binary equivalents of mask and number[0] are shown in the illustration. (Select
one answer)

a. 0000 0000
b. 1111 1111 **
c. 1111 0000

22. In the logical OR calculation the values of mask and number[0] are the same as
they were for the AND calculation. What is the result of the logical OR? (Select one
answer)

a. 0000 0000
b. 1111 1111 **
c. 1111 0000

23. In the logical XOR calculation, the values of mask and number[0] are the same as
they were for the AND calculation. What is the result of the logical XOR? (Select one
answer)

a. 1111 0000
b. 1111 1111
c. 0000 1111 **

24. The logical invert operates on only one number, so this calculation uses only
number[0]. What is the result? (Select one answer)

a. 1111 0000
b. 0000 1111
c. 1111 1111 **

25. What is the decimal value of small after the first shift left (<<) operation? ( Enter the
number.)

a. 3
b. 2
c. 5
26. What is the decimal value of big after the first shift right (>>) operation? ( Enter the
number.)

a. 8192
b. 8291
c. 8393

 
27. Which of the following statements shifts the binary digits in a value to the right?
(Select one answer)

a. Let = Let < 1;


b. Let = Let > 1;
c. Let = Let << 1;
d. Let = Let >> 1; **

28. Which of the following are true statements? (Select multiple answers)

a. Shifting left multiplies the value by two for each bit position shifted.
b. Shifting right multiplies the value by two for each bit position shifted.
c. Shifting left Divides the value by two for each bit position shifted.
d. Shifting right Divides the value by two for each bit position shifted.

29. Which of the following can specify the number of bit positions to be shifted? (Select
multiple answers)

a. A Numeric literal
b. A Numeric variable
c. A Logical invert.
d. An alphanumeric invert.

30. Which function is used to translate upper case letters to lower case letters? (Select
one answer)

a. Isupper ().
b. Islower ().
c. toupper() **
d. tolower ().

31. Which of the following logical operators will compare a pair of bits and produce a
result of 1 if only one bit is a 1? (Select one answer)

a. & ( The Logical And Operator )


b. | ( The Logical Or Operator )
c. The Logical XOR operator *
d. ~ The Logical invert operator.
32. Lines 16 through 19 define a second occurrence of the structure, animal, allocate
memory to it and assign values. What is the name of the pointer that points to this
occurrence of the structure? ___________________________________.

1. #include "stdio.h"
a. PET 2 2. #include "string.h"
b. PET 1 ** 3. #include "stdlib.h"
c. PET 3 4. void main( )
5. {
6. struct animal {
7. char name[25];
8. char breed[25];
9. int age;
10. } *pet1, *pet2, *pet3;
11. pet1 = (struct animal *)malloc(sizeof
(struct animal));
12. strcpy(pet1->name,"General");
13. strcpy(pet1->breed,"Mixed");
14. pet1->age = 1;
15. pet2 = pet1;
16. pet1 = (struct animal *)malloc(sizeof
(struct animal));
17. strcpy(pet1->name,"Frank");
18. strcpy(pet1->breed,"Labrador ");
19. pet1->age = 3;
20. pet3 = (struct animal *)malloc(sizeof
(struct animal));
21. strcpy(pet3->name,"Krystal");
22. strcpy(pet3->breed,"German Shepherd");
23. pet3->age = 4;
24. printf("%s is a %s, and is %d years old.
\n", pet1->name, pet1->breed, pet1->age);
25. printf("%s is a %s, and is %d years old.
\n", pet2->name, pet2->breed, pet2->age);
26. printf("%s is a %s, and is %d years old.
\n", pet3->name, pet3->breed, pet3->age);
27. pet1 = pet3;
28. free(pet3);
29. free(pet2);
30. }

33. What can be used rather than individual pointers to each structure element when
using dynamic allocation of memory for storage of a data structure? (Select one
answer)

a. Heap Storage.
b. Dynamic Memory Allocation.
c. An array of pointers.
d. The free() function.
34. Declare an array of pointers named car with 6 occurrences. _____________________

a. Car[6]
b. Int *Car = new *Car [6]
c. *Car[6]

35. On line 12, three pointers are declared which will be used to keep track of the
addresses of the occurrences of the structure. Starting on line 14 an occurrence of
the structure is defined. What is contained in the variable start after line 14
executes? (Select one answer).
1. #include "stdio.h"
2. #include "string.h"
3. #include "stdlib.h"
a. The Byte Size of the 4. #define RECORDS 2
structure. 5. void main( )
b. The Data Size of the 6. {
structure. 7. struct animal {
c. The Address of the 8. char name[25];
structure. 9. char breed[25];
10. int age;
11. struct animal *next;
12. } *point, *start, *prior;
13. int index;
14. start = (struct animal *)malloc
(sizeof(struct animal));
15. strcpy(start->name,"General");
16. strcpy(start->breed,"Mixed");
17. start->age = 4;
18. start->next = NULL;
19. prior = start;
20. for (index = 0;index < RECORDS;index++) {
21. point = (struct animal *)malloc
(sizeof(struct animal));
22. strcpy(point->name,"Frank");
23. strcpy(point->breed,"Labrador ");
24. point->age = 3;
25. prior->next = point;
26. point->next = NULL;
27. prior = point;
28. }

36. In line 25, the address of point is assigned to the element, next, of the structure
pointed to by the pointer, prior. What value did this pointer contain BEFORE line
25 was executed? (Select one answer).

1. #include "stdio.h"
a. The address of start. 2. #include "string.h"
b. The NULL value. 3. #include "stdlib.h"
c. The Address of next. 4. #define RECORDS 2
5. void main( )
6. {
7. struct animal {
8. char name[25];
9. char breed[25];
10. int age; Continue…
11. struct animal *next;
12. } *point, *start, *prior;
13. int index;
14. start = (struct animal *)malloc
(sizeof(struct animal));
15. strcpy(start->name,"General");
16. strcpy(start->breed,"Mixed");
17. start->age = 4;
18. start->next = NULL;
19. prior = start;
20. for (index = 0;index <
RECORDS;index++) {
21. point = (struct animal *)malloc
(sizeof(struct animal));
22. strcpy(point->name,"Frank");
23. strcpy(point->breed,"Labrador ");
24. point->age = 3;
25. prior->next = point;
26. point->next = NULL;
27. prior = point;
28. }

37. The statement executed by the inner for loop is line 12,
a putc( ) function. The putc( ) library function writes individual characters to a data
file. It writes to the file whose FILE pointer is named as an argument.
What file is it?

a. tenlines.txt
b. indexers
c. points
d. others.
38. The inner for loop will continue to execute as long as its control variable is TRUE.
What is the initial value of the control variable? (Select one answer)

a. 1
b. A
c. Indexer
d. 0

39. The first of the two arguments passed to putc( ) is the source data, the second is the
pointer to the target file. In line 12, the source is the character in an element
of the array, others. The target is the open file, tenlines.txt. When indexer reaches
the null character, which line executes next?

a. 13
b. 10
c. 12
d. 11
40. Which of the following are true when coding the fopen( ) function with the "a"
attribute? (Select multiple answers)

a. It causes coding the fopen() function with the “a” attribute cause data to be
written at the beginning of an existing file.

b. It causes coding the fopen() function with the “a” attribute cause data to
be written at the end of an existing file.

c. It causes coding the fopen() function with the “a” attribute will create a
new file if it does not already exist.

d. It causes coding the fopen() function with the “a” attribute will write
individual characters to a file.

41. The ________________ library function writes individual characters to a file.

a. PUTS
b. PUTC
c. GETS

42. If the file does exist, the program goes to line 8, and the do-while loop on line 9
executes. The getc( ) function reads one character from the specified file, and
returns it to the program in the variable specified. In this example, where does
getc(fp); put the character?

a. The Monitor.
b. c
c. Input Buffer
d. fp

43. What happens if you do not check for end-of-file?

a. The program reads forever.


b. The program returns an error.
c. “Files Does not exist” is displayed.
d. The contents of TENLINES.TXT is displayed.
44. Fill in the blank so that the code will open the file MYSTORY.TXT for reading.
fp = fopen("MYSTORY.TXT","_______");

a. R
b. W
c. WR

45. A file that is opened for reading must already exist. If it does not exist, the file
pointer is set to ________________________.

a. NULL
b. Append
c. Start

46. Why should getc( ) always test for end-of-file if it is issued with a loop? (Select one
answer)

a. If it does not test for End-Of-File, the loop will return more then one character
at a time when reading data.
b. If it does not test for End-Of-File, the loop will point to NULL.
c. If it does not test for End-Of-File, the loop will return the value –1.
d. If it does not test for End-Of-File, the loop will continue reading
indefinitely.

47. Notice that the fscanf( ) function call is coded in an assignment statement, which
returns a result in the variable c. If the array, oneword, contains the resulting
string, what does c contain?

a. The Number of words read.


b. The Text to be printed.
c. The End-Of-File indicator.
d. The File-type indicator.

48. Assume that the first line in the file, TENLINES.TXT reads: C's the Greatest!" What
is the value of c, after the first fscanf( ) executes? Type in the number.

a. 1
b. 11
c. 2

49. Why does printing from a file require two file pointers? (Select one answer)

a. One is for the file to read, other is for the file to be written.
b. One is for the file to be entered into memory, the other is for the file to be
printed on an external printer other than a monitor.
c. Both are for the file to be read.
d. Both are for the file to be entered into memory.
50. Which of the following fopen( ) statements will write to the standard DOS printer,
PRN, by specifying its standard filename? (Select one answer)

a. FILE *fp, *printer


b. putchar(c); putc(c,printer)
c. printer = fopen(“PRN”,”w”);
d. fp = fopen(“PRN”,”r”);

You might also like