Docsity

Questions with Solutions - Structured Programs in C | CSCE 206, Exams of Computer Science

Toggle sidebar
bg1
True/False Questions:
1. Compiler will give error information when invalid subscript is used to refer to one
element in an array. F
2. Two different struct types may not have the same member names. F
3. The strcmp(s1,s2) function has 2 parameters and 3 possible return values. T
4. We can access a dynamically allocated string through a char pointer variable. T
5. A linear search requires an array to be previously sorted. F
6. For a 4000 element array, a binary search would require 14 elements to be tested at most.
F
7. When an array is passed as an argument to a function, the array elements are duplicated
for use in the function. F
8. The typedef keyword is used for defining a new name for an existing type. T
9. The two function headers, int calculate(int a[], int b[]) and int
calculate( int *a, int *b) are equivalent in the sense that both of them can take
two array names as arguments. T
10. The following declaration allocates memory for one struct variable: F
struct student_info {
int level;
char department[10];
}
11. A linked-list structure can contain the same data as an array of structs. T
12. The size of a struct variable can be smaller than the summation of sizes of all its
elements. F
13. Variable-length array can be declared as global variable (outside any function). F
14. Since an array name can be interpreted as a pointer the following code is correct. F
int main(void) {
char pointer[5];
pointer++;
}
15. An array of size 10 will require 9 passes through the array (at the most) using a bubble
sort. T
Multiple Choice Questions:
16. Which of the following is the last character in ALL strings? D
A. '\n '
B. '\s '
C. '\t '
D. '\0 '
E. ' EOF'
17. What format is used to display a string in a printf()? E
A. %c
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Questions with Solutions - Structured Programs in C | CSCE 206 and more Exams Computer Science in PDF only on Docsity!

True/False Questions:

  1. Compiler will give error information when invalid subscript is used to refer to one element in an array. F
  2. Two different struct types may not have the same member names. F
  3. The strcmp(s1,s2) function has 2 parameters and 3 possible return values. T
  4. We can access a dynamically allocated string through a char pointer variable. T
  5. A linear search requires an array to be previously sorted. F
  6. For a 4000 element array, a binary search would require 14 elements to be tested at most. F
  7. When an array is passed as an argument to a function, the array elements are duplicated for use in the function. F
  8. The typedef keyword is used for defining a new name for an existing type. T
  9. The two function headers, int calculate(int a[], int b[]) and int calculate( int *a, int *b) are equivalent in the sense that both of them can take two array names as arguments. T
  10. The following declaration allocates memory for one struct variable: F struct student_info { int level; char department[10]; }
  11. A linked-list structure can contain the same data as an array of structs. T
  12. The size of a struct variable can be smaller than the summation of sizes of all its elements. F
  13. Variable-length array can be declared as global variable (outside any function). F
  14. Since an array name can be interpreted as a pointer the following code is correct. F int main(void) { char pointer[5]; pointer++; }
  15. An array of size 10 will require 9 passes through the array (at the most) using a bubble sort. T

Multiple Choice Questions:

  1. Which of the following is the last character in ALL strings? D A. '\n ' B. '\s ' C. '\t ' D. '\0 '

E. ' EOF'

  1. What format is used to display a string in a printf()? E A. %c

B. %f C. %str D. %t E. %s

  1. Given the following code segment: C int A[20]; int * p; Which of the following will allow p to reference the tenth element of the array A? A. p = A[10]; B. p = A+10; C. p = &A[9]; D. p = &A[2]; E. p = A[] + 9;
  2. What would be the output of the following? D char str[] = "I love C?\n"; printf("%d",sizeof(str)); (Assume a single space between each of the words) A. 13 B. 10 C. 9 D. 11 E. 12
  3. What is the output of the following code segment? C int main(void) { double myArray[4] = {1.0f, 2.0f, 4.0f, 8.0f}; double *first, *next; first = &myArray[0]; next = &myArray[3]; printf("%.2f", next-first); return 0; } A. 1. B. 4. C. 7. D. 3. E. 2.
  4. Which is a proper declaration for an array of struct my_struct? D struct my_struct{int id;}; A. struct my_struct * A[10]; B. struct my_struct[10];
  1. Which one is true about “HEAP”: (choose the most correct one) E A. Memory allocated by C's dynamic allocation functions is obtained from the heap. B. The heap is free memory that is not used by operating system. C. The size of the heap cannot usually be known in advance. D. It contains a fairly large amount of free space. E. All of the above
  2. Which one is correct?(Choose the most correct one) D A. The storage set aside by malloc is automatically initialized to zero. B. The storage set aside by calloc is not initialized and therefore start with a garbage value. C. If efficiency is an issue, programmers prefer calloc instead of malloc. D. malloc and calloc may return NULL if the system could not allocate enough memory space. E. A, B and C are correct
  3. To declare an array str that can hold a string of 20 characters, which one is a suitable declaration? D A. char str[11]; B. char str[20]; C. char str[19]; D. char str[21]; E. int str[10];
  4. Which of the following is illegal for the initialization of strings? (Choose the most correct one) A/E A. char s[] = {'a','b','c'}; B. char s[] = "abc"; C. char p[2]; p[0]= 'A'; p[1] = '\0'; D. char s3[] = "abs\0"; E. All of the above are legal
  5. Given the following definitions, what is the value of b[1][2]? D int b[2][3] = {1, 2, 3, 4, 5, 6}; A. 3 B. 4 C. 5 D. 6 E. 2
  6. The main difference between a structure and an array is: C A. Structures can contain arrays, but arrays cannot contain structures. B. Structures can store pointers, but arrays cannot. C. Structures can have components of various types, but arrays have components of the same type. D. Structures are of fixed size, but arrays can vary in size.

