Skip to main content
Logo image

Dive Into Systems: Exercises

Section 1.5 Arrays and Strings

Exercise 1.5.1. Arrays, Multiple-Choice (Q1).

    What is wrong with the following code?
    int arr[10], i;
    arr[0] = 0;
    for (i = 0; i < 10; i++) {
        printf("%d\n", arr[i]);
  • Only the first element is initialized, so this code is printing out undefined values.
  • Correct!
  • This is an off-by-one error; i’s value goes from 0 to 9 inside the loop, but this array’s indices should go from 1 to 10.
  • Incorrect. Array indices start at 0.
  • Only the first element is initialized, so the program will crash.
  • Incorrect. The program does not crash.
  • You cannot printf individual elements of an array, you have to print the whole array at once.
  • Incorrect
  • There is nothing wrong with this code.
  • Incorrect
You might be tempted to run this code for yourself and see what prints out, but this could deceive you into choosing the wrong answer.

Exercise 1.5.2. Arrays, Multiple-Choice (Q2).

    Suppose we have the following array declaration:
    int arr[10];
    How can we increase its size to 100?
  • We can’t. In C, arrays like this are a fixed size.
  • Correct!
  • arr.append(90);
  • Incorrect. The append() method is used for Python lists, not C arrays.
  • for (i = 0; i < 90; i++) { arr.append(0); }
  • Incorrect. The append() method is used for Python lists, not C arrays.
  • arr = int arr[100];
  • Incorrect.
This is an important difference between Python and C

Exercise 1.5.3. Arrays, Parsons Problem (Q1).

Given the following array of integers:
int arr[8];
Write code that will initialize all the elements to 42.

Exercise 1.5.4. Arrays, Parsons Problem (Q2).

Write a snippet of code that averages the number of elements in an int array called arr[100] and then prints out the average. Assume arr is already initialized with data.

Exercise 1.5.5. Arrays, Active Code.

Complete the implementation of the function find_min(int arr[], int len) below. Your functions should find the minimum in the passed array, which will always contain at least one element.

Exercise 1.5.6. Strings, Multiple-Choice (Q1).

    Suppose you have a string named str1 and you want to print it to the screen with a newline following it. How do you do so?
    char str[20];
    int len;
    strcpy(str, "hello there");
    len = strlen(str);
    How would you print str to the screen with a newline following it?
  • printf("%s\n",str);
  • Correct!
  • printf("%str\n",s);
  • Incorrect.
  • printf("%s %d\n",str,len);
  • Incorrect
  • printf("%s",str);
  • Incorrect, but close! This does not include a newline at the end!
Make sure you’re familiar with the "format string" system used by printf.

Exercise 1.5.7. Strings, Multiple-Choice (Q2).

    What is wrong with the following code?
    char str1[10];
    int len;
    str1[0] = 'h';
    str1[1] = 'i';
    str1[2] = '0';
    len = strlen(str1);
  • The string is not null terminated, the character ’0’ is not the same as ’\0’
  • Correct!
  • It should be the letter ’O’ instead of the character ’0’ to end the string
  • Incorrect. Character O is not what we are looking for. How are strings terminated?
  • Nothing is wrong, len is set to 2
  • Incorrect
  • Nothing is wrong, len is set to 3
  • Incorrect
This question will trigger undefined behavior, so it’s possible that if you try running the code you might get the wrong answer. Or it might crash!

Exercise 1.5.8. Strings (strlen).

Given the code snippet:
char str1[4];

str1[0] = 'a';
str1[1] = 'b';
str1[2] = '\0';
str1[3] = 'd';
What does a call to strlen(str1) return?
A string is terminated by the null character.

Exercise 1.5.9. Strings (strcpy).

Given the following code snippet:
char str[SIZE];

strcpy(str, "hello C");
What is the smallest possible value of SIZE that would ensure that strcpy would have enough space to store the copied string?

Exercise 1.5.10. Strings (strcat).

Suppose you want to concatenate two strings: a string str1 that contains the string "Hello" (which requires 6 bytes to store), and a string str2 that contains the string "World" (which also requires 6 bytes to store).
How many bytes would be needed to hold the concatenated string?
Hint 1.
If you strcat "Hello" and "World" together, you will get "HelloWorld" not "Hello World".
Hint 2.
Remember you need a byte to store the null terminator at the end of the string too.

Exercise 1.5.11. Strings, Active Code.

Write a new program that implements your own custom version of a string copy function that takes a destination and a source string and copies the source string to the destination string. Test it out in main by making calls to your string copy function with different string inputs and printing out the string copy results after each call.