CS 315 Quiz 1

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

What is the default floating point type in c? float double int long double

What is the default floating point type in c? float double (*) int long double Explanation: Constants in the source code such as 3.14 default to type double unless the are suffixed with an 'f' (float) or 'l' (long double).

What type of processor do the Raspberry Pis we use have? a) x86 b) x64 c) arm d) leg

What type of processor do the Raspberry Pis we use have? a) x86 b) x64 c) arm * d) leg - It's a simple question, but has a lot of weight.

Consider the following C code snippet: char ch; int i; i = 322; ch = i; What is the value of ch? A) 322 B) 74 C) NULL D) 66

Consider the following C code snippet: char ch; int i; i = 322; ch = i; What is the value of ch? A) 322 B) 74 C) NULL D) 66* The assignment will drop the upper bits of the int 322. The lower 8 bits of the number 322 represents the number 66 (321 - 256). So the value of ch will be (char)66 which happens to be 'B'.

How many bits in a byte? a. 1 b. 4 c. 9 d. 8

How many bits in a byte? a. 1 b. 4 c. 9 d. 8 (*) Explanation: There are 8 bits in a byte.

Topic: Pointers True or false: By declaring a pointer you allocate space for the pointer and the pointee. True False

Topic: Pointers True or false: By declaring a pointer you allocate space for the pointer and the pointee. True False (*) Explanation: When you declare a pointer, space is allocated for the pointer but not for the pointee, which must also be declared. For the relationship between a pointer and a pointee to work, you must remember three things: (1) the pointer has to be declared and allocated (2) the pointee has to be declared and allocated (3) the pointer has to be initialized so it points to the pointee

Topic: Raspberry Pi True/False? If a C program executes on your laptop's host OS and processor, simply sharing the executable file will allow you to run the code on your pi. A.) True, but only for machines that have the same OS B.) False

Topic: Raspberry Pi True/False? If a C program executes on your laptop's host OS and processor, simply sharing the executable file will allow you to run the code on your pi. A.) True, but only for machines that have the same OS B.) False (*) Explanation: The differing architectures and host operating systems between your laptop and the pi prohibit us from knowing with certainty that the code with execute without error. Even when running the same OS, you cannot be 100% confident. This is why developing directly on the pi itself is encouraged. .exe files are device specific. Therefore, it is necessary to compile code on the device you wish to run the program on in order to generate an executable specific to that environment.

Topic: Raspberry Pi Setup Using Internet Sharing, where does the Raspberry Pi get it's IP address from: a) The WiFi router b) Rasbian Linux c) The host OS on your laptop d) None of the Above

Topic: Raspberry Pi Setup Using Internet Sharing, where does the Raspberry Pi get it's IP address from: a) The WiFi router b) Rasbian Linux c) The host OS on your laptop d) None of the Above Answer: c) The host OS on your laptop Explanation: Internet Sharing found in MacOS, Windows, and Linux assigns an IP address to the connected Raspberry Pi so it can connect to the internet. Essentially, your laptop OS is acting like a router for your Raspberry Pi.

Topic: Signed and Unsigned variable type of char Signed is 0 to 255, unsigned is -128 to 127. True False

Topic: Signed and Unsigned variable type of char Signed is 0 to 255, unsigned is -128 to 127. True False (*) Explanation: If char's have 8 bits, unsigned char variables have values between 0 and 255. if char's have 8 bits, signed char variables have values from -128 and 127.

Topic: Ternary Operator 9 + (1 == 1) ? 5 : 10 What does the above expression evaluate to? 5 10 14 19

Topic: Ternary Operator 9 + (1 == 1) ? 5 : 10 What does the above expression evaluate to? 5 (*) 10 14 19 Explanation: The ternary operator has a really low precedence, so it is carried out after the '+' operator. Thus, 9+(1==1) is the expression being tested, not just 1==1. Also, non zero values are true, and zero is false.

True or False: To view the Raspberry Pi configuration menu in your terminal window, type the command $ sudo rpi-config into the command line of the Raspberry Pi. True False

True or False: To view the Raspberry Pi configuration menu in your terminal window, type the command $ sudo rpi-config into the command line of the Raspberry Pi. True False (*) Explanation: The correct command to view the Raspberry Pi's configuration menu is sudo raspi-config. To view the configuration menu, enter this command from the Raspberry Pi's command line.

