Binary/Hexadecimal
Pointers
Strings
Bitwise operators
100

What is the representable range of integers for a signed char (1 byte)

-128 to 127

100

Consider the following snippet:

char buf[10];

char * = "hello";


What does sizeof(buf) <= sizeof(char *) evaluate to?

False!

sizeof(buf) = 5

sizeof(char *) = 8

100

Will the following code execute successfully?


char * str = "testing...";

str[0] = 'r';

No because the string literal is in read-only memory.

100

What is the result of:

11001100 ^ 10101010

01100110

200

What is the value of the following unsigned int in decimal:


0x0fce

4046

200

Consider a random array:

int array[] = {0xffccffcc, 0xeeddeedd, 0x23232323};

int a = array[1];

int b = *(int*) ((char *) array + 3);

int c = *(int*) ((char *) array + 8);


what will be the values of a, b, c?

a = 0xeeddeedd

b = 0xcceeddee

d = 0x23232323

200
Consider the following snippet of C code:

char * str = "CS107";

char buf[5];

strncpy(buf, str, 5);

size_t y = strlen(buf);


What will be the value of y

Undefined behavior! It's not a null terminated string

200


Assume we have some random char y.

Which of the following is always true:

(i). (y | 0xFF) == 0xFF

(ii). (y & 0xFF) == 0xFF

(iii). (y & 0xFF) == y


(i) and (iii) are always true

300
Consider the following signed integer:

0xFFFFFFFF


What does this convert to in decimal

-1

300

What are the values of variables a, c, and d after the following code is executed:

char *str = strdup("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

char a = str[0];

char c = *(char *)((int *)str + 2);

str++;

char d = str[3];

a = 'A'

c = 'I'

d = 'E'

300

void fillWithStr(char * buffer, size_t buf_len, char * str){

for(int i = 0; i < buflen - strlen(str) - 1, i++){

strncpy(buffer + i, str, strlen(str) + 1);

}

}

Consider calling this function with an empty buffer of length 10 and  str = "test".  What will the final contents of the buffer be?

buffer = "tttttttest"

300

Given a char variable (1 byte), which bitmask would you use to mask the 3 most significant bits and the 3 least significant bits (all in one)?

11100111

400

Does the following statement return true or false?

(assume variable type is short):

-1 < 0U

This will return false.  C casts the signed argument to unsigned, and -1 as an unsigned integer is positive and has a large absolute value because of the leading 1.

400

Imagine I want to sort an array of char *'s.  I use q sort and pass the following comparison function:


int str_cmp(void * arg1, void * arg2){

if (arg1 == arg2) return 0;

if (arg1 < arg2) return 1;

return -1; 

}


What will happen to the array after we sort it using this comparison function?

Reverse order!  We are comparing the points, and the return value of the comparison function is backwards.

400

What is one way that you could manipulate a string literal such that you can change the characters within it.

char * literal = "wooohoooo!";

char buf[strlen(literal) +1];

strcpy(buf, literal, strlen(literal));

400

What is the objective of the following piece of code:


void func(int param){

int result = param & (param-1);

if (result == 0){

printf("true\n");

} else {

printf("false\n");

}

}


Hint: What does it print when param = 16?

What does it print when param = 14?

Detects powers of 2