جامعة البلقاء التطبيقية كلية الحصن الجامعية قسم تكنولوجيا المعلومات Al- Balqa Applied University Al-huson University College Dept. of Information Technology First Exam of Programming Language (C++) Name : -------------------------------------Date : 28/10/2014 Number : ----------------------------------Instructor: ------------------------Q1)[10 marks] Solve the following 10 Multiple Choice Questions: Q# 1 2 3 4 5 6 7 8 9 10 Sol. A C B B C D B D A C 1. The result of the following code segment is : int x2=5, y=3; int z= x2 + y *(4-x2%y+1); cout<< "the value of z="<<z<<endl; A B 2. the value of z=14 the value of z=5 A B 3. 8 C 7 6 D 5 The result of the following code segment is : int x= 2; int y; // x=x*2; y= x++; cout << "X=" << x << "\t y=" << y; X=4 y=5 C X=4 X=3 y=2 D X=5 The result of the following code segment is : int x=3; switch (x++) { case 1: cout<<'A' << " "; break; case 2: cout<<"AA"<<" "; break; case 3: cout<<"AAA"<<" "; default: cout<<"AAAA"<<endl; } A AA AAA AAAA C AAA AAA AAAA D AA A B 4. A B C the value of z=11 D the value of z=12 The result of the following code segment is : int x=7 ; if( x=8 ) cout<<x-1; else cout<<x+1; y=4 y=4 5. The result of the following code segment is : cout<<"This Exam is\r very easy"<<endl; A B 6. A B 7. C very easyis D Exam very easy The result of the following code segment is : int x=1,y=2; cout<<++x<<endl; cout<<y--<<'\t'<<x++<<endl; cout<<++x-y<<endl; 2 C 1 1 2 2 1 2 3 This Exam is\r very easy very easy 2 1 2 D 3 2 2 3 2 The result of the following code segment is : int x = 2; int y = 0; for ( ; y < 10; ++y) { if (y % x == 0) continue; else if (y == 8) break; else cout<<y ; } A B 8. 8 C 0 13579 D 02468 The result of the following code segment is : int num = 100; while (num <= 150) num = num + 5; cout << num << endl; A B 9. A B 150 C 160 165 D 155 Suppose j, sum, and num are int variables, and the input is 26 34 61 4 -1. What is the output of the code? sum = 0; cin >> num; for (int j = 1; j <= 4; j++) { sum = sum + num; cin >> num; } cout << sum << endl; 125 124 C D 126 122 10. The result of the following code segment is : for(int i=0;i<=5;i++){ for(int j=5;j>i;j--) cout<<"$"; cout<<endl;} A C $$$$$ $$$$$ $$$$$ $$$$$ $$$$$ $ $$ $$$ $$$$ $$$$$ B D $$$$$ $$$$ $$$ $$ $ None of them Q2) [2 marks] Convert the following code written using if .... then structure into switch case statements: If structure int x; cin>>x; if ((x>=0) &&(x<=2)) cout<<"This course is C++\n"; else if (x<=4) cout<<"This collage is HUC \n"; else cout<<"This University is BAU \n"; Switch case structure int x; cin>>x; switch (x) { case 0: case 1: case 2: cout<<"This course is C++\n"; break; case 3: case 4: cout<<"This collage is HUC \n"; break; default: cout<<"This University is BAU \n"; } Q3) [3 marks] find the syntax error and the logical error in the following C++ code: Line # 1 Statements Error correction int x==5; == (wrong assignment operator) int x=5; 2 float 5x; 5x (wrong name of identifier) x5 3 for(int i=100; i >0;i++) cout<<i<<endl; Logical error (infinite loop) for(int i=100; i >0;i--) 4 if (x=5) cout<<++x; int x$=3.5; Logical error (x=5) not logical operator 2 errors : 1- x$ (wrong if (x==5) cout<<++x; 5 1- x 6 int x=1; while (x<10); { x++ ; cout<<x<<endl; } name of identifier) 2- wrong value to the int X$=3.5; Logical error (infinite loop) Semicolon after while statement 2- x=3; int x=1; while (x<10) { x++ ; cout<<x<<endl; } Q4)[3 marks] Write C++ program to calculate the value of y where 𝑥2 𝑦 = { 2𝑥 −𝑥 𝑥=1 𝑥 = 100 } 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 [Hint: use switch case statement is your program and write a complete program] #include<iostream> using namespace std; int main() { int x,y; cin>>x; switch(x) { case 1: y=x*x; break; case 100: y=2*x; break; default: y=-1*x; break; } return 0; } Q5) [3 marks] Write a program in C++ to compute the sum of the following series, then print the result on the screen: 1 +3+5+...+99 [Hint: Use for statement in your program] #include<iostream> using namespace std; int main() { int sum =0; for (int i=1;i<=99;i+=2) sum+=i; cout<<"The value of the summation = "<<sum<<endl; return 0; }