Module 3 Quiz

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

Given this snippet of code, determine which of the following options will change the text in array to "Hello Doe" after execution? (Check all that apply) char array[] = "Hello Joe"; char *x; A. x = &array[0]; x = x + 6; *x = 'D'; B. x = &array; x = x + 6; x = 'D'; C. x = array; *(x + 6) = 'D'; D. x = array; x = x + 6; x = 'D';

The answer is C. x = array; // points the pointer *x to the first element in the array. *(x + 6) = 'D'; //moves the pointer over 6 times, to the 7th element in the array, remember arrays start at zero.

Given the following C code: char a[2][3] = { {'c', 'a', 't'}, {'d', 'o', 'g'} }; int i, j; for (i=0; i<2; i++) { for (j=0; j<3; j++) { printf("%c", a[i][j]); } } A. It prints: catdog B. It prints: cat C. A compilation error will occur at this line: printf("%c", a[i][j]); D. A compilation error will occur at this line: char a[2][3] = { { 'c', 'a', 't'}, { 'd', 'o', 'g'} };

A. It prints: catdog This is just a basic nested loop, should be easy enough to understand.

Given the following code: char *p = "hello", *s = "this is a string"; p = s; printf("%s\n", p); What will happen? A. It prints: this is a string B. It prints: hello C. A run time error will occur at this line: p = s; D. A compilation error will occur at this line: p = s;

A. It prints: this is a string p = s changes what p is pointing to to what s is pointing to.

What C/C++ constant variable can potentially be modified during the execution? A. const x = 5; B. #define x 5 C. permanent x = 5; D. None of these can be changed.

A. const x = 5; This isn't actually defining a const variable because there is no type after "const". Valid syntax would be const int x = 5; So we have to assume that the user made a custom "const" type which is not the same as the built in "const" keyword. B is the proper way to declare a constant in C. C. permanent is not a keyword.

Given the following snippet of code, answer the following two questions based on the code: typedef enum {Sun, Mon, Tue, Wed, Thu, Fri, Sat} days; days x = Mon, y = Sat; while (x != y) { x++; }y++; printf("x = %d, y = %d", x, y); 1. What value will be printed for variable x? 2. What value will be printed for variable y? (Hint: enums are really just integers...)

Answer 1: 6 Answer 2: 7 The hint is really helpful, when dealing with enums just change the 'Sun', 'Mon' to their integer equivalents. Note that when declaring enums if you leave out the starting value then zero is assumed.

A pointer variable can take the address of a memory location as its value. Read the given program. #include <stdio.h> main () { int i = 10, j = 20, *p, *q, **r; p = &i; *p = 40; q = &j; *q = 60; r = &p; **r = 80; printf("%d\n", i); //1st output printf("%d\n", j); //2nd output i = 10; j = 70; printf("%d\n", **r); //3rd output } Answer the following three questions. 1. The output of the 1st printf statement is ? 2. The output of the 2nd printf statement is ? 3.The output of the 3rd printf statement is ?

Answer 1: 80 Answer 2: 60 Answer 3: 10 Should be pretty self-explanatory if you remember: * means "value of" & means "address of" and that * and & cancel eachother out.

Given the C declaration: char a[] = "Hello"; char *p = a, *q; what operations are valid syntactically? Select all that apply. A. q = &&a; B. *q = *(&a[0]); C. q = &&a[0]; D. q = &(*p);

B. *q = *(&a[0]); D. q = &(*p); A and C are obviously wrong because q is not of the type **q. B is saying *q = a[0], which means "value of" q = element zero in a. D is saying pointer q = pointer p. This is valid because q and p are both pointer types. The exclusion of the asterisk means "pointer of" is the variable is of type pointer.

C/C++ has 2 pointer operators, which operator represents using the value at the location given by an address? A. Semicolon (:) B. Asterisk (*) C. Ampersand (&) D. Comma (,)

B. Asterisk (*) The ampersand is the other pointer operator and it's the exact opposite of the asterisk. The Asterisk operator get's the value of a pointer type variable. * means "value of" & means "address of"

Given the declaration: char A[3] = {'C', 'a', 'r'}, *p = A; what statement prints character a? A. printf("%c", *p+1); B. printf("%c", *(++p)); C. printf("%c", *p); D. printf("%c", *p++);

B. printf("%c", *(++p)); A pointer POINTS to the beginning of an array when declared, so you can increment/decrement the pointers. Moving the pointer like this acts the same as changing the index of an array, like array[i], if i is your index.

Consider the following snippet of code in a 32-bit computer. #define MAX 10 struct contact { char name[30]; char email[30]; int phone; }; struct contact contactbook[MAX]; int tail = 0; Which statement can read a name into the name field of the structure? A. scanf("%s", contactbook.name); B. scanf("%s", contactbook[tail].name); C. scanf("%s", &contactbook.name); D. scanf("%s", &contactbook[tail].name);

B. scanf("%s", contactbook[tail].name); structs are like very simple data classes, just classes with primitive types as members. struct contact contactbook[MAX]; // defines an array of contact structs Because contactbook is an array we must use an index to refer to a specific value, then use dot notation to get the member we want (.name in this case).

Given this snippet of code, what is the value of x after executing the last statement? int x = 10, *y, **z; y = &x; z = &y; **z = 1000; A. None of these values. B. 100 C. 1000 D. 10

C. 1000 Remember that an asterisk and an ampersand cancel eachother out so: *y = &x means y = x

When using an array of structures to store a collection, we typically have a variable storing the number of entries (called something like: tail, count, or size). In addition to the number of entries, what does this variable help to indicate? A. the starting address of the array. B. where to delete an existing item in the array. C. how far the search() function should go in searching the items of the array. D. where to insert a new item, if the items in the array do not need to be sorted.

C. how far the search() function should go in searching the items of the array. D. where to insert a new item, if the items in the array do not need to be sorted. For A you could use the pointer of the array. For B you should delete by a member of the struct, like .name


Conjuntos de estudio relacionados

Accounting Final Belmont Alleyne

View Set

Ch 9 Neurologic Problems LaCharity

View Set

Module 48: Introduction to Psychological Disorders

View Set

CRIS - Misc Lines - Ch.3 - Professional Liability Insurance in Construction

View Set

Ohm's Law and Electrical Quantities Unit-2

View Set