Expressions and Operators

Revised: February 3, 2007
By Richard G. Baldwin

File Pfsg00160.htm
Practice Text Index

Welcome

The practice tests in this series were written specifically for the benefit of my students in COSC 1315, Fundamentals of Programming.  They consists of questions, answers, and explanations.  The questions are based on the material covered in my series of online lecture notes for the course.  Each practice test is keyed to a specific lecture.  This practice test is keyed to lecture #160 titled Expressions and Operators.

Questions

160-1.  True or False.  Operators are the action elements of a computer program.  They perform actions such as:
• Dividing one variable by another variable
• Comparing one variable to another variable, etc.

160-2.  True or False.  In the expression shown in Listing 160-2, the plus character (+) is an operand while x and y characters are operators.

Listing 160-2.
 `x + y`

160-3.  True or False.  Assuming that x and y in the expression shown in Listing 160-3 are numeric primitive variables, the expression produces the sum of the values stored in the two variables.

Listing 160-3.
 `x + y`

160-4.  True or False.  In the expression shown in Listing 160-4, the variable x would be called the right operand and the variable y would be called the left operand.

Listing 160-4.

 `x + y`

160-5.  True or False.  C++ programs consist of statements, which in turn, consist of expressions.

160-6.  True or False.  An expression is a specific combination of operators and operands that evaluates to a particular result.  The operands can be variables, literals, or method calls that return a value.

160-7.  True or False.  A statement in C++ is a specific combination of expressions terminated by a semicolon.

160-8.  True or False.  Listing 160-8 contains a valid C++ statement.

Listing 160-8.

 `z = x + y;`

160-9.  True or False.  When the statement in Listing 160-9 is executed, values are retrieved from the variables named x and y.  The value of y is subtracted from the value of x, and the result is stored in (assigned to) the variable named z, replacing whatever value may previously have been contained in that variable.

Listing 160-9.

 `z = x + y;`

160-10.  True or False.  C++ provides operators that can be used to perform an action on one, two, or three operands.

160-11.  True or False.  An operator that operates on one operand is called a binary operator.

160-12.  True or False.   An operator that operates on two operands is called a binary operator.

160-13.  True or False.  An operator that operates on three operands is called a three-part operator.

160-14.  True or False.  Each operator behaves exclusively as a unary operator, a binary operator, or a ternary operator.

160-15.  True or False.  As a binary operator, the minus sign causes its right operand to be subtracted from its left operand (provided that the two operands evaluate to numeric values).

160-16.  True or False.  As a unary operator, the minus sign causes the algebraic sign of the left operand to be changed.

160-17.  True or False.  binary operators in C++ use infix notation.  This means that the operator appears between its operands.

160-18.  True or False.  Some unary operators in C++ use prefix notation and some unary operators use postfix notation.  For postfix notation, the operator appears before (to the left of) its operand.  For prefix notation, the operator appears after (to the right of) its operand.

160-19.  True or False.  As a result of performing the specified action, an operator can be said to return a value (or evaluate to a value) of a given type.

160-20.  True or False.  There are many different categories of operators used in C++, including all of the following::

• Scope Resolution
• Postfix
• Unary
• Comma
• Multiplicative
• Shift
• Upside down
• Relational and Equality
• Bitwise
• Logical
• Assignment
• Conditional
• Pointer-to-Member
• Reference

160-21.  True or False.  The binary arithmetic operators supported by C++ include those shown in Listing 160-21?

Listing 160-21.

 ```Operator    Description                        +        Adds its operands    -        Subtracts the right operand             from the left operand    *        Multiplies the operands     /        Divides the left operand by             the right operand    %        Remainder of dividing the              left operand by the right              operand - modulus operator ^ Exponentiation - raises a value to a specified power.```

160-22.  True or False.  Although there are some subtle issues involved in the use of mixed data types with arithmetic operators, generally speaking, if you mix the integer types with the floating types, the integer values will be automatically converted to floating values and the arithmetic will be performed using floating arithmetic.  Floating arithmetic produces a floating result.

160-23.  True or False.  If you perform integer division, the result will consist of only the whole-number part of the quotient.  The result will not be rounded to the next higher or lower whole number.  The remainder or fractional part will simply be discarded.

160-24.  True or False.  If you need the remainder that would be produced by integer division, you can get it by using the exponentiation operator

