Introduction to programing C/Quizzes/Variables/Answers

From Wikiversity
Jump to navigation Jump to search

Question 1[edit | edit source]

List all the native C types. State wether they store integer or floating point data.

  • char integer data
  • short integer data
  • int integer data
  • long integer data
  • float floating point data
  • double floating point data

Question 2[edit | edit source]

Declare an integer with the name totalMoney.

int totalMoney;

Question 3[edit | edit source]

What storage type would you use for the following ideas?

The area of a triangle?

A double. Its unlikely that the area of a triangle is always going to be an integer.

The number of people in the United States?

An integer. You can't have half a person.

The average number of people living in a house?

A double. Ok, I lied. In this case, you can have half a person. The lesson here- don't pay attention to just what the data is, but how you plan to use it.

The amount of money in your wallet?

A double. You likely have some change. The other thing you could do is to count money in cents and hold it in an integer. If you need to be 100% accurate, you want to do it the second way (floats round the number to the nearest power of 2).

The number of hours you worked last week?

An integer if you only can work full hours. A double if you can work half hours.

Question 4[edit | edit source]

What real type would you use for the following ideas?

The area of a triangle?

Area. It says exactly what it stores. If you know the units (square cm, square inches, etc) you should use that instead.

The number of people in the United States?

People. Population would also work well.

The average number of people living in a house?

AveragePeople. People is also good, if you don't need an integer number of people elsewhere.

The amount of money in your wallet?

Money. If you hold the number of cents instead of dollars, you probably want to use Cents instead.

The number of hours you worked last week?

Hours. This is an easy one.

Question 5[edit | edit source]

What names would you use to describe them?


The area of a triangle?

triangle, triangleArea, or something similar.

The number of people in the United States?

usPopulation, or something similar.

The average number of people living in a house?

peoplePerHouse, householdSize, or something similar.

The amount of money in your waller?

moneyInWallet, or something similar.

The number of hours you worked last week?

workedLastWeek

Question 6[edit | edit source]

What are the values of x, y, and z after each line of the following code?

int x,y,z;

We don't know, we didn't assign them any. All 3 are random.

x=7;

x=7. y and z are still random.

y=2*x+5;

x=7. y=19. z is still random.

z=y+x;

x=7. y=19. z=26

x=9;

x=9 y=19 z=26. Notice that only x changed.

z=x-8;

x=9 y=19 z=1