Test Bank for Problem Solving with C++: The Object of Programming, 8/e Chapter 2 C++ Basics

Similar documents
1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Computer Engineering Department CMPE110 Midterm Sample Questions, 2017/ Fall

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

CSci 1113 Midterm 1. Name: Student ID:

CMPE110, Sample questions

Practice test for midterm 1

REPETITION CONTROL STRUCTURE LOGO

Lab # 02. Basic Elements of C++ _ Part1

Logical Operators and if/else statement. If Statement. If/Else (4.3)

C++ Final Exam 2017/2018

The following expression causes a divide by zero error:

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

C++ Programming: From Problem Analysis to Program Design, Third Edition

CSI33 Data Structures

1) You want to determine whether time has run out. The following code correctly implements this:

FORM 2 (Please put your name, form #, and section # on the scantron!!!!)

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Why Is Repetition Needed?

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

8. Control statements

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

If Control Construct

Introduction to Programming using C++

Lecture 5 Tao Wang 1

Boolean Algebra Boolean Algebra

Name Section: M/W T/TH Number Definition Matching (8 Points)

Exam 3 Chapters 7 & 9

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Computer Programming : C++

INTRODUCTION TO COMPUTER SCIENCE - LAB

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

Homework #3 CS2255 Fall 2012

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Lecture 3 Tao Wang 1

C++ PROGRAMMING SKILLS Part 2 Programming Structures

CSCI 111 First Midterm Exam Spring Solutions 09.05am 09.55am, Wednesday, March 14, 2018

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Flow Control. CSC215 Lecture

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Add Subtract Multiply Divide

Understanding main() function Input/Output Streams

Practice test for midterm 2

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

1. In C++, reserved words are the same as predefined identifiers. a. True

CS Spring 05 - MidTerm

Study Guide for Test 2

Recognize the correct ordering of decisions in multiple branches Program simple and complex decision

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Chapter 4 - Notes Control Structures I (Selection)

Topics: Material through example 19 (types, operators, expressions, functions, selection, loops, arrays)

CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

CS242 COMPUTER PROGRAMMING

5. Control Statements

Name Section: M/W T/TH Number Definition Matching (6 Points)

C++ Programming: From Problem Analysis to Program Design, Third Edition

CSCE 206: Structured Programming in C++

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Chapter 4 Computer Science with C++ Name: Review Worksheet A Mr. Ferwerda

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Islamic University of Gaza Computer Engineering Dept. C++ Programming. For Industrial And Electrical Engineering By Instructor: Ruba A.

Score score < score < score < 65 Score < 50

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

CSCE Practice Midterm. Data Types

CSCE 121 ENGR 112 List of Topics for Exam 1

Introduction to Programming EC-105. Lecture 2

Chapter 3. More Flow of Control

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

CHAPTER : 9 FLOW OF CONTROL

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Today in CS161. Week #3. Learn about. Writing our First Program. See example demo programs. Data types (char, int, float) Input and Output (cin, cout)

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Pointers II. Class 31

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

LAB 4.1 Relational Operators and the if Statement

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Chapter 4: Control Structures I (Selection)

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program


CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

CSC 1300 Exam 4 Comprehensive-ish and Structs

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

Review. Relational Operators. The if Statement. CS 151 Review #4

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Transcription:

TRUE/FALSE 1. In the following code fragment, x has the value of 3. int x = 3; ANSWER: TRUE 2. The body of a do-while loop always executes at least once. ANSWER: TRUE 3. The body of a while loop may never execute. ANSWER: TRUE 4. The opposite of (x >3 && x < 10) is (x < 3 && x > 10) 5. The integer 0 is considered true. 6. Loops are used when we need our program to make a choice between two or more things. 7. It is legal to declare more than one variable in a single statement. ANSWER: TRUE 8. Variable names may begin with a number. 9. The opposite of less than is greater than 10. Every line in a program should have a comment. Short Answer 1. << is called the stream operator. ANSWER: insertion 2. The braces for a loop define the of the loop. ANSWER: body 3. A loop that always executes the loop body at least once is known as a loop. ANSWER: do-while 4. int myvalue; is called a. ANSWER: variable declaration 5. What is the opposite of ( x < 20 && x > 12)? ANSWER: (x >=20 x <= 12) 6. What is the correct conditional statement to determine if x is between 19 and 99? ANSWER: (x >19 && x < 99) 7. Each time a loop body executes is known as an. ANSWER: iteration 8. if- statements that are inside other if- statements are said to be. ANSWER: nested

9. >> is known as the stream operator. ANSWER: extraction 10. Is << used for input or output? ANSWER: output 11. The stream that is used for input from the keyboard is called. ANSWER: cin 12. The stream that is used for output to the screen is called. ANSWER: cout 13. Write the loop condition to continue a while loop as long as x is negative. ANSWER: while(x < 0) 14. When must we use braces to define the body of a contitional expression? ANSWER: When there are multiple statements in the body. 15. In a compound logical and (&&) expression, the evaluation of the expression stops once one of the terms of the expression is false. This is known as evaluation. ANSWER: short-circuit evaluation Multiple Choice 1. Which of the following is a valid identifier? a. 3com b. three_com c. 3_com d. 3-com e. dollar$ 2. Which of the following is not a valid identifier? a. return b. myint c. myinteger d. total3 ANSWER: A 3. What is the value of x after the following statements? int x, y, z; y = 10; z = 3; x = y * z + 3; a. Garbage b. 60 c. 30 d. 33 4. What is the value of x after the following statements?

x = 0; x = x + 30; a. 0 b. 30 c. 33 d. garbage 5. What is the value of x after the following statements? x = x + 30; a. 0 b. 30 c. 33 d. garbage 6. What is the output of the following code? float value; value = 33.5; cout << value << endl; a. 33.5 b. 33 c. value d. garbage ANSWER: A 7. What is the output of the following code? float value; value = 33.5; cout << "value" << endl; a. 33.5 b. 33 c. value d. garbage 8. What is the output of the following code? cout << "This is a \\" << endl; a. This is a b. This is a \ c. nothing, it is a syntax error d. This is a \ endl 9. Which of the following lines correctly reads a value from the keyboard and stores it in the variable named myfloat? a. cin > > myfloat; b. cin << myfloat; c. cin >> "myfloat";

d. cin >> myfloat >> endl; ANSWER: A 10. Another way to write the value 3452211903 is a. 3.452211903e09 b. 3.452211903e-09 c. 3.452211903x09 d. 3452211903e09 ANSWER: A 11. Which of the following statements is NOT legal? a. char ch='b'; b. char ch='0'; c. char ch=65; d. char ch="cc"; 12. What is the value of x after the following statements? float x; x = 15/4; a. 3.75 b. 4.0 c. 3.0 d. 60 13. What is the value of x after the following statements? x = 15/4; a. 15 b. 3 c. 4 d. 3.75 14. What is the value of x after the following statements? x = 15 %4; a. 15 b. 4 c. 3 d. 3.75 15. What is the value of x after the following statement? float x; x = 3.0 / 4.0 + 3 + 2 / 5 a. 5.75 b. 5.75 c. 1.75 d. 3.75

16. What is the value of x after the following statement? float x; x = 3.0 / 4.0 + (3 + 2 )/ 5 a. 5.75 b. 5.75 c. 1.75 d. 3.75 17. What is the value of x after the following statements? double x; x = 0; x += 3.0 * 4.0; x -= 2.0; a. 22.0 b. 12.0 c. 10.0 d. 14.0 18. Given the following code fragment and the input value of 4.0, what output is generated? float tax; float total; cout << "enter the cost of the item\n"; cin >> total; if ( total >= 3.0) tax = 0.10; cout << total + (total * tax) << endl; cout << total << endl; a. 3 b. 3.3 c. 4.0 d. 4.4 19. Given the following code fragment and the input value of 2.0, what output is generated? float tax; float total; cout << "enter the cost of the item\n";

cin >> total; if ( total >= 3.0) tax = 0.10; cout << total + (total * tax) << endl; cout << total << endl; a. 2.2 b. 2.0 c. 3.1 d. 4.4 20. If x has the value of 3, y has the value of -2, and w is 10, is the following condition true or false? if( x < 2 && w < y) a. true b. false 21. What is the correct way to write the condition y < x < z? a. (y < x < z) b. ( (y < x) && z) c. ((y > x) (y < z)) d. ((y < x) && (x < z)) 22. Given the following code fragment, and an input value of 3, what is the output that is generated? cout <<"Enter a value\n"; cin >> x; if(x=0) cout << "x is zero\n"; cout << "x is not zero\n"; a. x is zero b. x is not zero c. unable to determine d. x is 3 ANSWER: A (note it is an assignment!)

23. Given the following code fragment, and an input value of 5, what is the output? if( x< 3) cout << "small\n"; if( x < 4) cout << "medium\n"; if( x < 6) cout << "large\n"; cout << "giant\n"; a. small b. medium c. large d. giant 24. Given the following code fragment, what is the output? int x=5; if( x > 5) cout << "x is bigger than 5. "; cout <<"That is all. "; cout << "Goodbye\n"; a. x is bigger than 5. That is all b. x is bigger than 5 c. That is all. Goodbye d. Goodbye 25. Executing one or more statements one or more times is known as: a. selection b. iteration c. sequence d. algorithm

26. Given the following code fragment, what is the final value of y? int x, y; x = -1; y = 0; while(x <= 3) y += 2; x += 1; a. 2 b. 10 c. 6 d. 8 27. Given the following code fragment, what is the final value of y? int x, y; x = -1; y = 0; while(x < 3) y += 2; x += 1; a. 2 b. 10 c. 6 d. 8 28. What is the output of the following code fragment? int x=0; while( x < 5) cout << x << endl; x ++; cout << x << endl; a. 0 b. 5 c. 4 d. unable to determine (infinite loop) 29. What is the final value of x after the following fragment of code executes? int x=0; do x++; while(x > 0); a. 8

b. 9 c. 10 d. 11 e. infinite loop. ANSWER: E 30. Given the following code fragment, which of the following expressions is always true? cin >> x; a. if( x < 3) b. if( x==1) c. if( (x / 3) >1 ) d. if( x = 1)