Clemson CPSC 1010 Final Study

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

int i1 = 4, i2; int *p1, *p2; p1 = &i1; i2 = *p1 / 2 + 6; p2 = p1; After the above lines of code execute, what would be printed with this statement: printf( "%i", i2 );

8

This person from the 1800s is considered to have written the "first computer program". There is a computer programming language named after this person. Grace Murray Hopper Ada Lovelace Charles Babbage

Ada Lovelace

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? A. strcpy("Tigers", word); B. scanf("%s", &word); C. word[0]='H'; word[1]='i'; word[2]=' \0'; D. word = "Clemson";

C. word[0]='H'; word[1]='i'; word[2]=' \0';

These are the (low-level) types of instructions that, starting in the early 1950s, allowed the programmer to use a somewhat more English-like way of writing instructions by using abbreviations, or mnemonic codes, to refer to the bit-patterns. high-level language instructions machine language instructions assembly language instructions pseudo-code instructions

assembly language instructions

typedef struct { typedef struct student { char month[4]; char firstName[15]; int day, year; char lastName[20]; } dateOfBirth; int cuid; dateOfBirth dob; struct student* next; } student_t; How would you assign to the 3rd element of the array cpsc1010 the student1 student already declared ?

cpsc1010[2] = student1;

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

no output

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

o

typedef struct { typedef struct student { char month[4]; char firstName[15]; int day, year; char lastName[20]; } dateOfBirth; int cuid; dateOfBirth dob; struct student* next; } student_t; How would you declare an array of 45 students called cpsc1010 ?

student_t cpsc1010[45];

Use the structure below to answer the following questions: typedef struct { typedef struct student { char month[4]; char firstName[15]; int day, year; char lastName[20]; } dateOfBirth; int cuid; dateOfBirth dob; struct student* next; } student_t; How would you declare a student called student1 ?

student_t student1;

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

t

True/False: Automatic local variables - commonly just called local variables Retain their values across function invocations

False

True/False: Global variables Are considered good programming style and better than using arguments to functions

False

True/False: Global variables Are declared in the main function

False

True/False: It is a low-level programming language

False

True/False: Static, local variables May be accessed from other functions

False

True/False: An array declaration: Must specify the type of the elements

True

True/False: Automatic local variables - commonly just called local variables Are created each time a function is called

True

True/False: Global variables May be accessed from any function

True

True/False: Global variables May be initialized outside any function

True

True/False: It is a procedural programming language

True

True/False: It was created in the late 1960s.

True

True/False: Static, local variables Are declared inside functions

True

True/False: Static, local variables Are initialized once

True

True/False: Static, local variables Retain their values across function invocations

True

True/False: Unix was re-written in C in the early 1970s

True

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

Twice!!

What is the following binary number in octal? 101011111

537

How to include stats.h header file in the other files that need it?

#include "stats.h"

#include <stdio.h> int main(void) { int counter = 6; char symbol = 'l'; while (counter > 0) { printf("%c ", symbol--); counter--; } printf("\n"); }

l k j i h g

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

0101010

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

1 2 3 4 5

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

1, 4, 6, 7, 11

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

10

What is the following hexadecimal number in binary? A34D

1010001101001101

#include <stdio.h> #include <string.h> int main() { char word1[8] = "Clemson"; char word2[8] = {'C', 'l', 'e', 'm', 's', 'o', 'n', '\0'}; char word3[20]; if (word1 == word2) printf("word1 and word2 are the same\n"); else printf("word1 and word2 are not the same\n"); if (strcmp(word1,word2) == 0) printf("word1 and word2 are the same\n"); elseprintf("word1 and word2 are not the same\n"); strcpy(word2, "Tigers");strcpy(word1, "Go "); strcat(word3, word1); strcat(word3, word2); strcpy(word1, "!"); strcat(word3, word1); printf("\n%s\n", word3); printf("strlen(word3) is %d\n", (int)strlen(word3)); return 0;} By the end of the program, before the return 0; statement, how many total characters in all are in the word3 array?

11

After the following code executes, what would the value of nums[3] be? int nums[10], i = 0; while (i < 10) { nums[i] = (i + 20) % 3; i++; }

2

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

2

What will print by this printf() statement: int d = 4, e = 3, f = 2; printf("%d", ((-f + 4) * e) / (13 / d));

2

#include <stdio.h> #define SIZE 5 int main() { int i, j, tmp; int nums[SIZE] = {4, 7, 8, 2, 6}; 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;} What are the contents of the array after just the first iteration (or pass, or all the way through the inner for loop)?

2 7 8 4 6

What is the following binary number in decimal? 10111

23

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

3.00

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

3210 - -10123

int i1 = 4, i2; int *p1, *p2; p1 = &i1; i2 = *p1 / 2 + 6; p2 = p1; After the above lines of code execute, what would be printed with this statement: printf( "%i", *p2 );

4

#include <stdio.h> void addValue() { static int y = 3; y = y + 2;printf("%d-", y); }int main() { int y = 4; printf("%d-", y); addValue(); printf("%d-", y); y += 5;addValue(); printf("%d", y); // no dash after the value return 0;}

4-5-4-7-9

#include <stdio.h> void test1(int *param) { *param += 30;}int test2(int param) { param += 8;return param; } int main() { int num = 12; int *p = &num; test1(p); printf("%d", num); // This question - what value prints here? *p = test2(num);printf("%d", num); return 0; }

42

