Programming Final Questions

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

What is the output of this simple program with a "for" loop? #include <stdio.h> int main(void) { int i = 0; for (i = 1; i < 6; i++){ printf("%i ", i - 1); } }

0 1 2 3 4

Assume the correct code snippet: char vowels[] = { 'a', 'e', 'i', 'o', 'u' }; char *alphaPtr = vowels; alphaPtr += 2; printf ( "%c ", *(alphaPtr - 1) ); What will be the value printed out by the above printf() statement:

e

What is the output of this simple program with a "for" loop? #include <stdio.h> int main(void) { int x = 0; for (x = 0; x < 5; x++) { printf("%i ", x); } }

0 1 2 3 4

Accessing an array element whose subscript is out of range: (T/F) 1. will cause a compiler error 2. may cause a compiler error 3. will cause a runtime error 4. may cause a runtime error

1. F 2. F 3. F 4. T

The amount of memory allocated for an array depends on: (T/F) 1. the type of the elements 2. the number of elements 3. the initial values of the elements

1. T 2. T 3. F

What is true about do-while loops? (T/F) 1. the code in the body of a do-while loop is guaranteed to execute at least once 2. the do-while loop typically has the same three things that other loops have: a loop control variable declared and initialized before the loop, loop control variable that is updated somewhere in the loop, a loop condition that is evaluated after the execution of the loop 3. just as with for loops and while loops, no semi-colon is required at the end of the loop code, i.e. after the condition 4. a do-while loop is good choice for repeating a block of code and can be used in place of a for loop or a while loop in any situation

1. T 2. T 3. F 4. F

What will be printed to the screen? float pi = 3.14159265; printf("%.2f", pi);

3.14

Show what will be printed by the printf() statement: int g = 13, h = 2; printf("%.3f", (float)g / h);

6.500

Show what will be printed by the printf() statement: int t = 10, u = 2, v = 4, w = 3; printf("%d", (t * (u + v) + w) );

63

If x and y are declared this way: _Bool x = 0, y = 1; what value would this expression have? !(y && (y && !x))

False

What is the output, if any, of this simple program with a "while" loop? #include <stdio.h> int main(void) { int i = 4; while (i < 0) { i--; printf("%i ", i); } }

No output

Assume the correct code snippet: char vowels[] = { 'a', 'e', 'i', 'o', 'u' }; char *alphaPtr = vowels; *alphaPtr += 2; printf ( "%c ", *(alphaPtr + 3) ); What will be the value printed out by the above printf() statement:

o

Assume the correct code snippet: int num1; int *ptr1 = &num1; int *ptr2; What would you type to assign ptr2 to point to what ptr1 is pointing to? A. ptr2 = ptr1; B. ptr2 = num1; C. ptr2 = *ptr1; D. ptr2 = &ptr1;

A

If a and b are declared this way: int a = 100, b = -8; what value would this expression have? (a <= -3) || !(b >= 0)

True

Static, local variables (T/F): 1. are declared inside functions 2. are initialized once 3. may be accessed from other functions 4. retain their values across function invocations

1. T 2. T 3. F 4. T

An array declaration: (T/F) 1. may use a variable to specify the size of the array 2. must specify the type of the elements 3. must specify the values of all the array elements 4. must specify the size of the array if the values of the elements are not provided 5. may leave the size of the array unspecified if the values of the elements are provided 6. must specify the values of either all elements or no elements 7. may allow the values of some elements to remain unspecified

1. T 2. T 3. F 4. T 5. T 6. F 7. T

Show what will be printed by the printf() statement: int b = 10, c; c = ++b; printf("%d", c);

11

Show what will be printed by the printf() statement: int w = 3, x = 3, y = 12, z = 8; printf("%d", z % x + y / x * w);

14

Trace the execution of the following fragment and show what will be printed out to standard output. int m = 10, n = 5; int *mp, *np; mp = &m; np = &n; *mp = *mp + *np; *np = *mp - *np; printf("%d %d %d %d \n", m, *mp, n, *np);

15 15 10 10

Show what will be printed by the printf() statement: int a = 9; printf("%d", a -= 7);

2

Show what will be printed by the printf() statement: int i = 17, j; j = i % 3; printf("%d", j);

2

Show what will be printed by the printf() statement: int p = 7, q = 3; printf("%d", p % q + 1);

2

