CPU Business and Information Technology College
Fundamentals of programming Test II
Time allowed 1:15
Name _______________________________________ Id___________
Part I
Multiple Choice (1 points each)
_____1.The standard input stream, which refers to the keyboard, is called
a. cin b. cout c. return d. a and b
_____2.The body of a C++ function is surrounded by _____
a. parentheses b. angle brackets c. curly brackets d. square brackets
_____3.what c++ statement is used to display data on the monitor?
a.cin b.cout c.both
____4. Which for-loop will run a variable from 5 to 105 in steps of 4?
a. for(int i = 5; i <= 105; i += 4)
b. for(int i == 5; i > 105; i = i + 4)
c. for(int i == 5; i < 105; i = i + 4)
d. for(int i = 5; i > 105; i += 4)
_____5.Which expression will cause the while-loop to loop until x equals 1 or init is not 'g'?
a.while(x == 1 && init != 'g')
b.while((x == 1) || (init != 'g'))
c.while((x != 1) && (init == 'g'))
_____6.A do-loop is different from a while loop because
a. do-loops have a preset number of cycles for looping.
b. the code in a do-loop is executed at least one time.
c. while loops will only check the condition after each pass.
d. the break statement works in while-loops but not in do-loops
_____7.Which statement checks that an integer dig falls between the characters 1 and 9 ?
a. if(dig >= 1 && dig <= 9)
b. if(dig >= 1) else (dig <= 9)
c. if(dig > 1 || dig < 9)
d. None of the above
______8..You wants to display "too short" if height is less than 4 and "okay" otherwise. Your
best choice is to use a(n)
a. for-loop b. switch-statement c. if/else-statement d. array
______9. What built in data type should be used for real numbers in CPP?
a. int b. float c. double d. a and b e. b and c
______10. What are the values of x and y after the following program segment is executed?
int x = 4 , y = 3;
x++;
y += x++ +
a. x = 6 and y = 8 b. x = 6 and y = 9 c. x = 5 and y = 9 d. None
______11. Which statement is correct?
a. Every CPP statement in the main function body ends with a semi colon.
b. && conjunction returns true if either of the expression is true.
c. In CPP cin displays the value of a variable on the screen.
d. Compiler can identify syntax errors in the program.
______12. If x is 3 and y is y 7, what will be displayed using the following if statement?
if( x == 3)
cout<<”HI”;
else if(y ==7)
cout<<”Bye”
a. HI b. Bye c. both “HI” and “Bye”
_____13.What built in data type do you use if you want to enter alphabet in a variable?
a. alp b. char c. double d. float
______14. 91 % 7 evaluates to
a. 0 b. 1 c. 2 d. 3 e. 4
_____15.Which of the following statements will not affect the value of x?
a. X++; b.X += X --; c. X = X++; d. X = --X+1;
Part II
Write the output of the following code fragments in the space provided (Total 6 pts)
1. What is the output when the following code fragment is executed?(1 pts)
int found = 1, count = 5; Your Answer Here:-
if (!found || --count == 0)
cout << "danger" << “\n”;
cout << "count = " << count ;
2. Answer the questions below concerning the following fragment of code.(1 pt each)
int n;
cout << "Enter an integer: ";
cin >> n;
if (n < 10)
cout << "less than 10" << endl;
else if (n > 5)
cout << "greater than 5" << endl;
else
cout << "not interesting" << endl;
a.What will be the output of the fragment above if the interactive user enters the integer value 0 ?
Your Answer Here:-
b.What will be the output of the fragment above if the interactive user enters the integer value 15 ?
Your Answer Here:-
c. What will be the output of the fragment above if the interactive user enters the integer value 7 ?
Your Answer Here:-
d.What values for n will cause the output of the fragment above to be "not interesting"?
Your Answer Here:-
Part III
Write a complete CPP Program on the attached Sheet of paper clearly.(4 pts)
1. Write a CPP program which accepts two integers and display the largest.(2 pts)
2. Write a CPP program which reads three integers using array variable num and display their sum.
(2 pts)
Answers:- Part I: Multiple Choice
1. a. cin
2. c. curly brackets
3. b. cout
4. a. for(int i = 5; i <= 105; i += 4)
5. b. while((x == 1) || (init != 'g'))
6. b. the code in a do-loop is executed at least one time.
7. a. if(dig >= 1 && dig <= 9)
8. c. if/else-statement
9. e. b and c
10. c. x = 5 and y = 9
11. a. Every CPP statement in the main function body ends with a semi colon.
12. a. HI
13. b. char
14. b. 1
15. c. X = X++;
Part II: Code Fragments
1. Code Fragment
cpp
CopyEdit
int found = 1, count = 5;
if (!found || --count == 0)
cout << "danger" << “\n”;
cout << "count = " << count;
Your Answer Here:
Output:
count = 4
Explanation: Since !found evaluates to false, the condition is false, and the body of the if
statement is skipped. The count variable is decremented before being displayed.
2. Code Fragment
cpp
CopyEdit
int n;
cout << "Enter an integer: ";
cin >> n;
if (n < 10)
cout << "less than 10" << endl;
else if (n > 5)
cout << "greater than 5" << endl;
else
cout << "not interesting" << endl;
a. If the user enters 0:
Your Answer Here: less than 10
b. If the user enters 15:
Your Answer Here: greater than 5
c. If the user enters 7:
Your Answer Here: greater than 5
d. What values for n will cause the output to be "not interesting"?
Your Answer Here: No values, as the conditions for "not interesting" are never satisfied.
Part III: Complete C++ Programs
1. Program to accept two integers and display the largest
cpp
CopyEdit
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
if (num1 > num2)
cout << "The largest number is: " << num1 << endl;
else if (num2 > num1)
cout << "The largest number is: " << num2 << endl;
else
cout << "Both numbers are equal." << endl;
return 0;
}
2. Program to read three integers using an array and display their sum
cpp
CopyEdit
#include <iostream>
using namespace std;
int main() {
int num[3], sum = 0;
cout << "Enter three integers: ";
for (int i = 0; i < 3; i++) {
cin >> num[i];
sum += num[i];
}
cout << "The sum of the three integers is: " << sum << endl;
return 0;
}