Sheet 02: Variables, Expressions, and Statements Solution



State whether true or false:-

  1. The best way to write a program is to immediately type in some code and then debug it until it works.

  1. An algorithm can be written without using a programming language.

  2. Programs no longer require modification after they are written and debugged.

  3. Python identifiers must start with a letter or underscore.

  4. Keywords make good variable names.

  5. Expressions are built from literals, variables, and operators.

  6. In Python, x = x + 1 is a legal statement.

  1. Python does not allow the input of multiple values with a single statement.

  2. A counted loop is designed to iterate a specific number of times.

  1. In a flowchart, diamonds are used to show statement sequences, and rectangles are used for decision points.

  1. Information that is stored and manipulated by computers is called data.

  1. Since floating-point numbers are extremely accurate, they should generally be used instead of integers.

  1. Operations like addition and subtraction are defined in the math library.

  2. The number of possible arrangements of n items is equal to n!

  1. The sqrt function computes the squirt of a number.

  2. The float data type is identical to the mathematical concept of a real number.

  3. Computers represent numbers using base-2 (binary) representations.

  1. A hardware float can represent a larger range of values than a hardware integer.

  1. Type conversion functions such as float are a safe alternative to eval() for getting a number as user input.

Multiple Choice

  1. Which of the following is not a step in the software development process?

    1. specification

    2. testing/Debugging

    3. fee setting

    4. maintenance

  2. What is the correct Python formula for converting Celsius to Fahrenheit?

    1. F = (9/5)*C + 32

    2. F = 5/9*C- 32

    3. F=B2-4AC

    4. F = (212-32)/100

  1. The process of describing exactly what a computer program will do to solve a problem is called

    1. design

    2. implementation

    3. programming

    1. specification

  1. Which of the following is not a legal Python identifier?

    1. Spam

    2. spAm

    3. 2spam

    1. spam 4U

  1. Which of the following are not used in expressions?

    1. Variables

    2. Statements

    3. Operators

    4. literals

  2. Fragments of code that produce or calculate new data values are called

    1. identifiers

    2. expressions

    1. productive clauses

    2. assignment statements

  1. Which of the following is not a built-in Python data type?

    1. int

    2. float

    3. rational

    4. string

  2. Which of the following is not a built-in operation?

    1. +

    1. %

    2. abs ()

    3. sqrt ()

  1. In order to use functions in the math library, a program must include

    1. a comment

    2. a loop

    3. an operator

    4. an import statement

  1. The value of 4! is

    1. 9

    2. 24

    3. 41

    4. 120

  2. The most appropriate data type for storing the value of pi is

    1. Int

    2. Float

    3. Irrational

    1. string

  1. In a mixed-type expression involving ints and floats, Python will convert

    1. floats to ints

    2. ints to strings

    3. both floats and ints to strings

    4. ints to floats

  1. Which of the following is not a Python type-conversion function?

    1. float

    2. round

    3. int d) abs


1- Show the result of evaluating each expression. Be sure that the value is in the proper form to indicate its type (int or float). If the expression is illegal, explain why.

2- Translate each of the following mathematical expressions into an equivalent Python expression. You may assume that the math library has been imported (via import math).

Programming Exercises:

  1. Write a program that converts temperatures from Fahrenheit to Celsius.

  1. Write a program that converts distances measured in kilometers to miles. One kilometer is approximately 0.62 miles.

  1. Write a program to perform a unit conversion of your own choosing. Make sure that the program prints an introduction that explains what it does.

  1. Write a program to find the roots of the equation ax2+bx+c=0. You read the input parameters a, b, and c from the user emphasizing that the program finds only real roots.

  1. Let a, b, c be the lengths of the sides of a triangle. The area is given by:

= √ ( − )( − )( − ), ℎ ℎ ℎ . Write a program to read the triangle sides and compute its area.

error: Content is protected !!