Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Exam 2 Questions with Solutions - Elements of Computer Science | CS 149D, Exams of Computer Science

Exam 2 Material Type: Exam; Class: ELEMENTS OF COMPUTER SCIENCE; Subject: Computer Science; University: Old Dominion University; Term: Unknown 2006;

Typology: Exams

Pre 2010

Uploaded on 10/18/2008

koofers-user-7qb
koofers-user-7qb 🇺🇸

5

(1)

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 149 Exam 2 Name:____key_________
April 17, 2001
Instructions: this exam is open book and open notes. The University honor code applies. To receive credit
for the exam, you must sign the honor pledge at the end of the exam.
1. (15 pts) Change the following code fragment so that it uses a ``while'' instead of a ``for'' loop.
for ( k=10; k<15; k++ )
{
if ( k <= 12 )
cout << “twice k = “ << 2*k << endl << endl;
else
cout << “k = “ << k << endl;
}
k = 10;
while( k < 15 )
{
if ( k<= 12 )
cout << “twice k = “ << 2*k << endl << endl;
else
cout << “k = “ << k << endl;
k++;
}
What is the output from this code fragment?
twice k = 20
twice k = 22
twice k = 24
k = 13
k = 14
2. (10 pts) Write a simple C++ program that creates a text file called “problem2.txt” and writes the contents
of an integer array “Numbers” into it, each value on a separate line. The array should contain the
integers 1 through 25. Your program must contain at least 2 for loops.
#include <fstream.h>
void main()
{
int i;
fstream Outfile;
int Numbers[25];
for ( i=0; i < 25; i++ )
Numbers[i] = i+1;
Outfile.open( “problem2.txt”, os:: out );
for ( i = 0; i<25; i++ )
Outfile << Numbers[i] << endl;
}
pf3

Partial preview of the text

Download Exam 2 Questions with Solutions - Elements of Computer Science | CS 149D and more Exams Computer Science in PDF only on Docsity!

CS 149 Exam 2 Name:____ key _________

April 17, 2001

Instructions: this exam is open book and open notes. The University honor code applies. To receive credit for the exam, you must sign the honor pledge at the end of the exam.

1. (15 pts) Change the following code fragment so that it uses a while'' instead of afor'' loop.

for ( k=10; k<15; k++ ) { if ( k <= 12 ) cout << “twice k = “ << 2*k << endl << endl; else cout << “k = “ << k << endl; }

k = 10;

while( k < 15 )

if ( k<= 12 )

cout << “twice k = “ << 2*k << endl << endl;

else

cout << “k = “ << k << endl;

k++;

What is the output from this code fragment?

twice k = 20

twice k = 22

twice k = 24

k = 13

k = 14

2. (10 pts) Write a simple C++ program that creates a text file called “problem2.txt” and writes the contents

of an integer array “Numbers” into it, each value on a separate line. The array should contain the integers 1 through 25. Your program must contain at least 2 for loops.

#include <fstream.h>

void main()

int i;

fstream Outfile;

int Numbers[25];

for ( i=0; i < 25; i++ )

Numbers[i] = i+1;

Outfile.open( “problem2.txt”, os:: out );

for ( i = 0; i<25; i++ )

Outfile << Numbers[i] << endl;

3. (10 pts) What is the output of the following code fragment?

for( i=3; i<10; i++ ) 12 9 6 3

if ( i == 4 ) i += 5; for( j=4; j>0; j-- ) { cout << j*i << “ “; } cout << endl; }

4. (20 pts) Answer True/False for each of the following:

_ f _ a) Every valid C++ program must contain the line “void main()” _ f _ b) Every valid C++ program must contain the line “#include <iostream.h>” _ f _ c) The value of the C++ expression 5/2 is 2.5. _ t _ d) Th e value of the C++ expression pow(2,2) is 4. _ f _ e) Use of a “while” loop usually indicates that you know how many times the loop will repeat when the loop starts. _ f _ f) “=” is used in C++ to compare the values of two numbers, as in “if ( a=b )” _ t _ g) In C++, “!” is the “not” operator. _ t _ h) ^c can be used to stop an executing program. _ t _ i) If a C++ program uses the log function, it must include math.h. _ f _ j) If a C++ program uses setw, it must include iostream.h.

5. (15 pts) Assume the integer variable “a” contains the value 100 and the variable “b” contains 30.

Assume “Cint” is an integer and “Cfloat’ is a real. Show the output generated by each of the following statements.

a. cout << “The sum of “ << a << “ and “ << b “ is “ << a+b << endl;

syntax error, need “<<” after “ << b “ and before “ is “

b. Cint = a/b; cout << (Cint + 10)/2 << endl;

c. Cfloat = a/b; cout << (Cfloat + 10)/2 << endl;

d. cout << “The sum of “ << setw( 5) << a << “ and “ << setw(5) << b << “ is “ << setw( 4) << a+b << endl;

The sum of 100 and 30 is 130

e. Cfloat = 1.5; cout << floor( Cfloat ) << “ “ << ceil( Cfloat ) << endl;