Comp Sci 1050 Final

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

The number guessing game we played in class requires the person who knows the number to say the number is greater than, equal to, or less than the current guess. If a number between 1 and 200 (inclusive) is selected, what is the maximum guesses needed to find the number if the guesser uses binary search?

8

What will the following C code print? #include <stdio.h> #include <math.h>int main(void) { float a = 2.0; float b = 3.0; printf("%3.1f\n",pow(a,b)); }

8.0

Which of the following can have a pointer as an operand? A. ++ B. *= C. / D. * (that is, the multiplication operator) E. %

A. ++

Which statement is false? A. A static local variable is treated exactly the same as a global variable by the compiler. B. A static local array is not created and destroyed each time the function is entered and exited, respectively. C. Arrays that are defined static are automatically initialized once at compile time. D. A static local variable exists for the duration of the program. E. A static local variable has the same visibility as a non-static local variable.

A. A static local variable is treated exactly the same as a global variable by the compiler.

Which of the following is an incorrect expression to increment c by 1 in the increment expression of a "for header"? A. All of these are correct B. c++ C. c-=(-1) D. c+=1 E. ++c

A. All of these are correct

Which statement is generally false? A. Dereferencing an uninitialized pointer causes a syntax error. B. Dereferencing an uninitialized pointer could accidentally modify important data. C. Dereferencing an uninitialized pointer could lead to a fatal execution time error. D. Dereferencing an uninitialized pointer could lead to a segmentation fault. E. Initializing pointers is optional.

A. Dereferencing an uninitialized pointer causes a syntax error.

The definition char string1[] = "first"; is equivalent to: A. char string1[] = {'f', 'i', 'r', 's', 't', '\0'}; B. character string1[] = {'f', 'i', 'r', 's', 't', '\0'}; C. char * string1 = {'f','i','r','s','t','\0'}; D. char string1 = {'f', 'i', 'r', 's', 't', '\0'}; E. char string1[] = {'f', 'i', 'r', 's', 't'};

A. char string1[] = {'f', 'i', 'r', 's', 't', '\0'};

How many lines will this code print (i.e., how many times will the function printf() be called)? #include <stdio.h> int main(void) { int i; for (i=0;4;i++) { printf("%d\n",i); } }

An Infinite Number

Lines beginning with a # are processed

At preprocessor time

Which statement about the bubble sort is false? A. It takes roughly n * n comparisons where n is the number of elements. B. It is a high-performance sort. C. The bubble sort compares successive pairs of elements. D. It compares only adjacent elements with one another. E. It is easy to program.

B. It is a high-performance sort.

In C, which operator raises x to the y power in the following statements? A. x ** y B. None of these D. x @ y C. x !! y E. x ^ y

B. None of these

Which of of these claims regarding the switch statement is false? A. Many cases may all invoke the same code. B. The default case must be at the bottom of the switch after all the non-default cases. C. It's appropriate for algorithms that contain a series of decisions in which a variable or expression is tested separately for each of the constant integral values it may assume. D. It works only with integer type expressions. E. None of these are false.

B. The default case must be at the bottom of the switch after all the non-default cases.

Which statement about pointers is false? A. The letters ptr in a pointer variable name are optional. B. The indirection operator * distributes to all comma-separated variable names in a declaration. C. A pointer may be initialized to 0, NULL, or an address. D. They can be defined to point to objects of any data type. E. The ampersand & operator returns the address of a variable

B. The indirection operator * distributes to all comma-separated variable names in a declaration.

Which statement is false? A. In C, a string is essentially a pointer to its first character. B. The size of an array of strings is the sum of the lengths of the strings. C. Arrays may contain pointers. D. All pointer variables on a particular machine take up the same amount of storage. E. Each entry in an array of strings is actually a pointer to the first character of a string.

B. The size of an array of strings is the sum of the lengths of the strings.

Which of the following statements correctly prints "Driver" if the given age is greater than or equal to 16 and "Non-driver" if the given age is less than 16? A. age >= 16 : printf("Driver") ? printf("Non-driver"); B. printf(!(age<16) ? "Driver" : "Non-driver"); C. printf(age >= 16 : "Driver" : "Non-driver"); D. age >= 16 ? printf("Driver") ? printf("Non-driver"); E. printf(age >= 16 ? "Non-driver" : "Driver");

