+ All Categories
Home > Documents > 1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 9 Clicker Questions September 29, 2009.

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

Date post: 30-Dec-2015
Category:
Upload: oswin-wells
View: 221 times
Download: 0 times
Share this document with a friend
27
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 9 Clicker Questions September 29, 2009
Transcript
Page 1: 1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 9 Clicker Questions September 29, 2009.

1

CISC181 Introduction to Computer Science

Dr. McCoy

Lecture 9Clicker Questions

September 29, 2009

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

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) error2

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

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) error3

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

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

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

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

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

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

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

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.

<see next slide for explanation>7

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

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

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

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 these9

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

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‘

<answer on next slide>10

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

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

11

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

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

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

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

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

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

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

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

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

In the expression (j > 0 && j+1 == 10), which operator executes last?

a)>

b)&&

c)+

d)==

<explanation following>16

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

Display 2.3 Precedence of Operators (1 of 4)

2-17Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

Display 2.3 Precedence of Operators (2 of 4)

2-18Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

Display 2.3 Precedence of Operators (3 of 4)

2-19Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

Display 2.3 Precedence of Operators (4 of 4)

2-20Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

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 next page>21

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

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

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

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

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

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

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

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

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

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

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

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


Recommended