We know that int a, b, c, *d=&c, and which of the following statement can be used to ask the value of variable a, b and c from the user? A. scanf("%d%d%d", &a, &b, d) B. scanf("%d%d%d", a, b, d); C. scanf("%d%d%d", &a, &b, &d); D. scanf("%d%d%d", a, b,*d);

We know that int a, b, c, *d=&c, and which of the following statement can be used to ask the value of variable a, b and c from the user? A. scanf("%d%d%d", &a, &b, d) (*) B. scanf("%d%d%d", a, b, d); C. scanf("%d%d%d", &a, &b, &d); D. scanf("%d%d%d", a, b,*d); The answer is A because the address of c is assigned to pointer d, so the value that asked from the user can be assigned to d directly. As a result, we can eliminate C and D. For variable A and B, we are assigning the value to the address of a and b.

What is the 2s complement representation of -5? a.) 0101 b.) 1010 c.) 1011 d.) 1112

What is the 2s complement representation of -5? a.) 0101 b.) 1010 c.) 1011 (*) d.) 1112 Explanation: -5 can be represented in binary form as 2s compliment of 5. So, the Binary representation of 5 is 0101. We then find the 1s complement of 5 which is 1010. So, the 2s complement of 5 ends up being the 1s complement + 1( 1010 + 1). The answer is 1011(-5).

What is the output of the following code? #include <stdio.h> struct many_numbers { int a, b, c; } int main() { struct many_numbers nums = {.a = 1, .b = 2, .c = 3}; int* pointer = &nums.a; pointer = pointer + 1; printf("%d\n", *pointer) } a) 1 b) 2 c) 3 d) None of the above

What is the output of the following code? #include <stdio.h> struct many_numbers { int a, b, c; } int main() { struct many_numbers nums = {.a = 1, .b = 2, .c = 3}; int* pointer = &nums.a; pointer = pointer + 1; printf("%d\n", *pointer) } a) 1 b) 2 (*) c) 3 d) None of the above Explanation: When you add 1 to a pointer value, the actual value of the pointer is changed by the number of bytes that are contained in the type of the pointer. When 1 is added to the pointer to nums.a, the pointer now points to nums.b

What is the output of this code? #include <stdio.h> int main(int argc, char *argv[]) { int x[] = {1, 12, 30, 8}; printf("%d\n", *(x + 2)); return 0; } a) 3 b) 30 c) 1 d) 8

What is the output of this code? #include <stdio.h> int main(int argc, char *argv[]) { int x[] = {1, 12, 30, 8}; printf("%d\n", *(x + 2)); return 0; } a) 3 b) 30 (*) c) 1 d) 8 Explanation: The expression *(x + 2) does pointer arithmetic first because of the parentheses, advancing the pointer x from the start of the array to index 2 of the array. This is then dereferenced to get the value at index 2, which is 30.

The output of the following code is given below. Which missing line would generate this output? #include <stdio.h> int main(){ char str[] = "HelloWorld"; char *ptr; //---------- MISSING LINE printf("The value of 'ptr' is: %c", *ptr); } OUTPUT: The value of 'ptr' is: W a) ptr = ptr + 5; b) ptr = str[5]; c) ptr = &str[5];

The output of the following code is given below. Which missing line would generate this output? #include <stdio.h> int main(){ char str[] = "HelloWorld"; char *ptr; //---------- MISSING LINE printf("The value of 'ptr' is: %c", *ptr); } OUTPUT: The value of 'ptr' is: W a) ptr = ptr + 5; b) ptr = str[5]; c) ptr = &str[5]; (*) a is attempting pointer arithmetic to access the str[5] (ie 'W'). However, ptr was never explicitly set to point to str, so ptr cannot access any of the elements of str. b would throw an error at compile time because it is assigning char* to char; it is going from pointer to char without a cast c is correct because it uses the '&' symbol to referece the address of the element at str[5] (ie 'W')

The serial line connection allows you to connect to a Raspberry Pi without a network connection. True False

The serial line connection allows you to connect to a Raspberry Pi without a network connection. True (*) False Explanation: The serial connection is a way to directly interact with the Raspbian Linux console using a serial terminal program. No network connection like Ethernet or WiFi is needed. This is how we can configure Raspbian without a separate keyboard and mouse. You would still need to set up network configurations if you want to have internet access.

