Test 2 CSCI 130
Summer 2010
- Please answer each question fully, carefully and thoughtfully.
- No credit will be given for unreadable answers, please write neatly.
- Answer the questions in any order, but please number each answer.
- Make sure that your name is on your test.
- (3 points) Give the syntax for the if - else statement.
- (3 points) Give the flow of control for the if - else statement.
- (4 points) Write a code segment which prints the word even if the integer variable
myNumber
contains an even number and the word odd if the variable contains an odd number.
- (3 points) Give the syntax for the constructed if - else if - elsestatement.
- (4 points) Write a code segment which prints positive if the integer variable myNumber is positive, negative if the value is negative and zero if the value is zero.
- (3 points) Describe the switch statement.
- (4 points) Write a code segment which given an integer
digit
which contains a number between 0 and 9, prints out the name of the number. For example, if digit = 3
the word three should be printed.
- (3 points) Give the syntax for the while statement.
- (3 points) Describe the flow of control for the while statement.
- (4 points) Write a code segment which will print the first 100 multiples of three. (3,9,12, ...)
- (3 points) Give the syntax for the do-while statement.
- (3 points) Give the syntax for the for statement.
- (10 points) The file Data contains a set of numeric data, all floating point numbers. Write a code segment which will find the average of the data in the file.