COP 3223 MIDTERM

¡Supera tus tareas y exámenes ahora con Quizwiz!

Which header file should be included to use the pow() function? #include <stdlib.h> #include <limits.h> #include <math.h> #include <stdio.h>

#include <math.h>

Which include directive is needed to use the srand() function in a C program?

#include <stdlib.h>

What is the output of the following program? #include <stdio.h> int main(void) { for(int col=1;col<10;col++){ for(int row=10;row>col;row--){ printf("* "); } printf("\n"); } return 0; }

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

What is the output of the following program? #include <stdio.h> int main(void) { for(int col=1;col<10;col++){ for(int row=1;row<col;row++){ printf("* "); } printf("\n"); } return 0; }

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

What is the output of the following program? #include <stdio.h> int main(void) { for(int col=1;col<10;col++){ for(int row=1;row<5;row++){ printf("* "); } printf("\n"); } return 0; }

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

#include <stdio.h> int main(void) { for(int col=1;col<10;col++){ for(int row=10;row>col;row--){ if(row%2==0) printf("* "); } printf("\n"); } return 0; }

* * * * * * * * * * * * * * * * * * * * * * * * *

The data type of an object in memory determines ......

- what operations that can be performed on it - the set of values it can have - the size of the required memory for the data

What does the following program print? #include <stdio.h> int main( void ) { int count = 1; while ( count <= 5 ) { printf( "%s\n", count % 2 ? "0000" : "1111" ); (++count)+2; } return 0; }

0000 1111 0000 1111 0000

What does the following program print? #include <stdio.h> int main( void ) { int count = 1; while ( count) { printf( "%d\n", count ); count=0; } return 0; }

1

What is the output of the following program? #include <stdio.h> int main( void ) { int count1 = 1, count2 = 2; if ("0") { if (0) {printf( "%d\n", count1+1 );} else {printf("%d\n", count2-1 );} count1=0; } return 0; }

1

Which line number in the following program contains an error? 1. #include stdio 2. 3. // function main begins program execution 4. int main( void ) 5. { 6. printf( "Welcome to C!\n" ); 7. } // end function main

1

Order the following data hierarchy items from smallest to largest: Bit byte word

1 2 3

Identify the order in which the six phases of C programming occur: Edit Preprocess Compile Link Load Execute

1 2 3 4 5 6

What is the output of the following program? #include <stdio.h> int main( void ) { int count1 = 1, count2 = 2; if ("0") { {int count1 =count2+5; count1++; printf( "%d\n", count1+count2 );} } return 0; }

10

What is the output of the following program? 01. #include <stdio.h> 02. int main( void ) 03. { 04. int count = 10; 05. if (2>1) { 06. if (-1) {printf( "%d\n", count+1 );} 07. else {printf("%d\n", count-1 );} 08. count=0; 09. } 10. return 0; 11. }

11

What will be the output of the following snippet if n=1? c) switch ( n ) { case 1: printf( "1" ); case 2: printf( "2" ); break; default: printf( "3" ); break; }

12

What is the output of the following program? #include <stdio.h> // function main begins program execution int main( void ) { printf( "%d", (10%3 +1)); } // end function main

2

How many A's will be printed and how many B's will be printed with the following code snippet? i=0; while (i<5){ printf("A "); i+=3; } while (i<7){ printf("B "); i+=2; }

2 A's and 1 B's

What is the output of the following program? #include <stdio.h> // function main begins program execution int main( void ) { int A=1,B=2,C=3; A=B=C+1; printf( "%d", 2*A+3*B); } // end function main

20

What does the following program do? #include "stdio.h" int main(void) { int i=20; do { if ( i % 2 == 1 ) break; i += 2; printf( "%d ", i ); } while ( i > 50 ); return 0; }

22

