Practice Questions for Exam 1

The following are a set of questions to help you prepare for Exam 1. I recommend trying out these problems with paper and pencil. Remember that any material from the first four weeks is eligible, whether or not it appears in a problem below.

Topics these questions cover:

  • linux basics
  • documentation (preconditions & postconditions)
  • testing (assertions, test cases, black vs. white box)
  • basic C code (scanf for example)
  • loops
  • models of computation
  • arrays
  • basic types (characters, integers)
  • conditionals and booleans
  • integers and floating point numbers in C

Problem 1

Consider the following snippet of C code. The function numDigits takes a non-negative integer as a parameter and returns the number of digits that number contains. For example:

  • 40 has two digits
  • 123456 has six digits
  • 7 has one digit
#include <stdio.h>
#include <assert.h>

/* function numDigits
* preconditions:
*
*
* postconditions:
*
*
*/
int numDigits (int n){
  // insert an assert statement below




  // insert function details below







}//numDigits

int main(void){
  // an example of calling numDigits
  num = 10347;
  int dig;
  dig = numDigits(num); // I expect dig = 5
  printf("num = %d has %d digits \n", num, dig);

  return 0;
}//main

Part a:

Fill in the documentation for preconditions, postconditions, and one assertion corresponding to your precondition.

Part b:

Write the function numDigits which takes in a positive integer n as a parameter and returns the number of digits of n. Your function does not need to consider the case where n is not a positive integer. Hint: For a positive number, we can determine the number of digits by repeatedly dividing by 10.

Part c:

Come up with a list of test cases to assess the accuracy of your solution. Describe whether your tests could be described as black box or white box and why.

Problem 2

Write a series of Linux commands to complete the following set of tasks:

  • determine your current working directory
  • create a new folder named newFolder
  • move to the home directory
  • list the files in the current directory
  • make a copy of the file test.c named test2.c

Imagine that the below box is your terminal window on a Linux shell. Each appearance of the $ symbol indicates a command prompt. Hence, 5 appearences of $ for each of the 5 commands you’ve been asked to write.

shaw some-directory$

/home/user/some-directory

shaw some-directory$

shaw some-directory$

shaw ~$

csc151 csc161 test.c

shaw ~$

Problem 3

Consider the following C code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>

void fun1(int a[], int b){
  for(int i = 0; i< b; i++){
    a[i] = 0;
  }
}

int fun2(int a, int b){
  int squar = a*a;
  return 0;
}

int fun3(int a, int b){
  fun2(a,b);
  b = fun2(b,a);
  return b;
}

int main(void){
  int a,b;
  a = 1;
  b = 7;
  int c[5] = {2,4,6,8,10};

  b = fun3(a,b);
  fun1(c,5);
}

Part a

Write out a stack diagram for when the program counter is 17

Part b

Write out a stack diagram for when the program counter is 27

Part c

Write out a stack diagram for when the program counter is 28

Problem 4

Consider the following snippet of C code.

#include <stdio.h>
#include <stdbool.h>

/* A program that will read in an array of characters from the
* user, and then determine if every element of the array is the
* character equivalent to the index.
* For example:
*   [0,1,2,3]
*   [0]
*   [0, 1]
*   [0, 1, 2, 3, 4, 5, 6, 7, 8]
*/
int main(void){
  int size;
  bool validArray;

  // Ask the user for an integer,
  // save it in size,
  // and create an array of characters of that size







  // Ask the user to input the requested number of characters
  // Then read them in






  // Check whether the array is of the correct format






  // Print out the result
  if(validArray){
    printf("The array was of the correct format");
  }
  else{
    printf("The array was not of the correct format");
  }
  
  return 0;
}

Part a

Fill in the part where you prompt the user for an integer, and then read it in, and then create an array

Part b

Fill in the part where you prompt the user to enter characters, and read them in and save them to your array

Part c

Fill in the part where you determine whether the array is in the correct format. You can use the boolean validArray to keep track of your answer.

Problem 5

This question is about Integers and Floating Point numbers in C. In all cases, assume the IEEE standards for floating points that we learned in class. Also assume we have 8 bits of storage for integers.

Part a

How is 10 versus 10.0 stored in the computer?

Part b

Convert the decimal (base 10) 63.5 into binary (base 2).

Part c

Convert the binary (base 2) number 00111001 into decimal (base 10).

Part d

Convert the binary number from part c into it’s negative using two’s complement.