#include <stdio.h> void test1(int *param) { *param += 30;}int test2(int param) { param += 8;return param; } int main() { int num = 12; int *p = &num; test1(p); printf("%d", num); *p = test2(num);printf("%d", num); // This question - what value prints here? return 0; }

50

#include <stdio.h> int main(void) { int x = 4; int y = 7; if ( (x > y) || !(x = 2) ) printf("Success! "); else printf("Failure. "); }

Failure.

True/False: If you were passing an array that was declared to be of size 10 to a function called some_function, you would pass it this way: some_function(array[10]);

False

True/False: A linked list Can randomly (directly) access data values just like with arrays

False

True/False: A linked list Is a data structure consisting of a group of nodes that are contiguously right next to each other in memory

False

True/False: A linked list Is a linear collection of data elements in which order is not given by their physical placement in memory

False

True/False: An array declaration: Must specify the values of all the array elements

False

True/False: An instruction from the C programming language corresponds one-to-one with the instructions that are contained in the instruction set for that particular machine (processor).

False

True/False: Automatic local variables - commonly just called local variables Are declared outside any function

False

True/False: Automatic local variables - commonly just called local variables May be accessed from any function

False

#include <stdio.h> #include <string.h> int main() { char word1[8] = "Clemson"; char word2[8] = {'C', 'l', 'e', 'm', 's', 'o', 'n', '\0'}; char word3[20]; if (word1 == word2) printf("word1 and word2 are the same\n"); else printf("word1 and word2 are not the same\n"); if (strcmp(word1,word2) == 0) printf("word1 and word2 are the same\n"); elseprintf("word1 and word2 are not the same\n"); strcpy(word2, "Tigers"); strcpy(word1, "Go "); strcat(word3, word1); strcat(word3, word2); strcpy(word1, "!"); strcat(word3, word1); printf("\n%s\n", word3); // What is the output of this print statement? printf("strlen(word3) is %d\n", (int)strlen(word3)); return 0;}

Go Tigers!

char letters[] = { 'a', 'e', 'i', 'o', 'u' }; char *alphaPtr = letters; *(alphaPtr + 1) += 3; What effect will the above line of code have on the array?

It will move the pointer to the 2nd item in the array and change the e to h

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

Success!

Review Questions about 21-30 on Test 2

Too long to make quizlet

If s and t are declared this way: int s = 8, t = -2; what value would this expression have? !( !(s <= -3) && !(t <= 0) ) (True/False)

True

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

True

True/False: A linked list Can be used as an alternative to an array

True

True/False: A linked list Can grow and shrink as necessary much easier than arrays

True

True/False: A linked list Uses structures that contain a data member, which is a pointer of the same type of struct

True

True/False: A program written in C must be compiled before it can be executed

True

True/False: An array declaration: May allow the values of some elements to remain unspecified

True

True/False: An array declaration: May use a variable to specify the size of the array

True

True/False: An array declaration: Must specify the size of the array if the values of the elements are not provided

True

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 dbOps? gcc dbOps.c main.c db.c stats.c stats.h db.h gcc main.c db.c db.h stats.c stats.h dbOps gcc -o dbOps main.c db.c stats.c gcc main.c db.c stats.c dbOps.c

gcc -o dbOps main.c db.c stats.c

char aChar = 'f'; char *aCharPtr = &aChar; *aCharPtr = 'g'; aChar = 'h'; After the above lines of code execute, what would be printed with this statement: printf( "%c", *aCharPtr );

h

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

infinite output

#include <stdio.h> #include <string.h> int main() { char word1[8] = "Clemson"; char word2[8] = {'C', 'l', 'e', 'm', 's', 'o', 'n', '\0'}; char word3[20]; // What would be printed from this if-statement if (word1 == word2) printf("word1 and word2 are the same\n"); else printf("word1 and word2 are not the same\n"); if (strcmp(word1,word2) == 0) printf("word1 and word2 are the same\n"); elseprintf("word1 and word2 are not the same\n"); strcpy(word2, "Tigers");strcpy(word1, "Go "); strcat(word3, word1); strcat(word3, word2); strcpy(word1, "!"); strcat(word3, word1); printf("\n%s\n", word3); printf("strlen(word3) is %d\n", (int)strlen(word3)); return 0;}

word1 and word2 are not the same

#include <stdio.h> #include <string.h> int main() { char word1[8] = "Clemson"; char word2[8] = {'C', 'l', 'e', 'm', 's', 'o', 'n', '\0'}; char word3[20]; if (word1 == word2) printf("word1 and word2 are the same\n"); else printf("word1 and word2 are not the same\n"); // What would be printed from this if-statement if (strcmp(word1,word2) == 0) printf("word1 and word2 are the same\n"); elseprintf("word1 and word2 are not the same\n"); strcpy(word2, "Tigers");strcpy(word1, "Go "); strcat(word3, word1); strcat(word3, word2); strcpy(word1, "!"); strcat(word3, word1); printf("\n%s\n", word3); printf("strlen(word3) is %d\n", (int)strlen(word3)); return 0;}

word1 and word2 are the same

Consider the code below and show the expected output: (Choose the best response.) #include <stdio.h> int calculate(int value1, int value2) { value1 = (value1 + (value2 + 3) * value2); return value1;} int main() { int x = 20; int y = 10; y = calculate(x, 5); printf("x=%d, y=%d \n", x, y); return 0; }

x=20, y=60


Conjuntos de estudio relacionados

Assignment 7 - Insurance Distribution Systems and Channels

View Set

MACROecon FINAL prep (Tests 1-4)

View Set

A Primer on Antitrust and Securities Laws

View Set