Unit 5 - Practice Quiz
CSE109
1
Which header file must be included to use dynamic memory allocation functions like malloc and calloc?
<stdio.h>
<stdlib.h>
<string.h>
<mem.h>
2
What is the return type of the malloc function?
int *
char *
void *
null *
3 Which of the following functions allocates memory and initializes all bits to zero?
malloc
realloc
calloc
free
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 = (int*) malloc(10);
int *ptr = (int*) malloc(10 * sizeof(int));
int *ptr = malloc(int, 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?
delete
dealloc
remove
free
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?
'.'
'\n'
'\0'
'0'
17
What is the correct format specifier to read a string using scanf without reading spaces?
%c
%s
%str
%d
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
gets
fgets
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 >= '0' && ch <= '9')
if (ch == digit)
if (ch > '0')
22
Which header file defines string manipulation functions like strcpy and strlen?
<stdlib.h>
<string.h>
<ctype.h>
<text.h>
23
What does the function strlen(str) return?
str.
str including \0.
str excluding \0.
str.
24
What is the correct syntax to copy string src into dest?
strcopy(dest, src);
strcpy(src, dest);
strcpy(dest, src);
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?
itoa
atoi
str2int
to_int
29
Consider char *s = "Hello";. What happens if you try s[0] = 'h';?
30
What is the purpose of strchr(str, ch)?
ch in str.
ch in str.
ch from str.
ch in str.
31 Which function converts a character to uppercase?
to_upper()
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?
strchr
strrchr
strstr
strsub
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
c - 0
c - '0'
c + '0'
36
Which code correctly iterates through a string s?
for(i=0; s[i] != '\0'; i++)
for(i=0; i < s; i++)
for(i=0; s[i] != NULL; i++)
for(i=0; s[i] > 0; 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)?
strsplit
strtok
strbreak
strcut
40 When defining an array of strings, which declaration is valid?
char names[5][20];
char *names[5];
41
What is the return type of sizeof operator?
int
double
size_t
void
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?
for(int i=0; s[i]; i++) s[i] = tolower(s[i]);
for(int i=0; s[i]; i++) s[i] = s[i] - 32;
for(int i=0; s[i]; i++) tolower(s[i]);
s = tolower(s);
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?
%s
%c
%d
%chr
49
If realloc returns NULL, what happens to the original memory block?
50
What is the ASCII value of the null terminator \0?