User Tools

Site Tools


haas:spring2014:common:ka1

C/C++ Programming Knowledge Assessment

0x0

For the following values, list the smallest possible C data type it can fit into:

  • 32
  • 562
  • -32769
  • 98435984
  • 3.14
  • -65
  • 31415926535897
  • -24764

0x1

Analyze the following code, and answer the questions:

1
#include <stdio.h>
#define SIZE 31
 
int main()
{
    char data[SIZE];
    int i;
 
    printf("Enter a string of up to %d letters (upper and lowercase), NO spaces: ", (SIZE-1));
    scanf("%s", data);
 
    for(i = 0; i < SIZE; i++)
    {   
        if (data[i] != 0)
        {
            if ((data[i] == 97) || (data[i] == 101) || (data[i] == 105) || (data[i] == 111) || (data[i] == 117))
            {
                data[i] = data[i] - 32;
            }
        }
        else
        {
            break;
        }
    }
 
    printf("After processing, the string is: %s\n", data);
 
    return(0);
}
  • What does this program do?
  • What do both the if() statements in the loop do?
  • How does the program travel through the string?
  • How does the program change its data? Why?
  • What condition would cause the else to be evaluated?

0x2

Analyze the following code, and answer the questions:

1
#include <stdio.h>
#define SIZE 31
 
int main()
{
    char data[SIZE];
    int i, j = 0;
 
    printf("Enter a string of up to %d letters (upper and lowercase), NO spaces: ", (SIZE-1));
    scanf("%s", data);
 
    for(i = 0; i < SIZE; i++)
    {
        if (data[i] != 0)
        {
            if ((data[i] == 101) || (data[i] == 69))
            {
                j = j + 1;
            }
        }
        else
        {
            break;
        }
    }
 
    printf("After processing, j is: %d\n", j);
 
    return(0);
}
  • What does this program do? (What information does it provide?)
  • What is the inner if()-statement looking for?

0x3

Analyze the following code, and answer the questions:

1
#include <stdio.h>
#define SIZE 31
 
int main()
{
    char data[SIZE];
    int i, j = 0, k = 0;
 
    printf("Enter a string of up to %d letters (upper and lowercase), NO spaces: ", (SIZE-1));
    scanf("%s", data);
 
    for(i = 0; i < SIZE; i++)
    {
        if (data[i] != 0)
        {
            if ((data[i] >= 65) && (data[i] <= 90))
            {
                j = j + data[i];
                k++;
            }
        }
        else
        {
            break;
        }
    }
 
    i = j / k;
    j = i;
 
    printf("After processing, j is: %c\n", j);
 
    return(0);
}
  • What does this program do? (What information does it provide? However nonsensical.)
  • What is the inner if() statement looking for?
  • In what format is this program displaying j?

0x4

Analyze the following code, and answer the questions:

1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
#define SIZE 31
 
int main()
{
    char data[SIZE];
    int i, j = -1, k = 0;
 
    srand(time(NULL));
 
    for(i = 0; i < SIZE; i++)
    {
        data[i] = rand() % 2599 + 1;
    }
 
    for(i = 0; i < SIZE; i++)
    {
        if (data[i] > j)
            j = data[i];
    }
 
    printf("After processing, j is: %d\n", j);
 
    return(0);
}
  • What does this program do? (What information does it provide?)
  • What specifically is the first for() loop doing?
  • What is the second for() loop doing?
haas/spring2014/common/ka1.txt · Last modified: 2013/10/11 16:41 by 127.0.0.1