C++ Boolean Expressions and Selection Statements: Lecture Questions - Prof. Keith E. Hellm, Exams of Computer Science

A worksheet for students learning c++ with exercises on boolean expressions and selection statements. It includes true or false questions, explanations for false answers, and instructions for evaluating complex boolean expressions. The document also includes a solution key for the exercises.

Typology: Exams

Pre 2010

Uploaded on 08/18/2009

koofers-user-jkb-2
koofers-user-jkb-2 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI261 Lecture Questions C++ Boolean Expressions and
Selection Statements
This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a
useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in
general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be
happy to review your answers with you during office-hours, via Email, or instant messaging.
1. Match the C++ operators with the correct relational or Boolean description at the right
% <<
= >>
>= !
&& ||
++ <=
<> %=
< @
> !=
== ><
A is equal to
B is not equal to
C is less than
D is greater than
E is less than or equal to
F is greater than or equal to
G logical inverse or NOT
H logical intersection or AND
I logical union or OR
J Not a C++ relational or Boolean operator.
2. State the value (true or false) of each Boolean expression. One of them is buggy, and probably doesn’t reflect the
logic the programmer intended which is it?
(a)
(( 1 == 1 ) || ( 2 != 3 ))
True
(b)
(( 1 < 1.01 ) && ( 2 > 3 - 2 ))
True
(c)
( 1 <= 3 <= 1 )
True
(d)
( 2<=2 && 3==1 )
False
Solution: cis buggy, the programmer wants to write
(1<=3) && (3<=1)
. This is a contrived example,
Usually this type of error is written in code like this:
1 <= x <= 3
, where the programmer wants to test x
[1,3]. But for what values of
x
is this true or false? Does it really test what the programmer intended? Also,
note that band ?? are evaluated the same, since the parenthesis in bdo not change the order of operations.
3. Which of the following represents an instruction sequence in C++?
A. A single C++ statement except for a lone
;
on a line.
B. 1 or more C++ statements including a lone
;
on a line.
C. More than one C++ statement.
Solution: B
4. C++, like many programming languages, has selection statements or selection structures. Explain precisely what
they “select” in a program.
Solution: They select a sequence of instructions for the CPU to follow, also known as the code path.
pf2

Partial preview of the text

Download C++ Boolean Expressions and Selection Statements: Lecture Questions - Prof. Keith E. Hellm and more Exams Computer Science in PDF only on Docsity!

CSCI261 Lecture Questions

C++ Boolean Expressions and Selection Statements

This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be happy to review your answers with you during office-hours, via Email, or instant messaging.

  1. Match the C++ operators with the correct relational or Boolean description at the right

A is equal to B is not equal to C is less than D is greater than E is less than or equal to F is greater than or equal to G logical inverse or NOT H logical intersection or AND I logical union or OR J Not a C++ relational or Boolean operator.

  1. State the value (true or false) of each Boolean expression. One of them is buggy, and probably doesn’t reflect the logic the programmer intended – which is it? (a) (( 1 == 1 ) || ( 2 != 3 )) True (b) (( 1 < 1.01 ) && ( 2 > 3 - 2 )) True (c) ( 1 <= 3 <= 1 ) True (d) ( 2<=2 && 3==1 ) False

Solution: c is buggy, the programmer wants to write (1<=3) && (3<=1). This is a contrived example, Usually this type of error is written in code like this: 1 <= x <= 3 , where the programmer wants to test x ∈ [ 1 , 3 ]. But for what values of x is this true or false? Does it really test what the programmer intended? Also, note that b and ?? are evaluated the same, since the parenthesis in b do not change the order of operations.

  1. Which of the following represents an instruction sequence in C++? A. A single C++ statement except for a lone ; on a line. B. 1 or more C++ statements including a lone ; on a line. C. More than one C++ statement.

Solution: B

  1. C++, like many programming languages, has selection statements or selection structures. Explain precisely what they “select” in a program.

Solution: They select a sequence of instructions for the CPU to follow, also known as the code path.

CSCI261 Lecture Questions

C++ Boolean Expressions and Selection Statements

  1. What will be printed on the console by the code below? Use the chart at the right to record changes in variable values. 1 int x (9) , y (7) , z (2) , k (0); 2 double m (1.1) , j (0); 3 4 if (x > y ) { 5 if ( y > z && y > k) { 6 m - -; 7 } else { 8 k ++; 9 } 10 } else { 11 j ++; 12 } 13 14 cout << " m= " << m << endl ; 15 cout << " k= " << k << endl ; 16 cout << " j= " << j << endl ;

Line # x y z k m j

Solution: m=0. k= j=

Page 2