CSCI 152: Introduction to Computer Science
Quiz 1 Fall 2007

Student Name:

  • Date: Monday, February 12, 2007
  • Duration: 50 minutes
  • Open book, open notes
  • Quiz 1 consists of Two Parts
  • Part I –60 points –4short questions
  • Part II - 40 points - 2 open question

Part I: (70 points)

  1. Question 1 (15 points): What is the output of the following code?

#include < stdio.h >

int recFun (int a, int b );

int main( ){

int a = 3, b = 5;

printf ("%d\n", recFun(a, b) );

return 0;

}

int recFun (int a, int b){

if ( a == 0 )

return 1;

else

return ( b * recFun (a - 1, b) );

}

Answer:

Question 2 (15 points): What is the output of the following code?

#include < stdio.h >

#define SIZE 10

int main( ){

int a[SIZE] = {1, 3, 2, 5, 0, -9, 8, -8, 4, 7}, i, counter = 0;

for (i = 0; i < SIZE; i ++){

if ( a[i] % 3 == 0){

counter ++;

}

}

printf("%d\n", counter);

return 0;

}

Answer:

Question 3 (15 points): What is the output of the following code?

#include < stdio.h >

#define SIZE 10

int main( ){

int a[SIZE] = {0, 3, 0, 5, 0, -9, 0, -8, 4, 7}, i, counter = 0;

for (i = 0; i < SIZE; i ++){

if ( a[i] ) {

counter ++;

}

}

printf("%d\n", counter);

return 0;

}

Answer:

Question 4 (15 points): What is the output of the following code fragment?

#include < stdio.h >

void recFun (char letter, int k);

int main( ){

int k = 5;

char letter = ‘B’;

recFun(letter, k);

return 0;

}

void recFun (char letter, int k){

if ( k > 0){

printf ("%c", letter );

recFun (letter, k-1);

}

}

Answer:

Part II - 40 points

Question 1 (20 points)

Write a program that reads an array of 10 integers and finds the sum and product of all even elements. Write your answer here:

Question 2 (20 points)

Write a program that reads an array of 10 characters or less and prints the input string in the strange way: each letter will be printed three times. For example, if the input is yana the output should be yyyaaannnaaa