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

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
True or false A variable of type char can hold the value 301. ( F )
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 5: Control Structures II (Repetition)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Chapter 5: Control Structures II (Repetition)
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 4 Program Control Statements
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120 Introduction to Computer Science Iteration (Looping)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 05 (Part III) Control Statements: Part II.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
CPS120 Introduction to Computer Science Iteration (Looping)
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Midterm preview. double int = 2.0; True / FalseThe following is a syntactically correct variable declaration and assignment statement:
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
CPS120: Introduction to Computer Science Decision Making in Programs.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Review 1.
Chapter 4 – C Program Control
EGR 2261 Unit 4 Control Structures I: Selection
Introduction to C++ Programming
Chapter 4: Control Structures I (Selection)
CSC215 Lecture Control Flow.
Presentation transcript:

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) error 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 = 7 / 2; (a) 3.0 (b) 3.5 (c) 4.0 (d) 2.0 (e) error 3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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