Sei sulla pagina 1di 8

International University Sarajevo CS 201 Winter Semester

Computer Science Midterm 23. 11. 2015.

Ime:

Bodovi

Page: 1 2 3 4 5 6 7 Total
Points: 14 9 9 14 18 12 24 100
Score:

1. (2 points) What is a binary value of the decimal number 97?

Solution: 0110 0001

2. (2 points) What is a decimal value of the binary number 0100 1001?

Solution: 73

3. (2 points) How many different values can be represented using half a byte?

Solution: 16

4. (2 points) What is a programming language?

Solution: A programming language is a formal constructed language designed to communicate


instructions to a machine, particularly a computer. Programming languages can be used to create
programs to control the behavior of a machine or to express algorithms.

5. (2 points) Briefly describe how are two types of typecasting used.

Solution: Typecasting is changing an entity of one data type into another. Implicit type conversion
is an automatic type conversion by the compiler. Explicit type conversion is explicitly defined within
a program.

6. (2 points) Briefly describe two major types of programming control structures.

Solution: A control structure is a block of programming that analyzes variables and chooses a
direction in which to go based on given parameters. Branching structures take one of the number
of available execution paths, while loops allow for part of code to be repeated number of times.

7. (2 points) What is the result of the following expression: !((true || false) && !false)

1/8
CS 201 Midterm 23. 11. 2015.

Solution: false

8. (2 points) What is ASCII?

Solution: ASCII is a character-encoding scheme. ASCII codes represent text in computers.

9. (2 points) What is a local and what is a global variable?

Solution: Local variable references in the function or block in which it is declared override the same
variable name in the larger scope and are not available from the outside scope. IA global variable
is a variable which is visible throughout the program, unless shadowed.

10. (2 points) What is the difference between parameters passed by value vs. parameters passed by refer-
ence?

Solution: Passing by reference means the called functions’ parameter will be the same as the callers’
passed argument (not the value, but the identity - the variable itself). Pass by value means the called
functions’ parameter will be a copy of the callers’ passed argument. The value will be the same, but
the identity - the variable - is different.

11. (3 points) List the errors in the function below. You can rewrite the code correctly underlining changes.
bool my_fun () {
a = 5;
b = 3;
if ( a > b )
c = a + b;
else ( c < 10 & a < 6)
d = 7;
return d ;
}

Solution:
int my_fun () {
int a = 5;
int b = 3;
if ( a > b )
int c = a + b ;
if ( b < 10 && a < 6)
int d = 7;
return d ;
}

2/8
CS 201 Midterm 23. 11. 2015.

12. (3 points) List the errors in the block of code below. You can rewrite the code correctly underlining
changes.
for ( int i = 0 , i < 5 , i +=1);
{
int a = 3
if ( a = i )
cout >> i is 3 >> endl ;
}

Solution:
for ( int i = 0; i < 5; i +=1)
{
int a = 3;
if ( a == i )
cout << " i is 3 " << endl ;
}

13. (3 points) What does the following block of code print?


int i = 10;
cout << i ++ << " ";
cout << i << " ";
cout << ++ i << " ";
while ( i < 14)
i ++;
cout << i << " ";
cout << i / 5;

Solution: 10 11 12 14 2

14. (3 points) What does the following block of code print?


int f = 0;
int g = 1;
for ( int i = 0; i < 10; i ++) {
cout << f << " " ;
f = f + g;
g = f - g;
}

Solution: 0 1 1 2 3 5 8 13 21 34

3/8
CS 201 Midterm 23. 11. 2015.

15. (3 points) What does the following block of code print?


int num = 3;
for ( int i = 1; i < num ; i ++) {
cout << i << " " ;
if (( num % i ) == 0) {
cout << num ++ << " " ;
}
}

Solution: 1 3 2 4 3 4

16. (5 points) Surface area of a cylinder is: A = 2πrh + 2πr2 , while volume is: V = πr2 h. Write a program
that asks a user to enter radius and hight of a cylinder and then outputs area and volume of the cylinder.

Solution:
# include < iostream >

using namespace std ;

const double PI = 3.14159;

int main () {
double r , h ;
cout << " Please enter radius and height of a cylinder : " ;
cin >> r >> h ;
cout << " Area is : " << 2 * PI * r * h + 2 * PI * r * r << endl ;
cout << " Volume is : " << ( PI * r * r * h ) << endl ;
return 0;
}

17. (6 points) Write a program which calculates user’s yearly income tax. The program asks for a user’s
yearly income and outputs tax (in KM) which needs to be paid. Tax is calculated based on the following
ranges: for income under 10000 KM tax is 7%, for income between 10000KM and 25000 KM tax is 12%
and for income above 25000 KM tax is 17%.

Solution:
# include < iostream >

