cquizzzz
TRANSCRIPT
-
8/3/2019 Cquizzzz
1/14
Question #1: What is the correct output from the following code?55% on 9917 times asked
#include
int main(int argc, char** argv){
int x = 3;printf("%d", x++ + ++x);return 1;
}
3
5
6
7
undefined - correct
description:If you modify a variable more than one time in a single statement the behavior isundefined according to the C standard.
Question #2: What will the following code do?66% on 4743 times asked
int main(int argc, char** argv){char* ptr = NULL;free(ptr);return 0;
}
core dump
nothing, but the code is safe - correct
undefined behavior
description:Calling free on a NULL ptr is a no operation. It is not illegal and will not coredump. It is defined behavior.
-
8/3/2019 Cquizzzz
2/14
Question #3: Which of the following differences between malloc and calloc are true? 53% on 4066 times asked
1) malloc allocates number of bytes passed as argument
2) calloc allocates the product of number of elementsmultiplied by the size of each element,which are both passed as arguments.
3) both malloc and calloc return void*
4) both malloc and calloc initialize allocatedmemory to all 0
1, 2, 3, 4
1, 2, 3 - correct
1, 2
1
none of the statements are true
description:All statements are true except 4. calloc will initialize the memory to 0, but mallocdoes not initialize the memory.
Question #4: True or false. Calling free on the same address twice is ok.60% on 3398 times asked
true
false - correct
description:False. You can not call free 2 times on the same address. This may core dump!
Question #5: Which are true about: void *realloc(void *ptr, size_t size);
-
8/3/2019 Cquizzzz
3/14
46% on 3336 times asked
1) realloc changes the size of the allocatedmemory pointed to by the argument ptr
2) newly allocated memory will be uninitialized
3) you can pass a NULL ptr safely to realloc
4) realloc will guarantee not to move the datapointed to in ptr
1, 2, 3, 4
1, 2, 3 - correct
1, 2
1
none are true
description:4 is false. realloc may move your data to a new pointer and free the memory atthe old pointer. It will return the new pointer, or the old pointer if the data is not moved.
Question #6: In general which of the following functions should be faster for sendinginformation to a file?53% on 3114 times asked
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
ssize_t write(int fd, const void *buf, size_t count); - correct
description:In general, printf and fprintf should be slower than write. The printf functions areformatted printing and they have to be parsed where as write is the basic system call whichshould have less overhead.
Question #7: How many bytes of memory are used to store a long long data type?
-
8/3/2019 Cquizzzz
4/14
46% on 4536 times asked
4 bytes
8 bytes
16 bytes
32 bytes
It is implementation defined - correct
description:The answer is different on different systems as per the C language specificationwhich allows this.
Question #8: What should the program below print?50% on 3588 times asked
#include #include #include void myfunc(char** param){
++param;}int main(){
char* string = (char*)malloc(64);strcpy(string, "hello_World");myfunc(&string);myfunc(&string);printf("%s\n", string);// ignore memory leak for sake of quizreturn 0;
}
hello_World - correct
ello_World
llo_World
lo_World
Illegal memory access, undefined behavior
-
8/3/2019 Cquizzzz
5/14
description:Look at it carefully, the function is not actually modifying the data from main, sothe original pointer is unchanged.
Question #9: In theory, which is faster, the call to strcpy or the call to memcpy?66% on 2479 times asked
#include #include int main(){
char msg[12] = "Hello World";char buffer1[12];char buffer2[12];
strcpy(buffer1, msg);memcpy(buffer2, msg, sizeof(msg));return 0;
}
strcpy
memcpy - correct
they should have the same speed
description:memcpy should be faster because it does not need to check every byte for aNULL, it is copying a known size of data.
Question #10: True or False?int32_t is a data type that is guaranteed to be available on all standard conforming Cimplementations and represents a 32 bit signed integer type? 61% on 2229 times asked
true
false - correct
description:This data type is a 32 bit signed integer and available in stdint.h. HOWEVER: Ifan implementation does not provide any integer type with 32 bit widths they do not have todefine this data type.
-
8/3/2019 Cquizzzz
6/14
Question #11: Which of the following is the correct way to declare a function pointernamed pMyFunc that returns an int and has an int parameter?52% on 3341 times asked
*(int pMyFunc(int));
int ()(int)* pMyFunc;
int (*pMyFunc)(int); - correct
int *pMyFunc (int);
(int *pMyFunc int);
Question #12: For the code below which lines should be reported as errors by acompiler?44% on 3663 times asked
int main(int argc, char** argv){
const char* foo = "wow"; // line 1foo = "top"; // line 2
foo[0] = 1; // line 3
return 0;}
line 2
line 3 - correct
lines 2 and 3
none of the lines
description:foo is a pointer to a const string. The pointer can be reassigned but the data inthe string can not. Had it been char *const foo; then it would have been a constant pointer tochangeable data.
-
8/3/2019 Cquizzzz
7/14
Question #13: When running the program below, the malloc statement will always beexecuted?58% on 1867 times asked
#include
int* ptrToData;
int main(){
if (!ptrToData){ptrToData = (int*)malloc(sizeof(int) * 10);
}
free(ptrToData);return 0;
}
True, the malloc statement will always be executed - correct
False, depending on how ptrToData is initialized in the machine the mallocstatement might not get run.
description:Variables declared outside of functions or with the static specifier are alwaysinitialized to zero. Therefore this program has deterministic behavior.
Question #14: What number is output by the program below? (assuming 8 bytepointers)46% on 2419 times asked
#include
int main(){const char firstname[] = "bobby";const char* lastname = "eraserhead";printf("%lu\n", sizeof(firstname) +
sizeof(lastname));
return 0;}
8
14 - correct
-
8/3/2019 Cquizzzz
8/14
16
17
20
description:6 bytes for the firstname array and 8 bytes for the pointer lastname.
Question #15: what value should be printed by the program:57% on 1784 times asked
typedef union ds_{
short s;char c;} ds;
ds object;object.s = 0x0503;printf("%d\n", object.c);
0
3
5
0x0503
machine dependent - correct
description:The answer is machine dependent. On a little endian machine, 3 should beprinted on a big endian machine 5 should be printed.
Question #16: What value gets printed by the program below?48% on 2085 times asked
struct Foo{int x:1;int y:2;
-
8/3/2019 Cquizzzz
9/14
};
struct Foo obj;
obj.x = 5;
printf("%d\n", obj.x);
1
2
5
not defined - correct
description:X is a 1 bit bitfield. Assigning 5 to it is not defined.
Question #17: What value gets printed by the program below?56% on 1970 times asked
int w = 3;int x = 31;int y = 10;double z = x / y % w;
printf("%f\n", z);
0 - correct
1
3
undefined
machine dependent
description:31 / 10 is integer division which ignores the remainder and gives result 3. 3modulus 3 has a result of 0.
-
8/3/2019 Cquizzzz
10/14
Question #18: What gets printed by the code below? (Assume 1 byte characters)70% on 1861 times asked
char array[] = "foo";
printf("%lu\n", sizeof(array[1]));
0
1 - correct
2
3
4
description:It prints the size of a single character which we are assuming to be 1 byte.
Question #19: What gets printed?59% on 1705 times asked
int array[2][2] = {0, 1, 2, 3};int i;int sum = 0;
for (i =0; i < 4; ++i){
int x, y;
x = i % 2;
if (x){y = 0;
}else{
y = 1;}sum += array[x][y];
}
printf("%d\n", sum);
6 - correct
-
8/3/2019 Cquizzzz
11/14
7
8
9
10
description:Note the order of initialization of the members of the array is array[0][0]=0,array[0][1]=1, array[1][0]=2, array[1][1]=3. And the addition done is SUM =array[0][1]+array[1][0]+array[0][1]+array[1][0]
Question #20: What gets printed?44% on 2802 times asked
int i = 3;
if (!i)i++;i++;
if (i==3)i+=2;i+=2;
printf("%d\n", i);
3
5
6 - correct
7
9
description:A single statement is part of an if conditional if no curly braces are used.
Question #21: What gets printed?
-
8/3/2019 Cquizzzz
12/14
64% on 1563 times asked
printf("%d\n", 4 ?: 8);
4
0
8
NULL
program has a compiler error - correct
description:According to the C standard this program is invalid because it is missing an
expression between the ? and :. The interesting thing is that there is an extension to thewidely used GCC compiler that will make this code compile and the result will be 4. "Themiddle operand in a conditional expression may be omitted. Then if the first operand isnonzero, its value is the value of the conditional expression."
Question #22: what gets printed?45% on 1081 times asked
#include
int main(){
int ints[] = { 0, 1, 2, 3 };int* i1 = ints + 1;int a = ++*i1;int b = a + *i1;printf("%d\n", b);return 0;
}
3
4 - correct
5
6
-
8/3/2019 Cquizzzz
13/14
code has undefined behavior
description:the prefix operator and dereference operator have the same precedence andassociate from right to left
Question #23: What gets printed?48% on 871 times asked
#include
int main(){
int ints[] = { 0, 5, 10, 15 };
int* i2 = ints + 2;int a = *i2++;printf("%d#%d\n", a, *i2);return 0;
}
10#11
10#15 - correct
11#15
15#15
ill-formed syntax
description:The postfix increment operator has priority over the dereference operator. In thiscase the increment is done on the pointer but its value is not changed until the nextstatement.
Question #24: What gets printed?48% on 808 times asked
01 #include 0203 int main()04 {
-
8/3/2019 Cquizzzz
14/14
05 int ints[] = { 0, 1, 2, 3 };06 int* i1 = ints + 1;07 int* i2 = ints + 2;08 int a = ++*i1 + *i2++;09 int b = *++i1 + *i2--;10 printf("%d#%d", a, b);
11 return 0;12 }
5#6
4#6
4#5 - correct
Undefined behavior
Compiler error on line 9
description:The postfix increment operator has priority over the dereference operator whilethe prefix operator has the same precedence as the dereference operator. The dereferenceoperator and the prefix increment associate right to left.