Written Portion of Exam 1 - Sample

 

Answer all of the following questions. This is a "closed book" examination and you must work entirely on your own.
    1. (10 points) Choose the best answer to each of the following:
      (1) _____ In Python, + is
      1. A relational operator
      2. A unary operator
      3. A boolean operator
      4. All of the above
      5. None of the above
      (2) _____ In Python, -1 the - is
      1. A unary operator
      2. A binary operator
      3. Has boolean operands
      4. All of the above
      5. None of the above
      (3) _____ In Python, a variable is
      1. The implicit loss of precision in arithemtic operations
      2. The way the decrement operator changes
      3. A named space for holding a value
      4. All of the above
      5. None of the above
      (4) _____ In Python, the statement float cost
      1. Will cause a logic error
      2. Will cause a run-time error
      3. Will cause a repetitive stress error
      4. All of the above
      5. None of the above
      (5) _____ In the Python statement i = int(d) the operator = is a/an:
      1. Assignment operator
      2. Relational operator
      3. Typecast operator
      4. All of the above
      5. None of the above
      (6) _____ In the Python statement i = int(4.0) the operand of the int operator is a/an:
      1. Assignment operator
      2. Literal
      3. Typecast operator
      4. All of the above
      5. None of the above
      (7) _____ In Python, what kind of statement is i = 0?
      1. Boolean statement
      2. Declaration statement
      3. Typecast statement
      4. All of the above
      5. None of the above
      (8) _____ In Python, an assignment statement must contain which of the following?
      1. Curly brackets
      2. Parentheses
      3. An equal sign
      4. All of the above
      5. None of the above
      (9) _____ In Python, what is the default type for a variable set by an input statement?
      1. float
      2. int
      3. str
      4. None
      5. All of the above
2. (10 points) Evaluate each of the following expressions (which are written in Java) assuming that multiplication has higher precednce than addition and that all operators use left-to-right associativity. If the expression contains a syntax error, write the word "Error".
3 * 4 * 2

 

3 * 4 +

 

2 + 3 + 4 * 5

 

2 + 3 +* 4 * 5

 

3. (10 points) Provide a concrete example of each of the following (in Python). If appropriate, you may use the same example more than once.

Logic Error

Run-time error

Style error

Syntax error

4. (10 points) Provide a concise comparison of the two terms in each of the following.
+= and =

 

Declaration and assignment

 

float and int

 

binary operator and unary operator

 

integer division and division

 

5. (5 points) Write a single statement for each of the following that:
Declares price to be an int variable set to 5.

 

Assigns the value 40.99 to a variable named price.

 

Reduces a variable named price by 10 percent.

 

Assigns the result of an input statement of "Enter tax" to a variable named sales_tax.

 

Displays the value of a variable named sales_tax on the console (i.e., standard output) with two tab character in front of it.

 


6. (5 points) Show what will be printed by the following application (assuming it is compiled and executed properly).
"""Digit Checker"""
digit2 = 0
digit3 = 0
num1 = 1882
num2 = 4132
num3 = 71
num1 = int(num1 % 100)
num2 = int(num2 % 100)
num3 = int(num3 %100)
digit1 = int(num1 // 10)
digit2 = int(num2 // 10)
digit3 = int(num3 // 10)
print("Digits: ", digit1, digit2, digit3)

Back to Top