First Exam Solution (prepared by Dr. Amer Al-Badarneh) 1.
____ the algorithm refers to the process of translating the algorithm into a language that the computer can understand.
a.
Assembling
b.
2.
A programmer evaluates a program by ____ it on the computer.
a.
desk-checking
3.
Which of the following is true about the ALU?
a.
b.
Tracing
c.
running
Translating
d.
Coding
d.
compiling
c.
hand-tracing
It is considered the brain of the computer.
b.
The control unit is one of its components
c.
It caries out logical and arithmetic operations
d.
It stores the program counter
4.
The function of the compiler is to
a.
check if there is no syntax error
b.
translate the program code from high level language to machine language
c.
load the executable code to main memory
5.
To process a C++ program it should follow the following procedures
a.
Editor, Preprocessor, Linker, Compiler, Loader, Execution
b.
Editor, Preprocessor, Compiler, Linker, Loader, Execution
c.
Editor, Compiler, Preprocessor, Linker, Loader, Execution
d.
Editor, Linker, Compiler, Preprocessor, Loader, Execution
6.
The statement ____ tells the computer to get the current weekly pay from the keyboard, and store
d.
a and b
that amount in the currentPay variable. a.
cin >> currentPay;
b.
cin << currentPay;
c.
currentPay >> cin;
d.
currentPay << cin;
7.
What is the output of the following code? void main(){ int x; //
double y; x= 4; y=5; cout<<x+y<<endl;
} a.
Error: Undeclared identifier
b.
9
c.
9.00
d.
9.0
8.
Which of the following is not a keyword in C++?
a.
const
9.
Which of the following is an illegal C++ identifier (variable)?
a.
7_bottles
10.
What is the result of following code?
b.
long
b.
c.
if
_7_Bottles
d.
c.
Return
seven_bottles
d.
_Bott
cout <<static_cast<double>(static_cast<int>(22.5 - 3.5 )%5)+22.3; a.
26.3
b.
22
11.
What is the result of following code?
c.
22.3
d.
5
int x=2, y=4; x *= (2+y)*x; cout << x; a.
24
12.
A C++ program that prints 3 lines
a.
must contain 3 cout statements.
b.
must contain 2 cout statements.
c.
must contain 1 cout statement.
d.
may contain 1 or more cout statements
13.
Which of the following is a correct C++ statement?
a.
if x==1 cout << ”mustafa”
b.
if(x==2) cout << ”55”;
c.
if(x==1) cin << ”Just_just”
d.
if(x==6) cout >> ”cs155”;
14.
Which one of the following is a valid assignment?
a.
7edu = edu +1
b.
edu +7 = edu
c.
edu *= edu +7
d.
edu =* edu +7
15.
What is the output of
a.
-4
16.
What is the output of the following code?
a.
5
b.
30
c.
18
27
cout<<(4 % 2 * 6 / 3 - 5 + (7/4)); ? b.
-3.25
c.
const int x=5;
//line #1
x++;
//line #2
cout<<x<<endl;
//line #3
b.
d.
6
c.
-3
Error with line #2
d.
0
d.
Error with line #1
17.
Which of the following is equivalent to
a.
value = value + sum;
b.
sum = sum + 1; value = value + sum;
c.
value = value + sum;
d.
value = value + ++sum;
18.
What is the output produced by the following code fragment
?
value += sum++;
sum = sum + 1;
int i = 1, j = 2, k = 3; cout << i + static_cast<double>(5 % (j + 2)) / k << endl; a.
1
b.
1.33333
c.
19.
____ is an example of a selection structure.
a.
"drop the balloon in the red box"
b.
"repeat until you are directly in front of the chair"
c.
"repeat 20 times"
d.
"if the balloon is red, do this"
20.
The following is true about the logical expression
x%10 > 9
a.
It can be true or false, depending on the value of x
b.
c.
It is always false
21.
The statement that compares the value of an integer called sum against the value 65, and if it is less,
d.
0
d.
0.33333
.
It is always true
It is illegal because % is not a standard C++ operator
prints the text string "Sorry, try again", is a.
if (sum < "65")
cout << "Sorry, try again";
b.
if (sum <= 65)
cout << "Sorry, try again";
c.
if (65 == sum)
cout << "Sorry, try again";
d.
if (sum < 65)
cout << "Sorry, try again";
22.
The statement that compares total for equality to good_guess, and if equal prints the value of total, and if not equal prints the value of good_guess, is
a.
if (total < good_guess) cout << total; else cout << good_guess;
b.
if (total == good_guess) cout << good_guess; else cout << total;
c.
if (total = good_guess) cout << total); else cout << good_guess;
d.
if (total == good_guess) cout << total; else cout << good_guess;
23.
Consider the following C++ code. How much is added to premiumDue if driverAge is 22 and numTickets is 0? if(driverAge < 26)
premiumDue += 100;
if(driverAge > 50)
premiumDue -= 50;
if(numTickets == 2)
premiumDue += 60.25;
a.
-50
b.
10.25
c.
24.
When does the following code display “Yes”?
60.25
d.
100
if(department == 1 || 2) cout<<"Yes"; a.
When department is 1
b.
When department is 1 or 2
c.
Always
d.
Never
25.
What is the output of the following code int x=5, y=7; if(x < y++ || y++ <=10) cout<<x<<" else
"<<y<<endl;
cout<<"It is False"<<endl;
a.
5 9
b.
5 8
c.
5 7
d.
It is False
26.
The expression if (num != 65) cannot be replaced by
a.
if (num > 65 || num < 65)
b.
if ( !(num == 65))
c.
if (num – 65)
d.
if ( !(num – 65))
27.
What is the output of the following? int a = 5, b = 0; if (a) cout<< 7; cout<< 6; if (b){ cout<< 3; cout<< 5; }
a.
76
b.
35
28.
What is the output of the following code?
c.
635
d.
7635
d.
Nothing
if(–5) cout<<"OK"; else cout<<”Cancel”; a.
OK
b.
Cancel
c.
OK Cancel
29.
Consider the following pseudo code. If the sales amount is 15000, the value of the bonus amount at the end of the algorithm will be ____. 1. enter sales amount 2. calculate the bonus amount by multiplying the sales amount by .08 3. if (the sales are greater than or equal to 10000) else
add 150 to the bonus amount add 125 to the bonus amount
4. display the bonus amount a.
720
b.
845
c.
1200
d.
1350
30.
Write a program that prompts the user to input a number. The program should then output the number and a message saying whether the number is positive, negative, or zero. #include <iostream> using namespace std; void main(){ int number; cout << " Enter a number<<endl; cin >> number; cout << "The number "<< number; if ( number > 0 ) cout << " is positive" << endl; if ( number < 0 ) cout << " is negative" << endl; if ( number == 0 ) cout << " is zero" << endl; }
31.
Write a program that prompts the user to input a number. The program should then output the number and a message saying whether the number is even, odd, or zero. #include <iostream> using namespace std; void main(){ int number; cout << " Enter a number<<endl; cin >> number; cout << "The number "<< number; if ( number % 2 == 0 ) cout << " is Even" << endl; if ( number % 2 != if ( number == 0 ) }
0 ) cout << " is Odd" << endl; cout << " is zero" << endl;
32.
Find the errors in the following program? Re-write the code making it free from syntax errors.
Line1:
include <iostream>
// missing #
// missing using namespace std; Line2:
int main(
// missing );
{ Line3:
int x;
Line4:
cout << "Enter a value for x" << endl;
Line5:
cin > x;
// missing >
Line6:
x = 5 - 3
// missing ;
Line7:
return 0;
}
33.
Find the errors in the following program? Re-write the code making it free from syntax errors. Line1: #include <iostream> Line2: using namespace std; Line3: int mian() {
// change mian to main
Line4:
const char ch = 'A';
// remove const or
Line5:
int x;
Line6:
x = ch;
Line7:
y = x++
Line8:
ch = 'B';
Line9:
cout << "x: " << x << "\ty: " << y << "\tch: " << ch;
Line10: }
return 0;
// missing ; // assign to a constant identifier