<mosaic.cnfolio.com>
Introduction to Algorithms and Programming

# Practice exam questions

These practice questions demonstrate typical content and format of the exam questions. Sample solutions will be displayed each time an answer is saved so that you can compare your answer to the sample solution and your previously saved answers.

## QUESTION 1

Write pseudo code for a program to calculate the total amount of money in pounds when provided with a number of 10 pence, 20 pence and 50 pence coins. [10 marks]
Please login to save your answer.

## QUESTION 2

Evaluate the C program provided below. Line numbers have been provided for reference. Indicate specific line numbers in your answers when necessary.
1. Describe all valid input values that would display the following output: [4 marks]
• OrangeGrape
2. List all valid program outputs. [6 marks]

1. int main( void )
2. {
3.   int input = -100;
4.
5.   scanf( "%d", &input );
6.
7.   if ( input > 100 ) printf( "Orange" );
8.   else printf( "Apple" );
9.   if ( input < 200 ) printf( "Grape" );
10. }
Please login to save your answer.

## QUESTION 3

Write a C program that reads an input integer and display the day of the week corresponding to the input integer, where 1 is Sunday, 2 is Monday and 7 is Saturday. The program must display an error message if the input number is not between 1 and 7. [10 marks]
 Please login to save your answer.

## QUESTION 4

Write a C program that reads 3 input variables. If the first input is the addition sign, '+', then display the sum of the second and third input values. If the first input is a minus sign, '⎼', then subtract the third input value from the second input value and display the difference. [10 marks]
 Please login to save your answer.

## QUESTION 5

Evaluate the C program provided below. Line numbers have been provided for reference. Modify the source code to:
1. display a message when the input purchase quantity is higher than the number of DVD copies available. [4 marks]
2. display the total purchase price in British pounds when DVD copies are sold. [6 marks]

1. int main( void )
2. {
3.   int dvdAvailable = 250;
4.   int dvdPurchase = 0;
5.   int dvdPrice = 10; /* in pence */
6.
7.   printf( "How many DVD copies would you like to purchase? " );
8.   scanf( "%d", &dvdPurchase );
9. }
 Please login to save your answer.

## QUESTION 6

Write pseudo code for a program to display all odd whole numbers that are higher than zero and less than 900. [10 marks]
Please login to save your answer.

## QUESTION 7

Write a C program to display the ASCII values of all uppercase letters from A to Z. The program must use a for loop. [10 marks]
 Please login to save your answer.

## QUESTION 8

Write pseudo code for a program to evaluate 4 whole numbers and display a message for each number to indicate whether it is a multiple of 3, 5 or 7. [10 marks]
Please login to save your answer.

## QUESTION 9

Find the errors in the C program below. Re-write the complete program with changes to fix the errors. Line numbers have been provided for reference. [10 marks]

1. #include <stdio.h>
2.
3. int main( void );
4. {
5.   char model[] = 'desktop';
6.   int price = 400.50;
7.   long quantity = "11";
8.
9.   printf( "There are %d %s computers on sale for %.2f each.", quantity, price, model );
10. }
 Please login to save your answer.

## QUESTION 10

Write a C program that keeps asking for an input string until the length of the string is an even number. [10 marks]
 Please login to save your answer.

## QUESTION 11

Write a C function that implements the prototype provided below:
void displayMonth( int );

The function must display the full name of the month that matches the value of the input parameter. For example, the function must display June when 6 is the input parameter. The function must display a brief error message when the input parameter is invalid. [10 marks]
 Please login to save your answer.

## QUESTION 12

Write a C function that implements the prototype provided below:
float convert( char, float );

If the first parameter is 'K', the function must convert the second parameter from Kelvin to Fahrenheit and return the calculated value. If the first parameter is 'F', the function must convert the second parameter from Fahrenheit to Kelvin and return the calculated value. Use the formula below to convert the temperature values. [10 marks]
Kelvin = 5 / 9 * ( Fahrenheit + 459.67 )
 Please login to save your answer.

## QUESTION 13

Find the errors in the C program below. Re-write the complete program with changes to fix the errors. Line numbers have been provided for reference. [10 marks]

1. #include <stdio.h>
2.
3. int main( void )
4. {
5.   char code = "a";
6.   int flag = 31;
7.
8.   if ( code = "a" )
9.   {
10.     if ( flag => 20 ) printf( "Part 2 is done" );
11.
12.     if ( flag =! 30 ) printf( "Part 3 is done" );
13.   }
14.   then
15.   {
16.     printf( "Part 1 is done" );
17.   }
18. }
 Please login to save your answer.

## QUESTION 14

Evaluate the C program provided below. Line numbers have been provided for reference.
1. Describe all input values for which the program would not produce output. [2 marks]
2. List the program output when the input value is 8. [8 marks]

1. #include <stdio.h>
2.
3. int main( void )
4. {
5.   int counter, max;
6.
7.   scanf( "%d", &max );
8.
9.   for ( counter = 0; counter <= max; counter++ )
10.   {
11.     if ( ( counter % 3 ) < 2 ) printf( "%d", counter );
12.     else printf( "%d", counter - 1 );
13.   }
14. }
Please login to save your answer.

## QUESTION 15

Modify the following source code to correct all errors. Line numbers have been provided for reference. Indicate specific line numbers in your answers when necessary. [10 marks]

1. #include <stdio.h>
2.
3. int main( void )
4. {
5.   int marks[ 5 ] = { 72, 54, 61, 49, 67, 69 }
6.   int count;
7.
8.   for ( counter == 0; counter <= 6; counter++ )
9.   {
10.     if ( marks[ counter ] >= 70 ) { printf( "A \n" ); continue; }
11.     if ( marks[ counter ] >= 60 ) { printf( "B \n" ); continue; }
12.     if ( marks[ counter ] >= 50 ) { printf( "C \n" ); continue; }
13.     if ( marks[ counter ] >= 40 ) { printf( "D \n" ); continue; }
14.   }
15. }
 Please login to save your answer.

## QUESTION 16

Write a C function that implements the prototype provided below:
int count_letters_z( char* );

The function must return the number of 'z' characters found in the input word. For example, the function would return a value of 1 for the input string "zero". [10 marks]
 Please login to save your answer.