COSC 1315

Programming Fundamentals

 Practice Text #160

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:

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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.

Answer and Explanation

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

Answer and Explanation

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

Listing 160-8.

z = x + y;

Answer and Explanation

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;

Answer and Explanation 

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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

Answer and Explanation

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.

Answer and Explanation

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.

Answer and Explanation

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

Answer and Explanation

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.

Answer and Explanation

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.

Answer and Explanation

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.

Answer and Explanation

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

Answer and Explanation

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

Listing 160-25.
2 * a + 3

Answer and Explanation

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

Listing 160-26.
(2*(a + 3))

Answer and Explanation

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

Listing 160-27.
((2*a) + 3)

Answer and Explanation

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

Answer and Explanation

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?

Listing 160-29.
int main(){
  Expressions01::classMain();
  return 0;
}//end main

Answer and Explanation

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

Listing 160-30.
  static void classMain(){
    Expressions01* ptrToObject = 
                             new Expressions01();
    ptrToObject -> doSomething();
  }//End classMain function

Answer and Explanation

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

Listing 160-31.
  static void classMain(){
    Expressions01* ptrToObject = 
                             new Expressions01();
    ptrToObject -> doSomething();
  }//End classMain function

Answer and Explanation

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

Answer and Explanation

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

Listing 160-33.
cout << 11%3 << endl;

Answer and Explanation

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.

Answer and Explanation

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

Listing 160-35.
    char var = 65;
    cout << (short)var << endl;

Answer and Explanation

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

Listing 160-36.
    char var = 65;
    cout << var << endl;

Answer and Explanation

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.

Answer and Explanation

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

Answer and Explanation



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.

About the author

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.

Baldwin@DickBaldwin.com


Answers and Explanations

Answer 38

True

Back to Question 38

Explanation 38

Answer 37

False

Back to Question 37

Explanation 37

Answer 36

A.  The character A

Back to Question 36

Explanation 36

Answer 35

C.  The value 65

Back to Question 35

Explanation 35

Answer 34

False

Back to Question 34

Explanation 34

The character represented by the numeric value is displayed.

Answer 33

A.  2

Back to Question 33

Explanation 33

Answer 32

False

Back to Question 32

Explanation 32

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

Answer 31

True

Back to Question 31

Explanation 31

Answer 30

B.  Modulus operator

Back to Question 30

Explanation 30

Answer 29

B.  Scope resolution operator

Back to Question 29

Explanation 29

Answer 28

True

Back to Question 28

Explanation 28

Answer 27

B.  23

Back to Question 27

Explanation 27

Answer 26

A.  26

Back to Question 26

Explanation 26

Answer 25

B.  23

Back to Question 25

Explanation 25

Answer 24

False

Back to Question 24

Explanation 24

Use the modulus operator.

Answer 23

True

Back to Question 23

Explanation 23

Answer 22

True

Back to Question 22

Explanation 22

Answer 21

False

Back to Question 21

Explanation 21

C++ does not support an exponentiation operator.

Answer 20

False

Back to Question 20

Explanation 20

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

Answer 19

True

Back to Question 19

Explanation 19

Answer 18

False

Back to Question 18

Explanation 18

Terms are reversed.

Answer 17

True

Back to Question 17

Explanation 17

Answer 16

False

Back to Question 16

Explanation 16

The sign of its right operand is changed.

Answer 15

True

Back to Question 15

Explanation 15

Answer 14

False

Back to Question 14

Explanation 14

Answer 13

False

Back to Question 13

Explanation 13

A ternary operator.

Answer 12

True

Back to Question 12

Explanation 12

Answer 11

False

Back to Question 11

Explanation 11

It is called a unary operator.

Answer 10

True

Back to Question 10

Explanation 10

Answer 9

False

Back to Question 9

Explanation 9

Addition, not subtraction.

Answer 8

True

Back to Question 8

Explanation 8

Answer 7

True

Back to Question 7

Explanation 7

Answer 6

True

Back to Question 6

Explanation 6

Answer 5

True

Back to Question 5

Explanation 5

Answer 4

False

Back to Question 4

Explanation 4

The terms are backwards.

Answer 3

True

Back to Question 3

Explanation 3

Answer 2

False

Back to Question 2

Explanation 2

The terms are backwards.

Answer 1

True

Back to Question 1

Explanation 1



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.

About the author

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.

Baldwin@DickBaldwin.com

-end-