0x00. C - Hello, World
Quiz questions
Question #0
Which of the following are both valid comment syntaxes in ANSI C, and Betty-compliant?
Question #1
What is the common extension for a C header file?
Question #2
What are the different steps to form an executable file from C source code
Question #3
In which category belongs the C programming language?
Question #4
Which command can be used to compile a C source file?
Question #5
What is the common extension for a C source file?
0x01. C - Variables, if, else, while
Quiz questions
Question #0
Which of the following are valid
for
statements in ANSI C and Betty-compliant?
(Considering a
and b
two
variables of type int
)
Please select all correct answers
Question #1
What is the size of the float
data type?
Question #2
Which of the following are valid while
or
do/while
statements in ANSI C and
Betty-compliant? (Considering a
and
b
two variables of type int
)
Please select all correct answers
Question #3
What is the size of the unsigned int
data
type?
Question #4
Which of the following are valid
if
statements in ANSI C and Betty-compliant?
(Considering a
and b
two
variables of type int
)
Please select all correct answers
Question #5
What is the size of the char
data type?
0x02. C - Functions, nested loops
Quiz questions
Question #0
What is the ASCII value of 0
?
Question #1
What is the result of 12 % 10
?
Question #2
What is the ASCII value of a
?
Question #3
What is the result of 12 % 2
?
Question #4
What is the ASCII value of 5
?
Question #5
What is the ASCII value of -
?
Question #6
Which of these loop statements don’t exist?
Question #7
What is the ASCII value of J
?
Question #8
What is the result of 12 % 3
?
Question #9
What is the result of 89 % 7
?
Question #10
What is the ASCII value of A
?
0x03. C - Debugging
Quiz questions
Question #0
The following code gives this output. What is the error?
carrie@ubuntu:/debugging$ cat main.c
#include <stdio.h>
/**
* main - debugging example
* Return: 0
*/
int main(void)
{
int i;
int j;
int k;
i = 0;
j = 1000;
while (i < j)
{
k = j / 98;
i = i + k;
printf("%d\n", i);
j == j - 1;
}
return (0);
}
carrie@ubuntu:/debugging$
carrie@ubuntu:/debugging$ gcc -Wall -Werror -Wextra -pedantic main.
c
main.c: In function ‘main’:
main.c:20:3: error: statement with no effect [-Werror=unused-value]
j == j - 1;
^
cc1: all warnings being treated as errors
carrie@ubuntu:/debugging$
Question #1
Look at the following code.
carrie@ubuntu:/debugging$ cat main.c
#include <stdio.h>
/**
* main - debugging example
* Return: 0
*/
int main(void)
{
char *hello = "Hello, World!";
for (i = 0; hello[i] != '\0'; i++)
{
printf("%c", hello[i]);
}
printf("\n");
return (0);
}
carrie@ubuntu:/debugging$
carrie@ubuntu:/debugging$ gcc -Wall -Werror -Wextra -pedantic main.
c
main.c: In function ‘main’:
main.c:11:7: error: ‘i’ undeclared (first use in this function)
for (i = 0; hello[i] != '\0'; i++)
^
main.c:11:7: note: each undeclared identifier is reported only once for each function it appears in
main.c:9:8: error: variable ‘hello’ set but not used [-Werror=unused-but-set-variable]
char *hello = "Hello, World!";
^
cc1: all warnings being treated as errors
carrie@ubuntu:/debugging$
In the main.c
file, on what line is the first
error that the compiler returns?
Tips:
You do not have to know exactly what this code does yet (but you will soon!).
Question #2
This code doesn’t work as intended.
#include "main.h"
/**
* main - prints even numbers from 0 to 100
* Return: 0
*/
int main(void)
{
int i;
for (i = 0; i < 100; i++)
{
if (i % 2 != 0)
{
continue;
}
else
{
break;
}
printf("%d\n", i);
}
return(0);
}
Let’s add printf
statements to the code. What
information do the printf
statements tell us
about how our code is executed?
#include "main.h"
/**
* main - prints even numbers from 0 to 100
* Return: 0
*/
int main(void)
{
int i;
printf("Before loop\n");
for (i = 0; i < 100; i++)
{
if (i % 2 != 0)
{
printf("i is not even so don't print\n");
continue;
}
else
{
printf("i is even, break to print\n");
break;
}
printf("Outside of if/else, still inside for loop\n");
printf("%d\n", i);
}
printf("For loop exited\n");
return(0);
}
Question #3
The following code gives this incorrect output. Which of the following statements about what is causing the error is true?
carrie@ubuntu:/debugging$ cat main.c
#include <stdio.h>
/**
* main - debugging example
* Return: 0
*/
int main(void)
{
int i;
int j;
for (i = 0; i < 10; i++)
{
j = 0;
while (j < 10)
{
printf("%d", j);
}
printf("\n");
}
return (0);
}
carrie@ubuntu:/debugging$
carrie@ubuntu:/debugging$ gcc -Wall -Werror -Wextra -pedantic main.c
carrie@ubuntu:/debugging$ ./a.out
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 <...>
^Ccarrie@ubuntu:/debugging$
0x04. C - More functions, more nested loops
Quiz questions
Question #0
What is the output of the following piece of code?
int i;
i = -9;
while (i < 0)
{
printf("%d", -i);
i++;
}
Question #1
What is the output of the following piece of code?
int i;
for (i = 0; i < 10; i++)
{
printf("%d", i * 2);
}
Question #2
What is the return value of the following function?
int some_function(void)
{
printf("%d", 12);
return (98);
}
Question #3
What is the output of the following piece of code?
int i;
i = 9;
while (--i)
{
printf("%d", i);
}
Question #4
What is the output of the following piece of code?
int i;
i = 9;
while (i--)
{
printf("%d", i);
}
Question #5
What is the output of the following piece of code?
int i;
for (i = 48; i < 58; i++)
{
printf("%c", i);
}
Question #6
What is the output of the following piece of code?
int i;
i = 0;
while (i < 10)
{
i++;
printf("%d", i / 2);
}
Question #7
What is the return value of the following function?
int some_function(void)
{
int i;
for (i = 0; i < 10; i++)
{
printf("%d", i);
}
return(i);
}
Question #8
What is the output of the following piece of code?
int i;
i = 0;
while (i < 10)
{
printf("%d", i % 2);
i++;
}
0x05. C - Pointers, arrays and strings
Quiz questions
Question #0
The process of getting the value that is stored in the memory location pointed to by a pointer is called:
Question #1
What is the value of n
after the following
code is executed?
int n = 98;
int *p = &n;
*p = 402;
Question #2
If we have a variable called var
of type
int
, how can we get its address in memory?
Question #3
What is the value of n
after the following
code is executed?
int n = 98;
int *p = &n;
Question #4
What happens when one tries to access an illegal memory location?
Question #5
We declare the following variable
int arr[5];
What is the size in memory of the variable
arr
?
Question #6
Is it possible to declare a pointer to a pointer?
Question #7
What is the size of a pointer to a char
(on a
64-bit architecture)
Question #8
What is the identifier to print an address with
printf
?
Question #9
What is the value of n
after the following
code is executed?
int n = 98;
int *p = &n;
p = 402;
Question #10
What is the value of n
after the following
code is executed?
int n = 98;
int *p = &n;
*p++;
Question #11
We declare the following variable
int arr[5];
What is the equivalent of typing arr[2]
?
Question #12
What is the size of a pointer to an int
(on a
64-bit architecture)
0x06. C - More pointers, arrays and strings
Quiz questions
Question #0
Why is it important to reserve enough space for an extra character when declaring/allocating a string?
Question #1
What is wrong with the following code?
int n = 5;
int array[n];
int i = 3;
array[n] = i;
Question #2
What happens when one tries to dereference a pointer to NULL?
Question #3
What is/are the difference(s) between the two following variables? (Except their names)
char *s1 = "";
char *s2 = NULL;
Question #4
var = "Best";
What is the type of var
?
Question #5
What is wrong with the following code?
int n = 5;
int array[5];
int i = 3;
array[n] = i;
Question #6
What is wrong with the following code?
int n = 5;
int array[10];
int i = 3;
array[n] = i;
0x07. C - Even more pointers, arrays and strings
Quiz questions
Question #0
In this following code, what is the value of
a[3][1]
?
int a[5][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
Question #1
In this following code, what is the value of
a[0][0]
?
int a[5][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
Question #2
What is the size of *p
in this code?
int *p;
Question #3
What is the size of *p
in this code?
int **p;
Question #4
In this following code, what is the value of
a[1][1]
?
int a[5][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
Question #5
What is the size of p
in this code?
int *p;
Question #6
In this following code, what is the value of
a[3][0]
?
int a[5][2] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}};
Question #7
What is stored inside a pointer to a pointer to an int?
Question #8
What is the size of p
in this code?
int **p;
0x08. C - Recursion
Quiz questions
Question #0
What does this code print?
int print(int nb)
{
if (nb < 0)
{
return (0);
}
printf("%d", nb + print(nb - 1));
nb --;
return (nb);
}
int main(void)
{
print(4);
return (0);
}
Question #1
What does this code print?
void print(int nb)
{
printf("%d", nb);
nb ++;
if (nb < 10)
{
print(nb);
}
}
int main(void)
{
print(4);
return (0);
}
Question #2
What does this code print?
void print(int nb)
{
printf("%d", nb);
nb --;
if (nb > 0)
{
print(nb);
}
}
int main(void)
{
print(2);
return (0);
}
Question #3
What does this code print?
void print(int nb)
{
if (nb < 0)
{
return;
}
printf("%d", nb);
nb --;
print(nb);
}
int main(void)
{
print(4);
return (0);
}
Question #4
What does this code print?
void print(int nb)
{
printf("%d", nb);
-- nb;
if (nb > 0)
{
print(nb);
}
}
int main(void)
{
print(4);
return (0);
}
0x09. C - Static libraries
Quiz questions
Question #0
What command(s) can be used to list the symbols stored in a static library?
Question #1
What command is used to create a static library from object files?
Question #2
What is the format of a static library?
Question #3
What is the point of using ranlib
?
0x0A. C - argc, argv
Quiz questions
Question #0
In the following command, what is argv[2]
?
$ ./argv My School is fun
Question #1
What is argv[0]
Question #2
In the following command, what is argv[2]
?
$ ./argv "My School is fun"
Question #3
In the following command, what is argv[2]
?
$ ./argv "My School" "is fun"
Question #4
What is argc
?
Question #5
What is argv[argc]
?
Question #6
What is argv
?
0x0B. C - malloc, free
Quiz questions
Question #0
How many bytes will this statement allocate?
malloc(sizeof(char) * 10)
Question #1
How many bytes will this statement allocate?
malloc(sizeof(int) * 10)
Question #2
How many bytes will this statement allocate?
malloc((sizeof(char) * 10) + 1)
Question #3
How many bytes will this statement allocate?
malloc(10)
Question #4
What is Valgrind?
Question #5
How many bytes will this statement allocate?
malloc(sizeof(unsigned int) * 2)
Question #6
How many bytes will this statement allocate?
malloc(sizeof(int) * 4)
0x0C. C - More malloc, free
Quiz questions
Question #0
What is wrong with this code:
int cp(void)
{
char *s;
s = malloc(12);
strcpy(s, "Best School");
return (0);
}
Question #1
What will you see on the terminal?
int main(void)
{
int *ptr;
*ptr = 98;
printf("%d\n", *ptr);
return (0);
}
Question #2
malloc returns a pointer
Question #3
You can do this:
char *s;
s = strdup("Best School");
if (s != NULL)
{
free(s);
}
Question #4
You can do this:
char str[] = "Best School";
free (str);
Question #5
You can do this:
free("Best School");
Question #6
To allocate enough space for an array of 10 integers (on a 64bit, Linux machine), I can use:
Question #7
malloc returns an address
Question #8
The memory space reserved when calling
malloc
is on:
Question #9
If I want to copy the string “Best School” into a new space in memory, I can use this statement to reserve enough space for it (select all valid answers):
0x0D. C - Preprocessor
Quiz questions
Question #0
This portion of code is actually using the library stdlib.
#include <stdlib.h>
Question #1
The macro __FILE__
expands to the name of the
current input file, in the form of a C string constant.
Question #2
This code will try to allocate 1024 bytes in the heap:
#define BUFFER_SIZE 1024
malloc(BUFFER_SIZE)
Question #3
The preprocessor generates object code
Question #4
NULL
is a macro
Question #5
What is the gcc
option that runs only the
preprocessor?
Question #6
This is the correct way to define the macro
SUB
:
#define SUB(a, b) a - b
Question #7
The preprocessor removes all comments
Question #8
What are the steps of compilation?
Question #9
The preprocessor links our code with libraries.
Question #10
What will be the last 5 lines of the output of the command
gcc -E
on this code?
#include <stdlib.h>
int main(void)
{
NULL;
return (EXIT_SUCCESS);
}
Question #11
What does the macro TABLESIZE
expand to?
#define BUFSIZE 1020
#define TABLESIZE BUFSIZE
#undef BUFSIZE
#define BUFSIZE 37
Question #12
Why should we use include guards in our header files?
Question #13
The preprocessor generates assembly code
Question #14
What will be the output of this program? (on a standard 64 bits, Linux machine)
#include <stdio.h>
#include <stdlib.h>
#define int char
int main(void)
{
int i;
i = 5;
printf ("sizeof(i) = %lu", sizeof(i));
return (EXIT_SUCCESS);
}
0x0E. C - Structures, typedef
Quiz questions
Question #0
Those two codes do the same thing:
typedef struct point point;
struct point {
int x;
int y;
};
point p = {1, 2};
typedef struct point point;
struct point {
int x;
int y;
};
point p = { .y = 2, .x = 1 };
Question #1
You should write documentation for all the structures you create
Question #2
Given this code:
struct point {
int x;
int y;
};
struct point my_point = { 3, 7 };
struct point *p = &my_point;
To set the member y of my variable my_point to 98, I can do (select all valid answers):
Question #3
The general syntax for a struct declaration in C is:
struct tag_name {
type member1;
type member2;
/* declare as many members as desired, but the entire structure size must be known to the compiler. */
};
0x0F. C - Function pointers
Quiz questions
Question #0
What does a pointer to a function point to (check all correct answers if there is more than one)?
Question #1
If f
is a pointer to a function that takes no
parameter and returns an int
, you can call
the function pointed by f
this way (check all
correct answers if there is more than one):
Question #2
Which one is a pointer to a function?
Question #3
This void (*anjula[])(int, float)
is:
Question #4
To store the address of this function:
void neyo(void);
to the variable f
of type pointer to a
function that does not take any argument and does not
return anything, you would do (check all correct answers
if there is more than one):
0x12. C - Singly linked lists
Quiz questions
Question #0
Arrays Vs Linked Lists: select all true statements
Question #1
What’s the “tail” of a linked list?
Question #2
What’s a node? (select all possible answers)
Question #3
In a singly linked list, what are possible directions to traverse it? (select all possible answers)
Question #4
What’s the “head” of a linked list?
0x14. C - Bit manipulation
Quiz questions
Question #0
0x66 & 0x22 =
?
Question #1
~ 0x98 =
?
Question #2
0x89 >> 3 =
?
Question #3
0x01 & 0x00 =
?
Question #4
0x88 & 0x01 =
?
Question #5
0x89 & 0x01 =
?
Question #6
0x13 << 1 =
?
Question #7
0x44 | 0x22 =
?
Question #8
0x01 & 0x01 =
?
Question #9
0x01 | 0x00 =
?
Question #10
0x02 >> 1 =
?
Question #11
What is 0b001010010
in base10?
Question #12
What is 98
in base16?
Question #13
0x01 << 1 =
?
Question #14
What is 0b01101101
in base16?
Question #15
~ 0x12 =
?
Question #16
What is 98
in base2?
Question #17
What is 0x89
in base2?
Question #18
0x01 | 0x01 =
?
Question #19
What is 0x89
in base10?
0x15. C - File I/O
Quiz questions
Question #0
What is the oflag
used to open a file in mode
read + write?
Question #1
What happens if you try to write “Best” to the standard input on Ubuntu 14.04 LTS?
Tips:
Just try it! :)
Question #2
What is the return value of the system call
open
if it fails?
Question #3
What is the unistd
symbolic constant for the
Standard error?
Question #4
What system call would you use to write to a file descriptor? (select all correct answers)
Question #5
is open
a function or a system call? (select
all valid answers)
Question #6
why? #AlwaysAskWhy
Question #7
What is the correct combination of oflag
s
used to open a file with the mode write only, create it if
it doesn’t exist and append new content at the end if it
already exists?
Question #8
What is the unistd
symbolic constant for the
standard input?
Question #9
What is the oflag
used to open a file with
the mode read only?
Question #10
Without context, on Ubuntu 14.04 LTS,
write
is a … (please select all correct
answers):
Question #11
Most of the time, on a classic, modern Linux system, what
will be the value of the first file descriptor you will
get after opening a new file with open
(if
open
succeeds of course):
Question #12
Which of these answers are the equivalent of
O_RDWR
on Ubuntu 14.04 LTS? (select all
correct answers):
Tips:
Use printf
or read the headers to see the
definitions/values of these macros.
Question #13
What is the unistd
symbolic constant for the
standard output?
Question #14
When I am using
O_WRONLY | O_CREAT | O_APPEND
-> the
|
are bitwise operators.
0x1B. C - Sorting algorithms & Big O
Quiz questions
Question #0
What is the time complexity of this function / algorithm?
var factorial = function(n) {
if(n == 0) {
return 1
} else {
return n * factorial(n - 1);
}
}
Question #1
What is the time complexity of “popping” an element in a queue if you are given a pointer to both the head and the tail of the queue?
Question #2
What is the time complexity of setting the value of the nth element in a singly linked list? (Assuming you have a pointer to the node to set the value of)
Question #3
What is the time complexity of removing at index n in an unsorted array?
Question #4
What is the time complexity of this function / algorithm?
def func(n):
a=5
b=6
c=10
for i in range(n):
for j in range(n):
x = i * i
y = j * j
z = i * j
for k in range(n):
w = a*k + 45
v = b*b
d = 33
Question #5
What is the time complexity of this function / algorithm?
void f(int n)
{
int i;
for (i = 0; i < n; i += 98)
{
printf("[%d]\n", i);
}
}
Question #6
Assuming you have a pointer to the node to insert, what is the time complexity of inserting after the nth element of a doubly linked list?
Question #7
What is the time complexity of removing at index n from an unsorted Python 3 list?
Question #8
What is the time complexity of this function / algorithm?
void f(int n)
{
int i;
int j;
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
{
for (j = 1; j < n; j = j * 2)
{
printf("[%d] [%d]\n", i, j);
}
}
else
{
for (j = 0; j < n; j = j + 2)
{
printf("[%d] [%d]\n", i, j);
}
}
}
}
Question #9
What is the time complexity of this function / algorithm?
void f(unsigned int n)
{
int i;
int j;
for (i = 0; i < n; i++)
{
for (j = 1; j < n; j = j * 2)
{
printf("[%d] [%d]\n", i, j);
}
}
}
Question #10
What is the time complexity of accessing the nth element on an unsorted array?
Question #11
What is the time complexity of this function / algorithm?
foreach($numbers as $number)
{
echo $number;
}
Question #12
What is the time complexity of the “push” operation onto a stack?
Question #13
What is the time complexity of this function / algorithm?
void f(int n)
{
printf("n = %d\n", n);
}
Question #14
What is the time complexity of this function / algorithm?
void f(unsigned int n)
{
int i;
for (i = 1; i < n; i = i * 2)
{
printf("[%d]\n", i);
}
}
Question #15
What is the time complexity of accessing the nth element of a doubly linked list?
Question #16
What is the time complexity of searching for an element in a doubly linked list of size n?
Question #17
What is the time complexity accessing the nth element in an unsorted Python 3 list?
Question #18
What is the time complexity of this function / algorithm?
int Fibonacci(int number)
{
if (number <= 1) return number;
return Fibonacci(number - 2) + Fibonacci(number - 1);
}
Question #19
What is the worst case time complexity of insertion in a hash table with the implementation you used during the previous Hash Table C project (chaining)?
Question #20
What is the time complexity of accessing the nth element of a singly linked list?
Question #21
What is the time complexity of searching for an element in a singly linked list of size n?
Question #22
What is the time complexity of setting value at index n in an unsorted Python 3 list?
Question #23
What is the time complexity of this function / algorithm?
void f(int n)
{
int i;
int j;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
printf("[%d] [%d]\n", i, j);
}
}
}
Question #24
What is the time complexity of “pushing” an element into a queue if you are given a pointer to both the head and the tail of the queue?
Question #25
What is the time complexity of inserting after the nth element of a singly linked list? (Assuming you have a pointer to the node to insert)
Question #26
What is the time complexity of inserting at index n on an unsorted array?
Question #27
What is the time complexity of removing the nth element of a singly linked list? (Assuming you have a pointer to the node to remove)
Question #28
What is the time complexity of searching for an element in a queue of size n if you are given a pointer to both the head and the tail of the queue?
Question #29
What is the time complexity of inserting into an unsorted Python 3 list at index n?
Question #30
Assuming you have a pointer to the node to set the value of, what is the time complexity of setting the value of the nth element in a doubly linked list?
Question #31
What is the time complexity of worst case deletion from a hash table with the implementation you used during the previous Hash Table C project (chaining)?
Question #32
What is the time complexity of searching for an element - worst case - in a hash table with the implementation you used during the previous Hash Table C project (chaining)?
Question #33
What is the time complexity of setting a value at index n in an unsorted array?
Question #34
What is the time complexity of this function / algorithm?
void f(int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("[%d]\n", i);
}
}
Question #35
What is the time complexity of searching for an element in an unsorted array of size n?
Question #36
What is the time complexity of searching for an element in a stack of size n?
Question #37
What is the best case time complexity of insertion in a hash table with the implementation you used during the previous Hash Table C project (chaining)?
Question #38
What is the best case time complexity searching for an element in a hash table with the implementation you used during the previous Hash Table C project (chaining)?
Question #39
What is the time complexity of the “pop” operation onto a stack?
Question #40
What is the time complexity of best case deletion from a hash table with the implementation you used during the previous Hash Table C project (chaining)?
Question #41
Assuming you have a pointer to the node to remove, what is the time complexity of removing the nth element of a doubly linked list?
Question #42
What is the time complexity of searching for an element in an unsorted Python 3 list of size n?