Download CS141 Sample Test Questions for Test #1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Location arithmetic wikipedia , lookup

Transcript
CS141 Sample Test Questions for Test #1
These are sample questions for the first test in cs141. This is just a sampling. All material covered so far
in the course is “fair game” for the test.
Assignments and Arithmetic Expressions
Determine the value for x after each set of statements.
1.)
int x;
x = 3.2;
Answer:
ints can only hold whole numbers, so the answer is 3.
2.)
float x = 1;
x = x/2;
Answer:
x is a float and 2 is an int – mixed mode expression. The integer is promoted to a float and the result
1.0/2.0 = .5 is stored in x.
3.)
int x;
x = x - x;
Answer:
x has not been initialized, but anything minus itself is zero.
4.)
int x;
x = 5 / 3;
Answer:
5/3 is integer division, so the answer is 1.
5.)
float x;
x = 3.2 / 1;
Answer:
3.2 divided by 1 is a mixed mode expression. The int 1 is promoted to a float and the answer is 3.2.
6.)
float x;
int y = 1.0;
int z = 2.0;
x = y/z;
Answer: 1.0 and 2.0 are converted to ints when the are stored in y and z. y/z is integer division, so
the answer is 0.
7.)
int x = 1;
float y = 2.0;
x = x / y;
Answer: 1 divided by 2.0 is .5, but x cannot store anything but whole numbers, so the result is 0.
Determine the value of x and y after each set of statements.
8.)
int x = 1;
y = x++;
Answer: x = 2, y = 1.
9.)
int x = 1;
y = ++x;
Answer: y=2, x=2.
11.)
int x = 2;
int y = 3;
y = ++x + ++y;
Answer: x = 3, y = 7.
12.)
int x = 7 % 3;
Answer: The remainder of 7 divided by three is 1. (3 goes into 7 twice with one remainder).
Scoping Rules
13.) What value will this program print?
#include <iostream.h>
void main()
{
int x = 10;
{
x = 11;
int x = 12;
}
cout << x << "\n";
}
Answer: 11: The second declaration of x has gone out of scope.
If then-else
14.) Leap years are years that are divisible by 4, but not by 100, unless they are also divisible by 400.
Write an if-statement to determine if a given year is a leap year.
Answer:
if ( ( ((year % 4) == 0) && ((year %100) != 0) ) | |
( (year %400) == 0)
)
cout << “year is a leap year\n”;
15.) Odd numbered months before August have 31 days. Even numbered months after and including
August have 31 days. Use this rule to write a single if-statement to determine if a month has 31 days.
Answer:
enum {JANUARY=1, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST,
SEPTEMBER,
OCTOBER, NOVEMBER, DECEMBER};
if ( ((month < AUGUST) && ((month %2) == 1)) ||
((month >= AUGUST) && ((month %2) == 0))
)
cout << month << “ has 31 days\n”;
Switch statement
16.) Write a switch statement that identifies characters as vowels or consonants. You may assume that the
letter is one or the other. For our purposes, vowels are (a,e,i,o, and u).
Answer:
char letter;
:
switch(letter)
{
case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:
case ‘u’:
cout << letter << “ is a vowel\n”;
break;
default:
cout << letter << “is a consonant\n”;
}
17.) What is wrong with this switch statement which is supposed to tell if a number is 1 or 2.
switch number
{
case 2;
cout << number is two;
case 1;
cout << number is one;
};
Answer:
1.) number should be in parentheses
2.) cases should end with colons rather than semicolons (i.e., case2:, not case2;).
3.) case2 should be followed by a break.
4.) No semicolon is required after the closing curly brace.
5.) Quotes are needed in the cout statements.
Here is the corrected statement:
switch(number)
{
case 2:
cout << “number is two\n”;
break;
case 1:
cout << “number is one\n”;
break;
}
Loops
18.) Write a loop to find the smallest value in a float array called studentScore that has numberOfStudents
values.
Answer:
int i;
const float HIGHEST_POSSIBLE_SCORE = 100;
float smallestSeenSoFar = HIGHEST_POSSIBLE_SCORE;
for(i=0;i<numberOfStudents;i++)
{
if (studentScore[i] < smallestSeenSoFar)
{
smallestSeenSoFar = studentScore[i];
}
}
19.) An array called dailyTemps contains the high temperature for each day as recorded over the last 5
years (1,826 days). Write a loop that counts the number of days the temperature increased from the
previous day. Print the result.
Answer:
const int NUMBER_OF_TEMPERATURES = 1826;
int increases = 0;
int i;
for(i=1;i<NUMBER_OF_TEMPERATURES;i++)
{
if (dailyTemps[i-1] < dailyTemps[i])
{
++increases;
}
}
20.) There are two integer arrays, one called x, the other called y. There are MAX_ELEMENTS elements
in each array. Write a loop that copies all the values from x to y and from y to x such that the ith element
of x becomes the ith element of y and the ith element of y becomes the ith element of x.
Answer:
int i;
int temp;
for(i=0;i<MAX_ELEMENTS;i++)
{
temp = x[i]; // Save x[i] so we can overwrite x[i] without losing the value.
x[i] = y[i];
// Overwrite x[i] with the corresponding y[i].
y[i] = temp; // temp, which has the value from x[i] is now written to y[i].
}
21.) Write a loop that reads data into a float array called studentScore. The loop should terminate when
the user enters a negative value, or when MAX_STUDENTS students have been entered. The variable
numberOfStudents should be used to record the number of student scores entered. You may assume that
MAX_STUDENTS is >= 1.
Answer:
float studentScore[MAX_STUDENTS];
int numberOfStudents = 0;
do
{
cin >> studentScore[numberOfStudents];
++numberOfStudents;
} while( (numberOfStudents < MAX_STUDENTS) &&
(studentScore[numberOfStudents-1] > 0)
);
Arrays (single dimension)
22.) Write a statement to initialize a float array to 1 through 12.
Answer:
float myArray[] = {1,2,3,4,5,6,7,8,9,10,11,12};
23.) Write a loop to write values to an array called mod7 such that the ith element in mod7 contains i mod
7. The array should have size NUMBER_OF_MODS.
Answer:
int i;
int mod7[NUMBER_OF_MODS];
for(i=0;i<NUMBER_OF_MODS;i++)
{
mod7[i] = i % 7;
}
Arrays (two dimensions)
24.) Use a two dimensional array to represent a tic-tac-toe game. Use two nested loops to check all
columns and rows to determine if anyone has won the game.
Answer:
const int NUMBER_OF_ROWS;
const int NUMBER_OF_COLUMNS;
char ticTacToe[NUMBER_OF_ROWS][NUMBER_OF_COLUMNS]; // stores the board state.
// Check all the rows and columns for a won game
int columnNumber;
int rowNumber;
int numberOfXs; // counter for x’s seen in a single row or column
int numberOfOs; // counter for o’s seen in a single row or column
// Check each column
for(columnNumber=0;columnNumber<NUMBER_OF_COLUMNS;++columnNumber)
{
numberOfXs = 0; //
numberOfOs = 0;
// count the x’s and o’s in this column
for(rowNumber=0;rowNumber<NUMBER_OF_ROWS;++rowNumber)
{
if ( ticTacToe[rowNumber][columnNumber] == ‘x’) ++ numberOfXs;
if ( ticTacToe[rowNumber][columnNumber] == ‘o’) ++numberOfOs;
}
if ( (numberOfXs == NUMBER_OF_ROWS) | |
(numberOfOs == NUMBER_OF_ROWS))
cout << “There is a winner along column number “ << columnNumber\n”;
}
// Check each row
for(rowNumber=0;rowNumber<NUMBER_OF_ROWS;++rowNumber)
{
numberOfXs = 0;
numberOfOs = 0;
// count the x’s and o’s in this row
for(columnNumber=0;columnNumber<NUMBER_OF_COLUMNS;++columnNumber
{
if ( ticTacToe[rowNumber][columnNumber] == ‘x’) ++ numberOfXs;
if ( ticTacToe[rowNumber][columnNumber] == ‘o’) ++numberOfOs;
}
if ( (numberOfXs == NUMBER_OF_COLUMNS) | |
(numberOfOs == NUMBER_OF_COLUMNS))
cout << “There is a winner along row number “ << rowNumber\n”;
}
Binary, Octal, and Hex
25.) Determine the Binary, Octal, and Hexadecimal representations for 8421.
Answer: First convert 8421 to binary by dividing by 2 and recording the remainder.
8421 % 2 = 1
4210 % 2 = 0
2105 % 2 = 1
1052 % 2 = 0
526 % 2 = 0
263 % 2 = 1
131 % 2 = 1
65 % 2 = 1
32 % 2 = 0
16 % 2 = 0
8%2=0
4%2=0
2%2=0
1%2=1
Read the bits from bottom to top to get
10000011100101
Take these bits three at a time to get the Octal equivalent.
10 000 011 100 101
2 0 3 4 5
Take these bits four at a time to get the Hexadecimal equivalent.
10 0000 1110 0101
2 0
E
5
26.) What is 0x223 in decimal?
Answer:
2*162+2*161+3*160 = 2*256 + 2*16 + 3*1 = 547
27.) Add the following hex values
234
6c9
________________
Answer: 8fd
28.) Add the following octal values
527
345
_________________
Answer: 1074
29.)
11001010010
0101010111
Answer: 100100000000
Functions
30.) Write a function called “multiply” that multiplies two floating-point parameters and returns the
floating-point product.
31.) Write a function called “multiplyAndReplace” that multiplies two floating-point parameters and
changes the value of the second parameter to the resulting product. Here is an example of how the function
might be used:
float x = 10.0f;
float y = 20.0f;
multiplyAndReplace(x,y);
cout << y << endl; // Prints 200
32.) Pass by value and pass by reference
4.) What does this program print?
void mystery(float& a, float b)
{
a = a + b;
b = b * 2;
cout << a << endl; // cout #1
cout << b << endl; // cout #2
}
int main()
{
float x = 10;
float y = 20;
float z = 100;
mystery(x, y);
cout << z << endl; // cout #3
cout << x << endl; // cout #4
cout << y << endl; // cout #5
}
Answer:
cout #1 _________30_______________
cout #2__________40_______________
cout #3_________100________________
cout #4_________ 30________________
cout #5_________20_______________
6.) Draw the state of the memory model after the program in question 5 has executed.
Answer:
Address
100
104
108
112
Value
30
20
100
40
Data Type
float
float
float
float
Name
x (main), a (mystery)
y (main)
z
b (mystery)