B. printf(!(age<16) ? "Driver" : "Non-driver");

Which of these is not a valid identifier? A. A_xyz B. All of these are valid C. 1_xyz D. _axyz E. a_xyz1

C. 1_xyz

Which operation will find the remainder when 7 is divided by 2? A. mod(7,2) B. 7 | 2 C. 7 % 2 D. 7 ^ 2 E. 7 / 2

C. 7 % 2

Which of the following prototypes are not equivalent to the others? A. int fn(int a[]); B. int fn(int * a); C.All of these are equivalent D. Each of these are different (i.e., none of these are equivalent) E. int fn(int a[10]);

C. All of these are equivalent

Which statement is false? A. A static local variable is visible only in the scope in which it was declared. B. Arrays that are defined static are automatically initialized once at compile time. C. None of the answers are false. D. A static local array is not created and destroyed each time the function is entered and exited, respectively. E. A static local variable exists for the duration of the program.

C. None of the answers are false.

Which of the following is a repetition statement? A. break B. if C. while D. switch E. if ... else

C. While

Which of the following is false? A. the first element of an array is index zero B. arrays can be indexed by integer variables C. arrays can be indexed by float variables D. an integer expression may be used as a subscript E. the last element of an array is index array size - 1

C. arrays can be indexed by float variables

Which of the following is not a correct way to initialize an array? A. int n[5] = {}; B. int n[5] = {6, 6, 6}; C. int n[5] = {0, 7, 0, 3, 8, 2}; D. int n[] = {0, 7, 0, 3, 8, 2}; E. int n[5] = {7};

C. int n[5] = {0, 7, 0, 3, 8, 2};

Which of the following is false about a function being passed an array? A. it is able to modify the values stored in the array B. all of the answers are false C. it knows the size of the array it was passed D. it is passed the address of the first element in the array

C. it knows the size of the array it was passed

Which is not a scope for an identifier? A. block scope B. file scope C. procedure scope D. function-prototype scope E. function scope

C. procedure scope

A function that simply prints the values in an array by using pointer arithmetic on the parameter variable to iterate through the array should ideally be prototyped as which of these: A. void PrintArray(int * const a, int size); B. void PrintArray(int * a, int size); C. void PrintArray(const int * a, int size); D. any of these would work fine E. void PrintArray(const int * const a, int size);

C. void PrintArray(const int * a, int size);

A pointer cannot be assigned to

a pointer of a type other than its own type and void

Which of the following is not a correct way to initialize an array? a. int n[5] = {0, 7, 0, 3, 8, 2}; b. int n[] = {0, 7, 0, 3, 8, 2}; c. int n[5] = {7}; d. int n[5] = {6, 6, 6}; e. int n[5] = {};

a. int n[5] = {0, 7, 0, 3, 8, 2};

The number guessing game we played in class requires the person who knows the number to say the number is greater than, equal to, or less than the current guess. If a number between 1 and 100 (inclusive) is selected, what is the maximum guesses needed to find the number if the guesser uses binary search? a.7 b.8 c.6 d.100 e.5

a.7

What happens if you call free(NULL)? a.Nothing. b.A runtime error (such as a seg fault). c.All memory that has been allocated by malloc is freed (this is a "catch all"). d.This will not compile because NULL is an illegal argument to the free function. e.Undefined -this will vary from compiler to compiler.

a.Nothing.

A two-dimensional array element incorrectly referenced as a[x, y] is actually evaluated as

a[y]

When calling a function with arguments that should be modified, the __________ of those arguments are passed.

addresses

How many times will the following code print hello? i = 1; while (i <= 8) { printf("hello\n"); }

an infinite number of times

Symbolic constants...

are created by using the #define pre-processor directive

For this question related to your labs select the letter for the best code to fill in the missing blank: void Print2DArray(int array[], int rows, int cols) { for (int x=0; x<rows; x++) { printf("Row %d: ", x); for (int i=0; i<cols; i++) { printf("%02d", ___________); } } }