using namespace std ;

int main () {
double income ;
cout << " Please enter your yearly income : " ;
cin >> income ;

4/8
CS 201 Midterm 23. 11. 2015.

cout << " Tax to be paid : " ;


if ( income < 10000)
cout << income * 0.07;
else if ( income < 25000)
cout << income * 0.12;
else
cout << income * 0.17;
cout << " KM " << endl ;
return 0;
}

18. (8 points) Write a program which asks user to enter 10 numbers and prints out the two largest ones.

Solution:
# include < iostream >

using namespace std ;

int main () {
int max ;
int next_max ;
int n ;
cout << " Please enter 10 numbers : " << endl ;
cin >> max ;
cin >> n ;
if ( n > max ) {
next_max = max ;
max = n ;
}
else next_max = n ;
for ( int i = 0; i < 8; i ++) {
cin >> n ;
if ( n > max ) {
next_max = max ;
max = n ;
}
else if ( n > next_max ) {
next_max = n ;
}
}
cout << max << " " << next_max << endl ;
return 0;
}

19. (10 points) Write a program which simulates rolling of a die (die is a cube with 6 numbers on it used in
many games). Program should ask a user how many times a die should be rolled and it should output
the probability of rolling the number 6.

5/8
CS 201 Midterm 23. 11. 2015.

Solution:
# include < iostream >
# include < cstdlib >

using namespace std ;

int main () {
srand ( static_cast < unsigned int >( time (0)));
int n ;
int six_counter = 0;
cout << " Please enter number of simulations : " ;
cin >> n ;
for ( int i = 0; i < n ; i ++) {
if (( rand () % 6) + 1 == 6)
six_counter ++;
}
cout << ( static_cast < double >( six_counter ) / n ) * 100 << " % " << endl ;
return 0;
}

20. (12 points) Write a program that takes a number of rows as input and outputs the shape below (for
n = 5). The first row prints letter Z, the next row prints two letters Y, and so on. Output is in form of
a triangle with a point in a top right corner.
Z
YY
XXX
WWWW
VVVVV

Solution:
# include < iostream >

using namespace std ;

int main () {
int n ;
char z = ’Z ’;
cout << " Please enter a number : " ;
cin >> n ;
for ( int i = 0; i < n ; i ++) {
for ( int j = n ; j > i + 1; j - -)
cout << " " ;
for ( int j = 0; j < i + 1; j ++)
cout << static_cast < char >( z - i );
cout << endl ;
}
return 0;

6/8
CS 201 Midterm 23. 11. 2015.

21. (12 points) ISBN stands for international standard book identifier and is used to assign each book with
a 10 digit ID. The ID is said to be valid if the sum of all the ten digits, each multiplied by its (integer)
weight, descending from 10 to 1, is a multiple of 11. For example, to check if the ISBN 1617290904 is
true we perform following calculation:
s = (1 ∗ 10) + (6 ∗ 9) + (1 ∗ 8) + (7 ∗ 7) + (2 ∗ 6) + (9 ∗ 5) + (0 ∗ 4) + (9 ∗ 3) + (0 ∗ 2) + (4 ∗ 1)
Then, if s is divisible by 11 the ISBN is valid.
Write a program which asks user for an ISBN number and prints if it is valid. Use a function which
accepts a single int parameter representing an ISBN and returns wether it is valid or not. [You can
assume all ISBN numbers will start with non zero digit, and you can use modulus operator directly.]

Solution:
# include < iostream >

using namespace std ;

bool isValidISBN ( int ISBN ) {


int sum = 0;
for ( int i = 1; i <= 10; i ++) {
sum += ( ISBN % 10) * i ;
ISBN /= 10;
}
return sum % 11 == 0;
}

int main () {
int ISBN ;
cout << " Please enter ISBN : " ;
cin >> ISBN ;
cout << isValidISBN ( ISBN ) << endl ;
return 0;
}

22. (12 points) Write a program which calculates greatest common divisor of three numbers. [Reminder:
The greatest common divisor of two or more integers, when at least one of them is not zero, is the largest
positive integer that divides the numbers without a remainder. For example, the GCD of 4, 8 and 12 is
4.]

Solution:
# include < iostream >

using namespace std ;

int my_gcd ( int a , int b ) {

7/8
CS 201 Midterm 23. 11. 2015.

for ( int divisor = min (a , b ); divisor > 0; divisor - -)


if ( a % divisor == 0 && b % divisor == 0)
return divisor ;
}

int main () {
int a , b , c ;
cout << " Please enter three numbers : " ;
cin >> a >> b >> c ;
int d ;
d = my_gcd (a , b );
d = my_gcd (d , c );
cout << " GCD is : " << d << endl ;
return 0;
}

8/8

Potrebbero piacerti anche