What is the output of the program below? #include <stdio.h> int main(void) { int i, j; for (i = 0; i <= 4; i++) { for (j = 0; j < 5; j++) printf("*"); printf("\n"); } }

5 rows of 5 *

Large programs with many functions can become unwieldy. Dividing programs into multiple files of related functions promotes re-use of the functions and can reduce the time needed to re-compile the program. Imagine that you have a program with a main method, a collection of functions that read and write records to or from a database, and a collection of functions that perform statistical calculations on those records. You break the program into three implementation files: main.c , db.c and stats.c and create two header files: db.h and stats.h. Which of the following is the proper way to include the stats.h file in the other files that need it? A. #include "stats.h" B. include <stats.h> C. #include <stats.h> D. include "stats.h"

A

A high-level language differs from a low-level language in that: A. Statements in a high-level language correspond directly to assembly language instructions B. Statements in a low-level language correspond directly to machine instructions C. Statements in a low-level language are closer to natural language D. Statements in a high-level language correspond directly to machine instructions

B

What is the output of the program below? #include <stdio.h> int main(void) { int x = 3; int y = 2; if ( (x < y) || (x = 2) ) printf("Success! "); else printf("Failure. "); }

Failure.

What is the output of the program below? #include <stdio.h> int main(void) { int i, j; for (i = 1; i <= 5; i++) { for (j = 0; j <= 4; j++) printf("*"); } printf("\n"); }

One row of 25 *

What is the second argument to the scanf() function below supposed to be? int inputAge; printf("Enter your age as an integer: "); scanf("%d", );

&inputAge

What is the output of this simple program with a "for" loop? #include <stdio.h> int main(void) { int i = 0; for (i = 0; i <= 3; ++i) { printf("%i", i); } printf(" - "); for ( ; i <= 5; ++i) { printf("%i", i + 1); } }

0123 - 56

The following short program contains 5 (6) errors that will not compile. Which line numbers contain the errors? 1. #include stdio.h 2. int main (void); { 3. int input, result; 4. printf("Enter an integer: '); 5. scanf("%d", input); 6. result = input - 10: 7. // display value 8. printf("\n The result is %i \n\n", result + 5): 9. return 0; }

1, 2, 4, 5, 6, 8

Assume the correct code snippet: char word[] = {'t','e','s','t','\0'}; and the function setX, as defined here: void setX(char aString[], int position){ aString[position] = 'X'; } Which of the following function calls results in contents of the word array updating from "test" to "teXt"? (T/F) 1. setX(aString, 3); 2. setX(word, position); 3. aString = setX(word, 2); 4. setX(word, 2);

1. F 2. F 3. F 4. T

Assume the array declaration: char theString[] = {"Tigers are #1 in ACC!!"}; Assume there exists a function called my_strlen that takes in a string and returns an integer result. Which of the following code snippets would correctly call this function to find the length of theString? (T/F) 1. int len = my_strlen(theString[]); 2. int len = int my_strlen(char theString[]); 3. int len = my_strlen(theString); 4. my_strlen(theString[]);

1. F 2. F 3. T 4. F

Which of the following code snippets correctly creates a function prototype for the following function called areaOfTriangle that takes two integers, base and height, and returns a floating-point result? (T/F) 1. void areaOfTriangle(int, int); 2. floating-point areaOfTriangle(int base, int height); 3. float areaOfTriangle(int, int); 4. float areaOfTriangle(int base, int height);

1. F 2. F 3. T 4. T

Large programs with many functions can become unwieldy. Dividing programs into multiple files of related functions promotes re-use of the functions and can reduce the time needed to re-compile the program. Imagine that you have a program with a main method, a collection of functions that read and write records to or from a database, and a collection of functions that perform statistical calculations on those records. You break the program into three implementation files: main.c , db.c and stats.c and create two header files: db.h and stats.h. What is the command needed to compile all of these files into a single executable named db_ops? (T/F) 1. gcc main.c db.c stats.c db_ops.c 2. gcc -o db_ops main.c db.c stats.c 3. gcc db_ops.c main.c db.c stats.c stats.h db.h 4. gcc main.c db.c db.h stats.c stats.h db_ops

1. F 2. T 3. F 4. F

Local variables (T/F): 1. are declared outside any function 2. are created each time a function is called 3. retain their values across function invocations 4. always have a default initial value of zero

1. F 2. T 3. F 4. F