array[x*cols+i]

Which is not a formatting capability of printf? a.left justification b.centering c.right justification d. aligning a column of numbers so that decimal points appear one above the other. e.controlling whether a + appears next to positive numbers or not

b. centering

A character constant is a(n) __________ value represented as a character in single quotes a.short b.int c.long d.double e.pointer

b.int

The smallest data item in a computer, called a __________________, can assume the value 0 or 1.

bit

5.What will the following code print? #include <stdio.h> #include <malloc.h> int main(void) { int *a=malloc(sizeof(int)*4); a[0]=1; a[1]=2; a[2]=3; a[3]=4; int product=1; for (int i=0;i<4;i++) { product*=a[i]; } printf("product=%d\n",product); } a.product=0 b.product=8 c.product=24 d.Nothing, because there is a compilation error e.An error message, because there is a runtime error

c. product=24

The expression aptr->suit is equivalent to __________. a.aptr.suit b.*aptr.suit c.(*aptr).suit d.*aptr.(suit) e.(&aptr).suit

c.(*aptr).suit

Which of the following is false? a.A string may include letters, digits, and various special characters (i.e., +, -, * ). b.A string in C is an array of characters ending in the null character ('\0'). c.String literals are written inside of single quotes d.A string may be assigned in a definitionto either a character array or a variable of type char *. e.The string "ABCDE" requires space to store exactly 6 characters (bytes).2.

c.String literals are written inside of single quotes

Referencing elements outside the array bounds

can result in changes to the value of an unrelated variable

For this question related to your labs select the letter for the best code to fill in the missing blank: int GetIntegerArray( int * outputIntArray, int maxsize); int main(void) { int a[25]; int count; ________________ }

count=GetIntegerArray(a,25);

How many times will the for loop in the following code iterate? #include <stdio.h> int main(void) {int i; for (i=0;(i>=5);i++) {printf("%d\n",i);}} a. 1 b. 5 c. 6 d. 0 e. Infinite

d. 0

Symbolic constants.. a. can be assigned values in executable statements b. do not have to be initialized when they are defined c. must be evaluated at run-time d. are created by using the #define pre-processor directive e. can be used to specify array sizes, but this makes programs harder to understand

d. are created by using the #define pre-processor directive

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

Although the program might possibly work, what error is lurking in the following code? #include <stdio.h> #include <string.h> #include <malloc.h> #define MYSTRING "Once there was a way" int main(int argc, char *argv[]) { char * pString0 = malloc(4); char * pString1 = malloc(strlen(MYSTRING)); char * pString2 = malloc(4); pString0[0]='A'; pString0[1]='B'; pString0[2]='C'; pString0[3]='\0'; strcpy(pString1,MYSTRING); pString2[0]='D'; pString2[1]='E'; pString2[2]='F'; pString2[3]='\0'; printf("pString0 = %s\n",pString0); printf("pString1 = %s\n",pString1); printf("pString2 = %s\n",pString2); free(pString0); free(pString1); free(pString2); } a.There are no errors. This code is fine. b.Freeing memory after its contents have changed can cause errors. c.Not enough space is being allocated for pString0. d.Not enough space is being allocated for pString1. e.Not enough space is being allocated for pString2.

d.Not enough space is being allocated for pString1.

Which statement is false? a.The programmer must provide any file structure to meet the requirements of each particular application. b.A programmer can impose a record structure on a file. c.A file in C is just an unstructured collection of bytes. d.Records must be written to a C file in order by record key. e.The notion of a record of a file does not exist in C.

d.Records must be written to a C file in order by record key.

Which is not a format control string flag? a.- b.+ c.space d.newline e.#

d.newline

Which of the following is not a standard C formatted stream?a.standard input stream b.standard error stream c.standard output stream d.standard redirection stream e.None of these is a standard C formatted stream

d.standard redirection stream

.Which of the following is not a valid operation on a structure? a.Using the . operator on a variable declared as a structure to access a field in the structure. b.Assigning structure variables to structure variables of the same type. c.Taking the address of a structure variable. d.Using the sizeof operator to determine the size of a structure variable. e.Comparing structures of the same type with relational operators.

