Solved Final Exam - Introduction to Computer Programming | CS 10061, Exams of Computer Science

Material Type: Exam; Professor: Ruttan; Class: INTRODUCTION TO COMPUTER PROGRAMMING; Subject: Computer Science; University: Kent State University; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 02/25/2010

koofers-user-5cj
koofers-user-5cj 🇺🇸

9 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 10061 Sample Final Exam
Name__________________________________
Multiple Choice. Circle the correct answer (4 points each).
1. What is the effect of the following code fragment?
int num1 = 3;
int num2;
cin >> num2;
if ( num2 / num1 == 1 )
cout << "first option" << endl;
else
cout << "second option" << endl;
a) display first option if num2 is divisible by 3, and the second option if num2 is not
divisible by 3.
b) displays first option if num1 and num2 are equal to 1, and second option otherwise
c) displays first option if the number 3,4, or 5 are entered as input, and second option if
any other integer is entered as input
d) displays first option if num1 and num2 are equal and second option otherwise
2. What is the output of the following code fragment if the input values are 1 and 2 respectively?
int x;
int y;
cin >> x;
cin >> y;
cout << y;
cout << x << endl;
a) 1
2
b) 2 1
c) 2
1
d) 21
3. Which of the following directives must be included in a program in order to use C++ facilities
for file input and output?
a) #include <filestream>
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Solved Final Exam - Introduction to Computer Programming | CS 10061 and more Exams Computer Science in PDF only on Docsity!

CS 10061 Sample Final Exam

Name__________________________________

Multiple Choice. Circle the correct answer (4 points each).

1. What is the effect of the following code fragment?

int num1 = 3; int num2; cin >> num2; if ( num2 / num1 == 1 ) cout << "first option" << endl; else cout << "second option" << endl;

a) display first option if num2 is divisible by 3, and the second option if num2 is not

divisible by 3.

b) displays first option if num1 and num2 are equal to 1 , and second option otherwise

c) displays first option if the number 3,4, or 5 are entered as input, and second option if

any other integer is entered as input

d) displays first option if num1 and num2 are equal and second option otherwise

2. What is the output of the following code fragment if the input values are 1 and 2 respectively?

int x; int y; cin >> x; cin >> y; cout << y; cout << x << endl;

a) 1

b) 2 1 c) 2 1 d) 21

3. Which of the following directives must be included in a program in order to use C++ facilities

for file input and output?

a) #include

b) #include c) #include d) #include

4. Which is the correct C++ condition to check whether the value of integer x is between 5 and 9

(inclusive)?

a) 5 <= x <= 9

b) 5 < x < 10 c) 5 <= x && x <= 9 d) 5 <= x || x <= 9

5. What is displayed by the C++ statements that follow if the value input is 0?

cin >> color; if ((color == 0) && (color == 1)) cout << "red "; else if (color > 0) && (color < 2) cout << "blue "; else if (color <3) cout << "green "; else cout << "yellow "; cout << endl;

a) blue

b) green c) yellow

d) red blue green yellow

6. The value of the expression below is _____ if

int a = 2, b = 4, c = 8;

(a / b)*c

a) 0

b) 1

c) 2

d) 4

  1. What is the error in the line

int a=product ( 6, 5 );

Definition of sum:

int product( int& num1, int num2 ) { int num3; num3=num1; num1=num2; return( num3 * num2 ); }

a) A function cannot be used to initialize a value

b) The first parameter should be a variable c) The second parameter should be a variable d) Both parameters should be variables

13. What is the output from this program?

#include #include using namespace std; void doSomething ( int&, int& ); int main () { int first; int second; first = 1; second = 2; doSomething( first, second ); cout << first << ” ” << second << endl; return 0; } void doSomething( int& this, int& that ) { int theOther; theOther = 5; that = 2 + theOther; this = theOther * that; }

a) 35 2

b) 7 35 c) 35 7 d) 1 2

14. What is the output from this program?

#include #include using namespace std; double doSomething ( double, int ); int main () { double A; int B; double C; A = 1; B = 2; C=doSomething( A, B ); cout << A << B <<C <<endl; return 0; } double doSomething( double B, int A ) { int C; C=A+1; B =C/A + B; return B; }

a) 2.012.

b) 1.022. c) 2.012. d) 122

15. What is the value returned by funA(2.1,3.1) if

int funA(float x, float y){

int a,b;

a=x+1;b=y-1;

return funB(a,b);

int funB(int x, int y){

return (x%y +x/y);

a) 1

b) 2

c) 3

d) 4

3. Write a program fragment to extract numbers from the keyboard, compute the sum of the odd

numbers entered, and display the result. Use the integer 0 as a sentinel.

cout << “Enter the list of numbers, enter a 0 to terminate input” << endl;

int num=1,sum=0;

while (num !=0){

cin >> num

if (num % 2 == 1) sum = sum +num;

cout << “ The sum of the odd numbers entered is “ << sum << endl;

4. Write a program fragment that uses nested loops to display the following lines

for (i=1; i<11; i++){

cout << i;

for (j=i+1; j<100+i; j=j+2){

cout << “ “ << j;

cout << endl;

5. Define a function named divr that take two integers x and y and returns x%y and x/y. For

instance, given the number numbers 9 and 4, divr would return the values 2 and 1. Function divr has

two type integer input parameter and two integer output parameters.

void function(int x, int y, int& quotient, int& remainder){

quotient=x/y;

remainder=x % y;

return;

2. Write a code fragment to print out the array of characters defined by

char tble[30][15] ;

in a table with 30 rows and 15 columns with one space between each character in a row.

for (i =0; i< 30;i++){

for (j=0; j<15; j++)

cout << tble[i][j] <<” “;

cout << endl;

3. Write a function to print out a triangle with n lines that looks like this

o o o o o

o o o o

o o o

o o

o

void pretty_triangleDwn(int n){

int line_count=1;

while (line_count <= n ){

int spaces=1;

while (spaces<line_count){ //print out spaces

cout << " ";

spaces=spaces+1;

int letters=n-line_count+1;

while (letters >0 ){ //print out o's

cout << "o ";

letters=letters-1;

cout << endl;

line_count=line_count+1;

5. An image is stored in an array defined by

char pic[256][256][3];

Write a code fragment to produce an 8x8 checkerboard with blue and red squares.

int blockrow;blockcol;

for (i=0; i<256;i++){

blockrow=(i/32);

for (j=0;j<256;j++){

blockcol=j/32;

if ((blockrow+blockcol)%2 ==0){

pic[i][j][0]=255;

pic[i][j][1]=0;

pic[i][j][2]=0;

else {

pic[i][j][0]=0;

pic[i][j][1]=0;

pic[i][j][2]=255;