Assume the correct code snippet: char word[10]; Which of the following code snippets results in the contents of the word array being initialized to some string? (T/F) 1. word = "Clemson"; 2. strcpy(word, "Tigers"); 3. scanf("%s", &word); 4. word[0]='H'; word[1]='i'; word[2]=' \0';

1. F 2. T 3. F 4. T

Global variables (T/F): 1. are declared in the main function 2. may be initialized outside any function 3. may be accessed from any function 4. are considered good programming style and better than using arguments to functions

1. F 2. T 3. T 4. F

consider the code below and show which of the following are valid prototypes for the add function (Select all correct responses): #include <stdio.h> int add(int val, int inc) { val = val + inc; return val; } int main() { int x = 4; int y = 2; y = add(x, 5); printf("x=%d, y=%d \n", x, y); } 1. int add(int val, int inc); 2. void add(int x, int y); 3. int add(int, int); 4. void add(int, int);

1. T 2. F 3. T 4. F

Large programs with many functions can become unwieldy. Dividing programs into multiple files of related functions promotes re-use of the functions and can reduce the time needed to re-compile the program. Imagine that you have a program with a main method, a collection of functions that read and write records to or from a database, and a collection of functions that perform statistical calculations on those records. You break the program into three implementation files: main.c , db.c and stats.c and create two header files: db.h and stats.h. What would make sense for the header file stats.h to contain? (T/F) 1. function prototypes for the function implementations in stats.c 2. function prototypes for the function implementations in db.c 3. #define statements 4. #include statements

1. T 2. F 3. T 4. T

Please select all correct statements about functions and function prototypes from the choices below: (T/F) 1. The values that are passed in a function call are known as arguments 2. A formal parameter is the name of a parameter in a function header 3. Local variables defined inside a function always retain their values over subsequent calls to the function 4. C functions can return more than one values 5. A void return type indicates that no value is returned 6. A function will not return control back to the function where it was called from if the return statement is omitted causing the program to hang 7. To pass an array to a function, use the name of the array and the size of the array in brackets, e.g. "some_function(array[20]);" to pass an array of size 20 to some_function 8. "int minimum(int values[], int numElements); " is a valid function prototype

1. T 2. T 3. F 4. F 5. T 6. F 7. F 8. T

Functions of the operating system include: (T/F) 1. managing computer's input devices, such as keyboard and mouse 2. managing computer's resources, such as central processing unit and memory 3. providing a user interface 4. running applications

1. T 2. T 3. T 4. T

Referring to the block of code below where the sorting takes place, what is the approximate maximum number of times that statement "tmp = nums[i];" might execute for an array of size 5 (worst case scenario)? #include <stdio.h> #define SIZE 5 int main() { int i, j, tmp; int nums[SIZE] = {2, 3, 4, 1, 0}; for (i = 0; i < SIZE - 1; i++) { for (j = i + 1; j < SIZE; j++) { if (nums[i] < nums[j]) { tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } } } for (i = 0; i < SIZE; i++) printf("%d ", nums[i]); printf("\n"); return 0; }

10

Show what will be printed by the printf() statement: int r = 9, s; s = r++ + 2; printf("%d", s);

11

What is the output of this simple program with a "for" loop? #include <stdio.h> int main(void) { int i = 0; for ( ; i < 5; i = i + 1) { printf("%i", i + 1); } }

12345

consider the code below to determine what the expected output would be: #include <stdio.h> #define SIZE 5 int main() { int i, j, tmp; int nums[SIZE] = {2, 3, 4, 1, 0}; for (i = 0; i < SIZE - 1; i++) { for (j = i + 1; j < SIZE; j++) { if (nums[i] < nums[j]) { tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; } } } for (i = 0; i < SIZE; i++) printf("%d ", nums[i]); printf("\n"); return 0; }

4 3 2 1 0

Show what will be printed by the printf() statement: int m = 12, n = 4; printf("%d", (-m / n + n * m + m % n));

45

Assume the correct code snippet: int num1 = 3; int *ptr1 = &num1; *ptr1 = 8; num1 = 5; What would be printed after this statement: printf( "%i ", *ptr1 );

5

Show what will be printed by the printf() statement: int d = 8, e = 4, f = 2; printf("%d", ((f + 2) * e) / (30 / d) );

5