The size of a struct in C is always exactly the number of bytes needed to store all the members of the struct. True False

The size of a struct in C is always exactly the number of bytes needed to store all the members of the struct. True False (*) Explanation: Depending on the compiler and the word size, a struct may take up extra bytes in order to take up even word space (a struct that needs 11 bytes may take 12).

Topic ^ in the summary, but just in case you missed it. It is Structs, strcmp, functions, pointers. Consider the Following: #include < stdio.h > // please delete space until I find a better way to display it or #include < stdlib.h > // if not fixed by class just know I didn't find one. #include < math.h > #include < string.h > struct node { int value; char * secert; }; void switch_cakes(struct node * cake); int main(int argv, char * argc[]) { char * today = " Monday"; if(strcmp(today, "Monday ") == 0) { printf("Garfield... oh you...\n"); } struct node cake; cake.value = rand() % 5; cake.secert = "Totally not going to change.\n"; int i; for(i = pow(2, 31) - 1; i > 0 ; i--) { if(cake.value == i) { printf("Congrats now for part 1\n"); switch_cakes(&cake); } } printf("I cant believe you done this, Now I can't believe everyting is\n %s\n", cake.secert); } void switch_cakes(struct node * cake) { printf("Who are you?\n"); struct node is; is.value = 5; is.secert = "a Lie"; printf("I can tell you but let me get ready.\n"); cake = &is; printf("What have you done?!, now everything is \n"); while(rand() % 100090 != cake -> value) { // Nothing here just wasting your time... } printf("%s\n", cake->secert); } What Will print: ( A ) Nothing because this is code that is written at 10 pm so there will be no output. ( B ) Garfield... oh you... Congrats now for part 1 Who are you? I can tell you but let me get ready. What have you done?!, now everything is a Lie I cant believe you done this, Now I can't believe everything is Lie. ( C ) Garfield... oh you... Congrats now for part 1 Who are you? I can tell you but let me get ready. What have you done?!, now everything is a Lie I cant believe you done this, Now I can't believe everything is Totally not going to change. ( D ) Congrats now for part 1 Who are you? I can tell you but let me get ready. What have you done?!, now everything is a Lie I cant believe you done this, Now I can't believe everything is Totally not going to change.

Topic ^ in the summary, but just in case you missed it. It is Structs, strcmp, functions, pointers. Consider the Following: #include < stdio.h > // please delete space until I find a better way to display it or #include < stdlib.h > // if not fixed by class just know I didn't find one. #include < math.h > #include < string.h > struct node { int value; char * secert; }; void switch_cakes(struct node * cake); int main(int argv, char * argc[]) { char * today = " Monday"; if(strcmp(today, "Monday ") == 0) { printf("Garfield... oh you...\n"); } struct node cake; cake.value = rand() % 5; cake.secert = "Totally not going to change.\n"; int i; for(i = pow(2, 31) - 1; i > 0 ; i--) { if(cake.value == i) { printf("Congrats now for part 1\n"); switch_cakes(&cake); } } printf("I cant believe you done this, Now I can't believe everyting is\n %s\n", cake.secert); } void switch_cakes(struct node * cake) { printf("Who are you?\n"); struct node is; is.value = 5; is.secert = "a Lie"; printf("I can tell you but let me get ready.\n"); cake = &is; printf("What have you done?!, now everything is \n"); while(rand() % 100090 != cake -> value) { // Nothing here just wasting your time... } printf("%s\n", cake->secert); } What Will print: ( A ) Nothing because this is code that is written at 10 pm so there will be no output. ( B ) Garfield... oh you... Congrats now for part 1 Who are you? I can tell you but let me get ready. What have you done?!, now everything is a Lie I cant believe you done this, Now I can't believe everything is Lie. ( C ) Garfield... oh you... Congrats now for part 1 Who are you? I can tell you but let me get ready. What have you done?!, now everything is a Lie I cant believe you done this, Now I can't believe everything is Totally not going to change. ( D ) ( * ) Congrats now for part 1 Who are you? I can tell you but let me get ready. What have you done?!, now everything is a Lie I cant believe you done this, Now I can't believe everything is Totally not going to change. Explanation: So, You want to know the answer? Fine I'll tell you anyways... first yes it complies. 2nd strcmp does return 0 if both strings are the same, but are these strings really the same? 3rd. Local variables, lucky C has local variables and will not change the pointers in the function. or I believe it sends a copy of the pointer to the function.