What is the output for the following snippet of code? enum week{Mon, Tue, Wed, Thu, Fri, Sat, Sun}; int main(void) { enum week day; day = Thu; printf("%d\n", day); ...

3

What value is printed by the following snippet of code? int a = 2, b = 4; int numberList[]={1,2,3,4,5,6,7,8}; printf("%d\n", numberList[a / b + 2]++);

3

Which line number in the following program contains an error? 1. #include <stdio.h> 2. 3. / function main begins program execution 4. int main( void ) 5. { 6. printf( "Welcome to C!\n" ); 7. } // end function main

3 (comment incorrect format)

What is the output of the following program? #include <stdio.h> int main(void) { int n = 1; { int n = 2; { auto int n = 3; printf("%d ", n); } printf("%d ", n); } printf("%d\n", n); }

3 2 1

What is the output of the following program? #include <stdio.h> int main(void) { int n = 1; { n = 2; { n = 3; printf("%d ", n); } printf("%d ", n); } printf("%d\n", n); }

3 3 3

#include <stdio.h> int main( void ){ int i=2,j=3,x=1; if (i>0) { if(j>i){ x=j; x=j+1; printf("%d", x++); } else{ printf("%d",i); } } return 0; }

4

Count the number of the statements in the following code: #include <stdio.h> // function main begins program execution int main( void ) { int A=1,B=2,C=3; A=B+1; B=A+1; printf( "%d", A+B); } // end function main

4

In which line the first error is appeared (if any)? 1. #include <stdio.h> 2. int main(void) { 3. printf("Welcome. Please enter your Zipcode.\n"); 4. zipcode=32766; 5. int zipcode; 6. if (zipcode > 32715 && zipcode < 3400) 7. printf(" Please Visit our website, next Monday."); 8. return 0; 9. }

4

Which line number in the following program contains an error? 1. #include <stdio.h> 2. 3. // function main begins program execution 4. int main 5. { 6. printf( "Welcome to C!\n" ); 7. } // end function main

4

Given the following array declaration, what is the value at the 4th subscript of the array? int numberList[]={1,2,3,4,5,6,7,8};

5

What line below will generate a compile-time error? 1. #include <stdio.h> 2. 3. void func(const int num[]) 4. { 5. num[0] = 2; 6. } 7. 8. int main(void) 9. { 10. int num[] = {0 ,1 ,2}; 11. 12. func(num); 13. 14. printf("%d\n", num[0]); 15. return 0; 15. }

5

Which line number in the following program contains an error? 1. #include <stdio.h> 2. 3. // function main begins program execution 4. int main( void ) 5. { 6. printf( "Welcome to C!\n" ); 7. )

7

What is output value of f(a[2]) in the following code? void myFunc(int a[], int s); int main(void) { int a[] = {1,2,3,4,5,6,7,8,9,10}; myFunc( a, 10 ); printf("The value of f(a[2]) is: %d\n", a[2] ); return 0; } void myFunc(int a[], int s) { a[2] *= 2; }

6

What is the smallest value of b that can be used in the following array declaration? char[b] = "Hello";

6

Which line number in the following program contains an error? 1. #include <stdio.h> 2. 3. // function main begins program execution 4. int main( void ) 5. { 6. int x=2, 7. printf( "Welcome to C!\n" ); 8. }// end function main

6

Which line number in the following program contains an error? 1. #include <stdio.h> 2. 3. // function main begins program execution 4. int main( void ) 5. { 6 printf( "%d"); 7. } // end function main

6

Which line number(s) in the following program contain(s) an error? (Multiple answer) 1. #include <stdio.h> 2. 3. // function main begins program execution 4. int main( void ) 5. { 6. int x=2; y=3, z=x+y; 7. scanf("d%",&y); 8. printf( "Welcome to C!\n" ); 9. }// end function main

6, 7 (added semicolon in statement, should be %d)

Which line number(s) in the following program contain(s) an error? (Multiple answer) 1. #include <stdio.h> 2. 3. // function main begins program execution 4. int main( void ) 5. { 6. int x=2; y=3, z=x+y; 7. 8. Printf( "Welcome to C!\n" ); 9. }// end function main

6, 8 (semicolon in middle of statement, capital p)

What is the output of the following code: #include <stdio.h> int main( void ) { int x=1; printf(("%d"),x+(++x)+(++x)); return 0; }

7

What is the output of the following program? #include <stdio.h> // function main begins program execution int main( void ) { int A=1,B=2,C=3; A=B+1; B=A+1; printf( "%d", A+B); } // end function main

7

Which line number in the following program contains an error? 1. #include <stdio.h> 2. 3. // function main begins program execution 4. int main( void ) 5. { 6. int x=2, y=3, z=x+y; 7. scanf("%d","y"); 8. printf( "Welcome to C!\n" ); 9. }// end function main

7 (forgot the &)

How many bits (binary digits) are there in one byte?

8

What is the output of the following program? #include <stdio.h> // function main begins program execution int main( void ) { int A=1,B=2,C=3; A=B=C+1; printf( "%d", A+B); } // end function main

8

Which line number in the following program contains an error? 1. #include <stdio.h> 2. 3. // function main begins program execution 4. int main( void ) 5. { 6. int x=2, y=3, z=x+y; 7. 8. printf( "Welcome to C!\n ); 9. }// end function main

8

What value is output by the following code snippet? int number[] = {1,2,3,4,5,6,7,8,9,10}; int value; value = ++number[1 + 2 * 3]; printf("The name entered is: %d\n", value);

9

Which operator listed below has right to left associativity? = * % <=

=

Which operator listed below has the lowest precedence? * ++ (postfix) ?: +

?:

What does the following program do? #include <stdio.h> int main( void ) { int x; int y; int i; int j; printf( "Enter two integers in the range 1-20: " ); scanf( "%d%d", &y, &x ); for ( i = 1; i <= y; i++ ) { for ( j = 1; j <= x; j++ ) { printf( "*" ); } printf( "\n" ); } return 0; }

A square of * (y rows, x columns)

Which phase in C program development is responsible for translating the text of a C program into machine language-code?

Compile

Match the following data type as primitive or composite: Arrays

Composite

Match the following data type as primitive or composite: String

Composite

A __________ function is a function that calls itself either directly or indirectly.

Recursive

Which type of error is a run-time error or execution error, and is a fatal error that usually aborts the running program?

Divide by zero

What will be the output of the following snippet if value=101? switch ( value % 2 ) { case 0: printf( "Odd integer\n" ); case 1: printf( "Even integer\n" ); }

Even integer

T/F If an array is declared of size 10 as follows, name[10], then the array bounds checking of the C programming language will prevent a program from referring to an element that does not exit such as the 12th element.

False

T/F Only iteration can result in an infinite loop. Recursion can never result in an infinite loop since it has a base case.

False

T/F The compiler will always store variables with the "register" storage class in a register of the CPU or cache memory.

False

TRUE OR FALSE: Write a statement or a set of statements to accomplish the following tasks: Calculate the value of 2.5 raised to the power of 3 using the pow function. Print the result with a precision of 2 in a field width of 10 positions. Answer: Printf("%10.2f", pow(3,2.5));

False

The break statement is required in the default case of a switch selection statement. True False

False

The default case is required in the switch selection statement. True False

False

True or False "Write a statement or a set of statements to sum the odd integers between 1 and 99 using a for statement." Answer: for(int i=1;i<=99;i++) sum+=2i+1;

False

What is the output of the below code snippet? #include<stdio.h> main() { for(;;)printf("Hello"); }

Infinite loop

What is the output of the following snippet? For ( x = 100, x >= 1, x++ ) printf( "%d\n", x );

Infinite loop

Identify the following computer devices as either input or output units: Keyboard

Input

Identify the following computer devices as either input or output units: Mouse

Input

Which phase in C program development takes the executable image from disk and transfers it to memory?

Load

What type of programming language generally uses only 0's and 1's for writing programs?

Machine Language

Which logical unit of computer architecture typically stores information for rapid access and generally contains volatile information that is lost when the computer is powered off?

Memory Unit

Identify the following computer devices as either input or output units: Monitor

Output

Identify the following computer devices as either input or output units: Printer

Output

Match the following data type as primitive or composite: Float

Primitive

Match the following data type as primitive or composite: Integer

Primitive

Structured programming has shown that all C programs can be written using what forms of control? Sequence Iteration Selection Goto

Sequence Iteration Selection

What is the output value of following code? void myFunc(int a); int main(void) { int a[] = {1,2,3,4,5,6,7,8,9,10}; myFunc( a[2] ); printf("The value of f(a[2]) is: %d\n", a[2] ); return 0; } void myFunc(int e) { e = e * 2; }

The value of f(a[2]) is: 3

What does the following program print? 1. #include <stdio.h> 2. int main( void ) 3. { 4. int x = 1, 5. total = 0, y; 6. while ( x <= 3 ) { 7. y = x * x; 8. total += y; 9. ++x; 10. } 11. printf("Total is %d\n", total); 12. return 0; 13. }

Total is 14

T/F All arguments in C are passed by value.

True

T/F In the C programming language, arrays are stored in contiguous memory locations.

True

T/F Iteration and recursion can occur infinitely: An infinite loop occurs with iteration if the loop continuation test never becomes false; infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on the base case.

True

T/F Iteration and recursion each involve a termination test: Iteration terminates when the loop-continuation condition fails; recursion terminates when a base case is recognized.

True

T/F: You use blank lines, space characters and tab characters (i.e., "tabs") to make programs easier to read.

True

What is the output of the following code. 1. #include <stdio.h> 2. 3. // function main begins program execution 4. int main( void ) 5. { 6. Printf( "Welcome to" );Printf( "C!\n" ); 9. }// end function main

Welcome toC!

Can a main() be a recursive function, that is to say can main() call itself?

Yes

Can the C programming language be used to with 3-dimensional and higher order arrays? int array1[10][5][2]; int array2[10][5][2][12];

Yes

Is the following code snippet valid syntax? int array[5]; array[0] = 1; array[1] = 'A'; printf("%d\n", array[0]); printf("%d\n", array[1]);

Yes

char string1[] = "Hello"; char string2[] = {'H', 'e', 'l', 'l', 'o', '\0' }; Does string1 contain the same value as string2?

Yes

What is the output of the following program? #include <stdio.h> // function main begins program execution int main( void ) { printf( "\\Welcome to \"C!\"\\" ); } // end function main

\Welcome to "C!"\

Which variable from the code snippet below is responsible for shifting the value (i.e,. the first number in the desired range of the consecutive integers) of the random number being generated? int a,b,n; n = a + rand() % b;

a

If the array myArray[a][b] is used to represent a table of rows and columns, which subscript would typically be used for the columns?

b

Which variable from the code snippet below is responsible for scaling the value (i.e,. determining the desired range of the consecutive integers) of the random number being generated? int a,b,n; n = a + rand() % b;

b

Which of the following are not storage class specifiers? a) auto b) block c) register d) file e) extern f) static

