Variable Questions.

The answers to the first few questions of each part are at the bottom of the file.

1. Declare and initialise variables to do the following things:
        (a) Store the user’s answer to the question: “Do you want to continue (y/n):”
        (b) Store the radius of the circle
        (c) Store the answer of 6.52/2.34
        (d) Store the answer of 6.0/2
        (e) Stores the user’s answer to: “Press Q for quit, I for Index, H for Help”
        (f) Stores the math operator the user has chosen (+, -, / or %)
        (g) Stores the distance to the moon in metres.
        (h) Stores PI to 90 digits
        (i)  Stores the answer to 7 % 2
 
2.     (a) Order the following types from smallest to largest: long, short, int.
        (b) Order the following types from smallest to largest: double, long double, float
        (c) What occurs in memory after the line: int Yahoo;  is executed?
        (d) Which of the following names are acceptable for variables? Which are good choices?
                        (i)  Sides                            (v)  Num_People
                        (ii) 0                                   (vi) main
                        (iii) true                              (vii) x
                        (iv)  void                            (viii) Total_Purchaise_Price
 
3. What occurs in memory after the following code is executed?
        (a) char user_input = ‘ ‘;
        (b) user_input = ‘Q’;
        (c) float tax = 0.07;
        (d) const Days_Year = 365;

4. (Complex assignment statements) If I has 5 in it to begin with, what does it have in it after each of the following.

  1. i++

  2. i—

  3. i+=2

  4. i-=2

  5. i*=3

5. Which of the following operations are possible? (Pay close attention to the types of the variables)

int a, b; float c; long e, f; char g, h; String p;
  1. a=b;

  2. c=b;

  3. b=c;

  4. e=f+1;

  5. a=f;

  6. f=a;

  7. g=h

  8. g=a;

  9. a=g;

  10. p=g;

  11. p=g+h;

  12. p=””+g;

  13. p=g+””+h;

6. Fill in the following chart: the row heading is assigned the column heading.

In the spot marked x, the float variable is assigned a long variable.

float = long; (Can you put a float in a long?)

=

int

long

float

char

String

int

 

 

 

 

 

long

 

 

 

 

 

float

 

x

 

 

 

char

 

 

 

 

 

String

 

 

 

 

 

 

 

Selected Answers:

1. a. char answer = 'y'; OR String answer = "y". Note: single quotes on chars and double quotes on Strings. b. double radius = 1.2;

2 a. short, int, long c. a integer sized space is set aside in memory. the space is called yahoo.

3 a. a char sized space is set aside in memory. the section of memory is called user-input. user-input has a space (' ') put into to start off.