Study questions for Lecture Test 2 CS 115
These will not be collected. They are not guaranteed to be 100% of the test material. You can do these and discuss them with your TA or Dr. Keen.
if (speed < 40) cout << "slow"; else if (speed < 55) cout << "legal"; else cout << "ticket";Write a test plan for this code. Rewrite this code so that it does not use nested if's. (Hint: boolean/logical operators)
x = 10; if (x = 0) cout << "no"; else cout << "yes";
if (str == "OK") if (num > 5) cout << " white"; else cout << "blue";Write a test plan for this code. What are the possible outputs of this code? what are values that would cause them?
float f1 (int a, int b);
int main()
{
float v1;
int v2, v3, v4;
v2 = 5;
v3 = v2 * 2;
v4 = v2 + v3;
v1 = f1( v3, v4);
cout << v1 << f1 (v2, v4) << endl;
return 0;
}
float f1 (int a, int b)
{
return (a + b) / 2.0;
}
int fun1 (int p1)
{
cout << "red ";
return p1 + 3;
}
int fun2 (int p2)
{
cout << "green ";
return fun1(p2) * 3;
}
int main()
{
cout << fun2 (12) << endl;
cout << "yellow";
return 0;
}
a. char Answer = 'y';
while (Answer = 'y')
{
n++;
cout << "another loop? y/n";
cin >> Answer;
}
b. i = 0;
while (i < n)
{
n ++;
cout << i;
}
// Add numbers entered until sentinel -1 is encountered.
cin >> n;
while (n = -1)
sum = sum + n;
cin >> n;
0.0 - 0.1
0.1 - 0.2
0.2 - 0.3
0.3 - 0.4
etc.
0.9 - 1.0
int c = 5;
int x = 2;
cout << "x " << x << " c " << c << endl;
while (c > 0)
{
x = x * 2;
cout << "x " << x << " c " << c << endl;
c = c - 1;
}
int fun3 (int x, char y)
{
int z;
}
int main ()
{
char z;
}
if z is referred to in the main function, what type is it?
if z is referred to in the fun3 function, what type is it?
if x is referred to in the main function, what happens?
if y is referred to in fun3, what type is it?
int age; bool drinking; char state; if (age >= 18) if (state == 'K' || state == 'R') drinking = true; else if (state == 'C' && age > 25) drinking = true; else drinking = false; else if (age > 15) if (state == 'T' || state == 'M') drinking = true; else drinking = false; else drinking = false;
int x = 5, y = 7, z = 9; if (x + 2 > y - 5 && z > 3 || z == 10) cout << "yes"; else cout << "no";
void fun1 (int a);
int fun2 (int b);
int main ()
{
int x;
if (fun1 (x) > 3)
cout << "ok";
fun2 (x);
return 0;
}
if (b == c)
cout << "ok";
b = c + 1;
else
cout << "no";
b = c - 1;