Taking command line arguments

advertisement
THE UNIVERSITY OF CALGARY
FACULTY OF SCIENCE
TAKE HOME EXAM I
COMPUTER SCIENCE 235
FALL SESSION: LECTURE 01
NAME: _____________________________________________________
Please DO NOT write your ID number on this page
Instructions:
Date Available: October 3, 2008
Due Date: 12:30 pm October 9, 2008
Please submit a hardcopy of your answers directly to the instructor.
Answer the questions in the space provided.
This is an open book exam. You may use any sources to help you arrive at an answer
except for people. You may not work with any other students in the class. You may not
work with any other individuals, either in person or on-line to help you answer these
questions. When using literary sources to help you answer these questions, it is your
responsibility to ensure that your source is accurate. As usual, when using a source to
find an answer, you must cite the source.
The number of marks available for each question is indicated with the question.
Page 2 of 6
1. [10 marks] Consider the code in the box on the side #include <iostream>
when answering the following question.
using namespace std;
a. Identify one literal in the code.
b. Identify one variable in the code.
int main(){
int x;
cout << "Enter a number: ";
cin >> x;
double sum = 0.0;
while (x > 0) {
if (x % 2 == 0) {
sum = sum + 2;
} else if (x % 3 == 0) {
sum = sum + 3;
} else {
sum = sum + 1;
}
x = x - 1;
}
cout << "sum = " << sum << endl;
c. Identify one identifier in the code.
d. Identify one integer in the code.
e. Identify one boolean in the code.
f. Identify one expression in the code.
}
g. How many statements does the code have?
h. What should the user provide as input to ensure that ‘sum = sum + 2’ is
executed at least once?
i. What should the user provide as input to ensure that ‘sum = sum + 3’ is
executed at least 2 times?
j. What will be printed to the console when running the code if the user
enters ‘10’ when prompted for a number?
Computer Science 233 cont’d.
Page 3 of 6
ID Number: ________________________
2. [3 marks]What is the value of each of the following C++ expressions? Assume
that the value of x is 10.0 and the value of y is 26. Explain your answer.
a. x < (x * 2 – 17)
b. y / x
c. y / 10
3. [2 marks] Given that there are a total of 16 bits available to represent a signed
integer in binary,
a. What is the largest number that can be represented? (You may give your
answer as an expression containing a power of two if you wish.)
b. What is the smallest number that can be represented?
4. [6 marks]Consider the integer 721 (using decimal representation). (Note: none of
the explanations below should include the use of an electronic device of any
kind.)
a. Without using a calculator, represent this number using binary
representation. Explain how you arrived at your answer.
b. Without using a calculator, represent this number using hexadecimal
representation. Explain how you arrived at your answer.
c. Without using a calculator, represent this number using octal
representation. Explain how you arrived at your answer.
Page 4 of 6
5. Some programming languages are compiled and others are interpreted.
a. [3 marks] What is the difference between the two?
b. [3 marks] For each of the following programming languages, indicate if
the language is compiled or interpreted.
i. C++
ii. Python
iii. Pascal
iv. Cobol
v. Perl
vi. Java
6. [2 marks] Consider a 4 bit unsigned integer 1111. What do you expect to happen
when 0001 is added to it?
7. [2 marks] Consider the binary sequence 10110100. What additional information
do we need to determine what this binary sequence represents?
Computer Science 233 cont’d.
Page 5 of 6
ID Number: ________________________
8. [2 marks] Consider a class called Fraction that can be constructed by providing a
numerator and a denominator (in that order). Provide two ways in C++ to create
an instance (or variable) of type Fraction such that it contains the fraction ¾.
9. [2 marks] Why would the following C++ code snippet not compile?
const int x = 7;
x = 11;
10. [3 marks] Why won’t the following C++ code compile? (Do not provide the
compile error, explain in your own words what the error is.)
#include <iostream>
using namespace std;
int add(int x, int y){
int z = x + y;
return z;
}
int main(){
add(10, 20);
cout << "Sum is: " << z << endl;
}
Page 6 of 6
11. [5 marks] Consider the following C++ code.
#include <iostream>
using namespace std;
void temp(int a, int b, float& c){
c = static_cast<float>(a) / b;
a = 2*a;
b = static_cast<int>(c);
}
int main(){
int x = 7;
int y = 2;
float c;
temp(x,y,c);
cout << x << " " << y << " " << c << endl;
}
When running this, the cout statement in main shows that the value of x is 7,
the value of y is 2 and the value of c is 3.5. Give a trace that shows why these are
the values in the three variables.
Download