Exercise CSC415
Exercise CSC415
EXERCISE
PART A
1. Describe statement demonstrates the appropriate way to declare and initialize a variable
name employee to 0?
A. double employee;
B. double employee = 0;
C. int employee;
D. int employee = 0;
A. I, II, III
B. I, II, IV
C. II, III, IV
D. I, II, III, IV
5. What would be the best choice for the data type for a person’s take home pay?
A. int
B. float
C. char
D. string
6. Which of the following statements is correct?
7. What is the value of the following arithmetic expression if the variables have these values:
p = 5; s = 20; x = 6;
p+3*x–s
A. -37
B. 3
C. 28
D. 59
8. There is no limit on the size of the numbers that can be stored in the int data type.
A. True
B. False
9. There are only two possible values for the bool data type.
A. True
B. False
Question 1
Convert the following arithmetic equations into C++ assignment statements (Hint: can use
mathematical functions)
i.
ii.
iii. Z= (
Question 2:
Suppose x, y, z are int variables and x = 5 and y = 6. What value is assigned to each variable after
each statement executes? If a variable is undefined at a particular statement, report UND
(undefined).
x y z
x= (y++) + 3;
z= 2* x + (++y);
y= 2 * (++z)- (x++)
Question 3
a) int a = 11, c = 4, b;
double jumlah;
b = 20 + c;
c = ( b + 3 ) % c + 3;
cout<< "Value c is:" << c << " and b is: "<< b <<endl;
a = a + c;
jumlah = a / 2;
cout<< jumlah <<" is the value of jumlah. " <<endl;
b) int category = 2;
switch (category)
{
case 1 : cout<< “ Pelajar sahaja ”;
case 2 : cout<< “ Pensyarah sahaja ”;
case 3 : cout<< “ Kakitangan Kontrak sahaja ”; break;
default : break;
}
Question 4
Trace the following program and determine the output. [
a)
int bmi; OUTPUT
cout << “\n Enter BMI value “;
cin >> bmi;
if (bmi >=0 && bmi <=7)
cout<<”Thin”<<endl;
if (bmi >=8 && bmi <=10)
cout<<”Slim”<<endl;
if (bmi >=10 && bmi <=19)
cout<<”OK”<<endl;
if (bmi >=20)
cout<<”Fat”<<endl;
if ( a < b )
cout << "a < b" << endl;
if ( a > b )
cout << "a > b" << endl;
if ( d <= c )
cout << "d <= c" << endl;
if ( c != d )
cout << "c != d" << endl;
c)
int x = 12; OUTPUT
if (x > 12)
{if ( x < 15)
cout << "HELLO";}
else
cout << "BYE";
cout << "FRIEND";
Question 5
Let user input the value for radius and height. After that you calculate the volume of a cylinder using
the values. Use the formula: