College of Science in AL-Zulfi
Department Computer Science& Information
Object oriented programming
First Exam
Student Name: ______
ID number: ______
Question / Mark1
2
3
4
Total
Question 1: Fill the best answers for each question? (9 mark)
Number / A / B / C / D1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1. What will be printed by the following code?
int i=10, j, k;
j = --i - i--;
k = i-- + --i;
cout < i;
a. 10 b. 8
c. 6 d. 4
2. What will be printed by the following code?
int i=7, j=8, hsb=9;
if (i = j) hsb = i;
else hsb = j;
cout < i < " " < j < " " < hsb < "\n";
a. 7 8 7 b. 8 8 8
c. 7 8 8 d. 7 9 9
3. Given X= 5 and Y=7, which of the following independent expressions evaluates to -1?
a. X += Y - -Y;
b. X += -Y - -Y;
c. X -= --Y;
d. X -= Y++;
4. Which of the following statements prints 5?
a. cout < (3 + 15%7 + 7%4);
b. cout < (100/20 + 100%20);
c. cout < (5*5 – 20%5);
d. cout < (40%4 – 11%5);
5. What is the output of the following code, assuming that x is an integer variable with an initial value of 12:
if(x = 6) cout < x;
a. 6 b. 12
c. nothing d. a syntax error is produced
6. What will be the value of c after the following code executes?
int c = 9, x = 17;
if (3*c > 2*x) c += 2;
c = c + 2;
a. 8 b. 11 c. 23 d. 27
7. What does the following code fragment print?
int h = 10;
if (h <= 12)
cout < "Low bridge: ";
cout < "proceed with caution.";
a. Nothing is written.
b. Low bridge:
c. proceed with caution.
d. Low bridge: proceed with caution.
8. What the output of the following code segment?
int x = 0, y = 1, z=2;
if(x) z++; else ++z;
if(y) z++; else ++z;
cout < z;
a. 2 b. 3
c. 4 d. 5
9. What is the ouput of the following code?
int a = 7;
if(a = 0){ if (a==22) cout < a;}
else cout < a;
cout < 88;
a. 88 b. 788
c. 880 d. 088
10. How many of the following variable names are valid in C++?
For
score&
7_years_ago
_we
beat_FSU
A. 1
B. 2
C. 3
D. 4
11. How many of the following variable names are valid in C++?
1. _x
2. 4xy
3. if
4. x+y
5. _9y
A. 1 and 5
B. 2 and 4
C. 1, 3 and 5
D. 2, 3 and 4
12. What is the output for the C++ program?
#include <iostream.h>
int main (void) {
int x;
float y;
x=2.8;
y=4.1;
cout<x+y;
return 0;
}
A. 6.9
B. 6.1
C. 6.8
D. 7.1
E. 7.0
13. Which are valid variable names in C++?
1. quantity
2. #Account
3. Quantity
4. _taxchange2
A. 1 and 2
B. 1, 2 and 3
C. 1, 3 and 4
D. 2, 3 and 4
E. 3 and 4
14. What is the value of n?
int j,k,l,m,n;
j=4
cin > j;
k=3+j;
l=j+k;
m=l*k;
n=m+3;
cout<n;
A. 47
B. 80
C. 77
D. 52
E. 59
15. What is the output?
#include <iostream.h>
int main (void) {
int a,b,c;
float x,y;
a=3/5;
b=1.2;
c=a+b;
y=x=2*c;
cout<x+y;
return 0;
}
A. 0
B. 2
C. 4
D. 4.8
E. 7.2
16. Which of the following lines uses incorrect syntax?
1. cout<x<endl
2. cin>y;
3. cout>_rate;
4. cin>endl;
A. 1,3
B. 1,2,3,4
C. 1,2
D. 1,3,4
E. 3,4
17. What is the output of the following C++ program?
#include <iostream.h>
int main(void)
{
int a = 6;
int b = 5;
cout < a / b < ' ';
cout < a % b < ' ';
cout <(a | b)< ' ';
cout <(a & b)< '\n';
return 0;
}
A. 1 1 6 4
B. 1 1 7 4
C. 1 0 7 5
D. 1 0 7 6
E. 0 1 7 5
18. What is the output of the following C++ program?
#include <iostream.h>
int main(void) {
for( int j = 1; j < 6; ++j) cout < j++;
cout < endl;
return 0;
}
A. 135
B. 1357
C. 246
D. 2468
E. crash
19. What is the output of the following C++ program?
#include <iostream.h>
int main(void)
{
int i, sum = 0;
for (i = 0; i--< 3; sum += i++) sum += i++;
cout < sum;
return 0;
}
A. 3
B. 6
C. 9
D. 13
E. 11
20. What is the output of the following C++ program?
#include <iostream.h>
int main(void) {
int i = 2, j = 4;
while (j++ > i) i += 2;
cout < i < ' ' < j < endl;
return 0;
}
A. 6 7
B. 6 5
C. 4 5
D. 5 5
E 6 6
Q2: What does the following program print?
#include <iostream>
int main()
{
int count = 1;
while ( count <= 10 ) {
cout < (count % 3 ? "****" : "++++")< endl;
++count;
}
return 0;
}
Q3: 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>
int main()
{
int n;
cin>n;
return 0;
}
Q4:Find the errors in the following program? Re-write the code making it free from syntax errors.
Line1: include <iostream>
Line2: int main()
{
Line3: int x;
Line4: cout < "Enter a value for x <\n;
Line5: cin < x;
Line6: x = 5 – 3
Line7: return 0;
}
9