E. There is no difference between the two.

  1. Some of the uses of pointers in C are: (choose the most correct one) E A. array B. string C. call-by-reference D. linked-list E. All of the above
  2. What does the follwing code segment print? B int main() { char a[] = "CSCE206"; char* c = &a[3]; c++; printf("%c", *c); } A. C B. 2 C. E D. 0 E. S
  3. Given the following code segment:(Choose the most correct one) D #define N 10 int main(void) { int a[N]; a[10] = 10; } Choose the most correct statement. A. Compiler will complain about a[10]. Because subscript is out of range. B. Preprocessor constant CANNOT be used in declaration of array. C. Declaration of a[N] can be replaced by int a[n], n = 10; D. a[10]= 10 may cause the program to crash when running. E. All of the above.
  4. What can be said about the output of the following program B #include <stdio.h> int main(void) { double a[2], *p, *q;

p = &a[0]; q = p + 1;

E. 19

Use the following program for the next TWO problems int main(void) { char s1[] = "a brown cow"; char s2[] = "a white cat"; printf("%s%s", s2, s1); char* c = s2; c+=3; }

  1. What is the output of this program? A A. a white cata brown cow B. a brown cow C. a brown cowa white cat D. aa E. a white cat a brown cow
  2. What will be the output of the following statement? C printf("%c", *c); A. b B. r C. h D. w E. a
  3. Which of the following is not necessary when inserting a new node to a linked list: E A. Allocate memory for the new node B. Determine the insertion point C. Point the new node to its successor D. Point the predecessor to the new node E. Free the space allocated for successor of new node
  4. All of the following is valid except for: E char name[20]; char * pname = name; A. name[0]; B. pname[10]; C. *(pname + 5); D. *(name + 5); E. name = pname;
  5. What is the output? [Assume an int is 4 bytes long]. E int *p = (int *) 1000;

printf("%d", p+5); A. 1000 B. 1004 C. 1008 D. 1010 E. 1020

  1. What is the output of the following code? A int main() { char *p = "who_are_you?"; printf("%s%c%c", &p[5],p[2],p[8]); return 0; } A. re_you?oy B. who_aoy C. are_you?h_ D. ah_ E. roy
  2. Which one is correct definition for func1() that receives a two dimensional array? (Choose the most correct one) D A. void func1(int x[]) { /.../ } B. void func1(int x[2] []) { /.../ } C. void func1(int x[] []) { /.../ } D. void func1(int x[] [2]) { /.../ } E. All of the above are correct.
  3. What will be printed if the user enters “a.out She sells sea shells” at runtime? int main(int argc, char *argv[]) B {

C. x->a D. y->a E. x.y.a

  1. What is the output of the following code? E #include <string.h> #include <stdio.h> int main(void) { char s[16] = "woof"; char t[16] = "meow"; int i; strcat(s, t); for (i=strlen(s)-1; i>=0; i--) { putchar(s[i]); } return 0; } A. woof B. meow C. woemwoof D. foowwoem E. woemfoow
  2. What is the output of the following code? C #include <stdio.h> struct rec { char name[20]; int score; }; int main(void) { struct rec s[] = { {"Alice", 70}, {"Bob", 40}, {"Charlie", 90}, {"John", 80}}; printf("%s %d", s[3].name, s[0].score); return 0; } A. Alice 70 B. Bob 90 C. John 70 D. Charlie 80 E. It has syntax errors and won’t compile.