e. Comparing structures of the same type with relational operators.

Pointers are variables that contain __________ as their values.

memory addresses

Programs or data not actively being used by the other units are placed on the

secondary storage unit

Every statement in C must end with

semicolon (;)

Which of the following gives the number of elements in the array int r[]?

sizeof (r)/sizeof (int)

The ___________ selection statement performs one of many different actions, depending on the value of an expression.

switch

To prevent modification of array values in a function

the array parameter can be preceded by the const qualifier.

The for statement header for (i = 1; i < 100; ++i) performs the body of the loop for

values of the control variable from 1 through 99 in increments of 1.

A(n) _________________ is a location in memory where a value can be stored by a program.

Variable

If a = -12.0, b = 6.0, and c = 8.0, then what is printed by this code? printf("%.2f\n", sqrt(a + b * c));

6.00

Which line will the following program output? #include <stdio.h> int main(void) { int i=7; while(i-->1) { printf("%d ",i); } }

654321

The __________, or address operator, is a unary operator that returns the address of its operand.

&

If bPtr is assigned b (the name of an array), then array element b[3] can alternatively be referenced with the pointer expression __________.

*(bPtr+3)

How many times will the for loop in the following code iterate? #include <stdio.h> int main(void) { int i=0; for (;i>6;i++) { } }

0

What will the following code print? #include <stdio.h> int GetCount() { static int count=0; return count++; } int main(void) { int i; for (i=0;i<5;i++) { printf("%d ",GetCount()); } }

0 1 2 3 4

What will the following code print? #include <stdio.h> void PrintArrayWorker(int * p, int size) { for (int i=0; i<size; i++) { printf("%d ", *p); p++; } printf("\n"); } void PrintArrayWorker( int array[][3], int rows) { PrintArrayWorker( &array[0][0], rows*3); } int main(void) { int a[2][3]={{1,2,3},{4,5,6}}; PrintArray(a,2); }

1 2 3 4 5 6

One of JimR's daughters is age 16 decimal. What is her age represented as hexadecimal?

10

What will this code print? #include <stdio.h> int main(void) { int zz; for (zz=0;zz<20;zz++) { if (10==zz) break; } printf("%d\n",zz); }

10

Evaluate the expression: 6 + 2 * 3 % 4 + 4 % 5

12

What will this code print? #include <stdio.h> int main(void) { int i; for (i=0;i<20;++i) { if (10==i) continue; } printf("%d\n",i); }

20

Given that k is an integer array starting at location 2000, kPtr is initialized to k, and each integer is stored in 4 bytes of memory, what location does kPtr + 3 point to?

2012

One of JimR's daughters is age 16 hexadecimal. What is her age represented as decimal?

22

Given the following code, what is the value of b[0][1]? int b[2][2] = {1, 3, 4};

3

Assuming that t is an array and tPtr is initialized to t, what expression refers to the address of element 3? A. All of these refer to the address of element 3 B. tPtr[3] C. *(tPtr + 3) D. *(t + 3) E. &t[3]

E. &t[3]

Given the following declarations, which of the following expressions evaluates to 30? int b[] = {10,20,30,40}; int *bPtr = b; A. bPtr[2] B. *(b+2) C. *(bPtr+2) D. b[2] E. All of these evaluate to 30

E. All of these evaluate to 30

