HIGHER INSTITUTE OF TECHNOLOGY-10th of RAMADAN
EXAM DURATION: 60 MINUTES
COMPUTER SCIENCE DEPARTMENT
Apr. 2015
STRUCTURED PROGRAMMING
Exam 2
*Student Name:
* Student ID:
Mark:
30
_________________________________________________________________________________________ Q1: MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) The ________ causes a program to wait until information is typed at the keyboard and the Enter key is pressed. A) cin object B) preprocessor C) cout object D) output stream E) None of these Answer: A 2) The ________ operator always follows the cin object, and the ________ operator follows the cout object. A) conditional, binary B) >>, << C) <<, >> D) binary, unary E) None of these Answer: B 3) Which line in the following program will cause a compiler error? 1 2 3 4 5 6 7 8 9 10
#include <iostream> using namespace std; int main( ) { const int MY_VAL = 77; MY_VAL = 99; cout << MY_VAL << endl; return 0; }
A) 7
B) 6
C) 8
Answer: A
1
D) 9
4) After execution of the following code, what will be the value of input_value if the value 0 is entered at the keyboard at run time? cin >> input_value; if (input_value > 5) input_value = input_value + 5; else if (input_value > 2) input_value = input_value + 10; else input_value = input_value + 15;
A) 25
B) 0
C) 5
D) 15
E) 10
Answer: D 5) Which line in the following program will cause a compiler error? 1 2 3 4 5 6 7 8 9 10
#include <iostream> using namespace std; int main( ) { const int MY_VAL; MY_VAL = 77; cout << MY_VAL << endl; return 0; }
A) 8
B) 9
C) 6
D) 7
C) x * 2;
D) None of these
Answer: C 6) Which statement is equivalent to the following? x = x * 2; A) x *= 2;
B) x = x * x;
Answer: A 7) Which statement is equivalent to the following? number += 1; A) number = 1; C) number + 1;
B) number = number + 1; D) None of these
Answer: B
2
8) What is the value of average after the following code executes? double average; average = 1.0 + 2.0 + 3.0 / 3.0; A) 4.0
B) 6.0
C) 1.5
D) 2.0
Answer: A 9) What will the value of x be after the following statements execute? int x = 0; int y = 5; int z = 4; x = y + z * 2; A) 18
B) 0
C) 13
D) unknown
Answer: C 10) What will the value of result be after the following statement executes? result = 6 - 3 * 2 + 7 - 10 / 2 ; A) 6
B) 1.5
C) 2
D) 8
Answer: C 11) Assume that x is an int variable. What value is assigned to x after the following assignment statement is executed? x = -3 + 4 % 6 / 5; A) 1 B) -3 C) 0 D) 2 E) None of these Answer: B 12) In the following C++ statement, what will be executed first according to the order of precedence? result = 6 - 3 * 2 + 7 - 10 / 2 ; A) 7 - 10
B) 3 * 2
C) 6 - 3
Answer: B
3
D) 2 + 7
E) 10 / 2
13) What will be the output of the following code segment after the user enters 0 at the keyboard? int x = -1; cout << "Enter a 0 or a 1 from the keyboard: "; cin >> x; if (x) cout << "true" << endl; else cout << "false" << endl; A) Nothing will be displayed. C) true
B) false D) x
Answer: B 14) What is the output of the following segment of code if 4 is input by the user when asked to enter a number? int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 4: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout << total << endl;
A) 28 B) 3 C) 13 D) 0 E) None of these Answer: C 15) This operator is known as the logical OR operator. A) -B) || C) // D) # E) None of these Answer: B 16) Given that x = 2, y = 1, and z = 0, what will the following cout statement display? cout << "answer = " << (x || !y && z) << endl; A) answer = 1
B) answer = 0
C) answer = 2
Answer: A 4
D) None of these
17) Which line in the following program will cause a compiler error? 1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream> using namespace std; int main( ) { int number = 5;
}
if (number >= 0 && <= 100) cout << "passed.\n"; else cout << "failed.\n"; return 0;
A) 6
B) 9
C) 10
D) 8
C) 99
D) 0
C) false
D) -1
Answer: D 18) What is the output of the following code? int w = 98; int x = 99; int y = 0; int z = 1; if (x >= 99) { if (x < 99) cout << y << endl; else cout << z << endl; } else { if (x == 99) cout << x << endl; else cout << w << endl; }
A) 98
B) 1
Answer: B 19) What is the value of the following expression? true && false A) true
B) +1
Answer: C
5
20) This operator performs a logical NOT operation. A) >< B) -C) <> D) ! E) None of these Answer: D 21) In C++ the = operator indicates: A) equality B) negation C) subtraction D) assignment E) None of these Answer: D 22) What will the following segment of code output? score = 40; if (score > 95) cout << "Congratulations!\n"; cout << "That's a high score!\n"; cout << "This is a test question!" << endl; A) Congratulations! That's a high score! This is a test question! B) That's a high score! This is a test question! C) Congratulations! That's a high score! D) This is a test question! E) None of these Answer: B 23) This operator is used in C++ to represent equality. A) >< B) !! C) == D) = E) None of these Answer: C 24) This statement lets the value of a variable or expression determine where the program will branch to. A) select B) switch C) associative D) scope E) None of these Answer: B
6
25) Without this statement appearing in a switch construct, the program "falls through" all of the statements below the one with the matching case expression. A) switch B) scope C) exit D) break E) None of these Answer: D 26) What will the following segment of code output? You can assume the user enters a grade of 90 from the keyboard. cout << "Enter a test score: "; cin >> test_score; if (test_score < 60); cout << "You failed the test!" << endl; if (test_score > 60) cout << "You passed the test!" << endl; else cout << "You need to study for the next test!";
A) You passed the test! B) You failed the test! C) You failed the test! You did poorly on the test! D) You failed the test! You passed the test! E) None of these Answer: D 27) What will following segment of code output? int x = 5; if (x = 2) cout << "This is true!" << endl; else cout << "This is false!" << endl; cout << "This is all folks!" << endl; A) This is true! This is all folks! B) This is true! C) This is false! D) This is true! This is false! E) None of these Answer: A
7
28) What is assigned to the variable a given the statement below with the following assumptions: x = 10, y = 7, and z, a, and b are all int variables. a = x >= y; A) 0 B) 7 C) The string "x >= y" D) 10 E) 1 Answer: E 29) Which of the following expressions will determine whether x is less than or equal to y? A) x <= y B) x > y C) x =< y
D) x >= y
Answer: A 30) What will be the value of result after the following code has been executed? int a = 60; int b = 15; int result = 10; if (a = b) result *= 2; A) 20 C) 120
B) 10 D) This code will not compile.
Answer: A
8