160-25.  If the variable a has a value of 10, what result is produced by the expression shown in Listing 160-25?

• A.  26
• B.  23
• C.  45
• D.  None of the above.
Listing 160-25.
 `2 * a + 3`

160-26.  If the variable a has a value of 10, what result is produced by the expression shown in Listing 160-26?

• A.  26
• B.  23
• C.  45
• D.  None of the above.
Listing 160-26.
 `(2*(a + 3))`

160-27.  If the variable a has a value of 10, what result is produced by the expression shown in Listing 160-27?

• A.  26
• B.  23
• C.  45
• D.  None of the above.
Listing 160-27.
 `((2*a) + 3)`

160-28.  True or False.  The purpose of a cast operator is to force a conversion from one type to another.

160-29.  The main function shown in Listing 160-29 uses a specific operator to invoke a static function named classMain belonging to the class named Expressions01.  Which of the following operators does it use?

• A.  Assignment operator
• B.  Scope resolution operator
• C.  Cast operator
• D.  None of the above.
Listing 160-29.
 ```int main(){ Expressions01::classMain(); return 0; }//end main```

160-30.  Which one of the operators in the following list is not used in the classMain function shown in Listing 160-30.

• A.  Indirection operator
• B.  Modulus operator
• C.  new operator
• D.  Assignment operator
• E.  Pointer-to-member operator
Listing 160-30.
 ``` static void classMain(){ Expressions01* ptrToObject = new Expressions01(); ptrToObject -> doSomething(); }//End classMain function```

160-31.  True or False.  The classMain function shown in Listing 160-31 uses the Indirection, new, Assignment and Pointer-to-Member operators to:

• Instantiate an object in dynamic memory
• Save the object's address in a pointer variable
• Use the pointer variable to invoke an instance method named doSomething belonging to the object
Listing 160-31.
 ``` static void classMain(){ Expressions01* ptrToObject = new Expressions01(); ptrToObject -> doSomething(); }//End classMain function```

160-32.  True or False.  The code shown in Listing 160-32a produces the output shown in Listing 160-32b.

Listing 160-32a.
 ``` cout << "\nTypes of arithmetic" << endl; cout << " Float: 10.0/3.0 = " << 10.0/3.0 << endl; cout << " Mixed: 10.0/3 = " << 10.0/3 << endl; cout << " Integer: 10/3 = " << 10/3 << endl;```

Listing 160-32b.
 ```Types of arithmetic Float: 10.0/3.0 = 3.33333 Mixed: 10.0/3 = 3 Integer: 10/3 = 3```

160-33.  The statement shown in Listing 160-33 produces which of the following outputs?

• A.  2
• B.  3
• C.  4
• D.  None of the above.
Listing 160-33.
 `cout << 11%3 << endl;`

160-34.  True or False.  When the insertion operator is used along with cout to display a variable of type char, the numeric value stored in the variable is displayed.

160-35.  What output is produced by the code shown in Listing 160-35?

• A.  The character A
• B.  The character B
• C.  The value 65
• D.  The value 66
Listing 160-35.
 ``` char var = 65; cout << (short)var << endl;```

160-36.  What output is produced by the code shown in Listing 160-36?

• A.  The character A
• B.  The character B
• C.  The value 65
• D.  The value 66
Listing 160-36.
 ``` char var = 65; cout << var << endl;```

160-37.  True or False.  The use of a cast operator to change the type of a variable is permanent and lasts for the remaining duration of the program.

160-38.  True or False.  It is not possible to permanently change the type of a variable.

Copyright 2007, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

True

False

A.  The character A

C.  The value 65

False

The character represented by the numeric value is displayed.

A.  2

False

Error in the digits to the right of the decimal point for the second line of numeric output.

True

B.  Modulus operator

B.  Scope resolution operator

True

B.  23

A.  26

B.  23

False

Use the modulus operator.

True

True

False

C++ does not support an exponentiation operator.

False

There is no upside down category of operators in C++.

True

False

Terms are reversed.

True

False

The sign of its right operand is changed.

True

False

False

A ternary operator.

True

False

It is called a unary operator.

True

False

True

True

True

True

False

The terms are backwards.

True

False

The terms are backwards.

True

Copyright 2007, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.