Consider the code in the example below. What is the size of the array string2, in bytes? #include <stdio.h> #include <string.h> void sort(int[], int size); int main(int argc, char * argv[]) { char string1[] = "Tigers"; char string2[] = { 'T', 'i', 'g', 'e', 'r', 's' }; char string3[10] = "Tigers"; if (string1 == string2) printf("string1 == string2 \n"); else printf("not == \n"); printf("\n"); return 0; }

6

Assume the correct code snippet: int num1 = 3; int *ptr1 = &num1; int *ptr2 = &num1; num1 = 5; *ptr1 = 7; What will be the value printed after this statement: printf( "%i ", *ptr2 );

7

Consider the code in the example below. What is the size of the array string1, in bytes? #include <stdio.h> #include <string.h> void sort(int[], int size); int main(int argc, char * argv[]) { char string1[] = "Tigers"; char string2[] = { 'T', 'i', 'g', 'e', 'r', 's' }; char string3[10] = "Tigers"; if (string1 == string2) printf("string1 == string2 \n"); else printf("not == \n"); printf("\n"); return 0; }

7

Assume the correct code snippet: int num1 = 3; int *ptr1 = &num1; num1 = 4; *ptr1 = 8; What would be printed after this statement: printf( "%i ", num1 );

8

Assume the correct code snippet: char vowels[] = { 'a', 'e', 'i', 'o', 'u' }; char *alphaPtr = vowels; *(alphaPtr + 2) += 1; What effect will the above line of code have on the array? A. The pointer will stay where it is, but the 2nd item in the array will change from an e to g B. The pointer will stay where it is, but the 3rd item in the array will change from an i to j C. It will move the pointer to the 2nd item in the array and change the e to g D. It will move the pointer to the 3rd item in the array and change the i to j

B

There are steps to program development, beginning with creating an algorithm and then opening up an editor and typing up code. Which of the following show the rest of the steps in the correct order: A. pre-processor, linker, loader, assembler B. linker, assembler, pre-processor, loader C. assembler, loader, linker, pre-processor D. pre-processor, assembler, linker, loader

D

What is the output, if any, of this simple program with a "while" loop? #include <stdio.h> int main(void) { int i = 10; while (i <= 15) { printf("%i ", i); } }

Infinite output

char inputChar = 'P'; printf("%c", inputChar + 1); What will print out?

Q

What is the output of the program below? #include <stdio.h> int main(void) { int x = 3; int y = 2; if ( (x > y) && !(x == 2) ) printf("Success! "); else printf("Failure. "); }

Success!

If s and t are declared this way: int s = 4, t = 12; what value would this expression have? !( !(s >= -3) && (t <= 0) )

True

What is the output of the program below? #include <stdio.h> int main(void) { int x = 5; int y = 7; if ( (x < y) && (x == 10) ) printf("Success! "); printf("Twice!! "); }

Twice!!

What is the output, if any, of this simple program with a "while" loop? #include <stdio.h> int main(void) { int counter = 1; char symbol = 'a'; while (counter < 6) { printf("%c ", symbol); counter++; } printf("\n"); }

a a a a a

Assume the correct code snippet: char vowels[] = { 'a', 'e', 'i', 'o', 'u' }; char *alphaPtr = vowels; *alphaPtr += 2; printf ( "%c ", *alphaPtr); What will be the value printed out by the above printf() statement:

c

Consider the code in the example below. What is the output of the program? #include <stdio.h> #include <string.h> void sort(int[], int size); int main(int argc, char * argv[]) { char string1[] = "Tigers"; char string2[] = { 'T', 'i', 'g', 'e', 'r', 's' }; char string3[10] = "Tigers"; if (string1 == string2) printf("string1 == string2 \n"); else printf("not == \n"); printf("\n"); return 0; }

not ==

consider the code below and show the expected output: (Choose the best response.) #include <stdio.h> int add(int val, int inc) { val = val + inc; return val; } int main() { int x = 4; int y = 2; y = add(x, 5); printf("x=%d, y=%d \n", x, y); }

x=4, y=9


Kaugnay na mga set ng pag-aaral

Commercial Chapter 1 - Basic Aerodynamics

View Set

Astronomy Module 13, Astronomy, Exam 3

View Set

Quiz 1: The Cell and the Microscope

View Set

business of tourism and hospitality

View Set

Life Insurance Exam Simulation Missed Questions

View Set