Unit 5 - Practice Quiz
1
Which header file must be included to use dynamic memory allocation functions like malloc and calloc?
<stdlib.h>
<mem.h>
<string.h>
<stdio.h>
2
What is the return type of the malloc function?
void *
null *
char *
int *
3 Which of the following functions allocates memory and initializes all bits to zero?
free
realloc
calloc
malloc
4
In which memory segment is dynamically allocated memory (using malloc) stored?
5 What is the correct syntax to allocate an array of 10 integers dynamically?
int *ptr = malloc(int, 10);
int *ptr = (int) malloc(10 * sizeof(int));
int *ptr = (int*) malloc(10);
int *ptr = (int*) malloc(10 * sizeof(int));
6
What happens if malloc fails to allocate the requested memory?
NULL.
7
What is the primary purpose of the realloc function?
8 Which function is used to deallocate memory to prevent memory leaks?
free
delete
remove
dealloc
9 What constitutes a Memory Leak?
10
Consider the following code:
c
int p = (int)malloc(sizeof(int));
free(p);
*p = 10;
What is this scenario called?
11
What are the arguments required for the calloc function?
12
What is the result of sizeof(char) in C?
13
Identify the potential issue in this code snippet:
c
void fun() {
char str = (char )malloc(20);
strcpy(str, "Hello");
return;
}
14
If ptr is a NULL pointer, what does free(ptr) do?
15
When initializing a string using char s[] = "Hello";, what is the size of the array s?
16 Which character is used to terminate a string in C?
'\0'
'0'
'.'
'\n'
17
What is the correct format specifier to read a string using scanf without reading spaces?
%d
%c
%s
%str
18
Why is gets() generally avoided or removed in modern C standards?
19 Which function is the safest alternative to read a line of text including spaces?
scanf
fgets
gets
getchar
20
What will be the output of printf("%d", 'c' - 'a'); assuming ASCII encoding?
21
Which logic correctly checks if a character variable ch is a digit?
if (ch >= '0' && ch <= '9')
if (ch == digit)
if (ch >= 0 && ch <= 9)
if (ch > '0')
22
Which header file defines string manipulation functions like strcpy and strlen?
<ctype.h>
<text.h>
<stdlib.h>
<string.h>
23
What does the function strlen(str) return?
str.
str excluding \0.
str.
str including \0.
24
What is the correct syntax to copy string src into dest?
strcpy(dest, src);
strcopy(dest, src);
strcpy(src, dest);
dest = src;
25
What does strcat(s1, s2) do?
26
If strcmp(s1, s2) returns 0, what does it imply?
27
What value does strcmp("Apple", "Banana") likely return?
28 Which function is used to convert a string to an integer?
to_int
str2int
itoa
atoi
29
Consider char *s = "Hello";. What happens if you try s[0] = 'h';?
30
What is the purpose of strchr(str, ch)?
ch from str.
ch in str.
ch in str.
ch in str.
31 Which function converts a character to uppercase?
upper()
to_upper()
toupper()
strupr()
32 How do you calculate the memory size required to store a string of length ?
33 Which function finds the first occurrence of a substring within a string?
strsub
strstr
strrchr
strchr
34
What is the result of "10" + "20" in C?
35
What is the logic to convert a digit character c ('0'-'9') to its integer value?
c - 0
c - '0'
c + '0'
c
36
Which code correctly iterates through a string s?
for(i=0; s[i] != NULL; i++)
for(i=0; s[i] > 0; i++)
for(i=0; s[i] != '\0'; i++)
for(i=0; i < s; i++)
37
What happens in strcpy(str, "VeryLongString...") if str is declared as char str[5];?
38
What does strncpy offer over strcpy?
39 Which function is used to tokenize a string (split it by delimiters)?
strbreak
strsplit
strcut
strtok
40 When defining an array of strings, which declaration is valid?
char *names[5];
char names[5][20];
41
What is the return type of sizeof operator?
void
double
size_t
int
42 How do you correctly free a 2D array allocated via pointers to pointers?
free_all.
43
Given char str[] = "Code";, what is *(str + 1)?
44
What happens if you define a string as char s[5] = "Hello";?
45
Which loop correctly converts a string s to lowercase?
s = tolower(s);
for(int i=0; s[i]; i++) s[i] = s[i] - 32;
for(int i=0; s[i]; i++) tolower(s[i]);
for(int i=0; s[i]; i++) s[i] = tolower(s[i]);
46
What is the output of printf("%s", "Hello" + 2);?
47
Why must you cast the return value of malloc in C++ but not strictly in C?
void*.
void*, C++ does not.
48 Which format specifier reads a single character?
%d
%c
%chr
%s
49
If realloc returns NULL, what happens to the original memory block?
50
What is the ASCII value of the null terminator \0?