Topic: ASCII Converting and Casting Consider this code block: char c = '4'; int y; What is is proper conversion from '4' (char c) to 4 (y = 4)? A) y = (int) (c + '0'); B) y = int (c + '0'); C) y = (int) (c - '0'); D) y = int (c - '0');

Topic: ASCII Converting and Casting Consider this code block: char c = '4'; int y; What is is proper conversion from '4' (char c) to 4 (y = 4)? A) y = (int) (c + '0'); B) y = int (c + '0'); C) y = (int) (c - '0'); (*) D) y = int (c - '0'); Explanation: 1) You can can subtract the ascii value of the char from the ascii value of '0' to get the numeric distance between the two numbers, 2) Converting to int requires parentheses around the cast.

Topic: C Data Types Char, arrays, pointers, structs, bool, and double are all examples of native data types in C. True False

Topic: C Data Types Char, arrays, pointers, structs, bool, and double are all examples of native data types in C. True False (*) Explanation: A struct is considered to be a programmer-defined type, not a native type.

Topic: C Data Types What is the correct output #include <stdio.h> int main() { float c = 5.0; printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32); return 0; } The temperature in Fahrenheit is 41.00 The temperature in Fahrenheit is 23.00 The temperature in Fahrenheit is 37.00 Compiler error

Topic: C Data Types What is the correct output #include <stdio.h> int main() { float c = 5.0; printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32); return 0; } The temperature in Fahrenheit is 41.00 The temperature in Fahrenheit is 23.00 The temperature in Fahrenheit is 37.00 (*) Compiler error Explanation: Since 9 and 5 are integers, integer arithmetic happens in subexpression (9/5) and we get 1 as its value. To fix the above program, you can use 9.0 instead of 9 or 5.0 instead of 5 so that floating-point arithmetic happens.

Topic: C operators Consider the following program, what is the output? #include <stdio.h> int main(int argc, char **argv) { int a = 0, b = 0, c = 0; int x = a++ || ++b || --c; printf("a+b+c=%d x=%d\n", a + b + c, x); return 0; } a+b+c=0 x=1 a+b+c=1 x=1 a+b+c=2 x=1 a+b+c=1 x=0

Topic: C operators Consider the following program, what is the output? #include <stdio.h> int main(int argc, char **argv) { int a = 0, b = 0, c = 0; int x = a++ || ++b || --c; printf("a+b+c=%d x=%d\n", a + b + c, x); return 0; } a+b+c=0 x=1 a+b+c=1 x=1 a+b+c=2 x=1 (*) a+b+c=1 x=0 Explanation: a++ increments the value of a and return the original value, while ++b increment the value of b and return the incremented value. On the other hand, latter expressions after a true in OR will be skipped. So, a is 1, b is 1, c is still 0, and x is 1, which means true. Therefore, the output is a+b+c = 2, x = 1.

Topic: C pointers Consider the following C program. #include <stdio.h> int main(void) { char c1; char c2; char *p; c1 = 'a'; p = &c1; c2 = c1; *p = 'b'; c2 = 'c'; printf("%c\n", c1); } What is the output of this program? a b c

Topic: C pointers Consider the following C program. #include <stdio.h> int main(void) { char c1; char c2; char *p; c1 = 'a'; p = &c1; c2 = c1; *p = 'b'; c2 = 'c'; printf("%c\n", c1); } What is the output of this program? a b (*) c Explanation: Since p points at c1 (with the &), *p is the same c1. When *p is modified, it modifies c1. However, while c2 has the same value as c1 when c2 = c1 is called, when c2 is modified (c2 = 'c'), the value of c1 remains the same. Note that a char type is a primitive type.

Topic: C strings Consider the following variable definition: char *str = "hello\n"; How many bytes of memory are needed to store this string in memory? 5 6 7 8

Topic: C strings Consider the following variable definition: char *str = "hello\n"; How many bytes of memory are needed to store this string in memory? 5 6 7 (*) 8 Explanation: There are bytes for "hello", one byte for "\n", and one more byte for the null terminator that is provided by the C compiler and is necessary for all C strings to have.