block file

What is the scope of the variable a in the following program? #include <stdio.h> int cube(int n); int b = 10; int main(void) { int a = 10; printf("%d\n", cube(a)); } int cube(int x) { int y = x * x * x; return y; }

block scope

What is the scope of the variable x in the following program? #include <stdio.h> int cube(int n); int b = 10; int main(void) { int a = 10; printf("%d\n", cube(a)); } int cube(int x) { int y = x * x * x; return y; }

block scope

What is the output of the following program if the user enter 25, (age =25). #include <stdio.h> int main( void ) { int age, ageRange; printf(" Please enter your age to see the consered discount for you. "); scanf("%d", &age); if (age<18) ageRange=1; else if (age>=18 && age< 30) ageRange=2; else if (age>=30 && age<50) ageRange=3; else if (age>=50 && age<65) ageRange=4; else ageRange=5; switch(ageRange){ case 1: printf("discount 10%\n"); break; case 2: printf("discount 15%\n"); case 3: printf("discount 20%\n"); break; printf("discount 25%\n"); case 4: default: printf("discount 50%\n"); break; } return 0; }

discount 15% discount 20%

What is the output of the following program? #include<stdio.h> main() { int i = 1; printf("%d ",i++); if(i==3) break; if(i<=5) continue; }

error

