MIZZOU cs1050 exam 2
enum HOTEL {Marriott, Hilton, BestWestern, HolidayInn, Motel6, Super8}; enum ROOM {StandardKing, Standard2Queen, QueenSuite, KingSuite, PresidentialSuite}; int costMatrix[COUNTHOTELS][COUNTROOMTYPES]= { {200,200,350,375,550}, {180,210,325,350,600}, {125,125,240,250,400}, {130,135,250,275,450}, {119,119,239,249,399}, {118,118,240,247,389},}; void PrintHotelRoomsLessThan(int iLimit) { enum HOTEL hotel=0; enum ROOM room=0; for (hotel=Marriott; hotel<=Super8; hotel++) for (room=StandardKing; room<=PresidentialSuite; room++) if (costMatrix[hotel][room]<iLimit) printf("%s - %s ($%d)\n",GetHotelName(hotel),GetRoomName(room),GetRoomPrice(hotel,room)); } If one were to call the above function PrintHotelRoomsLessThan() with a parameter of 100, what would it print? a. Nothing. b. All of the hotels, rooms, and prices. c. It would cause a compiler error because 100 is a literal and cannot be changed by the function. d. It would cause a compiler error because the for loops must use integer values rather than names. e. It would print the hotel and room names, but no prices.
a. Nothing.
Which statement is true regarding the statement ++frequency[responses[answer]]; a. This statement increases the appropriate frequency counter depending on the value of responses[answer]. b. This statement increases both the frequency and responses counters at the answer index. c. This statement increases the appropriate answer counter depending on the value of frequency[responses]. d. This statement increases the appropriate responses counter depending on the value of frequency[answer]. e. This statement produces a syntax error because subscripts cannot be nested.
a. This statement increases the appropriate frequency counter depending on the value of responses[answer].
A function that modifies an array by using pointer arithmetic to set every value should have a parameter that is a. a nonconstant pointer to nonconstant data b. a nonconstant pointer to constant data c. a constant pointer to nonconstant data d. a constant pointer to constant data e. either a or c would work
a. a nonconstant pointer to nonconstant data
What will the output of the program be? #include<stdio.h> intmain() { static char *s[] = {"black", "white", "pink", "violet"}; char **ptr[] = {s+3, s+2, s+1, s}, ***p; p = ptr; ++p; printf("%s", **p+1); return 0; } a. ink b. lack c. ite d. let
a. ink
For the following statements, would printf("%s", arr); and printf("%s", ptr) produce the same result? char arr[] = "JavaTpoint"; char *ptr = "JavaTpoint"; a. yes b. no
a. yes
If array name arrayName is passed to a function, C automatically passes __________ which is equivalent. a. *arrayName[0] b. &arrayName[0] c. arrayName[1] d. arrayName[0] e. *arrayName
b. &arrayName[0]
What would be the equivalent pinter expression for referring the array element a[i][j]? a. *(a+i)+j b. *(*(a+i)+j) c. *(a+i+j) d. *(a)+i+j
b. *(*(a+i)+j)
How should the symbolic constant COUNTROOMTYPES be defined? a. It doesn't matter because the costMatrix is initialized anyway. b. 5 c. 6 d. 7 e. PresidentialSuite
b. 5
What is the output of this program? #include<stdio.h> int main() { intarr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8}; int *p, *q; p = &arr[1][1][1]; q = printf("%d, %d, ", *p, *q); return 0; } a. 8,1 b. 8,10 c. 10,2 d. 2,10
b. 8,10
Which of the following most closely reflects the principle of least privilege. a. Always grant a function at least enough access to the data in its parameters to accomplish its specified task. b. Always grant a function enough access to the data in its parameters to accomplish its specified task, but no more. c. Always grant a function full access to its parameters, to avoid unnecessary runtime checks. d. Always grant a function the least amount of access to the data in its parameters. e. Never grant a function access to the data in its parameters.
b. Always grant a function enough access to the data in its parameters to accomplish its specified task, but no more.
If I changed the code from costMatrix[hotel][room] to GetRoomPrice(hotel,room), what would happen? a. It would not compile because the function would be on the left side of the < sign. b. It would work just the same. c. It would compile, but the if statement would always be true (would print everything). d. It would compile, but the if statement would always be false (would print nothing). e. It would cause a runtime error the first time the if statement executed.
b. It would work just the same.
Consider what we discussed in class about how two-dimensional arrays are laid out in memory, and pick the best answer below that will complete this code void Print2DArray(int array[], int rows, int cols) { for (int i = 0; i < rows; i++) { printf("Row %d: ",i); for (int x = 0; x < cols; x++) { printf("%02d ",________________); } printf("\n"); } } a. array[x*cols+i] b. array[i*cols+x] c. array[x*rows+i] d. array[i,x] e. array[x,i]
b. array[i*cols+x]
A pointer to a block of memory is effectively the same as an array. a. true b. false
b. false
Are the expressions *ptr++ and ++*ptr the same? a. yes b. no
b. no
Is the NULL pointer the same as an uninitialized pointer? a. yes b. no
b. no
Is there any difference between the following prototypes? int fun(int arr[]); int funn(int arr[2]); a. yes b. no
b. no
Will the following program compile successfully? #include<stdio.h> int main() { char a[] = "Java"; Char *p = "Tpoint"; a = "Tpoint"; p = "Java"; printf("%s %s ", a, p); return 0; } a. yes b. no
b. no
What is (void *)0? a. representation of the void pointer b. representation of the NULL pointer
b. representation of the NULL pointer
The special conversion specifier for printing addresses is __________. a. %a b. %m c. %p d. %loc e. %%
c. %p
What's the output of the program? #include<stdio.h> int main() { int arr[1] = {10}; printf("%d ", arr[0]); return 0; } a. 0 b. 1 c. 10 d. garbage value
c. 10
What will the following code print? #include<stdio.h> int main(void) { int b[2][2] = {1, 2, 3}; printf("%d\n", b[0][1]); } a. 0 b. 1 c. 2 d. 3 e. This code will not compile
c. 2
What will be the output of this program? #include<stdio.h> void fun(int **p); int main() { int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0}; int *ptr; ptr = &a[1][1]; fun(&ptr); return 0; } void fun(int **p) { printf("%d ", **p); } a. 1 b. 2 c. 3 d. 4
c. 3
What is the output of this program? #include<stdio.h> int main() { float arr[] = {12.4, 2.3, 4.5, 6.7}; printf("%Id ", sizeof(arr)/sizeof(arr[0])); return 0; } a. 0 b. 1 c. 4 d. 5
c. 4
What is the output of this program on a 32-bit system? #include<stdio.h> int main() { printf("%Id, %Id ", sizeof(NULL), sizeof("")); return 0; } a. 2,1 b. 2,2 c. 4,1 d. 4,2
c. 4,1
How should the symbolic constant COUNTHOTELS be defined? a. It doesn't matter because the costMatrix is initialized anyway. b. 5 c. 6 d. 7 e. Super8
c. 6
When a compiler encounters a function parameter for a single-subscripted array of the form int a[], it converts the parameter to a. int a b. int &a c. int * a d. int * const a e. const int * a
c. int * a
Which is true about the following variables on a 64-bit platform (like tc.rnet.missouri.edu); char * p1[2] = {"Jim","Ries"}; char p2[2][5] = {"Jim","Ries"}; a. sizeof(p1) == sizeof(p2) b. sizeof(p1) < sizeof(p2) c. sizeof(p1) > sizeof(p2) d. sizeof(p1) is undefined e. sizeof(p2) is undefined
c. sizeof(p1) > sizeof(p2)
Four of the following expressions have the same value. Which of the following's value is different from the others? a. *(&Ptr) b. *&Ptr c. &*Ptr d. *Ptr e. Ptr
d. *Ptr
What's the output of this program? #include<stdio.h> int main() int i=3, *j; j = &i; printf("%d ", i**j*i+*j); return 0; } a. 27 b. 9 c. 3 d. 30
d. 30
What is the output of this program? #include<stdio.h> int main() { int a[3] = {1, 2, 3}; int *p = a; /* Assume base address of a is 500 and integer is 4 byte size */ printf("%p ", (void *)p); printf("%p ", *p); printf("%p ", *p++); printf("%p ", (void *)p); return 0; } a. 500 1 2 501 b. 500 1 1 501 c. 500 1 2 504 d. 500 1 1 504
d. 500 1 1 504
What will the following code do? #include <stdio.h> int main() { char * p = "JimR"; printf("p = %s\n",p); p[0] = 'A'; printf("p = %s\n",p); } a. Not compile (generate a compiler error) b. Print "p = JimR" and on the next line "p = AimR" c. Print "p = JimR" and on the next line "p = JimR" d. Generate a segmentation fault e. Cause an infinite loop due to memory overwrite
d. Generate a segmentation fault
What if the outer for loop were removed and you called PrintHotelRoomsLessThan() with 350? a. It would print nothing. b. It would print all of the hotels, rooms, and prices. c. It would cause a compiler error because there is no for loop for hotels. d. It would print Marriott - Standard King ($200) and Marriott - Standard Two Queens ($200) e. It would print all of the Super8 rooms except the Presidential Suite.
d. It would print Marriott - Standard King ($200) and Marriott - Standard Two Queens ($200)
Which is true of an enum? a. Defines a list of identifiers whose values start with 1 and increase by 1 as elements are added to the list. b. A symbolic constant should always be used instead of an enum. c. An identifier in an enum can be used as a mnemonic to replace a floating point literal. d. The value of the first identifier in an enum defaults to zero but can be made to start at any integer number. e. The values of identifiers in an enum must start at zero.
d. The value of the first identifier in an enum defaults to zero but can be made to start at any integer number.
The definition int *count; a. is a syntax error because only pointers can be defined with * notation. b. is a compile-time error. c. is a logic error. d. is a correct definition of integer pointer count. e. is a dereferenced integer called count.
d. is a correct definition of integer pointer count.
Assume string1 is a character array. Which of the following operations does not produce a string? a. string1[] = "test"; b. string1[] = "test\0"; c. string1[] = {'t', 'e', 's', 't', '\0'}; d. string1[] = {'t', 'e', 's', 't'}; e. string1[] = " ";
d. string1[] = {'t', 'e', 's', 't'};
What happens if a value is assigned to an array element whose index exceeds the array size? e.g. int a[5]; a[1000] = 10 a. the element will be set to 0 b. compile error c. the array size would grow d. the program may crash if some important data gets overwritten
d. the program may crash if some important data gets overwritten
Arrays are data structures consisting of related data items of the same __________. a. cardinality b. sort order c. subscript d. type e. element
d. type
Which is the correct prototype for the function fun() in the below program? #include<stdio.h> int main() { int a[3][4]; fun(a); return 0; } a. void fun(int*p[3][4]); b. void fun(int p[][]); c. void fun(int *p[][4]); d. void fun(int p[][4]);
d. void fun(int p[][4]);
What will the following code print? #include<stdio.h> int main(void) { int x=5; if (5==x) { int x=6; if (6==x) { int x=7; if (7==x) { } } printf("x = %d\n", x); } } a. Nothing. This code has a compilation error. b. Nothing. This code has a run-time error. c. x = 5 d. x = 6 e. x = 7
d. x = 6
Which statement about the following code snippet must be true? int i,n[11]; for (i = 1; i < 10; i++) { n[i] = 0; } a. It will not compile. b. It contains an off-by-one error. c. It contains a syntax error. d. It is initializing the first 10 elements of an array. e. It is initializing successive elements of an array.
e. It is initializing successive elements of an array.
What is wrong or illegal about the following code? void bubbleSort(int * const array, const size_t size) { void swap(int *element1Ptr, int *element2Ptr); for (unsigned int pass = 0; pass < size - 1; ++pass) { for (size_t j = 0; j < size - 1; ++j) { if (array[j] > array[j + 1]) { swap(&array[j], &array[j + 1]); } } } } a. It illegally contains a function prototype inside the implementation of a function. b. It sorts data in descending order rather than ascending order. c. The call to swap should pass array[j] and array[j+1] instead of their addresses. d. Since the array is passed to the function as const, the values cannot be swapped. e. There is nothing wrong and nothing illegal about the code.
e. There is nothing wrong and nothing illegal about the code.
Which statement is false? a. All pointer variables on a particular machine take up the same amount of storage. b. In C, a string is essentially a pointer to its first character. c. Arrays may contain pointers. d. Each entry in an array of strings is actually a pointer to the first character of a string. e. he size of an array of strings is the sum of the lengths of the strings.
e. he size of an array of strings is the sum of the lengths of the strings.
If there are fewer initializers than elements in the array, the remaining elements are __________. a. not initialized b. deleted c. ignored d. initialized to empty e. initialized to zero
e. initialized to zero
sizeof a. is a binary operator b. returns the total number of elements in an array c. usually returns a double d. returns the number of bytes in an array times the bytes in the array's data type e. returns the total number of bytes in an array
e. returns the total number of bytes in an array