Topic: C strings, structs Consider the Following: #include < stdio.h > #include < string.h > // Dont know if this works if not oh well... struct holder { int value; char * sentence; }; int main(int argc, char * argv[]) { struct holder curr; curr.value = 1; int i = curr.value; curr.sentence = "SSH my\0pi\n"; while(i < strlen(curr.sentence)) { printf("%s ", &curr.sentence[i]); i++; } } What will be printed: ( A ) Nothing you fool it does not even compile. ( B ) SSH mypi SH mypi H mypi mypi mypi ypi pi i (C) SSH my Sh my SSH my SH my H my my y ( D ) SH my H my my my y

Topic: C strings, structs Consider the Following: #include < stdio.h > #include < string.h > // Dont know if this works if not oh well... struct holder { int value; char * sentence; }; int main(int argc, char * argv[]) { struct holder curr; curr.value = 1; int i = curr.value; curr.sentence = "SSH my\0pi\n"; while(i < strlen(curr.sentence)) { printf("%s ", &curr.sentence[i]); i++; } } What will be printed: ( A ) Nothing you fool it does not even compile. ( B ) SSH mypi SH mypi H mypi mypi mypi ypi pi i (C) SSH my Sh my SSH my SH my H my my y ( D ) (*) SH my H my my my y So you came here instead of solving the problem yourself. How bold of you... So the reason it is the last one is because: A. yes it complies, geez... B. Theres a null byte in the middle of the string so it will not read the rest of the string and stop at the first null byte. C Look at what i is starting at... so it would not start at the beginning of the string.

Topic: Computer Abstractions - Processor The computer processor can natively execute which of the following? C source code Machine code Java code Python code

Topic: Computer Abstractions - Processor The computer processor can natively execute which of the following? C source code Machine code (*) Java code Python code Explanation: Computer processors only understand how to execute binary instructions that conform to the instruction set architecture of the processor (e.g., ARMv7, Intel x86, etc.). Therefore, B is true.

Topic: Console-Based Editors vi, vim, emacs, and ne are all console-based editors True False

Topic: Console-Based Editors vi, vim, emacs, and ne are all console-based editors True (*) False Answer: True Explanation: There are quite a few console-based editors that can be used; however, the class resources page recommends vi, vim, emacs, and ne, so I stuck with these for this solution.

Topic: Heap Memory malloc() initializes each block of memory to the default value of '0'. True False

Topic: Heap Memory malloc() initializes each block of memory to the default value of '0'. True False (*) Explanation: False. malloc() does not initialize the memory allocated and returns a pointer to the heap block or a null pointer if the allocation fails.

Topic: Int Size On the Raspberry Pi running Raspbian in 32-bit mode, how many bytes are in an int? 1 8 4 2

Topic: Int Size On the Raspberry Pi running Raspbian in 32-bit mode, how many bytes are in an int? 1 8 4 * 2 Explanation: Considering that you are running a raspberry pi with raspbian in 32-bit mode the total size of the int would be 4 bytes. If it was a 16 bit set on the same system then the size would be 2 bytes.

Topic: binary numbers. What's the greatest value that can be stored in an unsigned N-bit binary number? 2^N + 1 2^N - 1 4,294,967,295 2^N

Topic: binary numbers. What's the greatest value that can be stored in an unsigned N-bit binary number? 2^N + 1 2^N - 1 (*) 4,294,967,295 2^N Explanation: If we set each bit to 1 then we will be adding up the first n powers of 2 starting with 2^0. That turns out to be one less than 2^n.

True or False, would (1.0/3 + 1/3.0 + 1.0/3.0) == 1 return true (a non-zero value in C)? (a) true (b) false

True or False, would (1.0/3 + 1/3.0 + 1.0/3.0) == 1 return true (a non-zero value in C)? (a) true (b) false (*) Explanation: This may not always be true. Some machines may have single precision of 6 digits, and some may have double precision of 15 digits. The 1.0/3 (or other floating point division variants I've written) may return different floating point values across different machines.

True/False: Signed int and unsigned int values can represent the same number of unique values. True False

True/False: Signed int and unsigned int values can represent the same number of unique values. True (*) False Explanation: Signed ints and unsigned ints both store the same number of bits, so they have the same number of total unique values. Signed ints use their left-most bit to determine its sign, while unsigned ints only represents positive numbers.

What is the correct output of this code? #include void main() { char s[] = "C++"; printf("%s ",s); s++; printf("%s",s); } A : C++ C++ B : C++ ++ C : ++ ++ D : Compile error

What is the correct output of this code? #include void main() { char s[] = "C++"; printf("%s ",s); s++; printf("%s",s); } A : C++ C++ B : C++ ++ C : ++ ++ D : Compile error (*) Answer : D Explanation : 's' refers to a constant address and cannot be incremented.

What is the output of the following program? #include <stdio.h> void func1(int arr[]) { printf("%d\n", sizeof(arr)/sizeof(int)); } int main(int argc, char **argv) { int arr[] = {1, 2, 3, 4, 5, 6}; printf("%d ", sizeof(arr)/sizeof(int)); func1(arr); return 0; } a. 6 6 b. 6 1 c. 1 1 d. 1 6

What is the output of the following program? #include <stdio.h> void func1(int arr[]) { printf("%d\n", sizeof(arr)/sizeof(int)); } int main(int argc, char **argv) { int arr[] = {1, 2, 3, 4, 5, 6}; printf("%d ", sizeof(arr)/sizeof(int)); func1(arr); return 0; } a. 6 6 b. 6 1 (*) c. 1 1 d. 1 6 The answer is b because before we pass arr to func1, we still know how many bytes arr is holding. Once arr gets passed to func1, all func1 knows is the type of the pointer.

When integral types are mixed together in arithmetic expressions, how does the compiler deal with different widths present? (Ex. ('b' + 5)) A. Promotes the smaller type to be the same size as the larger type B. Demotes the larger type to be the same size as the smaller type C. Find a type with a size in the midpoint of the two types and convert each type by promoting the smaller type and demoting the larger type

When integral types are mixed together in arithmetic expressions, how does the compiler deal with different widths present? (Ex. ('b' + 5)) A. Promotes the smaller type to be the same size as the larger type (*) B. Demotes the larger type to be the same size as the smaller type C. Find a type with a size in the midpoint of the two types and convert each type by promoting the smaller type and demoting the larger type Explanation: Promotions are determined at compile time based purely on the types of the values in the expressions and do not lose information as they always convert from a type to compatible, larger type to avoid losing information.

"x << y" is equivalent to dividing x by 2^y. a) True b) False

"x << y" is equivalent to dividing x by 2^y. a) True b) False (*) Explanation: The ">>" operator is equivalent to dividing by a power of two (ie. x>>y = x / 2^y) and the "<<" operator is equivalent to multiplying by a power of two (ie x<<y = x * 2^y)

#include <stdio.h> int main(int argc, char **argv) { int b1 = 7, b2 = 11; printf("b1 ^ b2 = %d\n", b1^b2); return 0; } What it the output of the above code? a) b1 ^ b2 = 15 b) b1 ^ b2 = 11 c) b1 ^ b2 = 18 d) b1 ^ b2 = 12

#include <stdio.h> int main(int argc, char **argv) { int b1 = 7, b2 = 11; printf("b1 ^ b2 = %d\n", b1^b2); return 0; } What it the output of the above code? a) b1 ^ b2 = 15 b) b1 ^ b2 = 11 c) b1 ^ b2 = 18 d) b1 ^ b2 = 12 (*) Explanation: The ^ is an XOR operator, where the result is 1 if the two bits are different. 7 = 0111 and 11 = 1011.

#include <stdio.h> int main(void) { int a = 12, b = 2; printf("%d",a&b); return 0; } a) 1 b) 14 c) 0 d) 8

#include <stdio.h> int main(void) { int a = 12, b = 2; printf("%d",a&b); return 0; } a) 1 b) 14 c) 0(*) d) 8 Explanation: For bitwise AND, If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. The binary representation of 12 is 1100 and 2 is 0010 so the resulting bit is 0000

Assuming that ptr points to a primitive data type, what is the following equivalent to in regular arithmetic? ptr + i a) (ptr + (i * sizeof(*ptr))) b) (ptr + (i * sizeof(ptr))) c) (ptr + 4) d) (ptr + (i + sizeof(ptr)))

Assuming that ptr points to a primitive data type, what is the following equivalent to in regular arithmetic? ptr + i a) (ptr + (i * sizeof(*ptr))) (*) b) (ptr + (i * sizeof(ptr))) c) (ptr + 4) d) (ptr + (i + sizeof(ptr))) Correct Answer: a Explanation: Incrementing a pointer by a number i is equivalent to incrementing the pointer in memory by x bytes, where x is the size of of the data type that ptr points to, times i.

