C quiz with data types. control flow, method and programming skills
This quiz is in C Language quiz collection.
Start quizWhat is the correct output from the following code?
#include <stdio.h>
int main(int argc, char** argv)
{
int x = 3;
printf("%d", x++ + ++x);
return 1;
}
What will the following code do?
int main(int argc, char** argv){
char* ptr = NULL;
free(ptr);
return 0;
}
Which of the following differences between malloc and calloc are true?
1) malloc allocates number of bytes passed as argument
2) calloc allocates the product of number of elements
multiplied 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 allocated
memory to all 0
True or false. Calling free on the same address twice is ok.
Which are true about: void *realloc(void *ptr, size_t size);
1) realloc changes the size of the allocated
memory 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 data
pointed to in ptr
In general which of the following functions should be faster for sending information to a file?
How many bytes of memory are used to store a long long data type?
What should the program below print?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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 quiz
return 0;
}
In theory, which is faster, the call to strcpy or the call to memcpy?
#include <string.h>
#include <stdlib.h>
int main(){
char msg[12] = "Hello World";
char buffer1[12];
char buffer2[12];
strcpy(buffer1, msg);
memcpy(buffer2, msg, sizeof(msg));
return 0;
}
True or False?
int32_t is a data type that is guaranteed to be available on all standard conforming C implementations and represents a 32 bit signed integer type?
Which of the following is the correct way to declare a function pointer named pMyFunc that returns an int and has an int parameter?
For the code below which lines should be reported as errors by a compiler?
int main(int argc, char** argv){
const char* foo = "wow"; // line 1
foo = "top"; // line 2
foo[0] = 1; // line 3
return 0;
}
When running the program below, the malloc statement will always be executed?
#include <stdlib.h>
#include <stdio.h>
int* ptrToData;
int main(){
if (!ptrToData){
ptrToData = (int*)malloc(sizeof(int) * 10);
printf("%p\n", ptrToData);
}
free(ptrToData);
return 0;
}
What number is output by the program below? (assuming 8 byte pointers)
#include <stdio.h>
int main(){
const char firstname[] = "bobby";
const char* lastname = "eraserhead";
printf("%lu\n", sizeof(firstname) +
sizeof(lastname));
return 0;
}
what value should be printed by the program:
typedef union ds_{
short s;
char c;
} ds;
ds object;
object.s = 0x0503;
printf("%d\n", object.c);
What value gets printed by the program below?
struct Foo{
int x:1;
int y:2;
};
struct Foo obj;
obj.x = 5;
printf("%d\n", obj.x);
What value gets printed by the program below?
int w = 3;
int x = 31;
int y = 10;
double z = x / y % w;
printf("%f\n", z);
What gets printed by the code below? (Assume 1 byte characters)
char array[] = "foo";
printf("%lu\n", sizeof(array[1]));
What gets printed?
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);
What gets printed?
int i = 3;
if (!i)
i++;
i++;
if (i==3)
i+=2;
i+=2;
printf("%d\n", i);
© 2017 QuizBucket.org