# 20200507224523itp 132 Final Exam Spring 2020...................................................

Content type
User Generated
Subject
C Programming
School
Northern Virginia Community College
Type
Homework
Rating
Showing Page:
1/4
ITP 132
Final exam spring 2020
Name:
1. What is wrong with this code?
int num = 5;
cout << “number is: “ << num << “\n”;
int *ptr = & num;
ptrr = 9;
cout << “num: “ << *ptr << “\n”;
Answer: “ptrr” is not defined, and if you mean ptr = 9 then that is a wrong assignment since ptr
is a pointer and will be waiting for an address.
2. Each of the following functions has errors. Locate as many errors as you can.
A) void total(int valuel, value2, value3)
{
return valuel + value2 + value3;
}
Answer: the function is declared as void, but inside is returning a
value. Also, value2 and value3 are not declared with their data types.
B) double average(int valuel, int value2, int value3)
{
double average;
average = valuel + value2 + value3 / 3;
}
Answer: The average of 3 numbers must be the sum of
them divided by 3, thus, the brackets are missing. It should
be (value1 + value2 + value3)/3.0. Also, those are int
values, and the avg could be a real number. A cast must be
C) void area(int length, int width)
{
return length * width;
}
Answer: As the question A, the function is void but
tries to return a int value.
D) void getValue(int values)
{
cout « "Enter a value: ";
cin >> values;

Showing Page:
2/4
}
Answer: This function takes as parameter values but
then it overrides it when asking the user for a new input.
E) double getValue()
{
int inputValue;
cout « "Enter an integer: ";
cin >> inputValue;
return inputValue;
}
Answer: this funtion gets an int value but returns a
double value. Though this is not an error because of the
implicit cast, but is good to point that.
f) int getValue()
{
cout « "Enter a floating-point number: ";
cin » inputValue;
return inputValue;
}
Answer: Difference from the question before, this
wants to input a float value (error is that the variable was
not defined) and returns an int value (again, just to point
that the value will be cast)
3. Given the following declarations:
char c = ’A’;
char * p = &c;
char ** p2 = &p;
void * v = &p2;
Examine each of the following expressions. If the expression is illegal, write
ILLEGAL.
If the expression is legal, write its type (i.e. int ** or unsigned long\ etc):
&p2: char***
p2[2]: this is a type **char but the location was not assigned
p + 4: this is a type *char but the location was not assigned
&p2[4]: this is a type ***char but the location was not assigned
v[4]: ILLEGAL
4. What is wrong with the following code segment:

Showing Page:
3/4

End of Preview - Want to read all 4 pages?
Access Now
Unformatted Attachment Preview
ITP 132 Final exam spring 2020 Name: 1. What is wrong with this code? int num = 5; cout ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

### Review

Anonymous
Goes above and beyond expectations!

Studypool
4.7
Indeed
4.5
Sitejabber
4.4