Consider the following code snippets. Which correctly results in p pointing to the integer value 13 in memory? a) int* p; int i=10; p=&i; p*=13; b) int* p; p*=13; c) int* p; int i=10; p=&i; p=p+3; a b c

Consider the following code snippets. Which correctly results in p pointing to the integer value 13 in memory? a) int* p; int i=10; p=&i; p*=13; b) int* p; p*=13; c) int* p; int i=10; p=&i; p=p+3; a (*) b c Explanation: The pointer must be set to something before you can dereference it, and if you don't dereference p you are doing pointer arithmetic instead of updating the value pointed to by p.

Generally speaking, which of the following two data types in C will differ when compiled using a 32bit vs 64bit compiler? a)int and float b)int and pointer c)int and long d)long and pointer

Generally speaking, which of the following two data types in C will differ when compiled using a 32bit vs 64bit compiler? a)int and float b)int and pointer c)int and long d)long and pointer (*) Explanation: In most cases, using a 32bit compiler like the ones on our RPI's will make long and pointer as 4 bytes, compared to 8 bytes on our 64bit systems.

Given the following code, determine the final value of p : int *p; p = (int *) 200; p = p + 4; A) 204 B) 201 C) 216

Given the following code, determine the final value of p : int *p; p = (int *) 200; p = p + 4; A) 204 B) 201 C) 216 (*) Explanation: As discussed in class, pointers carry 4 bytes of memory therefore when adding 4 to the pointer p, multiply the number by the size/bytes of the data types p = 200 + (4 * 4bytes) = 216

The #include directive is processed by: The C compiler The C preprocessor The Linux shell None of the above

The #include directive is processed by: The C compiler The C preprocessor (*) The Linux shell None of the above Explanation: #include directives are processed by the C preprocessor as a first step in the compilation process

The following code will compile. True or False? #include<stdio.h> int main( ){ int a = 100; int b = 100; long int c = a * b; return 0; } A. True. B. False.

The following code will compile. True or False? #include<stdio.h> int main( ){ int a = 100; int b = 100; long int c = a * b; return 0; } A. True. B. False.(*) Answer: B, False. Explanation: According to the type conversion rule of C, the multiplication is implemented by the int type, so the a * b will probably overflow before it is mandatorily converted to long int and assigned to c. So, we should do the conversion before the multiplication. To revise it, we have to first coerce the int values into long ints first, then do the multiplication. which is: long int c = (long int)a * (long int)b;

What is the output of this code? #include <stdio.h> void foo(int *ptr) { *ptr = 12; } int main(void) { int x = 4; foo(&x); printf("%d", x); return 0; } a. 4 b. 12 c. This program won't compile

What is the output of this code? #include <stdio.h> void foo(int *ptr) { *ptr = 12; } int main(void) { int x = 4; foo(&x); printf("%d", x); return 0; } a. 4 b. 12 (*) c. This program won't compile Explanation: &: address operator gets address of a variable *: dereference operator accesses the value at an address In the function call 'foo(&x)', the address of x is passed so that it can be modified using its address. foo() expects a pointer to an integer, or address of an integer, and modifies the value at the address ptr. In the line '*ptr = 12', the value at address ptr becomes 12.

What type of value does sizeof return? A. Short B. Char C. Unsigned int D. Long

What type of value does sizeof return? A. Short B. Char C. Unsigned int (*) D. Long Answer: C Explanation: The size of function returns a unsigned int that is then used as size_t so that the correct amount of memory is provided for that unsigned int so that it can have the most optimization.

what will print out from the following code: int main() { char arr[] = {1, 2, 3}; char *p = arr; if(&p == &arr) printf("Same"); else printf("Not same"); } Same Not same

what will print out from the following code: int main() { char arr[] = {1, 2, 3}; char *p = arr; if(&p == &arr) printf("Same"); else printf("Not same"); } Same Not same(*) &p gives you the address of pointer p. but arr is a char array. &arr gives you the address of the first element in the array.


Conjuntos de estudio relacionados

Greek & Latin Roots - unit 10 (plur, multi/poly, mega)

View Set

CHEM 163-- Semester 1 Study Guide

View Set

Manufacturing Processes Final Exam Review

View Set