What will be the output of the following snippet ? int counter=20; Do { if ( counter % 2 == 0 ) { printf( "%d,", counter ); } counter += 2; } while ( counter > 100 );

error

Which of the following are types of scope that an identifier can have? a) file b) block c) register d) function e) extern f) static

file block function

Which of the following are valid array names? a) float array[10]; b) int long[10]; c) char money_int[10]; d) int 4cost[10]; e) int freq2run[10];

float array[10]; char money_int[10]; int freq2run[10];

What is the scope of the identifier here on line 10 of the program below? 1) #include <stdio.h> 2) 3) int main(void) 4) { 5) int a = 5; 6) // Don't write code like this! 7) goto here; 8) a = a * 2; 9) 10) here: 11) a = a * 3; 12) 13) printf("Value of a: %d\n", a); 14) }

function scope

Which is the only type of scope that can be assigned to a label?

function scope

Which one of the followings is a keyword in C? (multiple answer) integer {} int void iff

int void

y=++x

is a short cut for x=x+1;y=x; x is evaluated after it is incremented

y=x++

is a short cut for y=x;x=x+1; x is evaluated before it is incremented

What will be the output of the following program ? #include "stdio.h" int main(void) { int i=21; do { printf( "%d ", i ); i += 1; if ( i % 2 == 1 ) continue; } while ( i< 100 ); return 0; }

list of integers from 21 to 99 (inclusive)

Which of the following identifier names are invalid? long Variable_one total$amount 10sdigit aReallyLongVariableNamedVar

long total$amount 10sdigit (cant start with digit, cant have other symbol, long is reserved)

Which of the following are reserved keywords and can not be used for identifier names? long while ifelse number count

long while

Which output would be reasonable for the following code snippet? char str[] = "Hello"; printf("%c ", str[4]); printf("%s ", str); printf("%p\n", str);

o Hello 0x7ffe99be308a

Write a single C statement to accomplish the following: Assign the sum of x and y to z and increment the value of x by 1 after the calculation.

z=(x++)+y;

Which of the following is a logical OR operator? && || & |

||


Conjuntos de estudio relacionados

Square Roots and Irrational Numbers

View Set

Chapter 10 - Organizational Design and Control

View Set