Which statement about comments is false? A. Comments do not cause any machine language object code to be generated. B. Programmers insert comments to document programs and improve program readability. C. Comments begin and end with /* and */, respectively. D. Comments can contain special characters such as (, &, *, and / inside the comment block. E. Lengthy comments can cause poor execution-time performance.

E. Lengthy comments can cause poor execution-time performance.

Which statement is false? A. The position number within an array is more formally called a subscript. B. "Array element seven" and the "seventh element of an array" do not mean the same thing. This is a frequent source of off-by-one errors. C. To refer to a particular element in an array, we specify the name of the array and the position number of the element in the array. D. An array may be subscripted by a literal integer. E. The brackets used to enclose the subscript of an array are not an operator in C.

E. The brackets used to enclose the subscript of an array are not an operator in C.

Which statement is false? A. The value of a comma-separated list of expressions is the value of the rightmost expression in the list. B. The comma operator is often used to specify multiple initializations in one particular type of iteration statement. C. Comma operators evaluate lists of expressions from left to right. D. The type of a comma-separated list of expressions is the type of the rightmost expression in the list. E. There is no comma operator in C (a semicolon is used instead).

E. There is no comma operator in C (a semicolon is used instead)

The expression a * (b + c) is equivalent to the C expression A. a * pow(b,c) B. a * b + c C. ab + ac D. (a * b) + c E. a * b + a * c

E. a * b + a * c

Which of the following does not initialize all of the array elements to 0? A. int b[2][2] = {{0,0},{0,0}}; B. int b[2][2]; b[0][0] = b[0][1] = b[1][0] = b[1][1] = 0; C. int b[2][2]; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { b[i][j] = 0; } } D. int b[2][2] = {0}; E. all of these initialize all of their elements to 0.

E. all of these initialize all of their elements to 0.

Which statement about the following code snippet must be true? int x, n[11]; for (x = 1; x < = 10; x++;) { n[x] = 0; }

It contains an off-by-one error

Which of the following is true of this code used by an implementation of Bubble Sort: if (a[I] > a[I + 1]) { a[I] = a[I + 1]; a[I + 1] = a[I]; }

It fails to use a local temporary variable and so will lose values from the array.

What will the following code do? #include <stdio.h> int main(void) { ; }

It will run but do nothing

What will this code print? #include <stdio.h> int main(void) { int x; for (x=0;x<20;x++) { if (x=10) continue; } printf("%d\n",x); }

Nothing there is an infinite loop

Assume the variable grade is a float and has the value of 59.7. What will the following code print? if (grade >= 60) { printf("Passed\n"); }

Nothing will be printed

An identifier's _______________ is where the identifier can be referenced in a program.

Scope

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 values. 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.

If your main function looks as follows and the command line is "./myprog xyz", which of the following is true? int main(int argc, char * argv[]) a. argv[0] = "./a.out" and argv[1] = "xyz" b. argv[1] = "./a.out" and argv[2] = "xyz" c. argc = 1 and argv[1] = "xyz" d. argv[0] = 2 and argv[1] = "xyz" e. argc = 2 and argv[1] = "xyz"

e. argc = 2 and argv[1] = "xyz"

The isxdigit (is hex digit) function would return zero if passed which of the following: a.'a' b.'A' c.'2' d.'F' e.'g'

e.'g'

For this question related to your labs select the letter for the best code to fill in the missing blank: void SumArrays(int array1[], int array2[], int arrayOut[]int size) { ____________________ { arrayOut[I] = array1[i] + array2[i]; } }

for(int i=0; i<size; i++)

The ________ selection statement performs an action if a condition is true and skips that action if the condition is false.

if

A function prototype does not have to ________________.

include parameter names

In C, which statement would be used to define a 10 element integer array c?

int c[10];

Which definition tells the compiler to reserve 12 elements for integer array c?

int c[12];

What will the following code print? #include <stdio.h> int main(void) { int k=0; while (++k<10); printf("k=%d\n",k); }

k=10

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); } }

x=6

What will the following code print? #include <stdio.h> int main(void) { int x=4; { int x=5; { int x=6; { int x=7; } printf("x = %d\n",x); } } }

x=6

What will the following code print? int myfunction(int y) { y++; return y; } int main(void) { int y=7; myfunction(y); printf("y=%d\n",y); }

y=7


Kaugnay na mga set ng pag-aaral

PHED 1164 FINAL EXAM Study Guide

View Set

Solving Absolute Value Equations

View Set

Algebra 1, Practice Problems from Review (MOAKE/HAWKINS)

View Set

Study Questions ServSafe Chapter 5

View Set

FIN-210 Personal Finance Topic 4 SmartBook

View Set

Computer Science 201: Data Structures & Algorithms Ch. 3

View Set