Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 9 Clicker Questions September 29, 2009.

Similar presentations


Presentation on theme: "1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 9 Clicker Questions September 29, 2009."— Presentation transcript:

1 1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 9 Clicker Questions September 29, 2009

2 Assume that each of these appears in a complete C++ program, and that all necessary libraries have been included. Your task: if the code fragment has no errors, select the answer that corresponds to the value of x. If the code fragment won’t compile or will produce a run-time error, choose the answer “error”. double x = 9 % 5; (a) 1.8 (b) 3.0 (c) 4.0 (d) 5.0 (e) error 2

3 Assume that each of these appears in a complete C++ program, and that all necessary libraries have been included. Your task: if the code fragment has no errors, select the answer that corresponds to the value of x. If the code fragment won’t compile or will produce a run-time error, choose the answer “error”. double x = 7 / 2; (a) 3.0 (b) 3.5 (c) 4.0 (d) 2.0 (e) error 3

4 An l-value is a) an expression that can be only be placed on the left of any operator such as +, *, /, etc. b) assigned a value c) can never have a value fetched from it d) is designed for use by a left-handed person 4

5 What programming construct choice would you make for reading a list of an unknown number of homework grades for a single student. a) While b) Do-While c) For 5

6 What programming construct choice would you make for a program that tests a newly coded library function to see how it performs for different values of its arguments. a) While b) Do-While c) For 6

7 What is the value of the bool valued expression, 1 < x < 10? Does the value of this depend on the value of x? Explain, and give the expression that the programmer probably meant. a)This statement is incorrect as it is always false. b)This statement is correct and its value depends on x. c)This statement is incorrect and it is always true d)This statement is incorrect, but it does depend on the value of x. 7

8 Answer: c) This expression is always true. The value does not depend on x. This is the mathematicians’ shorthand for what the programmer probably meant: (1 < x)&&(x < 10) Explanation: The < operator associates (groups) left to right, so the expression evaluates as (1 < x) < 10. The expression (1 < x) evaluates to either false or true, regardless of x. These bool values convert to int values 0 or 1 when compared to int value 10. The expression evaluates to true for all values of x. 8

9 In a switch statement, when a break statement is encountered, an immediate transfer of control is made to a)the default case of the switch statement b) a goto statement c) the else clause d) the statement beyond the end of the switch statement e) none of these 9

10 In distinguishing an expression as true or false, C++ sees which of the following as true? (select all that work) a)True b)0 c)1 d)Any non-zero value e)The character 'F‘ 10

11 Answer: a), c), and d) e) ‘F’ is coerced char to bool, perhaps through an intermediate type 11

12 Which of the following determines the operator that is processed prior to another operator? a)Operator associativity b)Operator precedence c)Whether the operator is an arithmetic operator d)None of these determine the order in which operators are processed. 12

13 Which of the following loop statements is guaranteed to iterate the body of the loop at least once? a)while(control) body; b)do body while(control); c)for (initialize; test; update) body; d)none of the above e)all of the above 13

14 If this code fragment were executed in an otherwise correct and complete program, what would the output be? int a = 3, b = 2, c = 5; if (a > b) a = 4; if ( b > c) a = 5; else a = 6; cout << a < endl; (a) 3 (b) 4 (c) 5 (d) 6 (e) None of the above, the cout statement belongs to the else and so is skipped. 14

15 If the following code fragment is executed in an otherwise complete and correct program, which expression will be executed? Why? x = 0; if (x = 12) yes_statement; else no_statement; a)The no_statement will be executed because x is not 12. b)The statement has incorrect syntax so will not compile at all. c)x=12 is illegal in the Boolean expression of an if statement. d)The yes_statement will be executed. 15

16 In the expression (j > 0 && j+1 == 10), which operator executes last? a)> b)&& c)+ d)== 16

17 Display 2.3 Precedence of Operators (1 of 4) 2-17 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

18 Display 2.3 Precedence of Operators (2 of 4) 2-18 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

19 Display 2.3 Precedence of Operators (3 of 4) 2-19 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

20 Display 2.3 Precedence of Operators (4 of 4) 2-20 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

21 The statements int x = 1; int y; y = x++; (select all that apply) a)Assign y the value 2; b)Change the value of x to 2. c)Assign y the value 0; d)Assign y the value 1; e)The ++ is a postfix operator. 21

22 The statements int x = 1; int y; y = x++; (select all that apply) a)Assign y the value 2; b)Change the value of x to 2. c)Assign y the value 0; d)Assign y the value 1; e)The ++ is a postfix operator. Answer: b) d) and e) 22

23 Which of the following overloadings will be invoked by this call? g(1,2); a)int g(int count, double value); b)void g(double value, int count); c)void g(int value, int count); d)Neither, the compiler cannot decide which of these to use. 23

24 Which of the following function declarations with default arguments are correct? (select all that apply) a)void g(int length, int width, int height = 1); b)void g(int length=1, int width, int height); c)void g(int length, int width=1, int height = 1); d)void g(int length=1, int width=1, int height); 24

25 Which of the following function declarations with default arguments are correct? (select all that apply) a)void g(int length, int width, int height = 1); b)void g(int length=1, int width, int height); c)void g(int length, int width=1, int height = 1); d)void g(int length=1, int width=1, int height); Answer: a) and c) 25

26 A call to a C++ function is a)The name of the function followed by empty parentheses. b)The name of the function followed by any number of arguments, regardless of the number of parameters in the definition. c)The name of the function followed by a number of arguments not greater than the number of parameters in the definition. d)The name of the function followed by exactly the number of arguments as there are parameters in the definition. e)The name of the function only. 26

27 A void function can have a)no arguments b)as many arguments as the programmer wishes c)no more than 3 arguments d)exactly one argument 27


Download ppt "1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 9 Clicker Questions September 29, 2009."

Similar presentations


Ads by Google