315 Quiz 1 - Essential C

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

What is the name of the debugger useful in C programming? How is it initialized?

ANSWER: gdb debugger gdb ./program

False (Explanation: A struct is considered to be a programmer-defined type, not a native type.)

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

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

Explanation: There are 8 bits in a byte.

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.)

int main() { char arr[] = {1, 2, 3}; char *p = arr; if(&p == &arr) printf("Same"); else printf("Not same"); } Same Not same

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.)

vi, vim, emacs, and ne are all console-based editors True False

The following code will compile. True or False? int a = 100; int b = 100; long int c = a * b; 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 it is assigned to c, which is: long int c = (long int)a * b;

Consider the following code snippet: #include<stdio.h> void f(int const i) { i=5; } int main() { int x =10; f(x); return 0; } This program will compile without errors. True False

FALSE - You cannot update a const variable type.

What is the binary code for -55 on an 8 bit int? a. 11001001 (*) b. 00110111 c. 10101101 d. 11001101

STILL NEED AN ANSWER FOR THIS

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.

c (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 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];

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

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.

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

Arm

c 0 (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)

#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

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 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.

struct fraction { int numerator; int denominator; }; struct fraction f1, f2; //declare two fractions f1.numerator = 22; f1.denominator = 7; f2 = f1; True or False: f2.numerator = 22, f2.denominator = 7

Answer: True because f2 copies the entire struct of f1

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'.)

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

b (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.)

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

a (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.)

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

a+b+c=2 x=1 (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.)

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

7 (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.)

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

C (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.)

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');

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

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).

A signed int can hold [FILL] numbers than the range of numbers of an unsigned int: 1) 2^31 more 2) 2^31 less 3) 2 more 4) 0 more*

Explanation: Signed ints and unsigned ints have the same range, but it is shifted in the number line.

#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.

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.

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

FALSE - 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.

d (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.)

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

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)

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

Topic: Preprocessing C and String and Command line arguments #include <stdio.h> #include <stdlib.h> #define MAX_VAL 10 int main(int argc, char * argv[]) { int i = 0; int sum = 0; // cause who doesn't like math??? while(i < argc) { sum += atoi(argv[i]) + MAX_VAL; i++; } printf("%d", sum); printf("\n"); } What Will be printed after this: ./cquiz 1 2 3 4 ( A ) 10 ( B ) 50 ( C ) 60 ( * ) ( D ) 34 ( E ) Does not even Compile.

It complies, but the reason it is 60 and not 10, 50 is because it will try to convert ./cquiz to a number by atoi but since it is a string it will just add 10 to 0 so it will return 60.

4 (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.)

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

In C, the programmer is responsible for deallocated all unused heap allocated memory with the free() function. True False

TRUE - C provides no garbage collection, so anything allocated by a program, must be explicitly deallocated with the free() function.

The C preprocessor (#include directives are processed by the C preprocessor as a first step in the compilation process)

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

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).)

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

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: Signed and Unsigned variable type of char Signed is 0 to 255, unsigned is -128 to 127. True False

5 (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 in C are treated as True and zero is treated as False.)

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

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)

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

C (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.)

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

A (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.)

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);

C (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 2s complement representation of -5? a.) 0101 b.) 1010 c.) 1011 d.) 1112

C (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.)

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; } a. The temperature in Fahrenheit is 41.00 b. The temperature in Fahrenheit is 23.00 c. The temperature in Fahrenheit is 37.00 d. Compiler error

b 2 (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 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

b 2 (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 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

b 30 (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.)

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

b (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 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

2^N - 1 (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.)

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

A (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.)

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

int i = 42; int j; j = (i++ + 10); j = (++i + 10); what is the value of i and j after both assignments? a. i = 43 / j = 53 i = 44 / j = 54 b. i = 43 / j = 52 i = 44 / j = 54 c. i = 42 / j = 53 i = 43 / j = 54 d. i = 42 / j = 52 i = 43 / j = 53

explaination: the correct answer is B because i is incremented, but i++ is a post variant meaning that j represents i before the change. on the second assignment, i is incremented with a pre variant meaning that j represents i after the change.

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.)

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


Kaugnay na mga set ng pag-aaral

3.4 Asexual and Sexual Reproduction Question Set

View Set

Peds Adaptive Quizzing Questions - Toddler

View Set

InQuizitive Chapter 16: Fiscal Policy

View Set

Individual Life Insurance Contract - Provisions and Options

View Set

SIE- Understanding Products and Their Risks

View Set

Healthcare Law (Contracts, Torts, Stark, and Anti-Kickback)

View Set